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.

134 lines
4.3 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1994 - 1995.
  5. //
  6. // File: heapaloc.h
  7. //
  8. // Contents: Macros which wrap the standard memory API calls, redirecting
  9. // them to HeapAlloc.
  10. //
  11. // Functions: __inline HLOCAL HeapLocalAlloc (fuFlags, cbBytes)
  12. // __inline HLOCAL HeapLocalReAlloc (hMem, cbBytes, fuFlags)
  13. // __inline HLOCAL HeapLocalFree (HLOCAL hMem)
  14. //
  15. // History: 2-01-95 davepl Created
  16. // 12-15-97 t-saml changed to be used only for leak tracking
  17. //
  18. //--------------------------------------------------------------------------
  19. #ifndef _HEAPALOC_H
  20. #define _HEAPALOC_H 1
  21. #ifndef DEBUG
  22. #define IMSAddToList(bAdd, pv, cb)
  23. #else
  24. // Function to add/remove from leak detection list
  25. // In stocklib (shell\lib\debug.c)
  26. STDAPI_(void) IMSAddToList(BOOL bAdd, void*pv, SIZE_T cb);
  27. #ifdef _SHELLP_H_
  28. // Function to call in allocspy.dll (GetShellMallocSpy)
  29. typedef BOOL (__stdcall *PFNGSMS) (IShellMallocSpy **ppout);
  30. #endif
  31. #ifndef ASSERT
  32. #define ASSERT Assert
  33. #endif
  34. #ifdef LocalAlloc
  35. #error "HEAPALOC.H(42): LocalAlloc shouldn't be defined"
  36. #endif
  37. //
  38. // These are functions normally in comctl32, but there's no good reason to call
  39. // that dll, so handle them here. Since Chicago may still want to use these
  40. // shared memory routines, only "forward" them under NT.
  41. //
  42. #if defined(WINNT) && defined(_COMCTL32_)
  43. #define Alloc(cb) HeapLocalAlloc(LMEM_ZEROINIT | LMEM_FIXED, cb)
  44. #define ReAlloc(pb, cb) HeapLocalReAlloc(pb, cb, LMEM_ZEROINIT | LMEM_FIXED)
  45. //
  46. // Free() in comctl32 is just HeapFree(), so the return code reversing
  47. // in HeapLocalFree is the opposite of what we want. Reverse it
  48. // again here for now, and consider redefining Free() as just
  49. // HeapFree(g_hProcessHeap) if the compiler isn't smart enough
  50. // to generate the same code already.
  51. // REVIEW: who checks the return value from a free? What do you do if it fails?
  52. //
  53. #define Free(pb) (!HeapLocalFree(pb))
  54. #define GetSize(pb) HeapLocalSize(pb)
  55. #endif
  56. #if 0
  57. // GlobalAllocs cannot be trivially replaced since they are used for DDE, OLE,
  58. // and GDI operations. However, on a case-by-case version we can switch them
  59. // over to HeapGlobalAlloc as we identify instances that don't _really_ require
  60. // GlobalAllocs.
  61. #define GlobalAlloc(fuFlags, cbBytes) HeapGlobalAlloc(fuFlags, cbBytes)
  62. #define GlobalReAlloc(hMem, cbBytes, fuFlags) HeapGlobalReAlloc(hMem, cbBytes, fuFlags)
  63. #define GlobalSize(hMem) HeapGlobalSize(hMem)
  64. #define GlobalFree(hMem) HeapGlobalFree(hMem)
  65. #define GlobalCompact InvalidMemoryCall
  66. #define GlobalDiscard InvalidMemoryCall
  67. #define GlobalFlags InvalidMemoryCall
  68. #define GlobalHandle InvalidMemoryCall
  69. #define GlobalLock InvalidMemoryCall
  70. #define GlobalUnlock InvalidMemoryCall
  71. #endif
  72. __inline HLOCAL HeapLocalAlloc(IN UINT fuFlags, IN SIZE_T cbBytes)
  73. {
  74. void * pv;
  75. pv = LocalAlloc(fuFlags, cbBytes);
  76. IMSAddToList(TRUE, pv, cbBytes); // Add to leak tracking
  77. return (HLOCAL) pv;
  78. }
  79. __inline HLOCAL HeapLocalFree(HLOCAL hMem)
  80. {
  81. IMSAddToList(FALSE, hMem, 0); // Free leak tracking
  82. return LocalFree(hMem);
  83. }
  84. __inline HLOCAL HeapLocalReAlloc(IN HGLOBAL hMem,
  85. IN SIZE_T cbBytes,
  86. IN UINT fuFlags)
  87. {
  88. void * pv;
  89. // (DavePl) Why can we realloc on a null ptr?
  90. if (NULL == hMem)
  91. {
  92. return LocalAlloc(fuFlags, cbBytes);
  93. }
  94. pv = LocalReAlloc((void *) hMem, cbBytes, fuFlags);
  95. IMSAddToList(FALSE, hMem, 0); // Take out the old
  96. IMSAddToList(TRUE, pv, cbBytes); // And bring in the new
  97. return (HGLOBAL) pv;
  98. }
  99. // Redefine the standard memory APIs to thunk over to our Heap-based funcs
  100. #define LocalAlloc(fuFlags, cbBytes) HeapLocalAlloc(fuFlags, cbBytes)
  101. #define LocalReAlloc(hMem, cbBytes, fuFlags) HeapLocalReAlloc(hMem, cbBytes, fuFlags)
  102. #define LocalFree(hMem) HeapLocalFree(hMem)
  103. #endif
  104. #endif