NLMISC::CHeapMemory Class Reference

#include <heap_memory.h>


Detailed Description

A Heap manager. Work with any kind of memory. This heap manager is not designed for speed (because it stills use standard heap allocation), but for use with special memory or special cases where malloc/new cannot be used.
Author:
Lionel Berenguier

Nevrax France

Date:
2001

Definition at line 46 of file heap_memory.h.

Public Member Functions

void * allocate (uint size)
 CHeapMemory ()
 Constructor.

void free (void *ptr)
 free a block allocated with alloate(). no-op if NULL. nlstop() if don't find this block.

uint getHeapSize () const
 return the size passed in setHeap().

uint getHeapSizeUsed () const
 return the heap size allocated.

void initHeap (void *heap, uint size, uint align=4)
void reset ()
 reset the entire container. NB: no free() is made on the heap.

 ~CHeapMemory ()

Private Types

typedef TAllocatedSpaceMap::iterator ItAllocatedSpaceMap
typedef TEmptySpacePtrMap::iterator ItEmptySpacePtrMap
typedef TEmptySpaceSizeMap::iterator ItEmptySpaceSizeMap
typedef std::map< uint8 *,
uint
TAllocatedSpaceMap
typedef std::map< uint8 *,
CEmptySpace
TEmptySpacePtrMap
typedef std::multimap< uint,
uint8 * > 
TEmptySpaceSizeMap

Private Member Functions

void addEmptySpace (CEmptySpace &space)
void removeEmptySpace (CEmptySpace &space)

Private Attributes

uint _Alignment
TAllocatedSpaceMap _AllocatedSpaceMap
TEmptySpaceSizeMap _EmptySpaceMap
 for allocate method, the size -> empty space map.

TEmptySpacePtrMap _EmptySpaces
 The array of empty spaces.

uint8_HeapPtr
uint _HeapSize
uint _HeapSizeUsed


Member Typedef Documentation

typedef TAllocatedSpaceMap::iterator NLMISC::CHeapMemory::ItAllocatedSpaceMap [private]
 

Definition at line 106 of file heap_memory.h.

Referenced by free().

typedef TEmptySpacePtrMap::iterator NLMISC::CHeapMemory::ItEmptySpacePtrMap [private]
 

Definition at line 101 of file heap_memory.h.

Referenced by free().

typedef TEmptySpaceSizeMap::iterator NLMISC::CHeapMemory::ItEmptySpaceSizeMap [private]
 

Definition at line 85 of file heap_memory.h.

Referenced by allocate().

typedef std::map<uint8*, uint> NLMISC::CHeapMemory::TAllocatedSpaceMap [private]
 

Definition at line 105 of file heap_memory.h.

typedef std::map<uint8*, CEmptySpace> NLMISC::CHeapMemory::TEmptySpacePtrMap [private]
 

Definition at line 100 of file heap_memory.h.

typedef std::multimap<uint, uint8*> NLMISC::CHeapMemory::TEmptySpaceSizeMap [private]
 

Definition at line 84 of file heap_memory.h.


Constructor & Destructor Documentation

NLMISC::CHeapMemory::CHeapMemory  ) 
 

Constructor.

Definition at line 38 of file heap_memory.cpp.

References _Alignment, and reset().

00039 {
00040         reset();
00041         // For allocate to work even if the heap is not initialized.
00042         _Alignment= 4;
00043 }

NLMISC::CHeapMemory::~CHeapMemory  ) 
 

Definition at line 45 of file heap_memory.cpp.

References reset().

00046 {
00047         reset();
00048 }


Member Function Documentation

void NLMISC::CHeapMemory::addEmptySpace CEmptySpace space  )  [private]
 

Definition at line 107 of file heap_memory.cpp.

References _EmptySpaceMap, _EmptySpaces, NLMISC::CHeapMemory::CEmptySpace::Ptr, NLMISC::CHeapMemory::CEmptySpace::Size, and NLMISC::CHeapMemory::CEmptySpace::SizeIt.

Referenced by allocate(), free(), and initHeap().

00108 {
00109         // insert and get the iterator on the spaceMap.
00110         space.SizeIt= _EmptySpaceMap.insert( make_pair(space.Size, space.Ptr));
00111 
00112         // insert into the list of EmptySpaces.
00113         _EmptySpaces.insert( make_pair(space.Ptr, space) );
00114 }

