12 #if !defined(COINUTILS_MEMPOOL_MAXPOOLED)
13 # define COINUTILS_MEMPOOL_MAXPOOLED -1
16 #if (COINUTILS_MEMPOOL_MAXPOOLED >= 0)
18 #ifndef COINUTILS_MEMPOOL_ALIGNMENT
19 #define COINUTILS_MEMPOOL_ALIGNMENT 16
33 #if (COINUTILS_MEMPOOL_ALIGNMENT == 16)
34 static const std::size_t CoinAllocPtrShift = 4;
35 static const std::size_t CoinAllocRoundMask = ~((std::size_t)15);
36 #elif (COINUTILS_MEMPOOL_ALIGNMENT == 8)
37 static const std::size_t CoinAllocPtrShift = 3;
38 static const std::size_t CoinAllocRoundMask = ~((std::size_t)7);
40 #error "COINUTILS_MEMPOOL_ALIGNMENT must be defined as 8 or 16 (or this code needs to be changed :-)"
45 #ifndef COIN_MEMPOOL_SAVE_BLOCKHEADS
46 # define COIN_MEMPOOL_SAVE_BLOCKHEADS 0
54 #if (COIN_MEMPOOL_SAVE_BLOCKHEADS == 1)
56 std::size_t block_num;
57 std::size_t max_block_num;
59 #if defined(COINUTILS_PTHREADS) && (COINUTILS_PTHREAD == 1)
60 pthread_mutex_t mutex_;
64 const std::size_t entry_size_;
67 CoinMempool(
const CoinMempool&);
68 CoinMempool& operator=(
const CoinMempool&);
71 char* allocate_new_block();
72 inline void lock_mutex() {
73 #if defined(COINUTILS_PTHREADS) && (COINUTILS_PTHREAD == 1)
74 pthread_mutex_lock(&mutex_);
77 inline void unlock_mutex() {
78 #if defined(COINUTILS_PTHREADS) && (COINUTILS_PTHREAD == 1)
79 pthread_mutex_unlock(&mutex_);
84 CoinMempool(std::size_t size = 0);
88 inline void dealloc(
char *p)
90 char** pp = (
char**)p;
121 inline void* alloc(
const std::size_t n)
123 if (maxpooled_ <= 0) {
124 return std::malloc(n);
127 const std::size_t to_alloc =
128 ((n+COINUTILS_MEMPOOL_ALIGNMENT-1) & CoinAllocRoundMask) +
129 COINUTILS_MEMPOOL_ALIGNMENT;
130 CoinMempool* pool = NULL;
131 if (maxpooled_ > 0 && to_alloc >= (
size_t)maxpooled_) {
132 p =
static_cast<char*
>(std::malloc(to_alloc));
133 if (p == NULL)
throw std::bad_alloc();
135 pool = pool_ + (to_alloc >> CoinAllocPtrShift);
138 *((CoinMempool**)p) = pool;
139 return static_cast<void*
>(p+COINUTILS_MEMPOOL_ALIGNMENT);
142 inline void dealloc(
void* p)
144 if (maxpooled_ <= 0) {
149 char* base =
static_cast<char*
>(p)-COINUTILS_MEMPOOL_ALIGNMENT;
150 CoinMempool* pool = *((CoinMempool**)base);
160 extern CoinAlloc CoinAllocator;
164 #if defined(COINUTILS_MEMPOOL_OVERRIDE_NEW) && (COINUTILS_MEMPOOL_OVERRIDE_NEW == 1)
165 void*
operator new(std::size_t size)
throw (std::bad_alloc);
166 void*
operator new[](std::size_t)
throw (std::bad_alloc);
167 void operator delete(
void*)
throw();
168 void operator delete[](
void*)
throw();
169 void*
operator new(std::size_t,
const std::nothrow_t&)
throw();
170 void*
operator new[](std::size_t,
const std::nothrow_t&)
throw();
171 void operator delete(
void*,
const std::nothrow_t&)
throw();
172 void operator delete[](
void*,
const std::nothrow_t&)
throw();