block-allocator.hpp
Go to the documentation of this file.
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: 2009-09-08 21:10:29 +0200 (Tue, 08 Sep 2009) $ by $Author: schulte $ 00011 * $Revision: 9692 $ 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, int blocksize = 512> 00049 class BlockAllocator { 00050 private: 00052 class Block { 00053 public: 00054 T b[blocksize]; 00055 Block* next; 00056 }; 00058 Block* b; 00060 T* n; 00062 size_t _size; 00064 void allocate(void); 00065 public: 00067 BlockAllocator(void); 00069 ~BlockAllocator(void); 00071 T* operator ()(void); 00073 size_t size(void) const; 00074 }; 00075 00083 template<class T, int blocksize = 512> 00084 class BlockClient { 00085 public: 00087 static void* operator new(size_t s, BlockAllocator<T,blocksize>& ba); 00089 static void operator delete(void*, BlockAllocator<T,blocksize>& ba); 00091 static void operator delete(void*); 00092 }; 00093 00094 00095 00096 template<class T, int blocksize> 00097 forceinline 00098 BlockAllocator<T,blocksize>::BlockAllocator(void) { 00099 b = static_cast<Block*>(heap.ralloc(sizeof(Block))); 00100 b->next = NULL; 00101 n = &b->b[blocksize]; 00102 _size = sizeof(Block); 00103 } 00104 00105 template<class T, int blocksize> 00106 forceinline 00107 BlockAllocator<T,blocksize>::~BlockAllocator(void) { 00108 while (b != NULL) { 00109 Block* f = b; b = b->next; 00110 heap.rfree(f); 00111 } 00112 } 00113 00114 template<class T, int blocksize> 00115 forceinline T* 00116 BlockAllocator<T,blocksize>::operator ()(void) { 00117 T* t = --n; 00118 if (t == &b->b[0]) 00119 allocate(); 00120 return t; 00121 } 00122 00123 template<class T, int blocksize> 00124 void 00125 BlockAllocator<T,blocksize>::allocate(void) { 00126 // Allocate another block 00127 Block* nb = static_cast<Block*>(heap.ralloc(sizeof(Block))); 00128 nb->next = b; b = nb; 00129 n = &nb->b[blocksize]; 00130 _size += sizeof(Block); 00131 } 00132 00133 template<class T, int blocksize> 00134 forceinline size_t 00135 BlockAllocator<T,blocksize>::size(void) const { 00136 return _size; 00137 } 00138 00139 00140 00141 template<class T, int blocksize> 00142 forceinline void 00143 BlockClient<T,blocksize>::operator delete(void*, 00144 BlockAllocator<T,blocksize>&) { 00145 } 00146 template<class T, int blocksize> 00147 forceinline void 00148 BlockClient<T,blocksize>::operator delete(void*) { 00149 } 00150 template<class T, int blocksize> 00151 forceinline void* 00152 BlockClient<T,blocksize>::operator new(size_t, 00153 BlockAllocator<T,blocksize>& ba) { 00154 return ba(); 00155 } 00156 00157 }} 00158 00159 // STATISTICS: support-any