void * NLMISC::CHeapMemory::allocate uint  size  ) 
 

allocate a block of size bytes. return NULL if not enough space or if size==0. NB: for alignements consideration, allocation are aligned to 4 bytes.

Definition at line 118 of file heap_memory.cpp.

References _Alignment, _AllocatedSpaceMap, _EmptySpaceMap, _EmptySpaces, _HeapSizeUsed, addEmptySpace(), ItEmptySpaceSizeMap, NLMISC::CHeapMemory::CEmptySpace::Ptr, removeEmptySpace(), NLMISC::CHeapMemory::CEmptySpace::Size, size, and uint.

Referenced by NL3D::CVertexBufferHeap::allocate(), NL3D::CVertexArrayRangeATI::allocateVB(), and NL3D::CVertexArrayRangeNVidia::allocateVB().

00119 {
00120         if(size==0)
00121                 return NULL;
00122 
00123         // Manage alignement.
00124         size= (size + (_Alignment-1)) & (~(_Alignment-1));
00125 
00126 
00127         // retrieve the best block.
00128         //=========================
00129         CEmptySpace             bestSpace;
00130         // NB: do a copy, because of removeEmptySpace() which delete the space.
00131 
00132         // Find the smaller space which is >= than size.
00133         ItEmptySpaceSizeMap             it;
00134         it= _EmptySpaceMap.lower_bound(size);
00135 
00136         // if not found, alloc fails.
00137         if(it == _EmptySpaceMap.end())
00138                 return NULL;
00139         else
00140         {
00141                 // NB: this space must exist in the "array".
00142                 bestSpace= _EmptySpaces[it->second];
00143         }
00144 
00145 
00146         // remove this empty space from list.
00147         //=========================
00148         removeEmptySpace(bestSpace);
00149 
00150 
00151         // if any, add the space unused to the list.
00152         //=========================
00153         if(bestSpace.Size > size)
00154         {
00155                 CEmptySpace             space;
00156                 space.Ptr= bestSpace.Ptr + size;
00157                 space.Size= bestSpace.Size - size;
00158 
00159                 addEmptySpace(space);
00160         }
00161 
00162 
00163         // return / insert the allocated space.
00164         //=========================
00165         _AllocatedSpaceMap.insert(make_pair(bestSpace.Ptr, size));
00166         _HeapSizeUsed+= size;
00167 
00168         // return the ptr of start of this empty space.
00169         return bestSpace.Ptr;
00170 }

void NLMISC::CHeapMemory::free void *  ptr  ) 
 

free a block allocated with alloate(). no-op if NULL. nlstop() if don't find this block.

Definition at line 173 of file heap_memory.cpp.

References _AllocatedSpaceMap, _EmptySpaces, _HeapSizeUsed, addEmptySpace(), ItAllocatedSpaceMap, ItEmptySpacePtrMap, nlassert, nlstop, NLMISC::CHeapMemory::CEmptySpace::Ptr, removeEmptySpace(), NLMISC::CHeapMemory::CEmptySpace::Size, size, uint, and uint8.

Referenced by NL3D::CVertexBufferHeap::free(), NL3D::CVertexArrayRangeATI::freeVB(), and NL3D::CVertexArrayRangeNVidia::freeVB().

