11 #if !defined(COINUTILS_MEMPOOL_MAXPOOLED)
12 # define COINUTILS_MEMPOOL_MAXPOOLED -1
15 #if (COINUTILS_MEMPOOL_MAXPOOLED >= 0)
21 #ifndef COINUTILS_MEMPOOL_ALIGNMENT
22 #define COINUTILS_MEMPOOL_ALIGNMENT 16
36 #if (COINUTILS_MEMPOOL_ALIGNMENT == 16)
37 static const std::size_t CoinAllocPtrShift = 4;
38 static const std::size_t CoinAllocRoundMask = ~((std::size_t)15);
39 #elif (COINUTILS_MEMPOOL_ALIGNMENT == 8)
40 static const std::size_t CoinAllocPtrShift = 3;
41 static const std::size_t CoinAllocRoundMask = ~((std::size_t)7);
43 #error "COINUTILS_MEMPOOL_ALIGNMENT must be defined as 8 or 16 (or this code needs to be changed :-)"
48 #ifndef COIN_MEMPOOL_SAVE_BLOCKHEADS
49 # define COIN_MEMPOOL_SAVE_BLOCKHEADS 0
57 #if (COIN_MEMPOOL_SAVE_BLOCKHEADS == 1)
59 std::size_t block_num;
60 std::size_t max_block_num;
62 #if defined(COINUTILS_PTHREADS) && (COINUTILS_PTHREAD == 1)
63 pthread_mutex_t mutex_;
67 const std::size_t entry_size_;
70 CoinMempool(
const CoinMempool&);
71 CoinMempool& operator=(
const CoinMempool&);
74 char* allocate_new_block();
75 inline void lock_mutex() {
76 #if defined(COINUTILS_PTHREADS) && (COINUTILS_PTHREAD == 1)
77 pthread_mutex_lock(&mutex_);
80 inline void unlock_mutex() {
81 #if defined(COINUTILS_PTHREADS) && (COINUTILS_PTHREAD == 1)
82 pthread_mutex_unlock(&mutex_);
87 CoinMempool(std::size_t size = 0);
91 inline void dealloc(
char *p)
93 char** pp = (
char**)p;
124 inline void* alloc(
const std::size_t n)
126 if (maxpooled_ <= 0) {
127 return std::malloc(n);
130 const std::size_t to_alloc =
131 ((n+COINUTILS_MEMPOOL_ALIGNMENT-1) & CoinAllocRoundMask) +
132 COINUTILS_MEMPOOL_ALIGNMENT;
133 CoinMempool* pool = NULL;
134 if (maxpooled_ > 0 && to_alloc >= (
size_t)maxpooled_) {
135 p =
static_cast<char*
>(std::malloc(to_alloc));
136 if (p == NULL)
throw std::bad_alloc();
138 pool = pool_ + (to_alloc >> CoinAllocPtrShift);
141 *((CoinMempool**)p) = pool;
142 return static_cast<void*
>(p+COINUTILS_MEMPOOL_ALIGNMENT);
145 inline void dealloc(
void* p)
147 if (maxpooled_ <= 0) {
152 char* base =
static_cast<char*
>(p)-COINUTILS_MEMPOOL_ALIGNMENT;
153 CoinMempool* pool = *((CoinMempool**)base);
163 extern CoinAlloc CoinAllocator;
167 #if defined(COINUTILS_MEMPOOL_OVERRIDE_NEW) && (COINUTILS_MEMPOOL_OVERRIDE_NEW == 1)
168 void*
operator new(std::size_t size)
throw (std::bad_alloc);
169 void*
operator new[](std::size_t)
throw (std::bad_alloc);
170 void operator delete(
void*)
throw();
171 void operator delete[](
void*)
throw();
172 void*
operator new(std::size_t,
const std::nothrow_t&)
throw();
173 void*
operator new[](std::size_t,
const std::nothrow_t&)
throw();
174 void operator delete(
void*,
const std::nothrow_t&)
throw();
175 void operator delete[](
void*,
const std::nothrow_t&)
throw();