home *** CD-ROM | disk | FTP | other *** search
/ GameStar 2006 March / Gamestar_82_2006-03_dvd.iso / DVDStar / Editace / quake4_sdkv10.exe / source / idlib / rvHeapArena.h < prev    next >
C/C++ Source or Header  |  2005-11-14  |  5KB  |  140 lines

  1. //
  2. // rvHeapArena.h - Heap arena object that manages a set of heaps
  3. // Date: 12/13/04
  4. // Created by: Dwight Luetscher
  5. //
  6.  
  7. #ifndef __RV_HEAP_ARENA_H__
  8. #define __RV_HEAP_ARENA_H__
  9.  
  10. class rvHeap;
  11.  
  12. // Define some limits used by a heap arena
  13. static const int maxNumHeapsPerArena            = 16;    // maximum number of heaps that can be simultaneously initialized with the same arena
  14. static const int maxHeapStackDepth                = maxNumHeapsPerArena*2;    // maximum depth of each arena's heap stack
  15.  
  16.  
  17. class rvHeapArena {
  18. public:
  19.     rvHeapArena();                                // constructor
  20.     ~rvHeapArena();                                // destructor
  21.  
  22.     void Init( );                                // initializes this heap arena for use
  23.     void Shutdown( );                            // releases this heap arena from use (shutting down all associated heaps)
  24.  
  25.     ID_INLINE bool IsInitialized( ) const;        // returns true if this rvHeapArena object is currently initialized and not released, false otherwise
  26.  
  27.     // Push the current heap through the heap object itself (you can Pop() there as well)
  28.     void Pop( );                                // pops the top of the stack, restoring the previous heap as the active heap for this arena
  29.     ID_INLINE bool IsStackFull( );                // returns true if the heap arena stack is full, false otherwise
  30.  
  31.     ID_INLINE rvHeap *GetActiveHeap( );            // returns the active heap for this arena (from the top of the stack, NULL if stack is empty)
  32.  
  33.     ID_INLINE void EnterArenaCriticalSection();            // enters this heap arena's critical section
  34.     ID_INLINE void ExitArenaCriticalSection();            // exits this heap arena's critical section
  35.     
  36.     void *Allocate( unsigned int sizeBytes, int debugTag = 0);        // allocates the given amount of memory from this arena
  37.     void *Allocate16( unsigned int sizeBytes, int debugTag = 0);    // allocates the given amount of memory from this arena, aligned on a 16-byte boundary
  38.     void Free( void *p );                                            // free memory back to this arena
  39.  
  40.     int Msize( void *p );                        // returns the size, in bytes, of the allocation at the given address (including header, alignment bytes, etc).
  41.  
  42.     rvHeap *GetHeap( void *p );                    // returns the heap that the given allocation was made from, NULL for none
  43.  
  44.     ID_INLINE rvHeap *GetFirstHeap( );            // returns the first heap associated with this arena, NULL for none
  45.     rvHeap *GetNextHeap( rvHeap &rfPrevHeap );    // returns that follows the given one (associated with this arena), NULL for none
  46.  
  47.     void GetTagStats(int tag, int &num, int &size, int &peak);    // returns the total stats for a particular tag type (across all heaps managed by this arena)
  48.  
  49. protected:
  50.     // NOTE: we cannot use idList here - dynamic memory is not yet available.
  51.     rvHeap *m_heapStack[maxHeapStackDepth];        // stack of heap object pointers
  52.     CRITICAL_SECTION m_criticalSection;            // critical section associated with this heap
  53.     int m_tos;                                    // top of stack
  54.     rvHeap *m_heapList;                            // linked-list of all the heaps that are actively associated with this arena 
  55.     bool m_isInitialized;                        // set to true if this rvHeapArena object is currently initialized and not released
  56.  
  57.     void ResetValues( );                        // resets the data members to their pre-initialized state
  58.  
  59.     friend class rvHeap;                        // give the rvHeap class access to the following methods
  60.     // {
  61.     void Push( rvHeap &newActiveHeap );            // pushes the given heap onto the top of the stack making it the active one for this arena
  62.     void InitHeap( rvHeap &newActiveHeap );        // initializes the given heap to be under the care of this arena
  63.     void ShutdownHeap( rvHeap &newActiveHeap );    // releases the given heap from the care of this arena
  64.     // }
  65. };
  66.  
  67. // IsInitialized
  68. //
  69. // returns: true if this rvHeapArena object is currently initialized and not released, false otherwise
  70. ID_INLINE bool rvHeapArena::IsInitialized( ) const
  71. {
  72.     return m_isInitialized;
  73. }
  74.  
  75. // EnterArenaCriticalSection
  76. //
  77. // enters this heap arena's critical section
  78. ID_INLINE void rvHeapArena::EnterArenaCriticalSection() 
  79. {
  80.     ::EnterCriticalSection( &m_criticalSection );
  81. }
  82.  
  83. // ExitArenaCriticalSection
  84. //
  85. // exits this heap arena's critical section
  86. ID_INLINE void rvHeapArena::ExitArenaCriticalSection() 
  87. {
  88.     ::LeaveCriticalSection( &m_criticalSection );
  89. }    
  90.  
  91. // IsStackFull
  92. //
  93. // returns: true if the heap arena stack is full, false otherwise
  94. ID_INLINE bool rvHeapArena::IsStackFull( )
  95. {
  96.     bool full;
  97.  
  98.     EnterArenaCriticalSection();
  99.     full = m_tos >= (maxHeapStackDepth - 1);
  100.     ExitArenaCriticalSection();
  101.  
  102.     return full;
  103. }
  104.  
  105. // GetActiveHeap
  106. //
  107. // returns: the active heap for this arean (from the top of the stack, NULL if stack is empty)
  108. ID_INLINE rvHeap *rvHeapArena::GetActiveHeap( )
  109. {
  110.     rvHeap *tos;
  111.     EnterArenaCriticalSection();
  112.     if ( m_tos < 0 )
  113.     {
  114.         ExitArenaCriticalSection();
  115.         return NULL;
  116.     }
  117.     tos = m_heapStack[ m_tos ];
  118.     ExitArenaCriticalSection();
  119.     return tos;
  120. }
  121.  
  122. // GetFirstHeap
  123. //
  124. // returns: the first heap associated with this arena, NULL for none
  125. ID_INLINE rvHeap *rvHeapArena::GetFirstHeap( )
  126. {
  127.     rvHeap *firstHeap;
  128.  
  129.     EnterArenaCriticalSection();
  130.     firstHeap = m_heapList;
  131.     ExitArenaCriticalSection();
  132.  
  133.     return firstHeap;
  134. }
  135.  
  136. #endif    // #ifndef __RV_HEAP_MANAGER_H__
  137.  
  138.  
  139.  
  140.