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.

331 lines
5.8 KiB

  1. /*++
  2. Copyright (c) 1997-2001 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. // Private default dnsapi heap.
  15. //
  16. // Use non-process heap just so i don't have to debug a stress
  17. // failure every time some yahoo corrupts their heap.
  18. //
  19. HEAP_BLOB g_DnsApiHeap;
  20. //
  21. // Heap flags
  22. //
  23. #if DBG
  24. #define DNS_HEAP_FLAGS \
  25. ( HEAP_GROWABLE | \
  26. HEAP_TAIL_CHECKING_ENABLED | \
  27. HEAP_FREE_CHECKING_ENABLED | \
  28. HEAP_CREATE_ALIGN_16 | \
  29. HEAP_CLASS_1 )
  30. #else
  31. #define DNS_HEAP_FLAGS \
  32. ( HEAP_GROWABLE | \
  33. HEAP_CREATE_ALIGN_16 | \
  34. HEAP_CLASS_1 )
  35. #endif
  36. VOID
  37. Heap_Initialize(
  38. VOID
  39. )
  40. /*++
  41. Routine Description:
  42. Initialize heap routines.
  43. MUST call this in dnsapi.dll attach.
  44. Note this doesn't actually create the heap. For perf, don't
  45. do that until we actually get called.
  46. Arguments:
  47. None.
  48. Return Value:
  49. None.
  50. --*/
  51. {
  52. #ifdef PRIVATE_DNSHEAP
  53. //
  54. // use dnslib debug heap as private heap
  55. //
  56. LOCK_GENERAL();
  57. Dns_HeapInitialize(
  58. & g_DnsApiHeap,
  59. NULL, // no existing heap handle
  60. DNS_HEAP_FLAGS, // create flags
  61. #if DBG
  62. TRUE, // use debug headers
  63. #else
  64. FALSE, // no debug headers
  65. #endif
  66. TRUE, // dnslib uses this heap
  67. TRUE, // full heap checks
  68. 0, // no exception
  69. 0, // no default flags
  70. "dnslib", // bogus file
  71. 0 // bogus line no
  72. );
  73. UNLOCK_GENERAL();
  74. #endif
  75. }
  76. VOID
  77. Heap_Cleanup(
  78. VOID
  79. )
  80. /*++
  81. Routine Description:
  82. Delete heap.
  83. Need this to allow restart.
  84. Arguments:
  85. None.
  86. Return Value:
  87. None.
  88. --*/
  89. {
  90. #ifdef PRIVATE_DNSHEAP
  91. //
  92. // delete private heap
  93. //
  94. DNSDBG( ANY, ( "Heap_Cleanup()\n" ));
  95. Dns_HeapCleanup( &g_DnsApiHeap );
  96. #endif
  97. }
  98. //
  99. // Exported routines
  100. //
  101. VOID
  102. DnsApiHeapReset(
  103. IN DNS_ALLOC_FUNCTION pAlloc,
  104. IN DNS_REALLOC_FUNCTION pRealloc,
  105. IN DNS_FREE_FUNCTION pFree
  106. )
  107. /*++
  108. Routine Description:
  109. Resets heap routines used by dnsapi.dll routines.
  110. DnsApi.dll allocates memory using the dnslib.lib heap
  111. routines. This simply resets those routines to use pointers
  112. to users heap routines.
  113. Arguments:
  114. pAlloc -- ptr to desired alloc function
  115. pRealloc -- ptr to desired realloc function
  116. pFree -- ptr to desired free function
  117. Return Value:
  118. None.
  119. --*/
  120. {
  121. // redirect heap for dnslib
  122. Dns_LibHeapReset( pAlloc, pRealloc, pFree );
  123. }
  124. //
  125. // External access to DNS memory routines.
  126. //
  127. // Modules that use DNS API memory and can be called in the context
  128. // of DNS server or other process which points dnsapi.dll at another
  129. // heap, should use these routines rather than LocalAlloc\Free().
  130. //
  131. // Note: that these routines can simply call the dnslib routines.
  132. // This is because dnsapi ALWAYS keeps its heap in sync with dnslib
  133. // whether it is default heap or has been redirected. Since dnslib
  134. // heap routines have the redirection check, we can just call them
  135. // and they'll do the right thing, we don't need a redirection check
  136. // ourselves.
  137. //
  138. PVOID
  139. DnsApiAlloc(
  140. IN INT iSize
  141. )
  142. {
  143. return Dns_Alloc( iSize );
  144. }
  145. PVOID
  146. DnsApiRealloc(
  147. IN OUT PVOID pMem,
  148. IN INT iSize
  149. )
  150. {
  151. return Dns_Realloc( pMem, iSize );
  152. }
  153. VOID
  154. DnsApiFree(
  155. IN OUT PVOID pMem
  156. )
  157. {
  158. Dns_Free( pMem );
  159. }
  160. //
  161. // SDK public free
  162. //
  163. // Extensions to DNS_FREE_TYPE enum in windns.h to handle
  164. // system-public structures.
  165. // These are only used if freeing with DnsFree(), if using
  166. // DnsFreeConfigStructure() then use the ConfigId directly.
  167. //
  168. // For convenience free type is the same as the config id.
  169. // If this changes must change DnsFreeConfigStructure()
  170. // to do mapping.
  171. //
  172. // These conflict with old function defs, so must undef
  173. #undef DnsFreeNetworkInformation
  174. #undef DnsFreeSearchInformation
  175. #undef DnsFreeAdapterInformation
  176. #define DnsFreeNetworkInformation (DNS_FREE_TYPE)DnsConfigNetworkInformation
  177. #define DnsFreeSearchInformation (DNS_FREE_TYPE)DnsConfigSearchInformation
  178. #define DnsFreeAdapterInformation (DNS_FREE_TYPE)DnsConfigAdapterInformation
  179. #define DnsFreeNetInfo (DNS_FREE_TYPE)DnsConfigNetInfo
  180. VOID
  181. WINAPI
  182. DnsFree(
  183. IN OUT PVOID pData,
  184. IN DNS_FREE_TYPE FreeType
  185. )
  186. /*++
  187. Routine Description:
  188. Generic DNS data free.
  189. Arguments:
  190. pData -- data to free
  191. FreeType -- free type
  192. Return Value:
  193. None
  194. --*/
  195. {
  196. DNSDBG( TRACE, (
  197. "DnsFree( %p, %d )\n",
  198. pData,
  199. FreeType ));
  200. if ( !pData )
  201. {
  202. return;
  203. }
  204. //
  205. // free appropriate type
  206. //
  207. switch ( FreeType )
  208. {
  209. case DnsFreeFlat:
  210. DnsApiFree( pData );
  211. break;
  212. case DnsFreeRecordList:
  213. Dns_RecordListFree( (PDNS_RECORD)pData );
  214. break;
  215. case DnsFreeNetworkInformation:
  216. FreeNetworkInformation( pData );
  217. break;
  218. case DnsFreeSearchInformation:
  219. FreeSearchInformation( pData );
  220. break;
  221. case DnsFreeAdapterInformation:
  222. FreeAdapterInformation( pData );
  223. break;
  224. case DnsFreeNetInfo:
  225. NetInfo_Free( pData );
  226. break;
  227. default:
  228. ASSERT( FALSE );
  229. DnsApiFree( pData );
  230. break;
  231. }
  232. }
  233. //
  234. // End memory.c
  235. //