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.

157 lines
2.7 KiB

  1. /*++
  2. Copyright (c) 1997 Microsoft Corporation
  3. Module Name:
  4. resource.c
  5. Abstract:
  6. Generic resource management routines for the Cluster Network driver.
  7. Author:
  8. Mike Massa (mikemas) February 12, 1997
  9. Revision History:
  10. Who When What
  11. -------- -------- ----------------------------------------------
  12. mikemas 02-12-97 created
  13. Notes:
  14. --*/
  15. #include "precomp.h"
  16. #pragma hdrstop
  17. PCN_RESOURCE
  18. CnAllocateResource(
  19. IN PCN_RESOURCE_POOL Pool
  20. )
  21. /*++
  22. Routine Description:
  23. Allocates a resource from a resource pool.
  24. Arguments:
  25. Pool - A pointer to the pool from which to allocate a resource.
  26. Return Value:
  27. A pointer to the allocated resource if successful.
  28. NULL if unsuccessful.
  29. --*/
  30. {
  31. PCN_RESOURCE resource;
  32. PSINGLE_LIST_ENTRY entry = ExInterlockedPopEntrySList(
  33. &(Pool->ResourceList),
  34. &(Pool->ResourceListLock)
  35. );
  36. if (entry != NULL) {
  37. resource = CONTAINING_RECORD(entry, CN_RESOURCE, Linkage);
  38. }
  39. else {
  40. resource = (*(Pool->CreateRoutine))(Pool->CreateContext);
  41. if (resource != NULL) {
  42. CN_INIT_SIGNATURE(resource, CN_RESOURCE_SIG);
  43. resource->Pool = Pool;
  44. }
  45. }
  46. return(resource);
  47. } // CnAllocateResource
  48. VOID
  49. CnFreeResource(
  50. PCN_RESOURCE Resource
  51. )
  52. /*++
  53. Routine Description:
  54. Frees a resource back to a resource pool.
  55. Arguments:
  56. Resource - A pointer to the resource to free.
  57. Return Value:
  58. None.
  59. --*/
  60. {
  61. PCN_RESOURCE_POOL pool = Resource->Pool;
  62. if (ExQueryDepthSList(&(pool->ResourceList)) < pool->Depth) {
  63. ExInterlockedPushEntrySList(
  64. &(pool->ResourceList),
  65. &(Resource->Linkage),
  66. &(pool->ResourceListLock)
  67. );
  68. }
  69. else {
  70. (*(pool->DeleteRoutine))(Resource);
  71. }
  72. return;
  73. } // CnpFreeResource
  74. VOID
  75. CnDrainResourcePool(
  76. PCN_RESOURCE_POOL Pool
  77. )
  78. /*++
  79. Routine Description:
  80. Frees all cached resources in a resource pool in preparation for the
  81. pool to be destroyed. This routine does not free the memory containing
  82. the pool.
  83. Arguments:
  84. Pool - A pointer to the pool to drain.
  85. Return Value:
  86. None.
  87. --*/
  88. {
  89. PSINGLE_LIST_ENTRY entry;
  90. PCN_RESOURCE resource;
  91. while ( (entry = ExInterlockedPopEntrySList(
  92. &(Pool->ResourceList),
  93. &(Pool->ResourceListLock)
  94. )
  95. ) != NULL
  96. )
  97. {
  98. resource = CONTAINING_RECORD(entry, CN_RESOURCE, Linkage);
  99. (*(Pool->DeleteRoutine))(resource);
  100. }
  101. return;
  102. } // CnDrainResourcePool