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.

187 lines
4.2 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1997.
  5. //
  6. // File: Alloc.cpp
  7. //
  8. // Contents: Allocation routines
  9. //
  10. // Classes:
  11. //
  12. // Notes:
  13. //
  14. // History: 05-Nov-97 rogerg Created.
  15. //
  16. //--------------------------------------------------------------------------
  17. #include "lib.h"
  18. //+-------------------------------------------------------------------
  19. //
  20. // Function: ::operator new
  21. //
  22. // Synopsis: Our operator new implementation
  23. //
  24. // Arguments: [size] -- Size of memory to allocate
  25. //
  26. //
  27. // Notes:
  28. //
  29. //--------------------------------------------------------------------
  30. inline void* __cdecl operator new (size_t size)
  31. {
  32. return(ALLOC(size));
  33. }
  34. //+-------------------------------------------------------------------
  35. //
  36. // Function: ::operator delete
  37. //
  38. // Synopsis: Our operator deleteimplementation
  39. //
  40. // Arguments: lpv-- Pointer to memory to free
  41. //
  42. //
  43. // Notes:
  44. //
  45. //--------------------------------------------------------------------
  46. inline void __cdecl operator delete(void FAR* lpv)
  47. {
  48. FREE(lpv);
  49. }
  50. //
  51. // Allocator for MIDL stubs
  52. //
  53. //+-------------------------------------------------------------------
  54. //
  55. // Function: MIDL_user_allocate
  56. //
  57. // Synopsis:
  58. //
  59. // Arguments: lpv-- Pointer to memory to free
  60. //
  61. //
  62. // Notes:
  63. //
  64. //--------------------------------------------------------------------
  65. extern "C" void __RPC_FAR * __RPC_API
  66. MIDL_user_allocate(
  67. IN size_t len
  68. )
  69. {
  70. return ALLOC(len);
  71. }
  72. //+-------------------------------------------------------------------
  73. //
  74. // Function: MIDL_user_free
  75. //
  76. // Synopsis:
  77. //
  78. // Arguments: ptr-- Pointer to memory to free
  79. //
  80. //
  81. // Notes:
  82. //
  83. //--------------------------------------------------------------------
  84. extern "C" void __RPC_API
  85. MIDL_user_free(
  86. IN void __RPC_FAR * ptr
  87. )
  88. {
  89. FREE(ptr);
  90. }
  91. //+---------------------------------------------------------------------------
  92. //
  93. // function: ALLOC, public
  94. //
  95. // Synopsis: memory allocator
  96. //
  97. // Arguments: [cb] - requested size of memory to alloc.
  98. //
  99. // Returns: Pointer to newly allocated memory, NULL on failure
  100. //
  101. // Modifies:
  102. //
  103. // History: 05-Nov-97 rogerg Created.
  104. //
  105. //----------------------------------------------------------------------------
  106. LPVOID ALLOC(ULONG cb)
  107. {
  108. void *pv;
  109. pv = LocalAlloc(LPTR,cb);
  110. return pv;
  111. }
  112. //+---------------------------------------------------------------------------
  113. //
  114. // function: FREE, public
  115. //
  116. // Synopsis: memory destructor
  117. //
  118. // Arguments: [pv] - pointer to memory to be released.
  119. //
  120. // Returns:
  121. //
  122. // Modifies:
  123. //
  124. // History: 05-Nov-97 rogerg Created.
  125. //
  126. //----------------------------------------------------------------------------
  127. void FREE(void* pv)
  128. {
  129. LocalFree(pv);
  130. }
  131. //+---------------------------------------------------------------------------
  132. //
  133. // function: REALLOC, public
  134. //
  135. // Synopsis: reallocs memory
  136. //
  137. // Arguments: [ppv] - address of pointer to memory to be released.
  138. // [cb] - size to resize the memory to.
  139. //
  140. // Returns: ERROR_SUCCESS if memory was successfully reallocated.
  141. //
  142. // Win32 error value if memory was not successfully reallocated.
  143. // If an error occurs, the pointer addressed by ppv is unchanged,
  144. // the original memory buffer is left unchanged, and it is the
  145. // caller's responsibility to free the original memory buffer.
  146. //
  147. // Modifies:
  148. //
  149. // History: 22-Jul-98 rogerg Created.
  150. // 18-Jan-02 brianau Rewrote to prevent leak on realloc
  151. // failure.
  152. //
  153. //----------------------------------------------------------------------------
  154. DWORD REALLOC(void **ppv, ULONG cb)
  155. {
  156. Assert(ppv);
  157. Assert(*ppv);
  158. void *pvNew = LocalReAlloc(*ppv, cb, LMEM_MOVEABLE);
  159. if (pvNew)
  160. {
  161. *ppv = pvNew;
  162. return ERROR_SUCCESS;
  163. }
  164. return GetLastError();
  165. }