Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

121 lines
3.1 KiB

  1. /*++
  2. Copyright (c) 1998 Microsoft Corporation
  3. All rights reserved.
  4. Module:
  5. xmemwrpr.h
  6. Abstract:
  7. Wrapper of exchmem.
  8. User of this wrapper: you should create the heap using
  9. ExchMHeapCreate before allocating any memory from EXCHMEM.
  10. You should destroy the heap using ExchMHeapDestroy.
  11. For raw memory allocation from heap, you should use PvAlloc,
  12. PvRealloc and FreePv functions.
  13. For object creation, unless you overload new yourself in
  14. the class defintion, using "new" will go to EXCHMEM, using
  15. "XNEW" will go to EXCHMEM while at the same time catch the
  16. file and line information for that allocation ( this is
  17. for debug version only )
  18. Authors:
  19. KangYan Kangrong Yan Sept. 29, 1998
  20. History:
  21. 09/29/98 KangYan Created
  22. --*/
  23. #if !defined(_XMEMWRPR_H_)
  24. #define _XMEMWRPR_H_
  25. #include <exchmem.h>
  26. #include <dbgtrace.h>
  27. //
  28. // Define number of exchmem heaps if not already defined
  29. //
  30. #if !defined(NUM_EXCHMEM_HEAPS)
  31. #define NUM_EXCHMEM_HEAPS 0
  32. #endif
  33. //
  34. // Macros for major heap allocation functions. ( We follow
  35. // exchange store's convention here )
  36. //
  37. #if defined( DEBUG )
  38. #define PvAlloc(_cb) ExchMHeapAllocDebug(_cb, __FILE__, __LINE__)
  39. #define PvRealloc(_pv, _cb) ExchMHeapReAllocDebug(_pv, _cb, __FILE__, __LINE__)
  40. #define FreePv(_pv) ExchMHeapFree(_pv)
  41. #else
  42. #define PvAlloc(_cb) ExchMHeapAlloc(_cb)
  43. #define PvRealloc(_pv, _cb) ExchMHeapReAlloc(_pv, _cb)
  44. #define FreePv(_pv) ExchMHeapFree(_pv)
  45. #endif
  46. //
  47. // Operator XNEW, XDELETE are defined to replace "new" where xchmem wants to
  48. // be used so that we can pass in file name and line number of each allocation
  49. // to make catching leaks much easier in debug builds. In rtl builds,
  50. // the file name or line number will not be passed in
  51. //
  52. #if defined( DEBUG )
  53. #define XNEW new(__FILE__,__LINE__)
  54. #else
  55. #define XNEW new
  56. #endif
  57. #define XDELETE delete
  58. //
  59. // Overload global new operators
  60. //
  61. __inline void * __cdecl operator new(size_t size, char *szFile, unsigned int uiLine )
  62. {
  63. void *p = ExchMHeapAllocDebug( size, szFile, uiLine );
  64. SetLastError( p? NO_ERROR : ERROR_NOT_ENOUGH_MEMORY );
  65. return p;
  66. }
  67. __inline void * __cdecl operator new( size_t size )
  68. {
  69. void *p = ExchMHeapAlloc( size );
  70. SetLastError( p? NO_ERROR : ERROR_NOT_ENOUGH_MEMORY );
  71. return p;
  72. }
  73. //
  74. // Overload global delete operator, one version only
  75. //
  76. __inline void __cdecl operator delete( void *pv )
  77. {
  78. ExchMHeapFree( pv );
  79. SetLastError( NO_ERROR );
  80. }
  81. //
  82. // Create creation wrappers
  83. //
  84. __inline BOOL CreateGlobalHeap( DWORD cHeaps, DWORD dwFlag, DWORD dwInit, DWORD dwMax ) {
  85. if ( ExchMHeapCreate( cHeaps, dwFlag, dwInit, dwMax ) ) {
  86. SetLastError( NO_ERROR );
  87. return TRUE;
  88. } else {
  89. _ASSERT( 0 );
  90. return FALSE;
  91. }
  92. }
  93. __inline BOOL DestroyGlobalHeap() {
  94. if ( ExchMHeapDestroy() ) {
  95. return TRUE;
  96. } else {
  97. _ASSERT( 0 );
  98. return FALSE;
  99. }
  100. }
  101. #endif