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.

292 lines
4.3 KiB

  1. /*++
  2. Copyright (c) 1997-2000 Microsoft Corporation
  3. Module Name:
  4. memory.c
  5. Abstract:
  6. Domain Name System (DNS) Library
  7. Memory allocation routines for DNS library.
  8. Author:
  9. Jim Gilroy (jamesg) January, 1997
  10. Revision History:
  11. --*/
  12. #include "local.h"
  13. //
  14. // Memory allocation
  15. //
  16. // Some DNS library functions -- including the IP array and string utils
  17. // -- allocate memory. This memory allocation defaults to routines that
  18. // use LocalAlloc, LocalReAlloc, LocalFree.
  19. //
  20. // Dns_Api.dll users can reset the memory allocation through
  21. // Dns_ApiHeapReset(), which saves pointers to remote allocation functions
  22. // which will override default allocators.
  23. //
  24. //
  25. // Allow overide of default memory allocation
  26. //
  27. DNSLIB_ALLOC_FUNCTION pDnsAllocFunction = NULL;
  28. DNSLIB_REALLOC_FUNCTION pDnsReallocFunction = NULL;
  29. DNSLIB_FREE_FUNCTION pDnsFreeFunction = NULL;
  30. VOID
  31. Dns_LibHeapReset(
  32. IN DNSLIB_ALLOC_FUNCTION pAlloc,
  33. IN DNSLIB_REALLOC_FUNCTION pRealloc,
  34. IN DNSLIB_FREE_FUNCTION pFree
  35. )
  36. /*++
  37. Routine Description:
  38. Resets heap routines used by dnsapi.dll routines.
  39. Arguments:
  40. pAlloc -- ptr to desired alloc function
  41. pRealloc -- ptr to desired realloc function
  42. pFree -- ptr to desired free function
  43. Return Value:
  44. None.
  45. --*/
  46. {
  47. pDnsAllocFunction = pAlloc;
  48. pDnsReallocFunction = pRealloc;
  49. pDnsFreeFunction = pFree;
  50. }
  51. //
  52. // Exported public memory routines.
  53. //
  54. // These use whatever the current memory allocation routines are, and
  55. // hence will always handle memory in the same fashion as dnsapi.dll
  56. // internal routines.
  57. //
  58. PVOID
  59. Dns_Alloc(
  60. IN INT iSize
  61. )
  62. /*++
  63. Routine Description:
  64. Allocates memory.
  65. Arguments:
  66. iSize - number of bytes to allocate
  67. Return Value:
  68. Pointer to memory allocated.
  69. NULL if allocation fails.
  70. --*/
  71. {
  72. if ( pDnsAllocFunction )
  73. {
  74. return (*pDnsAllocFunction)( iSize );
  75. }
  76. // default
  77. return LocalAlloc( LPTR, iSize );
  78. }
  79. PVOID
  80. Dns_Realloc(
  81. IN OUT PVOID pMem,
  82. IN INT iSize
  83. )
  84. /*++
  85. Routine Description:
  86. Reallocates memory
  87. Arguments:
  88. pMem - ptr to existing memory to reallocated
  89. iSize - number of bytes to reallocate
  90. Return Value:
  91. Pointer to memory allocated.
  92. NULL if allocation fails.
  93. --*/
  94. {
  95. if ( pDnsReallocFunction )
  96. {
  97. return (*pDnsReallocFunction)( pMem, iSize );
  98. }
  99. // default
  100. return LocalReAlloc( pMem, iSize, 0 );
  101. }
  102. VOID
  103. Dns_Free(
  104. IN OUT PVOID pMem
  105. )
  106. /*++
  107. Routine Description:
  108. Free memory
  109. Arguments:
  110. pMem - ptr to existing memory to reallocated
  111. Return Value:
  112. None
  113. --*/
  114. {
  115. if ( !pMem )
  116. {
  117. return;
  118. }
  119. if ( pDnsFreeFunction )
  120. {
  121. (*pDnsFreeFunction)( pMem );
  122. return;
  123. }
  124. // default
  125. LocalFree( pMem );
  126. }
  127. PVOID
  128. Dns_AllocZero(
  129. IN INT iSize
  130. )
  131. /*++
  132. Routine Description:
  133. Allocates and zeros memory.
  134. Arguments:
  135. iSize - number of bytes to allocate
  136. Return Value:
  137. Pointer to memory allocated.
  138. NULL if allocation fails.
  139. --*/
  140. {
  141. if ( pDnsAllocFunction )
  142. {
  143. PCHAR ptr;
  144. ptr = (*pDnsAllocFunction)( iSize );
  145. if ( !ptr )
  146. {
  147. return( NULL );
  148. }
  149. RtlZeroMemory( ptr, iSize );
  150. return( ptr );
  151. }
  152. // default
  153. return LocalAlloc( LPTR, iSize );
  154. }
  155. //
  156. // Helpful util
  157. //
  158. PVOID
  159. Dns_AllocMemCopy(
  160. IN PVOID pMem,
  161. IN INT Size
  162. )
  163. /*++
  164. Routine Description:
  165. Allocates and copies memory
  166. Arguments:
  167. pMem - memory to copy
  168. Size - number of bytes to allocate
  169. Return Value:
  170. Pointer to memory allocated.
  171. NULL if allocation fails.
  172. --*/
  173. {
  174. PBYTE ptr;
  175. ptr = Dns_Alloc( Size );
  176. if ( ptr )
  177. {
  178. if ( pMem )
  179. {
  180. RtlCopyMemory(
  181. ptr,
  182. pMem,
  183. Size );
  184. }
  185. else
  186. {
  187. RtlZeroMemory( ptr, Size );
  188. }
  189. }
  190. return ptr;
  191. }
  192. //
  193. // End of memory.c
  194. //