Source code of Windows XP (NT5)
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.

97 lines
2.0 KiB

  1. /*****************************************************************/
  2. /** Microsoft Windows for Workgroups **/
  3. /** Copyright (C) Microsoft Corp., 1991-1992 **/
  4. /*****************************************************************/
  5. /* alloc.c --
  6. *
  7. * History:
  8. * 10/06/93 gregj Created.
  9. * 11/29/93 gregj Added debug instrumentation.
  10. *
  11. */
  12. #include "npcommon.h"
  13. #include <npalloc.h>
  14. #include <netlib.h>
  15. //====== Memory allocation functions =================================
  16. // Alloc a chunk of memory, quickly, with no 64k limit on size of
  17. // individual objects or total object size.
  18. //
  19. void * WINAPI MemAlloc(long cb)
  20. {
  21. return (void *)::LocalAlloc(LPTR, cb);
  22. }
  23. // Realloc one of above. If pb is NULL, then this function will do
  24. // an alloc for you.
  25. //
  26. void * WINAPI MemReAlloc(void * pb, long cb)
  27. {
  28. if (pb == NULL)
  29. return ::MemAlloc(cb);
  30. return (void *)::LocalReAlloc((HLOCAL)pb, cb, LMEM_MOVEABLE | LMEM_ZEROINIT);
  31. }
  32. // Free a chunk of memory alloced or realloced with above routines.
  33. //
  34. BOOL WINAPI MemFree(void * pb)
  35. {
  36. return ::LocalFree((HLOCAL)pb) ? TRUE : FALSE;
  37. }
  38. #ifdef DEBUG
  39. MEMWATCH::MEMWATCH(LPCSTR lpszLabel)
  40. : _lpszLabel(lpszLabel)
  41. {
  42. _info.pNext = NULL;
  43. _info.cAllocs = 0;
  44. _info.cFrees = 0;
  45. _info.cbAlloc = 0;
  46. _info.cbMaxAlloc = 0;
  47. _info.cbTotalAlloc = 0;
  48. fStats = TRUE;
  49. MemRegisterWatcher(&_info);
  50. }
  51. MEMWATCH::~MEMWATCH()
  52. {
  53. MemDeregisterWatcher(&_info);
  54. if (fStats || ((_info.cAllocs - _info.cFrees) != 0)) {
  55. if (!fStats) {
  56. OutputDebugString("Memory leak: ");
  57. }
  58. OutputDebugString(_lpszLabel);
  59. char szBuf[100];
  60. wsprintf(szBuf, "%d allocs, %d orphans, %d byte footprint, %d byte usage\r\n",
  61. _info.cAllocs,
  62. _info.cAllocs - _info.cFrees,
  63. _info.cbMaxAlloc,
  64. _info.cbTotalAlloc);
  65. OutputDebugString(szBuf);
  66. }
  67. }
  68. MemLeak::MemLeak(LPCSTR lpszLabel)
  69. : MEMWATCH(lpszLabel)
  70. {
  71. fStats = FALSE;
  72. }
  73. MemOff::MemOff()
  74. {
  75. pvContext = MemUpdateOff();
  76. }
  77. MemOff::~MemOff()
  78. {
  79. MemUpdateContinue(pvContext);
  80. }
  81. #endif /* DEBUG */