Home | nevrax.com |
|
heap_memory.hGo to the documentation of this file.00001 00007 /* Copyright, 2001 Nevrax Ltd. 00008 * 00009 * This file is part of NEVRAX NEL. 00010 * NEVRAX NEL is free software; you can redistribute it and/or modify 00011 * it under the terms of the GNU General Public License as published by 00012 * the Free Software Foundation; either version 2, or (at your option) 00013 * any later version. 00014 00015 * NEVRAX NEL is distributed in the hope that it will be useful, but 00016 * WITHOUT ANY WARRANTY; without even the implied warranty of 00017 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00018 * General Public License for more details. 00019 00020 * You should have received a copy of the GNU General Public License 00021 * along with NEVRAX NEL; see the file COPYING. If not, write to the 00022 * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, 00023 * MA 02111-1307, USA. 00024 */ 00025 00026 #ifndef NL_HEAP_MEMORY_H 00027 #define NL_HEAP_MEMORY_H 00028 00029 #include "nel/misc/types_nl.h" 00030 #include <map> 00031 00032 00033 namespace NLMISC 00034 { 00035 00036 00037 // *************************************************************************** 00046 class CHeapMemory 00047 { 00048 public: 00049 00051 CHeapMemory(); 00052 ~CHeapMemory(); 00053 00055 void reset(); 00063 void initHeap(void *heap, uint size, uint align=4); 00064 00065 00067 uint getHeapSize() const {return _HeapSize;} 00069 uint getHeapSizeUsed() const {return _HeapSizeUsed;} 00070 00071 00075 void *allocate(uint size); 00077 void free(void *ptr); 00078 00079 00080 // ********************* 00081 private: 00082 00083 // The map size -> EmptySpace. 00084 typedef std::multimap<uint, uint8*> TEmptySpaceSizeMap; 00085 typedef TEmptySpaceSizeMap::iterator ItEmptySpaceSizeMap; 00086 00087 00088 struct CEmptySpace 00089 { 00090 // The adress of this empty space, in the heap 00091 uint8 *Ptr; 00092 // The size of this empty space. 00093 uint Size; 00094 // An iterator of his place in the TEmptySpaceMap. 00095 ItEmptySpaceSizeMap SizeIt; 00096 }; 00097 00098 00099 // The map ptr -> EmptySpace. 00100 typedef std::map<uint8*, CEmptySpace> TEmptySpacePtrMap; 00101 typedef TEmptySpacePtrMap::iterator ItEmptySpacePtrMap; 00102 00103 00104 // The map ptr -> size: AllocatedSpace; 00105 typedef std::map<uint8*, uint> TAllocatedSpaceMap; 00106 typedef TAllocatedSpaceMap::iterator ItAllocatedSpaceMap; 00107 00108 00109 private: 00110 uint8 *_HeapPtr; 00111 uint _HeapSize; 00112 uint _HeapSizeUsed; 00113 uint _Alignment; 00114 00116 TEmptySpacePtrMap _EmptySpaces; 00118 TEmptySpaceSizeMap _EmptySpaceMap; 00119 // The map of allocated blocks. 00120 TAllocatedSpaceMap _AllocatedSpaceMap; 00121 00122 00123 void removeEmptySpace(CEmptySpace &space); 00124 void addEmptySpace(CEmptySpace &space); 00125 }; 00126 00127 00128 } // NLMISC 00129 00130 00131 #endif // NL_HEAP_MEMORY_H 00132 00133 /* End of heap_memory.h */ |