28 #include <tulip/ParallelTools.h> 30 static const size_t BUFFOBJ = 20;
63 template <
typename TYPE>
69 inline void *
operator new(
size_t sizeofObj) {
71 inline void *
operator new(size_t) {
73 assert(
sizeof(TYPE) == sizeofObj);
75 t = _memoryChunkManager.getObject();
79 inline void operator delete(
void *p) {
80 _memoryChunkManager.releaseObject(p);
84 class MemoryChunkManager {
86 ~MemoryChunkManager() {
87 for (
unsigned int i = 0; i < TLP_MAX_NB_THREADS; ++i) {
88 for (
size_t j = 0; j < _allocatedChunks[i].size(); ++j) {
89 free(_allocatedChunks[i][j]);
96 TYPE *result =
nullptr;
98 if (_freeObject[threadId].empty()) {
99 void *chunk = malloc(BUFFOBJ *
sizeof(TYPE));
100 TYPE *p =
static_cast<TYPE *
>(chunk);
101 _allocatedChunks[threadId].push_back(chunk);
103 for (
size_t j = 0; j < BUFFOBJ - 1; ++j) {
104 _freeObject[threadId].push_back(static_cast<void *>(p));
110 result =
static_cast<TYPE *
>(_freeObject[threadId].back());
111 _freeObject[threadId].pop_back();
117 void releaseObject(
void *p) {
119 _freeObject[threadId].push_back(p);
123 std::vector<void *> _allocatedChunks[TLP_MAX_NB_THREADS];
124 std::vector<void *> _freeObject[TLP_MAX_NB_THREADS];
127 static MemoryChunkManager _memoryChunkManager;
130 template <
typename TYPE>
131 typename MemoryPool<TYPE>::MemoryChunkManager MemoryPool<TYPE>::_memoryChunkManager;
133 #endif // MEMORYPOOL_H
static unsigned int getThreadNumber()