BWAPI
|
00001 /**************************************************************************** 00002 * Core Library Version 1.7, August 2004 00003 * Copyright (c) 1995-2004 Exact Computation Project 00004 * All rights reserved. 00005 * 00006 * This file is part of CORE (http://cs.nyu.edu/exact/core/); you may 00007 * redistribute it under the terms of the Q Public License version 1.0. 00008 * See the file LICENSE.QPL distributed with CORE. 00009 * 00010 * Licensees holding a valid commercial license may use this file in 00011 * accordance with the commercial license agreement provided with the 00012 * software. 00013 * 00014 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE 00015 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 00016 * 00017 * 00018 * File: MemoryPool.h 00019 * Synopsis: 00020 * a memory pool template class. 00021 * 00022 * Written by 00023 * Zilin Du <zilin@cs.nyu.edu> 00024 * Chee Yap <yap@cs.nyu.edu> 00025 * Sylvain Pion <pion@cs.nyu.edu> 00026 * 00027 * WWW URL: http://cs.nyu.edu/exact/ 00028 * Email: exact@cs.nyu.edu 00029 * 00030 * $URL: svn+ssh://scm.gforge.inria.fr/svn/cgal/branches/CGAL-3.5-branch/Core/include/CGAL/CORE/MemoryPool.h $ 00031 * $Id: MemoryPool.h 41683 2008-01-18 19:58:49Z spion $ 00032 ***************************************************************************/ 00033 #ifndef _CORE_MEMORYPOOL_H_ 00034 #define _CORE_MEMORYPOOL_H_ 00035 00036 #include <new> // for placement new 00037 #include <cassert> 00038 00039 CORE_BEGIN_NAMESPACE 00040 00041 #define CORE_EXPANSION_SIZE 1024 00042 template< class T, int nObjects = CORE_EXPANSION_SIZE > 00043 class MemoryPool { 00044 public: 00045 MemoryPool() : head( 0 ) {} 00046 00047 void* allocate(std::size_t size); 00048 void free(void* p); 00049 00050 // Access the corresponding static global allocator. 00051 static MemoryPool<T>& global_allocator() { 00052 return memPool; 00053 } 00054 00055 private: 00056 struct Thunk { 00057 T object; 00058 Thunk* next; 00059 }; 00060 00061 private: 00062 Thunk* head; // next available block in the pool 00063 00064 private: 00065 // Static global allocator. 00066 static MemoryPool<T, nObjects> memPool; 00067 }; 00068 00069 template <class T, int nObjects > 00070 MemoryPool<T, nObjects> MemoryPool<T, nObjects>::memPool; 00071 00072 template< class T, int nObjects > 00073 void* MemoryPool< T, nObjects >::allocate(std::size_t) { 00074 if ( head == 0 ) { // if no more memory in the pool 00075 const int last = nObjects - 1; 00076 00077 // use the global operator new to allocate a block for the pool 00078 Thunk* pool = reinterpret_cast<Thunk*>( 00079 ::operator new(nObjects * sizeof(Thunk))); 00080 00081 // initialize the chain (one-directional linked list) 00082 head = pool; 00083 for (int i = 0; i < last; ++i ) { 00084 pool[i].next = &pool[i+1]; 00085 } 00086 pool[last].next = 0; 00087 } 00088 00089 // set head to point to the next object in the list 00090 Thunk* currentThunk = head; 00091 head = currentThunk->next; 00092 00093 return currentThunk; 00094 } 00095 00096 template< class T, int nObjects > 00097 void MemoryPool< T, nObjects >::free(void* t) { 00098 assert(t != 0); 00099 if (t == 0) return; // for safety 00100 00101 // recycle the object memory, by putting it back into the chain 00102 reinterpret_cast<Thunk*>(t)->next = head; 00103 head = reinterpret_cast<Thunk*>(t); 00104 } 00105 00106 CORE_END_NAMESPACE 00107 #endif // _CORE_MEMORYPOOL_H_