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.

177 lines
3.5 KiB

  1. /*++
  2. Copyright (c) 1992 Microsoft Corporation
  3. Module Name:
  4. suballcp.h
  5. Abstract:
  6. This is the private include file for the suballocation
  7. package.
  8. Author:
  9. Dave Hastings (daveh) creation-date 25-Jan-1994
  10. Revision History:
  11. --*/
  12. #include <nt.h>
  13. #include <ntrtl.h>
  14. #include <nturtl.h>
  15. #include <windows.h>
  16. #include <malloc.h>
  17. #include <suballoc.h>
  18. //
  19. // Constants
  20. //
  21. //
  22. // Smallest chunk that will be sub allocated
  23. // 1024 was chosen currently, because that is the
  24. // smallest chunk XMS will allocate.
  25. //
  26. #define SUBALLOC_GRANULARITY 1024
  27. //
  28. // Assertions and macros
  29. //
  30. //
  31. // Force code to be stepped through
  32. //
  33. #if 0
  34. #define ASSERT_STEPTHROUGH DbgBreakPoint()
  35. #else
  36. #define ASSERT_STEPTHROUGH
  37. #endif
  38. //
  39. // Signature macros for SUBALLOCATION
  40. //
  41. #if DBG
  42. //
  43. // signature is "SubA"
  44. //
  45. #define INIT_SUBALLOC_SIGNATURE(p) p->Signature = (ULONG)0x41627553
  46. #define ASSERT_SUBALLOC(p) ASSERT((p->Signature == (ULONG)0x41627553))
  47. #else
  48. #define INIT_SUBALLOC_SIGNATURE(p)
  49. #define ASSERT_SUBALLOC(p)
  50. #endif
  51. //
  52. // Macro for extracting a bit from a bitfield of type char
  53. //
  54. #define GET_BIT_FROM_CHAR_ARRAY(p, i) \
  55. ((p[i/(sizeof(UCHAR) * 8)] >> (i % (sizeof(UCHAR) * 8))) & 1)
  56. //
  57. // Macro for setting a bit in a bitfield of type char
  58. //
  59. #define SET_BIT_IN_CHAR_ARRAY(p, i) \
  60. (p[i/(sizeof(UCHAR) * 8)] |= (1 << (i % (sizeof(UCHAR) * 8))))
  61. //
  62. // Macro for clearing a bit in a bitfield of type char
  63. //
  64. #define CLEAR_BIT_IN_CHAR_ARRAY(p, i) \
  65. (p[i/(sizeof(UCHAR) * 8)] &= ~(1 << (i % (sizeof(UCHAR) * 8))))
  66. //
  67. // Generate a sub alloc block index from an address
  68. //
  69. #define ADDRESS_TO_BLOCK_INDEX(p, i) \
  70. ((i - p->BaseAddress)/ SUBALLOC_GRANULARITY)
  71. //
  72. // Generate an address from a block index
  73. //
  74. #define BLOCK_INDEX_TO_ADDRESS(p, i) \
  75. (p->BaseAddress + i * SUBALLOC_GRANULARITY)
  76. // Round the allocated size to next allocation
  77. // granularity
  78. //
  79. #define ALLOC_ROUND(s) \
  80. (s + SUBALLOC_GRANULARITY - 1) / SUBALLOC_GRANULARITY
  81. //
  82. // Types
  83. //
  84. //
  85. // Enum for commit acctions
  86. //
  87. typedef enum {
  88. SACommit,
  89. SADecommit
  90. } COMMIT_ACTION;
  91. //
  92. // Structure for tracking the address space. Each chunk of
  93. // memory of SUBALLOC_GRANULARITY in size is represented by
  94. // a bit. Each chunk of memory of COMMIT_GRANULARITY is
  95. // represented by one bit of the array Allocated.
  96. //
  97. // ?? Should we add a field to indicate whether the chunk is
  98. // committed? We can always check for all allocated bits
  99. // zero, and use that as an indication that the chunk is
  100. // not committed.
  101. //
  102. //
  103. typedef struct _SubAllocation {
  104. #if DBG
  105. ULONG Signature;
  106. #endif
  107. PSACOMMITROUTINE CommitRoutine;
  108. PSACOMMITROUTINE DecommitRoutine;
  109. PSAMEMORYMOVEROUTINE MoveMemRoutine;
  110. ULONG BaseAddress;
  111. ULONG Size; // size in SUBALLOC_GRANULARITY
  112. ULONG FirstFree; // keeps block # of first free block
  113. // cuts alloc time in half
  114. //
  115. // bitfield with one bit per chunk. Bit set indicates
  116. // allocated. Bit clear indicates free. All bits
  117. // clear indicates un committed
  118. //
  119. UCHAR Allocated[1];
  120. } SUBALLOCATIONDATA, *PSUBALLOCATIONDATA;
  121. //
  122. // Internal Routines
  123. //
  124. BOOL
  125. CommitChunk(
  126. PSUBALLOCATIONDATA SubAllocation,
  127. ULONG StartChunk,
  128. ULONG Size,
  129. COMMIT_ACTION Action
  130. );
  131. BOOL
  132. IsBlockCommitted(
  133. PSUBALLOCATIONDATA SubAlloc,
  134. ULONG CurrentBlock
  135. );
  136. BOOL
  137. AllocateChunkAt(
  138. PSUBALLOCATIONDATA SubAlloc,
  139. ULONG Size,
  140. ULONG BlockIndex
  141. );
  142. BOOL
  143. FreeChunk(
  144. PSUBALLOCATIONDATA SubAlloc,
  145. ULONG Size,
  146. ULONG BlockIndex
  147. );