StRoot
1
Main Page
Related Pages
Modules
Namespaces
Classes
Files
Examples
File List
File Members
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Friends
Groups
Pages
StarClassLibrary
StMemoryPool.hh
1
/***************************************************************************
2
*
3
* $Id: StMemoryPool.hh,v 1.2 1999/11/18 17:55:46 ullrich Exp $
4
*
5
* Author: Thomas Ullrich, Nov 1999
6
***************************************************************************
7
*
8
* Description:
9
*
10
* Idea taken from:
11
* B. Stroustrup, The C++ Programming Language, 3ed edition,
12
* Chapter 19 (page 570)
13
*
14
* Should be used if a large number of small objects has to be
15
* frequently allocated from heap. It is much more efficient and
16
* faster than the standard new/delete operators.
17
* Add to the referring class X the following lines:
18
*
19
* class X {
20
* public:
21
* void* operator new(size_t) { return mPool.alloc(); }
22
* void operator delete(void* p) { mPool.free(p); }
23
* ...
24
* privat:
25
* static StMemoryPool mPool;
26
* ...
27
* }
28
*
29
* StMemoryPool X::mPool(sizeof(X));
30
*
31
* Note that the class is optimized for speed and compactness
32
* not for readability. Only single objects may be created,
33
* i.e, new X[100] will not work; however, in these cases the
34
* default operators new/delete will be called.
35
*
36
***************************************************************************
37
*
38
* $Log: StMemoryPool.hh,v $
39
* Revision 1.2 1999/11/18 17:55:46 ullrich
40
* Corrected reference to Stroustrup.
41
*
42
* Revision 1.1 1999/11/09 19:29:13 ullrich
43
* Initial Revision
44
*
45
**************************************************************************/
46
#ifndef ST_MEMORY_POOL_HH
47
#define ST_MEMORY_POOL_HH
48
49
50
class
StMemoryPool
{
51
public
:
52
StMemoryPool
(
unsigned
int
n);
53
~
StMemoryPool
();
54
55
void
* alloc();
56
void
free(
void
*);
57
58
private
:
59
StMemoryPool
();
60
StMemoryPool
(
StMemoryPool
&);
61
void
operator= (
StMemoryPool
&);
62
void
grow();
63
64
struct
Link { Link* next; };
65
struct
Chunk {
66
enum
{size = 16*1024-16};
67
Chunk* next;
68
char
mem[size];
69
};
70
Chunk *chunks;
71
Link* head;
72
const
unsigned
int
esize;
73
};
74
75
76
inline
void
*
77
StMemoryPool::alloc()
78
{
79
if
(head == 0) grow();
80
Link* p = head;
81
head = p->next;
82
return
p;
83
}
84
85
inline
void
86
StMemoryPool::free(
void
* b)
87
{
88
if
(b != 0) {
89
Link* p =
static_cast<
Link*
>
(b);
90
p->next = head;
91
head = p;
92
}
93
}
94
#endif
StMemoryPool
Definition:
StMemoryPool.hh:50
Generated by
1.8.5