Made on Kubuntu
00001 // Copyright (C) 2009-2010 Ferdinand Majerech 00002 // This file is part of MiniINI 00003 // For conditions of distribution and use, see copyright notice in LICENSE.txt 00004 00005 00006 #ifndef ALLOCATOR_H_INCLUDED 00007 #define ALLOCATOR_H_INCLUDED 00008 00009 #include "typedefs.h" 00010 #include "miniini_assert.h" 00011 00012 namespace miniini_private 00013 { 00015 00017 class Block 00018 { 00020 friend class Allocator; 00021 private: 00023 ui Allocated; 00025 ui Used; 00027 ui PtrsGiven; 00029 ui PtrsDeleted; 00031 c * Data; 00032 00033 public: 00038 Block(const ui alloc) 00039 :Allocated(alloc) 00040 ,Used(0) 00041 ,PtrsGiven(0) 00042 ,PtrsDeleted(0) 00043 ,Data(new c [Allocated]) 00044 {} 00045 00047 ~Block() 00048 { 00049 delete [] Data; 00050 } 00051 00053 ui GetRemainingSpace() const 00054 { 00055 return Allocated - Used; 00056 } 00057 00062 void Reallocate(const ui size) 00063 { 00064 //This can only be called before this block is used for the first time. 00065 MINIINI_ASSERT(!(Used || PtrsGiven || PtrsDeleted), 00066 "Block::Reallocate called on block that was already" 00067 "used."); 00068 Allocated = size; 00069 delete [] Data; 00070 Data = new c [Allocated]; 00071 } 00072 00073 private: 00074 00076 00077 Block(const Block &); 00078 00079 void operator = (const Block &); 00080 }; 00081 00085 class Allocator 00086 { 00087 private: 00089 ui NumBlocks; 00090 00095 ui CurrentBlock; 00096 00098 ui BlockMinSize; 00099 00101 Block * * Blocks; 00102 00103 public: 00109 Allocator(const ui size, const ui blocks); 00110 00112 void Trim(); 00113 00118 void DeleteSpace(c * const ptr); 00119 00124 c * NewSpace(const ui size); 00125 00127 ~Allocator(); 00128 00129 private: 00130 00134 void NextBlock(ui minsize); 00135 00137 void NewBlock(); 00138 00142 void DeleteBlock(ui index); 00143 00149 i FindBlock(c * const ptr); 00150 00151 //No copy construction or assignment. 00152 00153 Allocator(const Allocator &); 00154 00155 void operator = (const Allocator &); 00156 }; 00158 00159 } 00160 #endif