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.

77 lines
2.2 KiB

  1. //====================================================================
  2. // Debugging memory problems.
  3. //====================================================================
  4. #undef LocalAlloc
  5. #undef LocalReAlloc
  6. #undef LocalFree
  7. #define CBALLOCEXTRA (sizeof(LPARAM)+sizeof(UINT))
  8. #define HMEM2PTR(hMem,i) (((BYTE*)hMem)+i)
  9. void _StoreSigniture(HLOCAL hMem, UINT uByte, LPARAM lParam)
  10. {
  11. if (hMem)
  12. {
  13. SIZE_T uSize = LocalSize(hMem);
  14. ASSERT(uSize>=uByte+CBALLOCEXTRA);
  15. *(UINT*)HMEM2PTR(hMem, uSize-sizeof(UINT)) = uByte;
  16. *(LPARAM*)HMEM2PTR(hMem, uByte) = lParam;
  17. }
  18. }
  19. UINT _ValidateLocalMem(HLOCAL hMem, LPARAM lParam, LPCSTR pszText)
  20. {
  21. UINT uByte = 0;
  22. if (hMem)
  23. {
  24. SIZE_T uSize = LocalSize(hMem);
  25. if (uSize)
  26. {
  27. LPARAM lParamStored;
  28. uByte = *(UINT*)HMEM2PTR(hMem, uSize-sizeof(UINT));
  29. AssertMsg(uByte+CBALLOCEXTRA <= uSize,
  30. TEXT("cm ASSERT! Bogus uByte %d (%s for %x)"),
  31. uByte, pszText, hMem);
  32. lParamStored = *(LPARAM*)HMEM2PTR(hMem, uByte);
  33. AssertMsg(lParamStored==lParam, TEXT("cm ASSERT! Bad Signiture %x (%s for %x)"),
  34. lParamStored, pszText, hMem);
  35. }
  36. else
  37. {
  38. AssertMsg(0, TEXT("cm ASSERT! LocalSize is zero (%s for %x)"),
  39. pszText, hMem);
  40. }
  41. }
  42. return uByte;
  43. }
  44. HLOCAL WINAPI DebugLocalAlloc(UINT uFlags, UINT uBytes)
  45. {
  46. HLOCAL hMem = LocalAlloc(uFlags, uBytes+CBALLOCEXTRA);
  47. _StoreSigniture(hMem, uBytes, (LPARAM)hMem);
  48. return hMem;
  49. }
  50. HLOCAL WINAPI DebugLocalReAlloc(HLOCAL hMem, UINT uBytes, UINT uFlags)
  51. {
  52. HLOCAL hNew;
  53. _ValidateLocalMem(hMem, (LPARAM)hMem, "LocalReAlloc");
  54. hNew = LocalReAlloc(hMem, uBytes+CBALLOCEXTRA, uFlags);
  55. _StoreSigniture(hNew, uBytes, (LPARAM)hNew);
  56. return hNew;
  57. }
  58. HLOCAL WINAPI DebugLocalFree( HLOCAL hMem )
  59. {
  60. UINT uBytes = _ValidateLocalMem(hMem, (LPARAM)hMem, "LocalFree");
  61. if (uBytes)
  62. {
  63. _StoreSigniture(hMem, uBytes, (LPARAM)0xDDDDDDDDL);
  64. }
  65. else
  66. {
  67. AssertMsg(0, TEXT("cm ASSERT! LocalFree _ValidateLocalMem returned 0 for %x"), hMem);
  68. }
  69. return LocalFree(hMem);
  70. }