1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
|
/****************************************************************************
* Core Library Version 1.7, August 2004
* Copyright (c) 1995-2004 Exact Computation Project
* All rights reserved.
*
* This file is part of CGAL (www.cgal.org).
*
* File: MemoryPool.h
* Synopsis:
* a memory pool template class.
*
* Written by
* Zilin Du <zilin@cs.nyu.edu>
* Chee Yap <yap@cs.nyu.edu>
* Sylvain Pion <pion@cs.nyu.edu>
*
* WWW URL: https://cs.nyu.edu/exact/
* Email: exact@cs.nyu.edu
*
* $URL: https://github.com/CGAL/cgal/blob/v6.1/CGAL_Core/include/CGAL/CORE/MemoryPool.h $
* $Id: include/CGAL/CORE/MemoryPool.h b26b07a1242 $
* SPDX-License-Identifier: LGPL-3.0-or-later
***************************************************************************/
#ifndef _CORE_MEMORYPOOL_H_
#define _CORE_MEMORYPOOL_H_
#include <CGAL/config.h>
#include <CGAL/tss.h>
#include <new> // for placement new
#include <cassert>
#include <CGAL/assertions.h>
#include <vector>
namespace CORE {
#define CORE_EXPANSION_SIZE 1024
template< class T, int nObjects = CORE_EXPANSION_SIZE >
class MemoryPool {
private:
struct Thunk {
T object;
Thunk* next;
};
typedef MemoryPool<T,nObjects> Self;
public:
MemoryPool() : head( 0 ) {}
~MemoryPool()
{
//CGAL_warning_code(
std::size_t count = 0;
Thunk* t = head;
while(t!=0){
++count;
t = t->next;
}
//);
//CGAL_CORE_warning_msg(count == nObjects * blocks.size(),
// "Cannot delete memory as there are cyclic references");
if(count == nObjects * blocks.size()){
for(std::size_t i=0; i < blocks.size();i++){
::operator delete(blocks[i]);
}
}
// un-commenting the following line can help reproduce on Linux the
// assertion !blocks.empty() that is sometimes triggered with MSVC
// or AppleClang
// blocks.clear();
}
void* allocate(std::size_t size);
void free BOOST_PREVENT_MACRO_SUBSTITUTION (void* p);
// Access the corresponding static global allocator.
static MemoryPool<T,nObjects>& global_allocator() {
#if defined(CGAL_HAS_THREADS) // use the C++11 implementation
static thread_local Self memPool;
#else // not CGAL_HAS_THREADS
static Self memPool;
#endif // not CGAL_HAS_THREADS
return memPool;
}
private:
Thunk* head; // next available block in the pool
std::vector<void*> blocks;
};
template< class T, int nObjects >
void* MemoryPool< T, nObjects >::allocate(std::size_t) {
if ( head == 0 ) { // if no more memory in the pool
const int last = nObjects - 1;
// use the global operator new to allocate a block for the pool
Thunk* pool = reinterpret_cast<Thunk*>(
::operator new(nObjects * sizeof(Thunk)));
blocks.push_back(pool);
// initialize the chain (one-directional linked list)
head = pool;
for (int i = 0; i < last; ++i ) {
pool[i].next = &pool[i+1];
}
pool[last].next = 0;
}
// set head to point to the next object in the list
Thunk* currentThunk = head;
head = currentThunk->next;
return currentThunk;
}
template< class T, int nObjects >
void MemoryPool< T, nObjects >::free BOOST_PREVENT_MACRO_SUBSTITUTION (void* t) {
CGAL_assertion(t != 0);
if (t == 0) return; // for safety
if(blocks.empty()){
std::cerr << typeid(T).name() << std::endl;
}
CGAL_assertion (! blocks.empty());
// recycle the object memory, by putting it back into the chain
reinterpret_cast<Thunk*>(t)->next = head;
head = reinterpret_cast<Thunk*>(t);
}
} //namespace CORE
#endif // _CORE_MEMORYPOOL_H_
|