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.

342 lines
7.2 KiB

  1. /*++
  2. Copyright (c) 1989 Microsoft Corporation
  3. Module Name:
  4. blktable.c
  5. Abstract:
  6. This module implements routines for managing tables.
  7. Author:
  8. Chuck Lenzmeier (chuckl) 4-Oct-1989
  9. Revision History:
  10. --*/
  11. #include "precomp.h"
  12. #include "blktable.tmh"
  13. #pragma hdrstop
  14. #define BugCheckFileId SRV_FILE_BLKTABLE
  15. #ifdef ALLOC_PRAGMA
  16. #pragma alloc_text( PAGE, SrvAllocateTable )
  17. #endif
  18. #if 0
  19. NOT PAGEABLE -- SrvGrowTable
  20. NOT PAGEABLE -- SrvRemoveEntryTable
  21. #endif
  22. VOID
  23. SrvAllocateTable (
  24. IN PTABLE_HEADER TableHeader,
  25. IN ULONG NumberOfEntries,
  26. IN BOOLEAN Nonpaged
  27. )
  28. /*++
  29. Routine Description:
  30. This routine allocates a table and sets those fields that it can.
  31. Arguments:
  32. TableHeader - a pointer to the table header structure
  33. NumberOfEntries - the number of table entries to allocate
  34. Nonpaged - indicates whether the table should be allocated from
  35. nonpaged pool
  36. Return Value:
  37. None.
  38. --*/
  39. {
  40. SHORT i;
  41. CLONG tableSize;
  42. PTABLE_ENTRY table;
  43. PAGED_CODE( );
  44. //
  45. // Allocate space for the table.
  46. //
  47. tableSize = sizeof(TABLE_ENTRY) * NumberOfEntries;
  48. if ( Nonpaged ) {
  49. table = ALLOCATE_NONPAGED_POOL( tableSize, BlockTypeTable );
  50. } else {
  51. table = ALLOCATE_HEAP_COLD( tableSize, BlockTypeTable );
  52. }
  53. if ( table == NULL ) {
  54. INTERNAL_ERROR(
  55. ERROR_LEVEL_EXPECTED,
  56. "SrvAllocateTable: Unable to allocate %d bytes from paged pool.",
  57. tableSize,
  58. NULL
  59. );
  60. TableHeader->Table = NULL;
  61. return;
  62. }
  63. IF_DEBUG(HEAP) {
  64. SrvPrint1( "SrvAllocateTable: Allocated table at %p\n", table );
  65. }
  66. //
  67. // Initialize the table, creating a linked list of free entries.
  68. //
  69. RtlZeroMemory( table, tableSize );
  70. table[NumberOfEntries-1].NextFreeEntry = -1;
  71. for ( i = (SHORT)(NumberOfEntries - 2); i >= 0; i-- ) {
  72. table[i].NextFreeEntry = (SHORT)(i + 1);
  73. }
  74. //
  75. // Point the table header to the table and set the first and
  76. // free entry indexes.
  77. //
  78. TableHeader->Table = table;
  79. TableHeader->Nonpaged = Nonpaged;
  80. TableHeader->TableSize = (USHORT)NumberOfEntries;
  81. TableHeader->FirstFreeEntry = 0;
  82. TableHeader->LastFreeEntry = (SHORT)(NumberOfEntries-1);
  83. return;
  84. } // SrvAllocateTable
  85. BOOLEAN
  86. SrvGrowTable (
  87. IN PTABLE_HEADER TableHeader,
  88. IN ULONG NumberOfNewEntries,
  89. IN ULONG MaxNumberOfEntries,
  90. OPTIONAL OUT NTSTATUS* pStatus
  91. )
  92. /*++
  93. Routine Description:
  94. This routine grows a table by the number of entries specified. It
  95. allocates new space that is large enough to hold the expanded
  96. table, copies over the current table, initializes the entries
  97. that were added, and frees the old table.
  98. WARNING: The calling routine *must* hold a lock for the table to
  99. prevent access to the table while it is being copied over.
  100. Arguments:
  101. TableHeader - a pointer to the table header structure
  102. NumberOfNewEntries - the number of table entries to add to the table
  103. MaxNumberOfEntries - the maximum allowable size for the table
  104. pStatus - Optional return value. INSUFFICIENT_RESOURCES means mem allocation error,
  105. while INSUFF_SERVER_RESOURCES means we're over our table limit
  106. Return Value:
  107. BOOLEAN - TRUE if the table was successfully grown, FALSE otherwise.
  108. --*/
  109. {
  110. ULONG newTableSize, totalEntries, oldNumberOfEntries;
  111. USHORT i;
  112. PTABLE_ENTRY table;
  113. oldNumberOfEntries = TableHeader->TableSize;
  114. totalEntries = oldNumberOfEntries + NumberOfNewEntries;
  115. //
  116. // If the table is already at the maximum size, kick out the request.
  117. //
  118. if ( oldNumberOfEntries >= MaxNumberOfEntries ) {
  119. INTERNAL_ERROR(
  120. ERROR_LEVEL_EXPECTED,
  121. "SrvGrowTable: Unable to grow table at max size (%ld).",
  122. MaxNumberOfEntries,
  123. NULL
  124. );
  125. if( ARGUMENT_PRESENT(pStatus) )
  126. {
  127. *pStatus = STATUS_INSUFF_SERVER_RESOURCES;
  128. }
  129. return FALSE;
  130. }
  131. //
  132. // If adding the requested number would put the table size over the
  133. // maximum, allocate to the maximum size.
  134. //
  135. if ( totalEntries > MaxNumberOfEntries ) {
  136. totalEntries = MaxNumberOfEntries;
  137. NumberOfNewEntries = totalEntries - oldNumberOfEntries;
  138. }
  139. newTableSize = totalEntries * sizeof(TABLE_ENTRY);
  140. //
  141. // Allocate space for the new table.
  142. //
  143. if ( TableHeader->Nonpaged ) {
  144. table = ALLOCATE_NONPAGED_POOL( newTableSize, BlockTypeTable );
  145. } else {
  146. table = ALLOCATE_HEAP_COLD( newTableSize, BlockTypeTable );
  147. }
  148. if ( table == NULL ) {
  149. INTERNAL_ERROR(
  150. ERROR_LEVEL_EXPECTED,
  151. "SrvGrowTable: Unable to allocate %d bytes from paged pool",
  152. sizeof( BLOCK_HEADER ) + newTableSize,
  153. NULL
  154. );
  155. if( ARGUMENT_PRESENT(pStatus) )
  156. {
  157. *pStatus = STATUS_INSUFFICIENT_RESOURCES;
  158. }
  159. return FALSE;
  160. }
  161. IF_DEBUG(HEAP) {
  162. SrvPrint1( "SrvGrowTable: Allocated new table at %p\n", table );
  163. }
  164. //
  165. // Copy over the information from the old table. Zero the remainder
  166. // of the table.
  167. //
  168. RtlCopyMemory(
  169. table,
  170. TableHeader->Table,
  171. oldNumberOfEntries * sizeof(TABLE_ENTRY)
  172. );
  173. RtlZeroMemory(
  174. (PCHAR)table + (oldNumberOfEntries * sizeof(TABLE_ENTRY)),
  175. (totalEntries - oldNumberOfEntries) * sizeof(TABLE_ENTRY)
  176. );
  177. //
  178. // Free the old table.
  179. //
  180. SrvFreeTable( TableHeader );
  181. //
  182. // Initialize the new table locations in the free list of the table.
  183. //
  184. table[totalEntries-1].NextFreeEntry = -1;
  185. for ( i = (USHORT)(totalEntries-2); i >= oldNumberOfEntries; i-- ) {
  186. table[i].NextFreeEntry = (SHORT)(i + 1);
  187. }
  188. //
  189. // Reinitialize the fields of the table header. It is assumed that
  190. // the table did not previously have any free entries.
  191. //
  192. TableHeader->Table = table;
  193. TableHeader->TableSize = (USHORT)totalEntries;
  194. TableHeader->FirstFreeEntry = (SHORT)oldNumberOfEntries;
  195. TableHeader->LastFreeEntry = (SHORT)(totalEntries-1);
  196. if( ARGUMENT_PRESENT( pStatus ) )
  197. {
  198. *pStatus = STATUS_SUCCESS;
  199. }
  200. return TRUE;
  201. } // SrvGrowTable
  202. VOID
  203. SrvRemoveEntryTable (
  204. IN PTABLE_HEADER TableHeader,
  205. IN USHORT Index
  206. )
  207. /*++
  208. Routine Description:
  209. This function removes an entry from a table.
  210. *** The lock controlling access to the table must be held when this
  211. function is called.
  212. Arguments:
  213. Table - Address of table header.
  214. Index - Index within table of entry to remove.
  215. Return Value:
  216. None.
  217. --*/
  218. {
  219. PTABLE_ENTRY entry;
  220. ASSERT( Index < TableHeader->TableSize );
  221. entry = &TableHeader->Table[Index];
  222. if ( TableHeader->LastFreeEntry >= 0 ) {
  223. //
  224. // Free list was not empty.
  225. //
  226. TableHeader->Table[TableHeader->LastFreeEntry].NextFreeEntry = Index;
  227. TableHeader->LastFreeEntry = Index;
  228. } else {
  229. //
  230. // Free list was empty.
  231. //
  232. TableHeader->FirstFreeEntry = Index;
  233. TableHeader->LastFreeEntry = Index;
  234. }
  235. entry->Owner = NULL;
  236. entry->NextFreeEntry = -1;
  237. return;
  238. } // SrvRemoveEntryTable