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.

749 lines
18 KiB

  1. /*++
  2. Copyright (c) 1997-1999 Microsoft Corporation
  3. Module Name:
  4. alloc.c
  5. Abstract:
  6. WMI data structure allocation routines
  7. Author:
  8. 16-Jan-1997 AlanWar
  9. Revision History:
  10. --*/
  11. #include "wmikmp.h"
  12. // HEY: This is duplicated from wmium.h.
  13. //
  14. // This guid is for notifications of changes to registration
  15. // {B48D49A1-E777-11d0-A50C-00A0C9062910}
  16. GUID GUID_REGISTRATION_CHANGE_NOTIFICATION = {0xb48d49a1, 0xe777, 0x11d0, 0xa5, 0xc, 0x0, 0xa0, 0xc9, 0x6, 0x29, 0x10};
  17. //
  18. // This guid id for notifications of new mof resources being added
  19. // {B48D49A2-E777-11d0-A50C-00A0C9062910}
  20. GUID GUID_MOF_RESOURCE_ADDED_NOTIFICATION = {0xb48d49a2, 0xe777, 0x11d0, 0xa5, 0xc, 0x0, 0xa0, 0xc9, 0x6, 0x29, 0x10};
  21. //
  22. // This guid id for notifications of new mof resources being added
  23. // {B48D49A3-E777-11d0-A50C-00A0C9062910}
  24. GUID GUID_MOF_RESOURCE_REMOVED_NOTIFICATION = {0xb48d49a3, 0xe777, 0x11d0, 0xa5, 0xc, 0x0, 0xa0, 0xc9, 0x6, 0x29, 0x10};
  25. //
  26. // This defines the number of DataSources allocated in each DataSource chunk
  27. #if DBG
  28. #define DSCHUNKSIZE 4
  29. #else
  30. #define DSCHUNKSIZE 64
  31. #endif
  32. void WmipDSCleanup(
  33. PCHUNKINFO ChunkInfo,
  34. PENTRYHEADER Entry
  35. );
  36. CHUNKINFO WmipDSChunkInfo =
  37. {
  38. { NULL, NULL },
  39. sizeof(DATASOURCE),
  40. DSCHUNKSIZE,
  41. WmipDSCleanup,
  42. FLAG_ENTRY_REMOVE_LIST,
  43. DS_SIGNATURE
  44. };
  45. LIST_ENTRY WmipDSHead; // Head of registerd data source list
  46. PLIST_ENTRY WmipDSHeadPtr;
  47. //
  48. // This defines the number of GuidEntrys allocated in each GuidEntry chunk
  49. #if DBG
  50. #define GECHUNKSIZE 4
  51. #else
  52. #define GECHUNKSIZE 512
  53. #endif
  54. void WmipGECleanup(
  55. PCHUNKINFO ChunkInfo,
  56. PENTRYHEADER Entry
  57. );
  58. CHUNKINFO WmipGEChunkInfo =
  59. {
  60. { NULL, NULL },
  61. sizeof(GUIDENTRY),
  62. GECHUNKSIZE,
  63. WmipGECleanup,
  64. FLAG_ENTRY_REMOVE_LIST,
  65. GE_SIGNATURE
  66. };
  67. LIST_ENTRY WmipGEHead; // Head of registerd guid list
  68. PLIST_ENTRY WmipGEHeadPtr;
  69. //
  70. // This defines the number of InstanceSets allocated in each InstanceSet chunk
  71. #if DBG
  72. #define ISCHUNKSIZE 4
  73. #else
  74. #define ISCHUNKSIZE 2048
  75. #endif
  76. void WmipISCleanup(
  77. PCHUNKINFO ChunkInfo,
  78. PENTRYHEADER Entry
  79. );
  80. CHUNKINFO WmipISChunkInfo =
  81. {
  82. { NULL, NULL },
  83. sizeof(INSTANCESET),
  84. ISCHUNKSIZE,
  85. WmipISCleanup,
  86. 0,
  87. IS_SIGNATURE
  88. };
  89. #if DBG
  90. #define MRCHUNKSIZE 2
  91. #else
  92. #define MRCHUNKSIZE 16
  93. #endif
  94. void WmipMRCleanup(
  95. PCHUNKINFO ChunkInfo,
  96. PENTRYHEADER Entry
  97. );
  98. CHUNKINFO WmipMRChunkInfo =
  99. {
  100. { NULL, NULL },
  101. sizeof(MOFRESOURCE),
  102. MRCHUNKSIZE,
  103. WmipMRCleanup,
  104. FLAG_ENTRY_REMOVE_LIST,
  105. MR_SIGNATURE
  106. };
  107. LIST_ENTRY WmipMRHead; // Head of Mof Resource list
  108. PLIST_ENTRY WmipMRHeadPtr;
  109. LIST_ENTRY WmipGMHead; // Head of Guid Map List
  110. PLIST_ENTRY WmipGMHeadPtr;
  111. #ifdef ALLOC_PRAGMA
  112. #pragma alloc_text (PAGE, WmipDSCleanup)
  113. #pragma alloc_text (PAGE, WmipAllocDataSource)
  114. #pragma alloc_text (PAGE, WmipGECleanup)
  115. #pragma alloc_text (PAGE, WmipAllocGuidEntry)
  116. #pragma alloc_text (PAGE, WmipISCleanup)
  117. #pragma alloc_text (PAGE, WmipMRCleanup)
  118. #pragma alloc_text (PAGE, WmipFindGEByGuid)
  119. #pragma alloc_text (PAGE, WmipFindDSByProviderId)
  120. #pragma alloc_text (PAGE, WmipFindISByGuid)
  121. #pragma alloc_text (PAGE, WmipFindMRByNames)
  122. #pragma alloc_text (PAGE, WmipFindISinGEbyName)
  123. #pragma alloc_text (PAGE, WmipRealloc)
  124. #pragma alloc_text (PAGE, WmipIsNumber)
  125. #endif
  126. PBDATASOURCE WmipAllocDataSource(
  127. void
  128. )
  129. /*++
  130. Routine Description:
  131. Allocates a Data Source structure
  132. Arguments:
  133. Return Value:
  134. pointer to data source structure or NULL if one cannot be allocated
  135. --*/
  136. {
  137. PBDATASOURCE DataSource;
  138. DataSource = (PBDATASOURCE)WmipAllocEntry(&WmipDSChunkInfo);
  139. if (DataSource != NULL)
  140. {
  141. InitializeListHead(&DataSource->ISHead);
  142. DataSource->MofResourceCount = AVGMOFRESOURCECOUNT;
  143. DataSource->MofResources = DataSource->StaticMofResources;
  144. memset(DataSource->MofResources,
  145. 0,
  146. AVGMOFRESOURCECOUNT * sizeof(PMOFRESOURCE));
  147. }
  148. return(DataSource);
  149. }
  150. void WmipDSCleanup(
  151. PCHUNKINFO ChunkInfo,
  152. PENTRYHEADER Entry
  153. )
  154. /*++
  155. Routine Description:
  156. Cleans up data source structure and any other structures or handles
  157. associated with it.
  158. Arguments:
  159. Data source structure to free
  160. Return Value:
  161. --*/
  162. {
  163. PBDATASOURCE DataSource = (PBDATASOURCE)Entry;
  164. PBINSTANCESET InstanceSet;
  165. PLIST_ENTRY InstanceSetList;
  166. PMOFRESOURCE MofResource;
  167. ULONG i;
  168. WmipAssert(DataSource != NULL);
  169. WmipAssert(DataSource->Flags & FLAG_ENTRY_INVALID);
  170. WmipEnterSMCritSection();
  171. InstanceSetList = DataSource->ISHead.Flink;
  172. while (InstanceSetList != &DataSource->ISHead)
  173. {
  174. InstanceSet = CONTAINING_RECORD(InstanceSetList,
  175. INSTANCESET,
  176. DSISList);
  177. if (InstanceSet->GuidISList.Flink != NULL)
  178. {
  179. RemoveEntryList(&InstanceSet->GuidISList);
  180. InstanceSet->DataSource = NULL;
  181. InstanceSet->GuidEntry->ISCount--;
  182. }
  183. if ((InstanceSet->GuidEntry != NULL) &&
  184. (! (InstanceSet->Flags & IS_NEWLY_REGISTERED)))
  185. {
  186. if (IsEqualGUID(&InstanceSet->GuidEntry->Guid,
  187. &WmipBinaryMofGuid))
  188. {
  189. WmipLeaveSMCritSection();
  190. WmipGenerateBinaryMofNotification(InstanceSet,
  191. &GUID_MOF_RESOURCE_REMOVED_NOTIFICATION);
  192. WmipEnterSMCritSection();
  193. }
  194. WmipUnreferenceGE(InstanceSet->GuidEntry);
  195. }
  196. InstanceSet->GuidEntry = NULL;
  197. InstanceSetList = InstanceSetList->Flink;
  198. WmipUnreferenceIS(InstanceSet);
  199. }
  200. WmipLeaveSMCritSection();
  201. for (i = 0; i < DataSource->MofResourceCount; i++)
  202. {
  203. if (DataSource->MofResources[i] != NULL)
  204. {
  205. WmipUnreferenceMR(DataSource->MofResources[i]);
  206. }
  207. }
  208. if (DataSource->MofResources != DataSource->StaticMofResources)
  209. {
  210. WmipFree(DataSource->MofResources);
  211. }
  212. if (DataSource->RegistryPath != NULL)
  213. {
  214. WmipFree(DataSource->RegistryPath);
  215. }
  216. }
  217. void WmipGECleanup(
  218. PCHUNKINFO ChunkInfo,
  219. PENTRYHEADER Entry
  220. )
  221. /*++
  222. Routine Description:
  223. Cleans up guid entry structure and any other structures or handles
  224. associated with it.
  225. Arguments:
  226. GuidEntry structure to free
  227. Return Value:
  228. --*/
  229. {
  230. PBGUIDENTRY GuidEntry = (PBGUIDENTRY)Entry;
  231. WmipAssert(GuidEntry != NULL);
  232. WmipAssert(GuidEntry->Flags & FLAG_ENTRY_INVALID);
  233. if (GuidEntry->CollectInProgress != NULL)
  234. {
  235. ExFreePool(GuidEntry->CollectInProgress);
  236. GuidEntry->CollectInProgress = NULL;
  237. }
  238. }
  239. PBGUIDENTRY WmipAllocGuidEntry(
  240. void
  241. )
  242. {
  243. PBGUIDENTRY GuidEntry;
  244. PKEVENT Event;
  245. GuidEntry = NULL;
  246. Event = ExAllocatePoolWithTag(NonPagedPool,
  247. sizeof(KEVENT),
  248. WMIPOOLTAG);
  249. if (Event != NULL)
  250. {
  251. GuidEntry = (PBGUIDENTRY)WmipAllocEntry(&WmipGEChunkInfo);
  252. if (GuidEntry != NULL)
  253. {
  254. InitializeListHead(&GuidEntry->ISHead);
  255. InitializeListHead(&GuidEntry->ObjectHead);
  256. GuidEntry->CollectInProgress = Event;
  257. } else {
  258. ExFreePool(Event);
  259. }
  260. }
  261. return(GuidEntry);
  262. }
  263. void WmipISCleanup(
  264. PCHUNKINFO ChunkInfo,
  265. PENTRYHEADER Entry
  266. )
  267. {
  268. PBINSTANCESET InstanceSet = (PBINSTANCESET)Entry;
  269. WmipAssert(InstanceSet != NULL);
  270. WmipAssert(InstanceSet->Flags & FLAG_ENTRY_INVALID);
  271. if (InstanceSet->IsBaseName != NULL)
  272. {
  273. WmipFree(InstanceSet->IsBaseName);
  274. }
  275. if (InstanceSet->TraceGuidMap != NULL)
  276. {
  277. WmipFree(InstanceSet->TraceGuidMap);
  278. }
  279. }
  280. void WmipMRCleanup(
  281. IN PCHUNKINFO ChunkInfo,
  282. IN PENTRYHEADER Entry
  283. )
  284. {
  285. PMOFRESOURCE MofResource = (PMOFRESOURCE)Entry;
  286. PAGED_CODE();
  287. if ((MofResource->RegistryPath != NULL) &&
  288. (MofResource->MofResourceName != NULL))
  289. {
  290. WmipGenerateMofResourceNotification(MofResource->RegistryPath,
  291. MofResource->MofResourceName,
  292. &GUID_MOF_RESOURCE_REMOVED_NOTIFICATION,
  293. MofResource->Flags & MR_FLAG_USER_MODE ?
  294. MOFEVENT_ACTION_IMAGE_PATH :
  295. MOFEVENT_ACTION_REGISTRY_PATH);
  296. }
  297. if (MofResource->RegistryPath != NULL)
  298. {
  299. WmipFree(MofResource->RegistryPath);
  300. }
  301. if (MofResource->MofResourceName != NULL)
  302. {
  303. WmipFree(MofResource->MofResourceName);
  304. }
  305. }
  306. PBGUIDENTRY WmipFindGEByGuid(
  307. LPGUID Guid,
  308. BOOLEAN MakeTopOfList
  309. )
  310. /*++
  311. Routine Description:
  312. Searches guid list for first occurence of guid. Guid's refcount is
  313. incremented if found.
  314. Arguments:
  315. Guid is pointer to guid that is to be found
  316. MakeTopOfList is TRUE then if NE is found it is placed at the top of the
  317. NE list
  318. Return Value:
  319. pointer to guid entry pointer or NULL if not found
  320. --*/
  321. {
  322. PLIST_ENTRY GuidEntryList;
  323. PBGUIDENTRY GuidEntry;
  324. WmipEnterSMCritSection();
  325. GuidEntryList = WmipGEHeadPtr->Flink;
  326. while (GuidEntryList != WmipGEHeadPtr)
  327. {
  328. GuidEntry = CONTAINING_RECORD(GuidEntryList,
  329. GUIDENTRY,
  330. MainGEList);
  331. if (IsEqualGUID(Guid, &GuidEntry->Guid))
  332. {
  333. WmipReferenceGE(GuidEntry);
  334. if (MakeTopOfList)
  335. {
  336. RemoveEntryList(GuidEntryList);
  337. InsertHeadList(WmipGEHeadPtr, GuidEntryList);
  338. }
  339. WmipLeaveSMCritSection();
  340. WmipAssert(GuidEntry->Signature == GE_SIGNATURE);
  341. return(GuidEntry);
  342. }
  343. GuidEntryList = GuidEntryList->Flink;
  344. }
  345. WmipLeaveSMCritSection();
  346. return(NULL);
  347. }
  348. PBDATASOURCE WmipFindDSByProviderId(
  349. ULONG_PTR ProviderId
  350. )
  351. /*++
  352. Routine Description:
  353. This routine finds a DataSource on the provider id passed. DataSource's
  354. ref count is incremented if found
  355. Arguments:
  356. ProviderId is the data source provider id
  357. Return Value:
  358. DataSource pointer or NULL if no data source was found
  359. --*/
  360. {
  361. PLIST_ENTRY DataSourceList;
  362. PBDATASOURCE DataSource;
  363. WmipEnterSMCritSection();
  364. DataSourceList = WmipDSHeadPtr->Flink;
  365. while (DataSourceList != WmipDSHeadPtr)
  366. {
  367. DataSource = CONTAINING_RECORD(DataSourceList,
  368. DATASOURCE,
  369. MainDSList);
  370. if (DataSource->ProviderId == ProviderId)
  371. {
  372. WmipReferenceDS(DataSource);
  373. WmipLeaveSMCritSection();
  374. WmipAssert(DataSource->Signature == DS_SIGNATURE);
  375. return(DataSource);
  376. }
  377. DataSourceList = DataSourceList->Flink;
  378. }
  379. WmipLeaveSMCritSection();
  380. return(NULL);
  381. }
  382. PBINSTANCESET WmipFindISByGuid(
  383. PBDATASOURCE DataSource,
  384. GUID UNALIGNED *Guid
  385. )
  386. /*++
  387. Routine Description:
  388. This routine will find an instance set within a data source list for a
  389. specific guid. Note that any instance sets that have been replaceed
  390. (have IS_REPLACED_BY_REFERENCE) are ignored and not returned. The
  391. InstanceSet that is found has its reference count increased.
  392. Arguments:
  393. DataSource is the data source whose instance set list is searched
  394. Guid is a pointer to a guid which defines which instance set list to find
  395. Return Value:
  396. InstanceSet pointer or NULL if not found
  397. --*/
  398. {
  399. PBINSTANCESET InstanceSet;
  400. PLIST_ENTRY InstanceSetList;
  401. WmipEnterSMCritSection();
  402. InstanceSetList = DataSource->ISHead.Flink;
  403. while (InstanceSetList != &DataSource->ISHead)
  404. {
  405. InstanceSet = CONTAINING_RECORD(InstanceSetList,
  406. INSTANCESET,
  407. DSISList);
  408. if (IsEqualGUID(&InstanceSet->GuidEntry->Guid, Guid))
  409. {
  410. WmipReferenceIS(InstanceSet);
  411. WmipLeaveSMCritSection();
  412. WmipAssert(InstanceSet->Signature == IS_SIGNATURE);
  413. return(InstanceSet);
  414. }
  415. InstanceSetList = InstanceSetList->Flink;
  416. }
  417. WmipLeaveSMCritSection();
  418. return(NULL);
  419. }
  420. PMOFRESOURCE WmipFindMRByNames(
  421. LPCWSTR ImagePath,
  422. LPCWSTR MofResourceName
  423. )
  424. /*++
  425. Routine Description:
  426. Searches mof resource list for a MR that has the same image path and
  427. resource name. If ine is found a reference count is added to it.
  428. Arguments:
  429. ImagePath points at a string that has the full path to the image
  430. file that contains the MOF resource
  431. MofResourceName points at a string that has the name of the MOF
  432. resource
  433. Return Value:
  434. pointer to mof resource or NULL if not found
  435. --*/
  436. {
  437. PLIST_ENTRY MofResourceList;
  438. PMOFRESOURCE MofResource;
  439. WmipEnterSMCritSection();
  440. MofResourceList = WmipMRHeadPtr->Flink;
  441. while (MofResourceList != WmipMRHeadPtr)
  442. {
  443. MofResource = CONTAINING_RECORD(MofResourceList,
  444. MOFRESOURCE,
  445. MainMRList);
  446. if ((wcscmp(MofResource->RegistryPath, ImagePath) == 0) &&
  447. (wcscmp(MofResource->MofResourceName, MofResourceName) == 0))
  448. {
  449. WmipReferenceMR(MofResource);
  450. WmipLeaveSMCritSection();
  451. WmipAssert(MofResource->Signature == MR_SIGNATURE);
  452. return(MofResource);
  453. }
  454. MofResourceList = MofResourceList->Flink;
  455. }
  456. WmipLeaveSMCritSection();
  457. return(NULL);
  458. }
  459. BOOLEAN WmipIsNumber(
  460. LPCWSTR String
  461. )
  462. {
  463. while (*String != UNICODE_NULL)
  464. {
  465. if ((*String < L'0') || (*String > L'9'))
  466. {
  467. return(FALSE);
  468. }
  469. String++;
  470. }
  471. return(TRUE);
  472. }
  473. PBINSTANCESET WmipFindISinGEbyName(
  474. PBGUIDENTRY GuidEntry,
  475. PWCHAR InstanceName,
  476. PULONG InstanceIndex
  477. )
  478. /*++
  479. Routine Description:
  480. This routine finds the instance set containing the instance name passed
  481. within the GuidEntry passed. If found it will also return the index of
  482. the instance name within the instance set. The instance set found has its
  483. ref count incremented.
  484. Arguments:
  485. GuidEntry contains the instance sets to look through
  486. InstanceName is the instance name to look for
  487. *InstanceIndex return instance index within set
  488. Return Value:
  489. Instance set containing instance name or NULL of instance name not found
  490. --*/
  491. {
  492. PBINSTANCESET InstanceSet;
  493. PLIST_ENTRY InstanceSetList;
  494. ULONG BaseNameLen;
  495. PWCHAR InstanceNamePtr;
  496. ULONG InstanceNameIndex;
  497. ULONG InstanceSetFirstIndex, InstanceSetLastIndex;
  498. ULONG i;
  499. WmipEnterSMCritSection();
  500. InstanceSetList = GuidEntry->ISHead.Flink;
  501. while (InstanceSetList != &GuidEntry->ISHead)
  502. {
  503. InstanceSet = CONTAINING_RECORD(InstanceSetList,
  504. INSTANCESET,
  505. GuidISList);
  506. if (InstanceSet->Flags & IS_INSTANCE_BASENAME)
  507. {
  508. BaseNameLen = wcslen(InstanceSet->IsBaseName->BaseName);
  509. if (wcsncmp(InstanceName,
  510. InstanceSet->IsBaseName->BaseName,
  511. BaseNameLen) == 0)
  512. {
  513. InstanceNamePtr = InstanceName + BaseNameLen;
  514. InstanceNameIndex = _wtoi(InstanceNamePtr);
  515. InstanceSetFirstIndex = InstanceSet->IsBaseName->BaseIndex;
  516. InstanceSetLastIndex = InstanceSetFirstIndex + InstanceSet->Count;
  517. if (( (InstanceNameIndex >= InstanceSetFirstIndex) &&
  518. (InstanceNameIndex < InstanceSetLastIndex)) &&
  519. ((InstanceNameIndex != 0) || WmipIsNumber(InstanceNamePtr)))
  520. {
  521. InstanceSet = CONTAINING_RECORD(InstanceSetList,
  522. INSTANCESET,
  523. GuidISList);
  524. *InstanceIndex = InstanceNameIndex - InstanceSetFirstIndex;
  525. WmipReferenceIS(InstanceSet);
  526. WmipAssert(InstanceSet->Signature == IS_SIGNATURE);
  527. WmipLeaveSMCritSection();
  528. return(InstanceSet);
  529. }
  530. }
  531. } else if (InstanceSet->Flags & IS_INSTANCE_STATICNAMES) {
  532. for (i = 0; i < InstanceSet->Count; i++)
  533. {
  534. if (wcscmp(InstanceName,
  535. InstanceSet->IsStaticNames->StaticNamePtr[i]) == 0)
  536. {
  537. InstanceSet = CONTAINING_RECORD(InstanceSetList,
  538. INSTANCESET,
  539. GuidISList);
  540. *InstanceIndex = i;
  541. WmipReferenceIS(InstanceSet);
  542. WmipAssert(InstanceSet->Signature == IS_SIGNATURE);
  543. WmipLeaveSMCritSection();
  544. return(InstanceSet);
  545. }
  546. }
  547. }
  548. InstanceSetList = InstanceSetList->Flink;
  549. }
  550. WmipLeaveSMCritSection();
  551. return(NULL);
  552. }
  553. BOOLEAN WmipRealloc(
  554. PVOID *Buffer,
  555. ULONG CurrentSize,
  556. ULONG NewSize,
  557. BOOLEAN FreeOriginalBuffer
  558. )
  559. /*++
  560. Routine Description:
  561. Reallocate a buffer to a larger size while preserving data
  562. Arguments:
  563. Buffer on entry has the buffer to be reallocated, on exit has the new
  564. buffer
  565. CurrentSize is the current size of the buffer
  566. NewSize has the new size desired
  567. Return Value:
  568. TRUE if realloc was successful
  569. --*/
  570. {
  571. PVOID NewBuffer;
  572. WmipAssert(NewSize > CurrentSize);
  573. NewBuffer = WmipAlloc(NewSize);
  574. if (NewBuffer != NULL)
  575. {
  576. memset(NewBuffer, 0, NewSize);
  577. memcpy(NewBuffer, *Buffer, CurrentSize);
  578. if (FreeOriginalBuffer)
  579. {
  580. WmipFree(*Buffer);
  581. }
  582. *Buffer = NewBuffer;
  583. return(TRUE);
  584. }
  585. return(FALSE);
  586. }