119namespace Gecode {
namespace Kernel {
143 void alloc_fill(
SharedMemory& sm,
size_t s,
bool first);
157 static size_t sz2i(
size_t);
159 static size_t i2sz(
size_t);
173 void reuse(
void* p,
size_t s);
178namespace Gecode {
namespace Kernel {
192 while (heap.hc !=
nullptr) {
203 while ((heap.hc !=
nullptr) && (heap.hc->size < l)) {
210 if (heap.hc ==
nullptr) {
211 assert(heap.n_hc == 0);
228 hc->next = heap.hc; heap.hc =
hc;
266namespace Gecode {
namespace Kernel {
274 MemoryManager::sz2i(
size_t s) {
281 MemoryManager::i2sz(
size_t i) {
300 return &cur_hc->area[0];
304 MemoryManager::alloc_fill(
SharedMemory& sm,
size_t sz,
bool first) {
313 size_t overhead =
sizeof(HeapChunk) -
sizeof(
double);
316 size_t allocate = ((sz > cur_hcsz) ?
317 (
static_cast<size_t>(sz / cur_hcsz) + 1) * cur_hcsz
320 HeapChunk* hc = sm.
alloc(allocate,sz);
322 lsz = hc->size - overhead;
325 requested = hc->size;
326 hc->next =
nullptr; cur_hc = hc;
328 requested += hc->size;
329 hc->next = cur_hc->
next; cur_hc->
next = hc;
331#ifdef GECODE_MEMORY_CHECK
332 for (
char* c = start; c < (start+lsz); c++)
339 : cur_hcsz(
MemoryConfig::hcsz_min), requested(0), slack(nullptr) {
340 alloc_fill(sm,cur_hcsz,
true);
349 : cur_hcsz(mm.cur_hcsz), requested(0), slack(nullptr) {
353 (s_sub*2 < cur_hcsz))
355 alloc_fill(sm,cur_hcsz+s_sub,
true);
371 }
while (hc !=
nullptr);
382#ifdef GECODE_MEMORY_CHECK
384 char* c =
static_cast<char*
>(p);
392 size_t aligned_s = s;
397 if (aligned_p==
nullptr ||
403 rc->
size = aligned_s;
406 size_t i = sz2i(aligned_s);
408 f->
next(fl[i]); fl[i]=f;
424 fl_refill<s>(sm); f = fl[i];
442 l->
next(fl[i]); fl[i] = f;
449 if (slack !=
nullptr) {
464 }
while (m !=
nullptr);
472 (
static_cast<unsigned int>(i)+1)*sz));
Base-class for freelist-managed objects.
FreeList ** nextRef(void)
Return pointer to next link in freelist object.
FreeList(void)
Use uninitialized.
FreeList * next(void) const
Return next freelist object.
FreeList * _next
Pointer to next freelist object.
Memory chunk allocated from heap with proper alignment.
double area[1]
Start of memory area inside chunk.
Memory chunk with size information.
size_t size
Size of chunk.
MemoryChunk * next
Next chunk.
void * fl_alloc(SharedMemory &sm)
Allocate free list element of size s.
void release(SharedMemory &sm)
Release all allocated heap chunks.
void reuse(void *p, size_t s)
Store for reusal, if of sufficient size for free list.
void * alloc(SharedMemory &sm, size_t s)
Allocate memory of size s.
MemoryManager(SharedMemory &sm)
Constructor initialization.
void fl_dispose(FreeList *f, FreeList *l)
Release all free list elements of size s between f and l (inclusive).
void * subscriptions(void) const
Get the memory area for subscriptions.
Shared object for several memory areas.
SharedMemory(void)
Initialize.
HeapChunk * alloc(size_t s, size_t l)
Return heap chunk, preferable of size s, but at least of size l.
HeapChunk * hc
A list of cached heap chunks.
void free(HeapChunk *hc)
Free heap chunk (or cache for later).
unsigned int n_hc
How many heap chunks are available for caching.
~SharedMemory(void)
Destructor.
A lock as a scoped frontend for a mutex.
A mutex for mutual exclausion among several threads.
#define GECODE_MEMORY_ALIGNMENT
Memory alignment.
Heap heap
The single global heap.
#define GECODE_KERNEL_EXPORT
Parameters defining memory management policy for spaces.
const int hcsz_inc_ratio
Increment ratio for chunk size.
const size_t hcsz_max
Maximal size of a heap chunk requested from the OS.
const int fl_size_max
Maximal size for free list element.
void align(size_t &s, size_t a=GECODE_MEMORY_ALIGNMENT)
Align size s to the required alignment a.
const int fl_unit_size
Unit size for free lists.
const unsigned int n_hc_cache
How many heap chunks should be cached at most.
const size_t hcsz_min
Minimal size of a heap chunk requested from the OS.
const int fl_refill
Number of free lists elements to allocate.
const int fl_size_min
Minimal size for free list element.
const int hcsz_dec_ratio
Decrement ratio for chunk size.
Gecode toplevel namespace
T ptr_cast(void *p)
Cast p into pointer of type T.