forked from lamproae/ncc
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmem_pool.h
134 lines (118 loc) · 2.23 KB
/
mem_pool.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
//--------------------------------------------------------------------------
//
// arrays of automatically increasing size.
//
// earray<X> for few elements, reallocation + memcpy every 64
// mem_pool<X> for up to a LOT of elements (64*128*256)
//
//-------------------------------------------------------------------------
#ifndef mem_pool_INCLUDED
#define mem_pool_INCLUDED
#include <string.h>
#define TXX template<class X>
TXX class mem_pool
{
#define PSEG 256
#define NSEG 128
#define TSEG 64
X ***p;
int sp;
public:
mem_pool ();
int alloc ();
inline X &operator [] (int);
void pop ();
int nr () { return sp; }
void copy (X**);
void destroy ();
};
#define I1A(x) (x / (PSEG*NSEG))
#define I2A(x) ((x % (PSEG*NSEG)) / PSEG)
#define I3A(x) ((x % (PSEG*NSEG)) % PSEG)
TXX mem_pool<X>::mem_pool ()
{
sp = 0;
p = new X** [TSEG];
}
TXX int mem_pool<X>::alloc ()
{
if (sp % (PSEG*NSEG) == 0)
p [I1A (sp)] = new X* [NSEG];
if (sp % PSEG == 0) {
p [I1A (sp)] [I2A (sp)] = new X [PSEG];
memset (p [I1A (sp)] [I2A (sp)], 0, PSEG * sizeof (X));
}
return sp++;
}
TXX X &mem_pool<X>::operator [] (int i)
{
return p [I1A (i)] [I2A (i)] [I3A (i)];
}
TXX void mem_pool<X>::pop ()
{
if (sp) sp--;
}
TXX void mem_pool<X>::copy (X **di)
{
int i;
X *d;
if (sp == 0) return;
if (*di == NULL) *di = new X [sp];
d = *di;
for (i = 0; i + PSEG < sp; i += PSEG)
memcpy (&d [i], p [I1A(i)][I2A(i)], PSEG * sizeof (X));
memcpy (&d [i], p [I1A(i)][I2A(i)], (sp - i) * sizeof (X));
}
TXX void mem_pool<X>::destroy ()
{
int i;
for (i = 0; i < sp; i += PSEG)
delete [] p [I1A (i)] [I2A (i)];
for (i = 0; i < sp; i += PSEG*NSEG)
delete [] p [I1A (i)];
delete [] p;
}
TXX class earray
{
#define SSEG 64
int ms;
X *Realloc (int);
public:
int nr;
X *x;
earray ();
inline int alloc ();
void freeze ();
void erase ();
};
TXX earray<X>::earray ()
{
ms = nr = 0;
x = NULL;
}
TXX X *earray<X>::Realloc (int s)
{
X *r = new X [s];
if (x) {
memcpy (r, x, nr * sizeof (X));
delete [] x;
}
return x = r;
}
TXX int earray<X>::alloc ()
{
if (ms == nr)
x = Realloc (ms += SSEG);
return nr++;
}
TXX void earray<X>::freeze ()
{
if (nr) x = Realloc (ms = nr);
}
TXX void earray<X>::erase ()
{
if (x) delete [] x;
x = NULL;
ms = nr = 0;
}
#endif