22 #include <spot/misc/common.hh>
23 #include <unordered_map>
25 #if SPOT_DEBUG && defined(HAVE_VALGRIND_MEMCHECK_H)
26 #include <valgrind/memcheck.h>
35 static const size_t alignment_ = 2 *
sizeof(size_t) - 1;
39 : free_start_(nullptr), free_end_(nullptr), chunklist_(nullptr)
48 chunk_* prev = chunklist_->prev;
54 size_t fixsize(
size_t size)
const
56 if (size <
sizeof(block_))
57 size =
sizeof(block_);
59 return (size + alignment_ - 1) & ~(alignment_ - 1);
68 block_*& f = freelist_[size];
73 #if SPOT_DEBUG && defined(HAVE_VALGRIND_MEMCHECK_H)
74 VALGRIND_MALLOCLIKE_BLOCK(f, size, 0,
false);
77 VALGRIND_MAKE_MEM_DEFINED(f,
sizeof(block_*));
87 if (free_start_ + size > free_end_)
89 const size_t requested = (size > 128 ? size : 128) * 8192 - 64;
90 chunk_* c =
reinterpret_cast<chunk_*
>(malloc(requested));
92 throw std::bad_alloc();
96 free_start_ = c->data_ + size;
97 free_end_ = c->data_ + requested;
100 void* res = free_start_;
102 #if SPOT_DEBUG && defined(HAVE_VALGRIND_MEMCHECK_H)
103 VALGRIND_MALLOCLIKE_BLOCK(res, size, 0,
false);
121 size = fixsize(size);
122 block_* b =
reinterpret_cast<block_*
>(
const_cast<void*
>(ptr));
123 block_*& f = freelist_[size];
126 #if SPOT_DEBUG && defined(HAVE_VALGRIND_MEMCHECK_H)
127 VALGRIND_FREELIKE_BLOCK(ptr, 0);
132 struct block_ { block_* next; };
133 std::unordered_map<size_t, block_*> freelist_;
137 union chunk_ { chunk_* prev;
char data_[1]; }* chunklist_;
A multiple-size memory pool implementation.
Definition: mspool.hh:34
void * allocate(size_t size)
Allocate size bytes of memory.
Definition: mspool.hh:64
~multiple_size_pool()
Free any memory allocated by this pool.
Definition: mspool.hh:44
void deallocate(const void *ptr, size_t size)
Recycle size bytes of memory.
Definition: mspool.hh:118
multiple_size_pool()
Create a pool.
Definition: mspool.hh:38
Definition: automata.hh:27