00174 {
00175         if(ptr==NULL)
00176                 return;
00177 
00178         // Must find the array in allocated spaces.
00179         //==========================
00180         ItAllocatedSpaceMap             itAlloc= _AllocatedSpaceMap.find((uint8*)ptr);
00181         if(itAlloc == _AllocatedSpaceMap.end())
00182         {
00183                 nlstop;
00184                 return;
00185         }
00186         uint    size= itAlloc->second;
00187 
00188         // free this space from allocated Spaces.
00189         _AllocatedSpaceMap.erase(itAlloc);
00190         _HeapSizeUsed-= size;
00191 
00192 
00193         // Must find previous or/and next empty space, if any.
00194         //==========================
00195         ItEmptySpacePtrMap              itPrevious, itNext;
00196 
00197         // find the empty space which is immediately >= than ptr.
00198         itNext= _EmptySpaces.lower_bound((uint8*)ptr);
00199         // NB: it may be end(), if it is the last block (very rare).
00200 
00201         // some check. next empty space ptr must be after ptr.
00202         if(itNext!=_EmptySpaces.end())
00203         {
00204                 nlassert(itNext->second.Ptr >= (uint8*)ptr + size);
00205         }
00206 
00207         // if itNext is not the first empty space, there is an empty space before us.
00208         if( itNext!= _EmptySpaces.begin() )
00209         {
00210                 // NB: work even if itNext==end().
00211                 itPrevious= itNext;
00212                 itPrevious--;
00213                 // some check. previous empty space ptr must be before ptr.
00214                 nlassert(itPrevious!=_EmptySpaces.end());
00215                 nlassert(itPrevious->second.Ptr + itPrevious->second.Size <= (uint8*)ptr );
00216         }
00217         else
00218                 itPrevious= _EmptySpaces.end();
00219 
00220 
00221         // if next exist.
00222         if(itNext!=_EmptySpaces.end())
00223         {
00224                 // If Previous is not just after allocated ptr, it means that there is some allocated blocks beetween,
00225                 // so it is not a valid empty space to concat.
00226                 if(itNext->second.Ptr != (uint8*)ptr + size)
00227                         itNext= _EmptySpaces.end();
00228         }
00229         // if previous exist.
00230         if(itPrevious!=_EmptySpaces.end())
00231         {
00232                 // If Previous is not just before allocated ptr, it means that there is some allocated blocks beetween,
00233                 // so it is not a valid empty space to concat.
00234                 if(itPrevious->second.Ptr + itPrevious->second.Size != (uint8*)ptr )
00235                         itPrevious=_EmptySpaces.end();
00236         }
00237 
00238 
00239 
00240         // According to configuration, build the new empty space, mreging previous and next, and remove old ones.
00241         //==========================
00242         CEmptySpace             newSpace;
00243 
00244         // if no previous empty space, then newSpace start at ptr.
00245         if(itPrevious == _EmptySpaces.end())
00246         {
00247                 // Start with old allocated block.
00248                 newSpace.Ptr= (uint8*)ptr;
00249                 newSpace.Size= size;
00250         }
00251         // else, start at previous Ptr.
00252         else
00253         {
00254                 // Start with previous block. size is previous size + allocated block size.
00255                 newSpace.Ptr= itPrevious->second.Ptr;
00256                 newSpace.Size= itPrevious->second.Size + size;
00257         }
00258 
00259         // if next empty space, must inc size.
00260         if(itNext != _EmptySpaces.end())
00261         {
00262                 newSpace.Size+= itNext->second.Size;
00263         }
00264 
00265 
00266         // remove old empty space, and add new one.
00267         //==========================
00268 
00269         // remove old empty spaces.
00270         if(itPrevious != _EmptySpaces.end())
00271                 removeEmptySpace(itPrevious->second);
00272         if(itNext != _EmptySpaces.end())
00273                 removeEmptySpace(itNext->second);
00274 
00275 
00276         // Add the new concatenated empty space.
00277         addEmptySpace(newSpace);
00278 }

uint NLMISC::CHeapMemory::getHeapSize  )  const [inline]
 

return the size passed in setHeap().

Definition at line 67 of file heap_memory.h.

References _HeapSize, and uint.

00067 {return _HeapSize;}

uint NLMISC::CHeapMemory::getHeapSizeUsed  )  const [inline]
 

return the heap size allocated.

Definition at line 69 of file heap_memory.h.

References _HeapSizeUsed, and uint.

00069 {return _HeapSizeUsed;}

void NLMISC::CHeapMemory::initHeap void *  heap,
uint  size,
uint  align = 4
 

init the heap. this reset() the heap. heap ptr is stored in this class, but no CHeapMemory methods write or read into. They use standard heap instead (new / delete).

Parameters:
heap the heap ptr. heap should be at least N-bytes aligned, where N is the alignment you need (see param align, 4 by default).
align Any size given to allocate() will be rounded to match this alignement. Valid values are 4,8,16, or 32. 4 by default.

Definition at line 64 of file heap_memory.cpp.

