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.

136 lines
2.6 KiB

  1. // Copyright (c) 1997-2002 Microsoft Corporation
  2. //
  3. // Module:
  4. //
  5. // NSU memory utilities
  6. //
  7. // Abstract:
  8. //
  9. // Contains the code that provides memory allocation deallocation routines.
  10. //
  11. // Author:
  12. //
  13. // kmurthy 2/5/02
  14. //
  15. // Environment:
  16. //
  17. // User mode
  18. //
  19. // Revision History:
  20. //
  21. #include <precomp.h>
  22. #include "Nsu.h"
  23. //
  24. // Description:
  25. //
  26. // Allocates memory from the Heap.
  27. //
  28. // Arguments:
  29. //
  30. // dwBytes [IN]: Number of bytes to allocate
  31. // dwFlags [IN]: Flags that could be HEAP_ZERO_MEMORY
  32. //
  33. // Return Value:
  34. //
  35. // Pointer to allocated memory, NULL on error
  36. //
  37. PVOID NsuAlloc(SIZE_T dwBytes,DWORD dwFlags)
  38. {
  39. return HeapAlloc(GetProcessHeap(), dwFlags, dwBytes);
  40. }
  41. //
  42. // Description:
  43. //
  44. // Frees previously allocated memory and sets pointer to
  45. // memory, to FREED_POINTER.
  46. //
  47. // Arguments:
  48. //
  49. // ppMem [IN-OUT]: Pointer to pointer to allocated memory
  50. //
  51. // Return Value:
  52. //
  53. // ERROR_SUCCESS on success.
  54. // ERROR_INVALID_PARAMETER, if pointer is invalid or has
  55. // already been freed.
  56. // Other Win32 errors on failure.
  57. //
  58. DWORD NsuFree(PVOID *ppMem)
  59. {
  60. BOOL fRet = TRUE;
  61. DWORD dwErr = ERROR_SUCCESS;
  62. //Check if pointer is invalid or if
  63. //allocated pointer is pointing to FREED_POINTER
  64. if((!ppMem) || (*ppMem == FREED_POINTER)){
  65. dwErr = ERROR_INVALID_PARAMETER;
  66. NSU_BAIL_OUT
  67. }
  68. fRet = HeapFree(GetProcessHeap(), 0, *ppMem);
  69. if(fRet){
  70. *ppMem = FREED_POINTER;
  71. }
  72. else{
  73. dwErr = GetLastError();
  74. NSU_BAIL_OUT
  75. }
  76. NSU_CLEANUP:
  77. return dwErr;
  78. }
  79. // Description:
  80. //
  81. // Frees previously allocated memory and sets pointer to
  82. // memory, to FREED_POINTER. In addition, it checks if pointer is
  83. // already NULL, in which case it will simply return ERROR_SUCCESS.
  84. //
  85. // Arguments:
  86. //
  87. // ppMem [IN-OUT]: Pointer to pointer to allocated memory
  88. //
  89. // Return Value:
  90. //
  91. // ERROR_SUCCESS on success.
  92. // ERROR_INVALID_PARAMETER, if pointer is invalid or has
  93. // already been freed.
  94. // Other Win32 errors on failure.
  95. //
  96. DWORD NsuFree0(PVOID *ppMem)
  97. {
  98. BOOL fRet = TRUE;
  99. DWORD dwErr = ERROR_SUCCESS;
  100. //Check if pointer is invalid or if
  101. //allocated pointer is pointing to FREED_POINTER
  102. if((!ppMem) || (*ppMem == FREED_POINTER)){
  103. dwErr = ERROR_INVALID_PARAMETER;
  104. NSU_BAIL_OUT
  105. }
  106. //Check if the memory was not allocated
  107. //
  108. if(*ppMem == NULL){
  109. NSU_BAIL_OUT
  110. }
  111. fRet = HeapFree(GetProcessHeap(), 0, *ppMem);
  112. if(fRet){
  113. *ppMem = FREED_POINTER;
  114. }
  115. else{
  116. dwErr = GetLastError();
  117. NSU_BAIL_OUT
  118. }
  119. NSU_CLEANUP:
  120. return dwErr;
  121. }