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.

254 lines
3.9 KiB

  1. /*++
  2. Copyright (c) 1992 Microsoft Corporation
  3. Module Name:
  4. membmgr.hxx
  5. Abstract:
  6. This class offers two classes for the management of fixed
  7. size blocks of memory. The first class STATIC_MEM_BLOCK_MGR
  8. allows the user to allocate and free fixed size memory blocks
  9. up to the specified limit that the class was initialized to.
  10. The second class MEM_BLOCK_MGR offers a scheme for memory block
  11. management that will grow as the clients needs increase.
  12. --*/
  13. #if !defined(_MEM_BLOCK_MGR_DEFN_)
  14. #define _MEM_BLOCK_MGR_DEFN_
  15. #include "array.hxx"
  16. #include "bitvect.hxx"
  17. DECLARE_CLASS( STATIC_MEM_BLOCK_MGR );
  18. class STATIC_MEM_BLOCK_MGR : public OBJECT {
  19. public:
  20. DECLARE_CONSTRUCTOR( STATIC_MEM_BLOCK_MGR );
  21. VIRTUAL
  22. ~STATIC_MEM_BLOCK_MGR(
  23. );
  24. NONVIRTUAL
  25. BOOLEAN
  26. Initialize(
  27. IN ULONG MemBlockSize,
  28. IN ULONG NumBlocks DEFAULT 128
  29. );
  30. NONVIRTUAL
  31. PVOID
  32. Alloc(
  33. );
  34. NONVIRTUAL
  35. BOOLEAN
  36. Free(
  37. OUT PVOID MemBlock
  38. );
  39. NONVIRTUAL
  40. ULONG
  41. QueryBlockSize(
  42. ) CONST;
  43. NONVIRTUAL
  44. ULONG
  45. QueryNumBlocks(
  46. ) CONST;
  47. private:
  48. NONVIRTUAL
  49. VOID
  50. Construct(
  51. );
  52. NONVIRTUAL
  53. VOID
  54. Destroy(
  55. );
  56. PCHAR _heap;
  57. ULONG _num_blocks;
  58. ULONG _block_size;
  59. ULONG _num_allocated;
  60. ULONG _next_alloc;
  61. BITVECTOR _bitvector;
  62. };
  63. INLINE
  64. ULONG
  65. STATIC_MEM_BLOCK_MGR::QueryBlockSize(
  66. ) CONST
  67. /*++
  68. Routine Description:
  69. This routine return the number of bytes in a block returned
  70. by this object.
  71. Arguments:
  72. None.
  73. Return Value:
  74. The number of bytes per block.
  75. --*/
  76. {
  77. return _block_size;
  78. }
  79. INLINE
  80. ULONG
  81. STATIC_MEM_BLOCK_MGR::QueryNumBlocks(
  82. ) CONST
  83. /*++
  84. Routine Description:
  85. This routine return the number of blocks contained
  86. by this object.
  87. Arguments:
  88. None.
  89. Return Value:
  90. The number of blocks.
  91. --*/
  92. {
  93. return _num_blocks;
  94. }
  95. DECLARE_CLASS( MEM_BLOCK_MGR );
  96. class MEM_BLOCK_MGR : public OBJECT {
  97. public:
  98. ULIB_EXPORT
  99. DECLARE_CONSTRUCTOR( MEM_BLOCK_MGR );
  100. VIRTUAL
  101. ULIB_EXPORT
  102. ~MEM_BLOCK_MGR(
  103. );
  104. NONVIRTUAL
  105. ULIB_EXPORT
  106. BOOLEAN
  107. Initialize(
  108. IN ULONG MemBlockSize,
  109. IN ULONG InitialNumBlocks DEFAULT 128
  110. );
  111. NONVIRTUAL
  112. ULIB_EXPORT
  113. PVOID
  114. Alloc(
  115. );
  116. NONVIRTUAL
  117. ULIB_EXPORT
  118. BOOLEAN
  119. Free(
  120. OUT PVOID MemBlock
  121. );
  122. NONVIRTUAL
  123. ULONG
  124. QueryBlockSize(
  125. ) CONST;
  126. private:
  127. NONVIRTUAL
  128. VOID
  129. Construct(
  130. );
  131. NONVIRTUAL
  132. VOID
  133. Destroy(
  134. );
  135. PSTATIC_MEM_BLOCK_MGR _static_mem_list[32];
  136. };
  137. INLINE
  138. ULONG
  139. MEM_BLOCK_MGR::QueryBlockSize(
  140. ) CONST
  141. /*++
  142. Routine Description:
  143. This routine return the number of bytes in a block returned
  144. by this object.
  145. Arguments:
  146. None.
  147. Return Value:
  148. The number of bytes per block.
  149. --*/
  150. {
  151. return _static_mem_list[0] ? _static_mem_list[0]->QueryBlockSize() : 0;
  152. }
  153. INLINE
  154. PVOID
  155. operator new(
  156. IN size_t Size,
  157. IN PVOID Pointer
  158. )
  159. /*++
  160. Routine Description:
  161. This is an explicit placement version of the 'new' operator
  162. which clients of these classes may wish to use in order to
  163. call the constructor on their newly allocated objects.
  164. Arguments:
  165. Size - Supplies the size of the buffer.
  166. Pointer - Supplies a pointer to the buffer.
  167. Return Value:
  168. This function returns the passed in pointer.
  169. --*/
  170. {
  171. return Pointer;
  172. }
  173. #endif // _MEM_BLOCK_MGR_DEFN_