42#ifdef GECODE_PEAKHEAP_MALLOC_H
46#ifdef GECODE_PEAKHEAP_MALLOC_MALLOC_H
47#include <malloc/malloc.h>
79 T*
alloc(
long unsigned int n);
95 T*
alloc(
unsigned int n);
111 void free(T* b,
long unsigned int n);
119 void free(T* b,
long int n);
127 void free(T* b,
unsigned int n);
135 void free(T* b,
int n);
148 T*
realloc(T* b,
long unsigned int n,
long unsigned int m);
161 T*
realloc(T* b,
long int n,
long int m);
174 T*
realloc(T* b,
unsigned int n,
unsigned int m);
187 T*
realloc(T* b,
int n,
int m);
196 T**
realloc(T** b,
long unsigned int n,
long unsigned int m);
205 T**
realloc(T** b,
long int n,
long int m);
214 T**
realloc(T** b,
unsigned int n,
unsigned int m);
223 T**
realloc(T** b,
int n,
int m);
233 static T*
copy(T* d,
const T* s,
long unsigned int n);
243 static T*
copy(T* d,
const T* s,
long int n);
253 static T*
copy(T* d,
const T* s,
unsigned int n);
263 static T*
copy(T* d,
const T* s,
int n);
272 static T**
copy(T** d,
const T** s,
long unsigned int n);
281 static T**
copy(T** d,
const T** s,
long int n);
290 static T**
copy(T** d,
const T** s,
unsigned int n);
299 static T**
copy(T** d,
const T** s,
int n);
308 void rfree(
void* p,
size_t s);
312#ifdef GECODE_PEAKHEAP
324 static void*
operator new(
size_t s) =
delete;
326 static void operator delete(
void* p) =
delete;
349 static void*
operator new(
size_t s);
351 static void operator delete(
void* p);
362#ifdef GECODE_HAS_FAULT_INJECTION
366#ifdef GECODE_PEAKHEAP
368 _cur += GECODE_MSIZE(p);
369 _peak = std::max(_peak,_cur);
379#ifdef GECODE_PEAKHEAP
381 _cur -= GECODE_MSIZE(p);
389#ifdef GECODE_PEAKHEAP
391 _cur -= GECODE_MSIZE(p);
399#ifdef GECODE_HAS_FAULT_INJECTION
402#ifdef GECODE_PEAKHEAP
404 _cur -= GECODE_MSIZE(p);
408#ifdef GECODE_PEAKHEAP
410 _cur += GECODE_MSIZE(p);
411 _peak = std::max(_peak,_cur);
414 if (p !=
nullptr || s == 0)
425 HeapAllocated::operator
new(
size_t s) {
426 return heap.ralloc(s);
429 HeapAllocated::operator
delete(
void* p) {
442 T* p =
static_cast<T*
>(
ralloc(
sizeof(T)*n));
443 for (
long unsigned int i=0U; i<n; i++)
444 (
void)
new (p+i) T();
451 return alloc<T>(
static_cast<long unsigned int>(n));
456 return alloc<T>(
static_cast<long unsigned int>(n));
462 return alloc<T>(
static_cast<long unsigned int>(n));
468 for (
long unsigned int i=0U; i<n; i++)
476 free<T>(b,
static_cast<long unsigned int>(n));
481 free<T>(b,
static_cast<long unsigned int>(n));
487 free<T>(b,
static_cast<long unsigned int>(n));
495 T* p =
static_cast<T*
>(
ralloc(
sizeof(T)*m));
496 for (
long unsigned int i=0U; i<std::min(n,m); i++)
497 (
void)
new (p+i) T(b[i]);
498 for (
long unsigned int i=n; i<m; i++)
499 (
void)
new (p+i) T();
506 assert((n >= 0) && (m >= 0));
507 return realloc<T>(b,
static_cast<long unsigned int>(n),
508 static_cast<long unsigned int>(m));
513 return realloc<T>(b,
static_cast<long unsigned int>(n),
514 static_cast<long unsigned int>(m));
519 assert((n >= 0) && (m >= 0));
520 return realloc<T>(b,
static_cast<long unsigned int>(n),
521 static_cast<long unsigned int>(m));
524#define GECODE_SUPPORT_REALLOC(T) \
527 Heap::realloc<T>(T* b, long unsigned int, long unsigned int m) { \
528 return static_cast<T*>(rrealloc(b,m*sizeof(T))); \
532 Heap::realloc<T>(T* b, long int n, long int m) { \
533 assert((n >= 0) && (m >= 0)); \
534 return realloc<T>(b,static_cast<long unsigned int>(n), \
535 static_cast<long unsigned int>(m)); \
539 Heap::realloc<T>(T* b, unsigned int n, unsigned int m) { \
540 return realloc<T>(b,static_cast<long unsigned int>(n), \
541 static_cast<long unsigned int>(m)); \
545 Heap::realloc<T>(T* b, int n, int m) { \
546 assert((n >= 0) && (m >= 0)); \
547 return realloc<T>(b,static_cast<long unsigned int>(n), \
548 static_cast<long unsigned int>(m)); \
563#undef GECODE_SUPPORT_REALLOC
568 return static_cast<T**
>(
rrealloc(b,m*
sizeof(T*)));
573 assert((n >= 0) && (m >= 0));
574 return realloc<T*>(b,
static_cast<long unsigned int>(n),
575 static_cast<long unsigned int>(m));
580 return realloc<T*>(b,
static_cast<long unsigned int>(n),
581 static_cast<long unsigned int>(m));
586 assert((n >= 0) && (m >= 0));
587 return realloc<T*>(b,
static_cast<long unsigned int>(n),
588 static_cast<long unsigned int>(m));
594 for (
long unsigned int i=0U; i<n; i++)
602 return copy<T>(d,s,
static_cast<long unsigned int>(n));
607 return copy<T>(d,s,
static_cast<long unsigned int>(n));
613 return copy<T>(d,s,
static_cast<long unsigned int>(n));
616#define GECODE_SUPPORT_COPY(T) \
619 Heap::copy(T* d, const T* s, long unsigned int n) { \
620 return static_cast<T*>(Support::allocator.memcpy(d,s,n*sizeof(T))); \
624 Heap::copy(T* d, const T* s, long int n) { \
626 return copy<T>(d,s,static_cast<long unsigned int>(n)); \
630 Heap::copy(T* d, const T* s, unsigned int n) { \
631 return copy<T>(d,s,static_cast<long unsigned int>(n)); \
635 Heap::copy(T* d, const T* s, int n) { \
637 return copy<T>(d,s,static_cast<long unsigned int>(n)); \
652#undef GECODE_SUPPORT_COPY
663 return copy<T*>(d,s,
static_cast<long unsigned int>(n));
668 return copy<T*>(d,s,
static_cast<long unsigned int>(n));
674 return copy<T*>(d,s,
static_cast<long unsigned int>(n));
677#ifdef GECODE_PEAKHEAP
Base class for heap allocated objects.
Heap memory management class
Heap(const Heap &)=delete
Copy constructor (disabled).
Heap(void)
Default constructor (ensuring that only a single instance is created).
void * rrealloc(void *p, size_t s)
Change memory block starting at p to size s.
T * realloc(T *b, long unsigned int n, long unsigned int m)
Reallocate block of n objects starting at b to m objects of type T from heap.
static T * copy(T *d, const T *s, long unsigned int n)
Copy n objects starting at s to d.
void free(T *b, long unsigned int n)
Delete n objects starting at b.
T * alloc(long unsigned int n)
Allocate block of n objects of type T from heap.
void rfree(void *p)
Free memory block starting at p.
const Heap & operator=(const Heap &)=delete
Assignment operator (disabled).
void * ralloc(size_t s)
Allocate s bytes from heap.
Exception: Memory exhausted
A mutex for mutual exclausion among several threads.
Heap heap
The single global heap.
Allocator allocator
The single global default memory allocator.
#define GECODE_SUPPORT_REALLOC(T)
#define GECODE_SUPPORT_COPY(T)
void check(Phase p)
Check failpoint for phase p.
Gecode toplevel namespace
#define GECODE_SUPPORT_EXPORT