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.

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