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
53 #if (COIN_MEMPOOL_SAVE_BLOCKHEADS == 1)
55 std::size_t block_num;
56 std::size_t max_block_num;
58 #if defined(COINUTILS_PTHREADS) && (COINUTILS_PTHREAD == 1)
59 pthread_mutex_t mutex_;
63 const std::size_t entry_size_;
66 CoinMempool(
const CoinMempool &);
67 CoinMempool &operator=(
const CoinMempool &);
70 char *allocate_new_block();
71 inline void lock_mutex()
73 #if defined(COINUTILS_PTHREADS) && (COINUTILS_PTHREAD == 1)
74 pthread_mutex_lock(&mutex_);
77 inline void unlock_mutex()
79 #if defined(COINUTILS_PTHREADS) && (COINUTILS_PTHREAD == 1)
80 pthread_mutex_unlock(&mutex_);
85 CoinMempool(std::size_t size = 0);
89 inline void dealloc(
char *p)
91 char **pp = (
char **)p;
122 inline void *alloc(
const std::size_t n)
124 if (maxpooled_ <= 0) {
125 return std::malloc(n);
128 const std::size_t to_alloc = ((n + COINUTILS_MEMPOOL_ALIGNMENT - 1) & CoinAllocRoundMask) + COINUTILS_MEMPOOL_ALIGNMENT;
129 CoinMempool *pool = NULL;
130 if (maxpooled_ > 0 && to_alloc >= (
size_t)maxpooled_) {
131 p =
static_cast< char *
>(std::malloc(to_alloc));
133 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();