source: proiecte/swift/trunk/lib/hoard-371/src/bumpalloc.h @ 176

Last change on this file since 176 was 176, checked in by (none), 14 years ago
  • imported repo from "guagal"
File size: 1.0 KB
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
10namespace Hoard {
11
12template <int ChunkSize,
13          class super>
14class BumpAlloc : public super {
15public:
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
37private:
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.