References _Alignment, _HeapPtr, _HeapSize, _HeapSizeUsed, addEmptySpace(), nlstop, NLMISC::CHeapMemory::CEmptySpace::Ptr, reset(), NLMISC::CHeapMemory::CEmptySpace::Size, size, uint, and uint8.

Referenced by NL3D::CVertexArrayRangeATI::allocate(), NL3D::CVertexArrayRangeNVidia::allocate(), and NL3D::CVertexBufferHeap::init().

00065 {
00066         // setup alignement.
00067         if(align!=4 && align!=8 && align!=16 && align!=32)
00068         {
00069                 nlstop;
00070                 align= 4;
00071         }
00072         _Alignment= align;
00073 
00074         // Manage alignement.
00075         size= (size) & (~(_Alignment-1));
00076 
00077         // clear container.
00078         reset();
00079         if(heap==0 || size==0)
00080                 return;
00081 
00082         _HeapPtr= (uint8*)heap;
00083         _HeapSize= size;
00084         _HeapSizeUsed= 0;
00085 
00086         // Add the only one empty space.
00087         CEmptySpace             space;
00088         space.Ptr= _HeapPtr;
00089         space.Size= _HeapSize;
00090 
00091         addEmptySpace(space);
00092 }

void NLMISC::CHeapMemory::removeEmptySpace CEmptySpace space  )  [private]
 

Definition at line 96 of file heap_memory.cpp.

References _EmptySpaceMap, _EmptySpaces, NLMISC::CHeapMemory::CEmptySpace::Ptr, and NLMISC::CHeapMemory::CEmptySpace::SizeIt.

Referenced by allocate(), and free().

00097 {
00098         // remove the iterator on the spaceMap.
00099         _EmptySpaceMap.erase( space.SizeIt );
00100 
00101         // remove from the list of EmptySpaces. NB: must fo it after all, because "space" may be deleted.
00102         _EmptySpaces.erase( space.Ptr );
00103 }

void NLMISC::CHeapMemory::reset  ) 
 

reset the entire container. NB: no free() is made on the heap.

Definition at line 52 of file heap_memory.cpp.

References _AllocatedSpaceMap, _EmptySpaceMap, _EmptySpaces, _HeapPtr, _HeapSize, and _HeapSizeUsed.

Referenced by CHeapMemory(), NL3D::CVertexArrayRangeATI::free(), NL3D::CVertexArrayRangeNVidia::free(), initHeap(), and ~CHeapMemory().

00053 {
00054         _EmptySpaces.clear();
00055         _EmptySpaceMap.clear();
00056         _AllocatedSpaceMap.clear();
00057         _HeapPtr= NULL;
00058         _HeapSize= 0;
00059         _HeapSizeUsed= 0;
00060 }


Field Documentation

uint NLMISC::CHeapMemory::_Alignment [private]
 

Definition at line 113 of file heap_memory.h.

Referenced by allocate(), CHeapMemory(), and initHeap().

TAllocatedSpaceMap NLMISC::CHeapMemory::_AllocatedSpaceMap [private]
 

Definition at line 120 of file heap_memory.h.

Referenced by allocate(), free(), and reset().

TEmptySpaceSizeMap NLMISC::CHeapMemory::_EmptySpaceMap [private]
 

for allocate method, the size -> empty space map.

Definition at line 118 of file heap_memory.h.

Referenced by addEmptySpace(), allocate(), removeEmptySpace(), and reset().

TEmptySpacePtrMap NLMISC::CHeapMemory::_EmptySpaces [private]
 

The array of empty spaces.

Definition at line 116 of file heap_memory.h.

Referenced by addEmptySpace(), allocate(), free(), removeEmptySpace(), and reset().

uint8* NLMISC::CHeapMemory::_HeapPtr [private]
 

Definition at line 110 of file heap_memory.h.

Referenced by initHeap(), and reset().

uint NLMISC::CHeapMemory::_HeapSize [private]
 

Definition at line 111 of file heap_memory.h.

Referenced by getHeapSize(), initHeap(), and reset().

uint NLMISC::CHeapMemory::_HeapSizeUsed [private]
 

Definition at line 112 of file heap_memory.h.

Referenced by allocate(), free(), getHeapSizeUsed(), initHeap(), and reset().


The documentation for this class was generated from the following files:
Generated on Tue Mar 16 13:16:53 2004 for NeL by doxygen 1.3.6