Leaked source code of windows server 2003
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.

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