summaryrefslogtreecommitdiff
path: root/klm/util/pool.hh
blob: 72f8a0c8b6de83feaf777ea0bded7d65e2f6d04a (plain)
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
// Very simple pool.  It can only allocate memory.  And all of the memory it
// allocates must be freed at the same time.  

#ifndef UTIL_POOL__
#define UTIL_POOL__

#include <vector>

#include <stdint.h>

namespace util {

class Pool {
  public:
    Pool();

    ~Pool();

    void *Allocate(std::size_t size) {
      void *ret = current_;
      current_ += size;
      if (current_ < current_end_) {
        return ret;
      } else {
        return More(size);
      }
    }

    void FreeAll();

  private:
    void *More(std::size_t size);

    std::vector<void *> free_list_;

    uint8_t *current_, *current_end_;

    // no copying
    Pool(const Pool &);
    Pool &operator=(const Pool &);
}; 

} // namespace util

#endif // UTIL_POOL__