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

  1. //
  2. // rvMemSys.h - Memory management system
  3. // Date: 12/17/04
  4. // Created by: Dwight Luetscher
  5. //
  6. // Date: 04/01/05
  7. // Modified by: Marcus Whitlock
  8. // Added permanent heap and new heap push function to push the heap that contains
  9. // a specified piece of memory.
  10. //
  11. // Date: 04/04/05
  12. // Modified by: Marcus Whitlock
  13. // Added rvAutoHeapCtxt class for push and auto-pop of heap context.
  14. //
  15.  
  16. #ifndef __RV_MEM_SYS_H__
  17. #define __RV_MEM_SYS_H__
  18.  
  19. typedef enum 
  20. {
  21.     RV_HEAP_ID_DEFAULT,            // heap that exists on application startup
  22.     RV_HEAP_ID_PERMANENT,        // heap for allocations that have permanent (application scope) lifetime
  23.     RV_HEAP_ID_LEVEL,            // heap for allocations that have a level lifetime
  24.     RV_HEAP_ID_MULTIPLE_FRAME,    // heap for run-time allocations that have a lifetime of multiple draw frames 
  25.     RV_HEAP_ID_SINGLE_FRAME,    // heap for run-time allocations that have a lifetime of a single draw frame
  26.     RV_HEAP_ID_TEMPORARY,        // heap for objects that have a short lifetime (temporaries generally used for level loading)
  27.     RV_HEAP_ID_IO_TEMP,            // heap for allocations that are temporaries used in I/O operations like level loading or writing out data
  28.     rv_heap_ID_max_count        // just a count, not a valid type
  29. Rv_Sys_Heap_ID_t;
  30.  
  31. static const uint MAX_SYSTEM_HEAPS    = (uint) rv_heap_ID_max_count;
  32.  
  33. #ifdef _RV_MEM_SYS_SUPPORT
  34.  
  35. //
  36. //    _RV_MEM_SYS_SUPPORT is defined
  37. //
  38.  
  39. extern rvHeapArena *currentHeapArena;    
  40.  
  41. // Functions for getting and setting the system heaps.
  42. void    rvSetSysHeap( Rv_Sys_Heap_ID_t sysHeapID, rvHeap *heapPtr );                    // associates a heap with the given system heap ID value
  43. rvHeap* rvGetSysHeap( Rv_Sys_Heap_ID_t sysHeapID );                                        // retrieves the specified system heap
  44. void    rvGetAllSysHeaps( rvHeap *destSystemHeapArray[MAX_SYSTEM_HEAPS] );                // retrieves all the MAX_SYSTEM_HEAPS heap pointers into the given array
  45. void    rvSetAllSysHeaps( rvHeap *srcSystemHeapArray[MAX_SYSTEM_HEAPS] );                // associates all the MAX_SYSTEM_HEAPS heap pointers from the given array with their corresponding id value
  46. bool    rvPushHeapContainingMemory( const void* mem );                                    // pushes the heap containg the memory specified to the top of the arena stack, making it current - mwhitlock
  47.  
  48. void rvEnterArenaCriticalSection( );                            // enters the heap arena critical section
  49. void rvExitArenaCriticalSection( );                                // exits the heap arena critical section
  50. void rvPushSysHeap(Rv_Sys_Heap_ID_t sysHeapID);                    // pushes the system heap associated with the given identifier to the top of the arena stack, making it current
  51.  
  52. // Useful in situations where a heap is pushed, but a return on error or an
  53. // exception could cause the stack to be unwound, bypassing the heap pop
  54. // operation - mwhitlock.
  55. class rvAutoHeapCtxt
  56. {
  57.     bool mPushed;
  58.  
  59. public:
  60.     rvAutoHeapCtxt(void) :
  61.         mPushed(false)
  62.     {
  63.         // Should never call this.
  64.         assert(0);
  65.     }
  66.  
  67.     rvAutoHeapCtxt(Rv_Sys_Heap_ID_t sysHeapID) :
  68.         mPushed(false)
  69.     {
  70.         rvPushSysHeap(sysHeapID);
  71.         mPushed = true;
  72.     }
  73.  
  74.     rvAutoHeapCtxt(const void* mem) :
  75.         mPushed(false)
  76.     {
  77.         mPushed = rvPushHeapContainingMemory( mem );
  78.     }
  79.  
  80.     ~rvAutoHeapCtxt(void)
  81.     {
  82.         if(mPushed)
  83.         {
  84.             currentHeapArena->Pop();
  85.         }
  86.     }
  87. };
  88.  
  89. //
  90. //    RV_PUSH_SYS_HEAP_ID()
  91. //    Push system heaps by their ID (always available to idLib, Game and executable).
  92. //
  93. #define RV_PUSH_SYS_HEAP_ID(sysHeapID)                rvPushSysHeap(sysHeapID)
  94. #define RV_PUSH_SYS_HEAP_ID_AUTO(varName,sysHeapID)    rvAutoHeapCtxt varName(sysHeapID)
  95.  
  96. //
  97. //    RV_PUSH_HEAP_MEM()
  98. //    Push the heap containing the piece of memory pointed to. Note that if the
  99. //    piece of memory is not from a heap, no heap will be pushed.
  100. //
  101. #define RV_PUSH_HEAP_MEM(memPtr)                    rvPushHeapContainingMemory(memPtr)
  102. #define RV_PUSH_HEAP_MEM_AUTO(varName,memPtr)        rvAutoHeapCtxt varName(memPtr)
  103.  
  104. //
  105. //    RV_PUSH_HEAP_PTR()
  106. //    Local heaps used mainly by executable (idLib and Game would use these only
  107. //    if heap was passed in)
  108. //
  109. #define RV_PUSH_HEAP_PTR(heapPtr)                    ( (heapPtr)->PushCurrent() )
  110.  
  111. //
  112. //    RV_PUSH_SYS_HEAP()
  113. //    Pop top of heap stack, regardless of how it was pushed.
  114. //
  115. #define RV_POP_HEAP()                                ( currentHeapArena->Pop() )
  116.  
  117. // The following versions enter/exit the heap arena's critical section so that
  118. // critical section protection remains active between a push/pop pair (NOTE that
  119. // the heap and heap arena are always protected by critical sections within a single method call)
  120. #define RV_PUSH_SYS_HEAP_ENTER_CRIT_SECT(sysHeapID)    { rvEnterArenaCriticalSection( ); rvPushSysHeap( sysHeapID ); }
  121. #define RV_PUSH_HEAP_ENTER_CRIT_SECT(heapPtr)        { rvEnterArenaCriticalSection( ); (heapPtr)->PushCurrent( ); }
  122. #define RV_POP_HEAP_EXIT_CRIT_SECT()                { currentHeapArena->Pop( ); rvExitArenaCriticalSection( ); }
  123.  
  124. #else    // #ifdef _RV_MEM_SYS_SUPPORT
  125.  
  126. //
  127. //    _RV_MEM_SYS_SUPPORT is not defined
  128. //
  129.  
  130. #define RV_PUSH_SYS_HEAP_ID(sysHeapID)
  131. #define RV_PUSH_SYS_HEAP_ID_AUTO(varName,sysHeapID)
  132. #define RV_PUSH_HEAP_MEM(memPtr)
  133. #define RV_PUSH_HEAP_MEM_AUTO(varName,memPtr)
  134. #define RV_PUSH_HEAP_PTR(heapPtr)
  135. #define RV_POP_HEAP()    
  136.  
  137. #define RV_PUSH_SYS_HEAP_ENTER_CRIT_SECT(sysHeapID)
  138. #define RV_PUSH_HEAP_ENTER_CRIT_SECT(heapPtr)    
  139. #define RV_POP_HEAP_EXIT_CRIT_SECT()            
  140.  
  141. #endif    // #else not #ifdef _RV_MEM_SYS_SUPPORT
  142.  
  143. #endif    // #ifndef __RV_MEM_SYS_H__
  144.