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.

138 lines
2.3 KiB

  1. /*++
  2. Copyright (c) 1999 Microsoft Corporation
  3. All rights reserved.
  4. Module Name:
  5. srvrmem.c
  6. Abstract:
  7. Memory Allocation Routines for spoolsv.exe.
  8. Author:
  9. Khaled Sedky (khaleds) 13-Jan-1999
  10. Revision History:
  11. --*/
  12. #include <nt.h>
  13. #include <ntrtl.h>
  14. #include <nturtl.h>
  15. #include <ntddrdr.h>
  16. #include <stdio.h>
  17. #include <windows.h>
  18. #include <winspool.h>
  19. #include "server.h"
  20. #include "client.h"
  21. #ifndef _SRVRMEM_H_
  22. #include "srvrmem.h"
  23. #endif
  24. LPVOID
  25. SrvrAllocSplMem(
  26. DWORD cb
  27. )
  28. /*++
  29. Routine Description:
  30. This function will allocate local memory. It will possibly allocate extra
  31. memory and fill this with debugging information for the debugging version.
  32. Arguments:
  33. cb - The amount of memory to allocate
  34. Return Value:
  35. NON-NULL - A pointer to the allocated memory
  36. FALSE/NULL - The operation failed. Extended error status is available
  37. using GetLastError.
  38. --*/
  39. {
  40. LPDWORD pMem;
  41. DWORD cbNew;
  42. cb = DWORD_ALIGN_UP(cb);
  43. cbNew = cb+sizeof(DWORD_PTR)+sizeof(DWORD);
  44. pMem=(LPDWORD)LocalAlloc(LPTR, cbNew);
  45. if (!pMem) {
  46. DBGMSG( DBG_WARNING, ("Memory Allocation failed for %d bytes\n", cbNew ));
  47. return 0;
  48. }
  49. *pMem=cb;
  50. *(LPDWORD)((LPBYTE)pMem+cbNew-sizeof(DWORD))=0xdeadbeef;
  51. return (LPVOID)(pMem+sizeof(DWORD_PTR)/sizeof(DWORD));
  52. }
  53. BOOL
  54. SrvrFreeSplMem(
  55. LPVOID pMem
  56. )
  57. {
  58. DWORD cbNew;
  59. LPDWORD pNewMem;
  60. if( !pMem ){
  61. return TRUE;
  62. }
  63. pNewMem = pMem;
  64. pNewMem -= sizeof(DWORD_PTR) / sizeof(DWORD);
  65. cbNew = *pNewMem;
  66. if (*(LPDWORD)((LPBYTE)pMem + cbNew) != 0xdeadbeef) {
  67. DBGMSG(DBG_ERROR, ("DllFreeSplMem Corrupt Memory in winspool : %0lx\n", pNewMem));
  68. return FALSE;
  69. }
  70. memset(pNewMem, 0x65, cbNew);
  71. LocalFree((LPVOID)pNewMem);
  72. return TRUE;
  73. }
  74. LPVOID
  75. SrvrReallocSplMem(
  76. LPVOID pOldMem,
  77. DWORD cbOld,
  78. DWORD cbNew
  79. )
  80. {
  81. LPVOID pNewMem;
  82. pNewMem=SrvrAllocSplMem(cbNew);
  83. if (!pNewMem)
  84. {
  85. DBGMSG( DBG_WARNING, ("Memory ReAllocation failed for %d bytes\n", cbNew ));
  86. }
  87. else
  88. {
  89. if (pOldMem)
  90. {
  91. if (cbOld)
  92. {
  93. memcpy(pNewMem, pOldMem, min(cbNew, cbOld));
  94. }
  95. SrvrFreeSplMem(pOldMem);
  96. }
  97. }
  98. return pNewMem;
  99. }