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.

129 lines
2.2 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 "precomp.h"
  13. #include "server.h"
  14. #include "srvrmem.h"
  15. LPVOID
  16. SrvrAllocSplMem(
  17. DWORD cb
  18. )
  19. /*++
  20. Routine Description:
  21. This function will allocate local memory. It will possibly allocate extra
  22. memory and fill this with debugging information for the debugging version.
  23. Arguments:
  24. cb - The amount of memory to allocate
  25. Return Value:
  26. NON-NULL - A pointer to the allocated memory
  27. FALSE/NULL - The operation failed. Extended error status is available
  28. using GetLastError.
  29. --*/
  30. {
  31. LPDWORD pMem;
  32. DWORD cbNew;
  33. cb = DWORD_ALIGN_UP(cb);
  34. cbNew = cb+sizeof(DWORD_PTR)+sizeof(DWORD);
  35. pMem=(LPDWORD)LocalAlloc(LPTR, cbNew);
  36. if (!pMem) {
  37. DBGMSG( DBG_WARNING, ("Memory Allocation failed for %d bytes\n", cbNew ));
  38. return 0;
  39. }
  40. *pMem=cb;
  41. *(LPDWORD)((LPBYTE)pMem+cbNew-sizeof(DWORD))=0xdeadbeef;
  42. return (LPVOID)(pMem+sizeof(DWORD_PTR)/sizeof(DWORD));
  43. }
  44. BOOL
  45. SrvrFreeSplMem(
  46. LPVOID pMem
  47. )
  48. {
  49. DWORD cbNew;
  50. LPDWORD pNewMem;
  51. if( !pMem ){
  52. return TRUE;
  53. }
  54. pNewMem = pMem;
  55. pNewMem -= sizeof(DWORD_PTR) / sizeof(DWORD);
  56. cbNew = *pNewMem;
  57. if (*(LPDWORD)((LPBYTE)pMem + cbNew) != 0xdeadbeef) {
  58. DBGMSG(DBG_ERROR, ("DllFreeSplMem Corrupt Memory in winspool : %0lx\n", pNewMem));
  59. return FALSE;
  60. }
  61. memset(pNewMem, 0x65, cbNew);
  62. LocalFree((LPVOID)pNewMem);
  63. return TRUE;
  64. }
  65. LPVOID
  66. SrvrReallocSplMem(
  67. LPVOID pOldMem,
  68. DWORD cbOld,
  69. DWORD cbNew
  70. )
  71. {
  72. LPVOID pNewMem;
  73. pNewMem=SrvrAllocSplMem(cbNew);
  74. if (!pNewMem)
  75. {
  76. DBGMSG( DBG_WARNING, ("Memory ReAllocation failed for %d bytes\n", cbNew ));
  77. }
  78. else
  79. {
  80. if (pOldMem)
  81. {
  82. if (cbOld)
  83. {
  84. memcpy(pNewMem, pOldMem, min(cbNew, cbOld));
  85. }
  86. SrvrFreeSplMem(pOldMem);
  87. }
  88. }
  89. return pNewMem;
  90. }