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.

413 lines
12 KiB

  1. /*
  2. * memmgr.h
  3. *
  4. * Copyright (c) 1993 - 1995 by DataBeam Corporation, Lexington, KY
  5. *
  6. * Abstract:
  7. * This is the interface file for the MemoryManager class. This class
  8. * is used to efficiently manage the passing of data through a system.
  9. * There are two primary techniques it uses to accomplish the goal of
  10. * efficiency:
  11. *
  12. * 1. Use of locally managed "blocked" memory. When this class is
  13. * instantiated, it allocates a large block of memory which it then
  14. * chops up into various size blocks. These blocks are then used
  15. * to hold data, rather than having to do system calls every time
  16. * some memory is needed.
  17. *
  18. * 2. Use of a "copy on lock" algorithm. When memory is first
  19. * allocated, the source data is NOT yet copied to it. Copy
  20. * operations will implicitly use the reference rather than copying
  21. * the data. If the data needs to be retained longer than the
  22. * expected life-span of the reference, then a Lock command can be
  23. * sent to the block to cause it to be copied.
  24. *
  25. * When an object needs to allocate memory to hold some data, it calls
  26. * an allocate function within an object of this class. Assuming that
  27. * the request can be satisfied, a pointer to a Memory object is returned.
  28. * This Memory object remembers two addresses: the address of the reference
  29. * buffer (where the source data is); and the address of the copy buffer
  30. * (which is the buffer allocated to hold the data). As mentioned above,
  31. * the data is NOT copied to the copy buffer as part of the allocation
  32. * process. The data is not copied until the Memory object is locked
  33. * for the first time.
  34. *
  35. * Objects of this class keep a list of available buffers. There is one
  36. * list for each size block that is available. One of the constructor
  37. * parameters can be used to control how much data is allocated up front,
  38. * and what size blocks it is chopped up into. This makes this class very
  39. * flexible in how it can be used.
  40. *
  41. * Caveats:
  42. * None.
  43. *
  44. * Author:
  45. * James P. Galvin, Jr.
  46. */
  47. #ifndef _MEMORY_MANAGER2_H_
  48. #define _MEMORY_MANAGER2_H_
  49. /*
  50. * These are the errors that can be returned from some of the memory manager
  51. * member functions.
  52. */
  53. typedef enum
  54. {
  55. MEMORY_MANAGER_NO_ERROR,
  56. MEMORY_MANAGER_ALLOCATION_FAILURE,
  57. MEMORY_MANAGER_INVALID_PARAMETER
  58. } MemoryManagerError;
  59. typedef MemoryManagerError * PMemoryManagerError;
  60. /*
  61. * An array of this structure is passed into the constructor to define the
  62. * number and size of blocks to be created by this object.
  63. */
  64. typedef struct
  65. {
  66. ULong block_size;
  67. ULong block_count;
  68. } MemoryTemplate;
  69. typedef MemoryTemplate * PMemoryTemplate;
  70. /*
  71. * This structure is used to maintain general information about the shared
  72. * memory region that has to be shared between all users of it.
  73. */
  74. typedef struct
  75. {
  76. ULong free_stack_offset;
  77. ULong free_stack_count;
  78. ULong block_information_offset;
  79. ULong total_block_count;
  80. ULong current_block_count;
  81. } MemoryInformation;
  82. typedef MemoryInformation * PMemoryInformation;
  83. /*
  84. * This structure is used to keep information about each memory block that is
  85. * being managed by an instance of this class.
  86. */
  87. typedef struct
  88. {
  89. ULong block_offset;
  90. ULong length;
  91. ULong free_stack_offset;
  92. ULong lock_count;
  93. ULong flags;
  94. } BlockInformation;
  95. typedef BlockInformation * PBlockInformation;
  96. /*
  97. * These are the masks for manipulating the flags of a BlockInformation structure
  98. */
  99. #define FREE_FLAG 0x1
  100. #define COMMIT_FLAG 0x2
  101. /*
  102. * The following are definitions for macros used to handle space and space
  103. * requirements in relation to page boundaries in the system.
  104. */
  105. #define EXPAND_TO_PAGE_BOUNDARY(p) (((p) + dwSystemPageSize - 1) & (~ (dwSystemPageSize - 1)))
  106. /*
  107. * The following number is used when the caller asks for the number of buffers remaining
  108. * within a Memory Manager where allocations are not restricted. The intention is
  109. * that this number is very large and enough for the caller to think that all its
  110. * allocation requests will succeed.
  111. */
  112. #define LARGE_BUFFER_COUNT 0x1000
  113. /*
  114. * These typedefs define a container that is used to hold block information
  115. * structure pointers. This is used to hold information about blocks that
  116. * are externally allocated, but are managed by this class.
  117. */
  118. typedef DictionaryClass BlockInformationList;
  119. /*
  120. * This is the class definition for the MemoryManager class.
  121. */
  122. class MemoryManager
  123. {
  124. public:
  125. MemoryManager ();
  126. MemoryManager (
  127. PMemoryTemplate memory_template,
  128. ULong memory_count,
  129. PMemoryManagerError memory_manager_error,
  130. ULong ulMaxExternalBlocks,
  131. BOOL bAllocsRestricted = TRUE);
  132. virtual ~MemoryManager ();
  133. virtual PMemory AllocateMemory (
  134. PUChar reference_ptr,
  135. ULong length,
  136. MemoryLockMode memory_lock_mode =
  137. MEMORY_LOCK_NORMAL);
  138. virtual Void FreeMemory (
  139. PMemory memory);
  140. virtual PMemory CreateMemory (
  141. BlockNumber block_number,
  142. MemoryLockMode memory_lock_mode =
  143. MEMORY_LOCK_NORMAL);
  144. virtual Void LockMemory (
  145. PMemory memory);
  146. virtual Void UnlockMemory (
  147. PMemory memory);
  148. ULong GetBufferCount ()
  149. {
  150. return((bAllocs_Restricted) ? Memory_Information->current_block_count : LARGE_BUFFER_COUNT);
  151. };
  152. virtual ULong GetBufferCount (
  153. ULong length);
  154. private:
  155. Void ReleaseMemory (
  156. PMemory memory);
  157. protected:
  158. ULong CalculateMemoryBufferSize (
  159. PMemoryTemplate memory_template,
  160. ULong memory_count,
  161. ULong * pulCommittedBytes);
  162. Void AllocateMemoryBuffer (
  163. ULong memory_buffer_size);
  164. Void InitializeMemoryBuffer (
  165. PMemoryTemplate memory_template,
  166. ULong memory_count);
  167. static DWORD dwSystemPageSize;
  168. HPUChar Memory_Buffer;
  169. PMemoryInformation Memory_Information;
  170. PFreeStack Free_Stack;
  171. ULong Free_Stack_Count;
  172. HPUChar Block_Information;
  173. BlockInformationList *pExternal_Block_Information;
  174. ULong Max_External_Blocks;
  175. BOOL fIsSharedMemory;
  176. BOOL bAllocs_Restricted;
  177. };
  178. typedef MemoryManager * PMemoryManager;
  179. /*
  180. * MemoryManager (
  181. * PMemoryTemplate memory_template,
  182. * USHORT memory_count,
  183. * PMemoryManagerError memory_manager_error)
  184. *
  185. * Functional Description:
  186. * This is the constructor for the MemoryManager class. It uses the
  187. * information in the specified memory templates to allocate a block
  188. * of memory and chop it up into fixed size pieces. It then puts
  189. * these pieces into a set of free block lists, so that it can allocate
  190. * memory on an as needed basis.
  191. *
  192. * Formal Parameters:
  193. * memory_template
  194. * This is the base address of an array of memory template structures.
  195. * Each element of this structure specifies how many blocks of a
  196. * specified block size should be allocated. The constructor scans
  197. * the array, totaling the required memory, and then makes one memory
  198. * allocation call. It then chops the memory as specified by the
  199. * memory templates. It is VERY important the memory templates be
  200. * specified in ascending order of block sizes. In other words,
  201. * smaller blocks should be specified first.
  202. * memory_count
  203. * This simply indicates how mamy memory templates there are in the
  204. * list.
  205. * memory_manager_error
  206. * This is the return value from the constructor. If anything besides
  207. * MEMORY_MANAGER_NO_ERROR is returned, the object was not able to
  208. * initialize itself properly, and should be destroyed immediately
  209. * without being used.
  210. *
  211. * Return Value:
  212. * None.
  213. *
  214. * Side Effects:
  215. * None.
  216. *
  217. * Caveats:
  218. * None.
  219. */
  220. /*
  221. * ~MemoryManager ()
  222. *
  223. * Functional Description:
  224. * This is the destructor for the MemoryManager class. It frees up all
  225. * resources being used by the Memory Manager object, including the
  226. * memory block allocated to hold all user data.
  227. *
  228. * Formal Parameters:
  229. * None.
  230. *
  231. * Return Value:
  232. * None.
  233. *
  234. * Side Effects:
  235. * None.
  236. *
  237. * Caveats:
  238. * None.
  239. */
  240. /*
  241. * PMemory AllocateMemory (
  242. * PUChar address,
  243. * ULong length)
  244. *
  245. * Functional Description:
  246. * This function is used to allocate a piece of memory from the Memory
  247. * Manager object. Note that the return value is not a pointer to the
  248. * memory, but rather, a pointer to a Memory object. The memory object
  249. * contains a buffer that is large enough to handle the reference data.
  250. *
  251. * Note that the reference data is not automatically copied into the
  252. * copy buffer of the Memory object. This copy operation does not occur
  253. * until the first time the Memory object is locked (through a Memory
  254. * Manager call, as defined below).
  255. *
  256. *
  257. * Formal Parameters:
  258. * address
  259. * This is the address of the reference data (or the source data).
  260. * length
  261. * This is the length of the reference data.
  262. *
  263. * Return Value:
  264. * A pointer to a Memory object if the request is successful.
  265. * NULL otherwise.
  266. *
  267. * Side Effects:
  268. * None.
  269. *
  270. * Caveats:
  271. * None.
  272. */
  273. /*
  274. * Void FreeMemory (
  275. * PMemory memory)
  276. *
  277. * Functional Description:
  278. * This function is used to free a previously allocated Memory object.
  279. * Note that if the lock count of the Memory object is not 0 (zero), the
  280. * object will not actually be freed yet. This call merely enables the
  281. * object to be freed (when the lock count does hit 0).
  282. *
  283. * In summary, for a Memory object to actually be freed, two conditions
  284. * must exist simultaneously: the Memory object must have been freed
  285. * with a call to this function; and the lock count must hit zero.
  286. *
  287. * Formal Parameters:
  288. * memory
  289. * This is a pointer to the Memory object being freed.
  290. *
  291. * Return Value:
  292. * None.
  293. *
  294. * Side Effects:
  295. * None.
  296. *
  297. * Caveats:
  298. * None.
  299. */
  300. /*
  301. * Void LockMemory (
  302. * PMemory memory)
  303. *
  304. * Functional Description:
  305. * This function is sued to lock an existing Memory object. A locked
  306. * Memory object will not be freed until the lock count hits zero.
  307. *
  308. * When the lock count transitions from 0 to 1, the reference data is
  309. * copied into the internal copy buffer.
  310. *
  311. * Formal Parameters:
  312. * memory
  313. * This is a pointer to the Memory object being locked.
  314. *
  315. * Return Value:
  316. * None.
  317. *
  318. * Side Effects:
  319. * None.
  320. *
  321. * Caveats:
  322. * None.
  323. */
  324. /*
  325. * Void UnlockMemory (
  326. * PMemory memory)
  327. *
  328. * Functional Description:
  329. * This function is used to unlock a Memory object that was previously
  330. * locked. Each time an object is unlocked, the lock count is decremented.
  331. * When the lock count hits zero, the memory will be freed if-and-only-if
  332. * the FreeMemory call has also been made. In essence, for a Memory
  333. * object to be freed, a call to FreeMemory must have been made, AND the
  334. * lock count must be zero.
  335. *
  336. * Formal Parameters:
  337. * memory
  338. * This is a pointer to the Memory object being unlocked.
  339. *
  340. * Return Value:
  341. * None.
  342. *
  343. * Side Effects:
  344. * None.
  345. *
  346. * Caveats:
  347. * None.
  348. */
  349. /*
  350. * ULong GetBufferCount ()
  351. *
  352. * Functional Description:
  353. * This function is used to determine the total number of available
  354. * buffers that remain in the pool. This should be used to determine
  355. * general resource levels only. It cannot be used to determine whether
  356. * or not there is a buffer of a particular size.
  357. *
  358. * Formal Parameters:
  359. * None.
  360. *
  361. * Return Value:
  362. * The total number of buffers available (regardless of size).
  363. *
  364. * Side Effects:
  365. * None.
  366. *
  367. * Caveats:
  368. * None.
  369. */
  370. /*
  371. * ULong GetBufferCount (
  372. * ULong buffer_size)
  373. *
  374. * Functional Description:
  375. * This function is used to determine the number of X size buffers
  376. * that remain in the pool.
  377. *
  378. * Formal Parameters:
  379. * buffer_size
  380. * The buffer size that we want to count.
  381. *
  382. * Return Value:
  383. * The number of 'buffer_size' buffers available.
  384. *
  385. * Side Effects:
  386. * None.
  387. *
  388. * Caveats:
  389. * None.
  390. */
  391. #endif