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.

78 lines
1.8 KiB

  1. //+----------------------------------------------------------------------------
  2. //
  3. // File: regutil.cpp
  4. //
  5. // Module: CMSETUP.LIB
  6. //
  7. // Synopsis: Memory utility functions taken from cmutil. Bare minimum of functionality
  8. // used in Cmutil, but gives a simple Heapalloc wrapper.
  9. //
  10. // Copyright (c) 1998-1999 Microsoft Corporation
  11. //
  12. // Author: quintinb Created 10/06/98
  13. //
  14. //+----------------------------------------------------------------------------
  15. #ifndef __SETUPMEM_CPP
  16. #define __SETUPMEM_CPP
  17. #include "cmsetup.h"
  18. //+----------------------------------------------------------------------------
  19. // definitions
  20. //+----------------------------------------------------------------------------
  21. #ifdef DEBUG
  22. LONG g_lMallocCnt = 0; // a counter to detect memory leak
  23. #endif
  24. void *CmRealloc(void *pvPtr, size_t nBytes)
  25. {
  26. void* p = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, pvPtr, nBytes);
  27. CMASSERTMSG(p, TEXT("CmRealloc failed"));
  28. return p;
  29. }
  30. void *CmMalloc(size_t nBytes)
  31. {
  32. #ifdef DEBUG
  33. InterlockedIncrement(&g_lMallocCnt);
  34. #endif
  35. MYDBGASSERT(nBytes < 1024*1024); // It should be less than 1 MB
  36. void* p = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, nBytes);
  37. CMASSERTMSG(p, TEXT("CmMalloc failed"));
  38. return p;
  39. }
  40. void CmFree(void *pvPtr)
  41. {
  42. if (pvPtr)
  43. {
  44. MYVERIFY(HeapFree(GetProcessHeap(), 0, pvPtr));
  45. #ifdef DEBUG
  46. InterlockedDecrement(&g_lMallocCnt);
  47. #endif
  48. }
  49. }
  50. void EndDebugMemory()
  51. {
  52. #ifdef DEBUG
  53. if (g_lMallocCnt)
  54. {
  55. TCHAR buf[256];
  56. wsprintf(buf, TEXT("Detect Memory Leak of %d blocks"), g_lMallocCnt);
  57. CMASSERTMSG(FALSE, buf);
  58. }
  59. #endif
  60. }
  61. #endif //__SETUPMEM_CPP