Line | |
---|
1 | #ifndef _BUMPALLOC_H_ |
---|
2 | #define _BUMPALLOC_H_ |
---|
3 | |
---|
4 | /** |
---|
5 | * @class BumpAlloc |
---|
6 | * @brief Obtains memory in chunks and bumps a pointer through the chunks. |
---|
7 | * @author Emery Berger <http://www.cs.umass.edu/~emery> |
---|
8 | */ |
---|
9 | |
---|
10 | namespace Hoard { |
---|
11 | |
---|
12 | template <int ChunkSize, |
---|
13 | class super> |
---|
14 | class BumpAlloc : public super { |
---|
15 | public: |
---|
16 | |
---|
17 | BumpAlloc (void) |
---|
18 | : _bump (NULL), |
---|
19 | _remaining (0) |
---|
20 | {} |
---|
21 | |
---|
22 | inline void * malloc (size_t sz) { |
---|
23 | // If there's not enough space left to fulfill this request, get |
---|
24 | // another chunk. |
---|
25 | if (_remaining < sz) { |
---|
26 | refill(sz); |
---|
27 | } |
---|
28 | char * old = _bump; |
---|
29 | _bump += sz; |
---|
30 | _remaining -= sz; |
---|
31 | return old; |
---|
32 | } |
---|
33 | |
---|
34 | /// Free is disabled (we only bump, never reclaim). |
---|
35 | inline void free (void *) {} |
---|
36 | |
---|
37 | private: |
---|
38 | |
---|
39 | /// The bump pointer. |
---|
40 | char * _bump; |
---|
41 | |
---|
42 | /// How much space remains in the current chunk. |
---|
43 | size_t _remaining; |
---|
44 | |
---|
45 | // Get another chunk. |
---|
46 | void refill (size_t sz) { |
---|
47 | if (sz < ChunkSize) { |
---|
48 | sz = ChunkSize; |
---|
49 | } |
---|
50 | _bump = (char *) super::malloc (sz); |
---|
51 | _remaining = sz; |
---|
52 | } |
---|
53 | |
---|
54 | }; |
---|
55 | |
---|
56 | }; |
---|
57 | |
---|
58 | #endif |
---|
Note: See
TracBrowser
for help on using the repository browser.