00001 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */ 00002 /* 00003 * Main authors: 00004 * Christian Schulte <schulte@gecode.org> 00005 * 00006 * Copyright: 00007 * Christian Schulte, 2004 00008 * 00009 * Last modified: 00010 * $Date: 2007-09-11 14:20:32 +0200 (Tue, 11 Sep 2007) $ by $Author: schulte $ 00011 * $Revision: 4971 $ 00012 * 00013 * This file is part of Gecode, the generic constraint 00014 * development environment: 00015 * http://www.gecode.org 00016 * 00017 * Permission is hereby granted, free of charge, to any person obtaining 00018 * a copy of this software and associated documentation files (the 00019 * "Software"), to deal in the Software without restriction, including 00020 * without limitation the rights to use, copy, modify, merge, publish, 00021 * distribute, sublicense, and/or sell copies of the Software, and to 00022 * permit persons to whom the Software is furnished to do so, subject to 00023 * the following conditions: 00024 * 00025 * The above copyright notice and this permission notice shall be 00026 * included in all copies or substantial portions of the Software. 00027 * 00028 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 00029 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 00030 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 00031 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 00032 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 00033 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 00034 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00035 * 00036 */ 00037 00038 namespace Gecode { namespace Support { 00039 00048 template <class T> 00049 class BlockAllocator { 00050 private: 00052 class Block { 00053 public: 00054 static const int blocksize = 512; 00055 T b[blocksize]; 00056 Block* next; 00057 }; 00059 Block* b; 00061 T* n; 00063 size_t _size; 00065 void allocate(void); 00066 public: 00068 BlockAllocator(void); 00070 ~BlockAllocator(void); 00072 T* operator()(void); 00074 size_t size(void) const; 00075 }; 00076 00084 template <class T> 00085 class BlockClient { 00086 public: 00088 static void* operator new(size_t s, BlockAllocator<T>& ba); 00090 static void operator delete(void*, BlockAllocator<T>& ba); 00092 static void operator delete(void*); 00093 }; 00094 00095 00096 00097 template <class T> 00098 forceinline 00099 BlockAllocator<T>::BlockAllocator(void) { 00100 b = static_cast<Block*>(Memory::malloc(sizeof(Block))); 00101 b->next = NULL; 00102 n = &b->b[Block::blocksize]; 00103 _size = sizeof(Block); 00104 } 00105 00106 template <class T> 00107 forceinline 00108 BlockAllocator<T>::~BlockAllocator(void) { 00109 while (b != NULL) { 00110 Block* f = b; b = b->next; 00111 Memory::free(f); 00112 } 00113 } 00114 00115 template <class T> 00116 forceinline T* 00117 BlockAllocator<T>::operator()(void) { 00118 T* t = --n; 00119 if (t == &b->b[0]) 00120 allocate(); 00121 return t; 00122 } 00123 00124 template <class T> 00125 void 00126 BlockAllocator<T>::allocate(void) { 00127 // Allocate another block 00128 Block* nb = static_cast<Block*>(Memory::malloc(sizeof(Block))); 00129 nb->next = b; b = nb; 00130 n = &nb->b[Block::blocksize]; 00131 _size += sizeof(Block); 00132 } 00133 00134 template <class T> 00135 forceinline size_t 00136 BlockAllocator<T>::size(void) const { 00137 return _size; 00138 } 00139 00140 00141 00142 template <class T> 00143 forceinline void 00144 BlockClient<T>::operator delete(void*, BlockAllocator<T>&) { 00145 } 00146 template <class T> 00147 forceinline void 00148 BlockClient<T>::operator delete(void*) { 00149 } 00150 template <class T> 00151 forceinline void* 00152 BlockClient<T>::operator new(size_t, BlockAllocator<T>& ba) { 00153 return ba(); 00154 } 00155 00156 }} 00157 00158 // STATISTICS: support-any