home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1997 May / Pcwk0597.iso / sybase / starbuck / hpp.z / WMEMORY.HPP < prev    next >
C/C++ Source or Header  |  1996-10-18  |  8KB  |  236 lines

  1. /*************************************************************************
  2.  *
  3.  * WMemory 
  4.  *
  5.  *************************************************************************/
  6.  
  7. #ifndef _WMEMORY_HPP_INCLUDED
  8. #define _WMEMORY_HPP_INCLUDED
  9.  
  10. #ifndef _WNO_PRAGMA_PUSH
  11. #pragma pack(push,8);
  12. #pragma enum int;
  13. #endif
  14.  
  15. #ifndef _WDEF_HPP_INCLUDED
  16. #  include "wdef.hpp"
  17. #endif
  18. #ifndef _WLLIST_HPP_INCLUDED
  19. #  include "wllist.hpp"
  20. #endif
  21.  
  22. //
  23. // Memory tracking flags
  24. //
  25.  
  26. #define WMEMORY_CHECKFOROVERWRITE 0x00000001L
  27. #define WMEMORY_TRACKMEMORY       0x00000002L
  28. #define WMEMORY_TRAPNOMEMORY      0x00000004L
  29. #define WMEMORY_WALKHEAPS         0x00000008L
  30. #define WMEMORY_USE_HEAPALLOC     0x00000010L
  31. #define WMEMORY_PRINT_ALL         0x80000000L
  32. #define WMEMORY_ZERO_MEMORY       0x40000000L
  33.  
  34. #define WMEMORY_ALLCHECKS         (WMEMORY_CHECKFOROVERWRITE|\
  35.                                    WMEMORY_TRACKMEMORY|\
  36.                                    WMEMORY_WALKHEAPS)
  37.  
  38. #define WMEMORY_NOCHECKS          0x00000000L
  39.  
  40. //
  41. // WMemoryAllocator -- Use this structure to define the low-level
  42. //                     C-type functions used to allocate memory.
  43. //                     You must declare the structure statically.
  44. //
  45.  
  46. typedef void * WHeapHandle;
  47.  
  48. typedef void * __stdcall (*WHeapAllocator)( WHeapHandle heap, WULong memSize );
  49. typedef void * __stdcall (*WHeapReallocator)( WHeapHandle heap, void *mem,
  50.                                               WULong newSize );
  51. typedef WBool  __stdcall (*WHeapDeallocator)( WHeapHandle heap, void *mem );
  52. typedef WBool  __stdcall (*WHeapValidator)( WHeapHandle heap, void *mem );
  53.  
  54. typedef WHeapHandle __stdcall (*WHeapCreator)( WULong initialSize,
  55.                                                WULong maximumSize );
  56. typedef WBool       __stdcall (*WHeapDestructor)( WHeapHandle heap );
  57.  
  58. struct WCMCLASS WMemoryHeap {
  59.     wllist_link       link;
  60.     const WChar      *heapName;
  61.     WHeapHandle       heap;
  62.     WULong            initialSize;
  63.     WULong            maximumSize;
  64.  
  65.     WHeapAllocator    allocate;
  66.     WHeapDeallocator  deallocate;
  67.     WHeapReallocator  reallocate;
  68.     WHeapValidator    validate;
  69.     WHeapCreator      create;
  70.     WHeapDestructor   destruct;
  71. };
  72.  
  73. //
  74. // Memory dumping -- for debugging purposes.
  75. //
  76.  
  77. typedef WBool WCMDEF (*WMemoryDumper)( void *memory,
  78.                                         const WChar **memoryType,
  79.                                         WChar *buffer, WULong len );
  80.  
  81. #ifdef _DEBUG
  82. #define WAttachDumper(mem,dump) WMemory::AttachDumpRoutine((void*)mem,dump)
  83. #else
  84. #define WAttachDumper(mem,dump)
  85. #endif
  86.  
  87. //
  88. // WMemory
  89. //
  90.  
  91. class WCMCLASS WMemory {
  92.  
  93.     public:
  94.  
  95.         /**********************************************************
  96.          * Constructors and Destructors
  97.          *********************************************************/
  98.  
  99.         WMemory();
  100.  
  101.         virtual ~WMemory();
  102.  
  103.         /**********************************************************
  104.          * Properties
  105.          *********************************************************/
  106.  
  107.         // ArrayHeap
  108.  
  109.         static WMemoryHeap *GetArrayHeap();
  110.         static WBool        SetArrayHeap( WMemoryHeap *heap );
  111.  
  112.         // Heap
  113.  
  114.         static WMemoryHeap *GetHeap();
  115.         static WBool        SetHeap( WMemoryHeap *heap );
  116.  
  117.         // CurrentMemoryUsage
  118.  
  119.         static WULong GetCurrentMemoryUsage();
  120.     
  121.         // PeakMemoryUsage
  122.  
  123.         static WULong GetPeakMemoryUsage();
  124.     
  125.         /**********************************************************
  126.          * Methods
  127.          *********************************************************/
  128.  
  129.         // Allocate
  130.         //
  131.         //     Allocate memory from a given heap.  If no heap
  132.         //     specified, use the thread default.
  133.  
  134.         static void  *Allocate( WULong size, WBool array,
  135.                                 WMemoryHeap *allocator=NULL );
  136.         static void  *Allocate( WULong size, WBool array,
  137.                                 const WChar *functionName,
  138.                                 const WChar *fileName, WULong line,
  139.                                 WMemoryHeap *allocator=NULL );
  140.  
  141.         // AttachDumpRoutine
  142.         //
  143.         //     After a piece of memory has been allocated, call
  144.         //     this routine to attach a specific dump routine to it.
  145.  
  146.         static WBool  AttachDumpRoutine( void *memory,
  147.                                          WMemoryDumper dumpRoutine );
  148.  
  149.         // Deallocate
  150.         //
  151.         //     Deallocate a specific piece of memory.
  152.  
  153.         static WBool  Deallocate( void *memory, WBool array );
  154.         static WBool  Deallocate( void *memory, WBool array,
  155.                                   const WChar *functionName,
  156.                                   const WChar *fileName, WULong line );
  157.  
  158.         // Reallocate
  159.         //
  160.         //     Reallocate a specific piece of memory.
  161.  
  162.         static void  *Reallocate( void *memory, WULong newSize,
  163.                                   WBool array );
  164.         static void  *Reallocate( void *memory, WULong newSize,
  165.                                   WBool array,
  166.                                   const WChar *functionName,
  167.                                   const WChar *fileName, WULong line );
  168.  
  169.         // Validate
  170.         //
  171.         //     Check to see if a piece of memory is valid.  If NULL
  172.         //     is passed, checks whole heap.  Only implemented in
  173.         //     the debugging DLL.
  174.  
  175.         static WBool Validate( void *memory, WMemoryHeap *allocator );
  176.  
  177.         /**********************************************************
  178.          * Internal
  179.          *********************************************************/
  180.  
  181.         // LogNextDelete
  182.         //
  183.         //     Set the location that the next delete will come from.
  184.         //     Used in debugging mode with memory tracking.
  185.  
  186.         static WBool LogNextDelete( void *ptr, const WChar *functionName,
  187.                                     const WChar *fileName, WULong line );
  188.  
  189.         // FreeDeleteLog
  190.         //
  191.         //     Free the delete log for a thread.  Call occasionally.
  192.  
  193.         static WBool FreeDeleteLog();
  194.  
  195.         /**********************************************************
  196.          * Data members
  197.          *********************************************************/
  198.  
  199.     public:
  200. };
  201.  
  202. //
  203. // Use these macros to redefine new & delete
  204. //
  205.  
  206. #define WREDEFINENEW \
  207.     void *operator new( size_t size ) \
  208.                      { return WMemory::Allocate( size, FALSE ); } \
  209.     void *operator new( size_t size, const WChar *funcName, \
  210.                         const WChar *fileName, WULong line ) \
  211.                      { return WMemory::Allocate( size, FALSE, funcName, \
  212.                                                  fileName, line ); } \
  213.     void *operator new( size_t, void *p ) { return p; } \
  214.     void *operator new []( size_t size ) \
  215.                      { return WMemory::Allocate( size, TRUE ); } \
  216.     void *operator new []( size_t size, const WChar *funcName,\
  217.                            const WChar *fileName, WULong line ) \
  218.                      { return WMemory::Allocate( size, TRUE, funcName,\
  219.                                                  fileName, line ); } \
  220.     void *operator new []( size_t, void *p ) { return p; }
  221.  
  222. #define WREDEFINEDELETE \
  223.     void operator delete( void *p ) { WMemory::Deallocate( p, FALSE ); } \
  224.     void operator delete []( void *p ) { WMemory::Deallocate( p, TRUE ); }
  225.  
  226. #define W_REDEFINENEW    WREDEFINENEW
  227. #define W_REDEFINEDELETE WREDEFINEDELETE
  228.  
  229. #ifndef _WNO_PRAGMA_PUSH
  230. #pragma enum pop;
  231. #pragma pack(pop);
  232. #endif
  233.  
  234. #endif // _WMEMORY_HPP_INCLUDED
  235.  
  236.