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 <cassert> 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: 00035 00039 Block(const ui alloc) 00040 :Allocated(alloc) 00041 ,Used(0) 00042 ,PtrsGiven(0) 00043 ,PtrsDeleted(0) 00044 ,Data(new c [Allocated]) 00045 {} 00046 00048 ~Block() 00049 { 00050 delete [] Data; 00051 } 00052 00054 ui GetFreeSpace() const 00055 { 00056 return Allocated - Used; 00057 } 00058 00060 00064 void Reallocate(const ui size) 00065 { 00066 //This can only be called before this block is used for the first time. 00067 assert(!(Used || PtrsGiven || PtrsDeleted)); 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 00083 00086 class Allocator 00087 { 00088 private: 00090 ui NumBlocks; 00091 00093 /* 00094 * When the first block gets filled, we start taking memory from the 00095 * second one, and so on. 00096 */ 00097 ui CurrentBlock; 00098 00100 ui BlockMinSize; 00101 00103 Block * * Blocks; 00104 00105 public: 00107 00112 Allocator(const ui size, const ui blocks); 00113 00115 void Trim(); 00116 00118 00122 void DeleteSpace(c * const ptr); 00123 00125 00129 c * NewSpace(const ui size); 00130 00132 ~Allocator(); 00133 00134 private: 00135 00137 00140 void NewBlock(const ui minsize); 00141 00143 00144 Allocator(const Allocator &); 00145 00146 void operator = (const Allocator &); 00147 }; 00149 00150 } 00151 #endif