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.

151 lines
3.5 KiB

  1. /**************************************************************************\
  2. *
  3. * Copyright (c) 1998 Microsoft Corporation
  4. *
  5. * Module Name:
  6. *
  7. * Memory management functions
  8. *
  9. * Abstract:
  10. *
  11. * Wrapper functions for memory management.
  12. * This file is C-includable.
  13. *
  14. * Revision History:
  15. *
  16. * 07/08/1999 agodfrey
  17. * Created it.
  18. * 09/07/1999 agodfrey
  19. * Moved the code into Runtime\mem.h
  20. *
  21. \**************************************************************************/
  22. #ifndef _MEM_H
  23. #define _MEM_H
  24. #include <malloc.h>
  25. #include "tags.h"
  26. #define GpMemset memset
  27. #define GpMemcpy memcpy
  28. #define GpMemcmp memcmp
  29. // Enable memory allocation checking only on the DBG build
  30. #if DBG
  31. #define GPMEM_ALLOC_CHK 1
  32. #define GPMEM_ALLOC_CHK_LIST 1 // List leaked blocks in debug output
  33. #endif
  34. #ifdef __cplusplus
  35. #define GPMEMHEAPINITIAL 32768 // 32K initial heap size
  36. #define GPMEMHEAPLIMIT 0 // No limit
  37. #define GPMEMHEAPFLAGS 0 // Our common Heap API flags
  38. //--------------------------------------------------------------------------
  39. // Building for native DLL
  40. //--------------------------------------------------------------------------
  41. // Our memory allocation functions.
  42. //
  43. // This file (only) is made C-includable so that we can use it in
  44. // dropped C code.
  45. extern "C" {
  46. #endif
  47. #if GPMEM_ALLOC_CHK_LIST
  48. void * __stdcall GpMallocDebug(size_t size, char *filename, int line);
  49. #define GpMalloc(size) GpMallocDebug(size, __FILE__, __LINE__)
  50. #if DBG
  51. void * __stdcall GpMallocAPIDebug(size_t size, char *fileName, int lineNumber);
  52. #define GpMallocAPI(size) GpMallocAPIDebug(size, __FILE__, __LINE__)
  53. #endif
  54. void GpTagMalloc(void * mem, GpTag tag, int bApi);
  55. #else
  56. void * __stdcall GpMalloc( size_t size );
  57. #if DBG
  58. // This is used to track API allocations on the debug build.
  59. void * __stdcall GpMallocAPI( size_t size );
  60. #endif
  61. #define GpTagMalloc(x,y,z)
  62. #endif
  63. void * __stdcall GpRealloc( void *memblock, size_t size );
  64. void __stdcall GpFree( void *memblock );
  65. #ifdef __cplusplus
  66. }
  67. // Hook new and delete
  68. #pragma optimize ( "t", on)
  69. // Don't ask me why we need 'static' here. But we do - otherwise
  70. // it generates out-of-line versions which cause a link clash with Office.
  71. static inline void* __cdecl operator new(size_t size)
  72. {
  73. return GpMalloc(size);
  74. }
  75. static inline void __cdecl operator delete(void* p)
  76. {
  77. GpFree(p);
  78. }
  79. static inline void* __cdecl operator new[](size_t size)
  80. {
  81. return GpMalloc(size);
  82. }
  83. static inline void __cdecl operator delete[](void* p)
  84. {
  85. GpFree(p);
  86. }
  87. static inline void* __cdecl operator new(size_t size, GpTag tag, int bApi)
  88. {
  89. #if GPMEM_ALLOC_CHK_LIST
  90. void * mem = GpMalloc(size);
  91. GpTagMalloc(mem, tag, bApi);
  92. return mem;
  93. #else
  94. return GpMalloc(size);
  95. #endif
  96. }
  97. static inline void* __cdecl operator new[](size_t size, GpTag tag, int bApi)
  98. {
  99. #if GPMEM_ALLOC_CHK_LIST
  100. void * mem = GpMalloc(size);
  101. GpTagMalloc(mem, tag, bApi);
  102. return mem;
  103. #else
  104. return GpMalloc(size);
  105. #endif
  106. }
  107. #pragma optimize ("", on)
  108. // TODO:
  109. //
  110. // Imaging code needs to hook to GpMalloc, GpFree etc.
  111. #endif
  112. /*
  113. * Assert that we didn't leak any memory.
  114. * Can only be called during shutdown.
  115. */
  116. extern void GpAssertShutdownNoMemoryLeaks();
  117. extern void GpInitializeAllocFailures();
  118. extern void GpDoneInitializeAllocFailureMode();
  119. extern void GpStartInitializeAllocFailureMode();
  120. #endif