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.

284 lines
6.4 KiB

  1. /*
  2. * memory.h
  3. *
  4. * Copyright (c) 1993 - 1995 by DataBeam Corporation, Lexington, KY
  5. *
  6. * Abstract:
  7. * This is the interface file for the Memory class. Instances of this
  8. * class are used to pass data around the system.
  9. *
  10. * Each instance of this class maintains two pointers. The first is a
  11. * pointer to the reference data (or the source data) which this object
  12. * is responsible for representing. The second is a pointer to a copy
  13. * buffer, which is a piece of allocated memory that a Memory object
  14. * can copy the data into if necessary.
  15. *
  16. * When a Memory object is created, both of these addresses are passed
  17. * in to it. It does not, however, copy the data from the reference
  18. * buffer to the copy buffer just yet. If anyone asks the address of the
  19. * buffer, it will simply return the reference pointer. However, the
  20. * first time the buffer is locked, the data will be copied from the
  21. * reference buffer to the copy buffer for safe keeping. In essence,
  22. * the lock function tells the Memory object that someone is interested
  23. * in the data for longer than the reference buffer will remain valid.
  24. *
  25. * After the object is locked, a call to retrieve a memory pointer will
  26. * result in the copy pointer being returned.
  27. *
  28. * Each time the lock function is called, a lock count is incremented.
  29. * The copy operation only takes place the first time the buffer is
  30. * locked, however.
  31. *
  32. * In addition to maintaining a lock count, this object keeps a flag
  33. * indicating whether or not it has been freed by the allocator. This
  34. * freeing really means that the object is enabled to be freed as soon
  35. * as the lock count hits zero.
  36. *
  37. * Caveats:
  38. * None.
  39. *
  40. * Author:
  41. * James P. Galvin, Jr.
  42. */
  43. #ifndef _MEMORY_
  44. #define _MEMORY_
  45. #include "signatr.h"
  46. #define MEMORY_PRIORITIES 3
  47. typedef enum {
  48. HIGHEST_PRIORITY = 0,
  49. RECV_PRIORITY = 1,
  50. SEND_PRIORITY = 2
  51. } MemoryPriority;
  52. /*
  53. * This is the class definition for the Memory class.
  54. */
  55. class Memory;
  56. typedef Memory * PMemory;
  57. class Memory
  58. {
  59. public:
  60. Memory (PUChar reference_ptr,
  61. ULong length,
  62. PUChar copy_ptr);
  63. ~Memory ()
  64. {
  65. ASSERT(SIGNATURE_MATCH(this, MemorySignature));
  66. };
  67. Void Init (PUChar reference_ptr,
  68. ULong length,
  69. MemoryPriority priority,
  70. PUChar copy_ptr);
  71. PUChar GetPointer ()
  72. {
  73. ASSERT(SIGNATURE_MATCH(this, MemorySignature));
  74. return (Copy_Ptr);
  75. }
  76. ULong GetLength ()
  77. {
  78. ASSERT(SIGNATURE_MATCH(this, MemorySignature));
  79. return (Length);
  80. }
  81. int GetLockCount ()
  82. {
  83. ASSERT(SIGNATURE_MATCH(this, MemorySignature));
  84. return ((int) lLock);
  85. };
  86. MemoryPriority GetMemoryPriority ()
  87. {
  88. return m_priority;
  89. };
  90. Void Lock ()
  91. {
  92. ASSERT(SIGNATURE_MATCH(this, MemorySignature));
  93. InterlockedIncrement (& lLock);
  94. TRACE_OUT (("Memory::Lock: buffer at address %p. Lock count: %d",
  95. (UINT_PTR) Copy_Ptr, lLock));
  96. ASSERT (lLock > 0);
  97. };
  98. long Unlock ()
  99. {
  100. ASSERT(SIGNATURE_MATCH(this, MemorySignature));
  101. ASSERT (lLock > 0);
  102. TRACE_OUT (("Memory::UnLock: buffer at address %p. Lock count: %d",
  103. (UINT_PTR) Copy_Ptr, lLock - 1));
  104. return (InterlockedDecrement (&lLock));
  105. }
  106. private:
  107. ULong Length;
  108. PUChar Copy_Ptr;
  109. long lLock;
  110. MemoryPriority m_priority;
  111. /*
  112. * NOTEs:
  113. * 1. The Memory class can not have virtual member functions, because
  114. * of the Init() member.
  115. * 2. sizeof(Memory) should be DWORD-aligned, because of the
  116. * AllocateMemory implementation.
  117. */
  118. #ifndef SHIP_BUILD
  119. public:
  120. char mSignature[SIGNATURE_LENGTH];
  121. #endif // SHIP_BUILD
  122. };
  123. /*
  124. * Memory (
  125. * PUChar reference_ptr,
  126. * ULong length,
  127. * PUChar copy_ptr)
  128. *
  129. * Functional Description:
  130. * This is the constructor for the Memory class. All it does is
  131. * initialize the instance variable with the passed in values.
  132. *
  133. * Formal Parameters:
  134. * reference_ptr (i)
  135. * This is a pointer to the data that is to represented by this
  136. * Memory object.
  137. * length (i)
  138. * This is the length of the reference buffer.
  139. * copy_ptr (i)
  140. * This is the address of an allocated buffer that the Memory object
  141. * can use to preserve the contents of the reference buffer if a lock
  142. * operation occurs.
  143. *
  144. * Return Value:
  145. * None.
  146. *
  147. * Side Effects:
  148. * None.
  149. *
  150. * Caveats:
  151. * None.
  152. */
  153. /*
  154. * ~Memory ()
  155. *
  156. * Functional Description:
  157. * This is the destructor for the Memory class. It does nothing at this
  158. * time. Note that it is the responsibility of the memory manager that
  159. * is using Memory objects to free up the memory.
  160. *
  161. * Formal Parameters:
  162. * None.
  163. *
  164. * Return Value:
  165. * None.
  166. *
  167. * Side Effects:
  168. * None.
  169. *
  170. * Caveats:
  171. * None.
  172. */
  173. /*
  174. * ULong GetLength ()
  175. *
  176. * Functional Description:
  177. * This function retrieves the length of the data being represented by
  178. * this object.
  179. *
  180. * Formal Parameters:
  181. * None.
  182. *
  183. * Return Value:
  184. * The length of the data.
  185. *
  186. * Side Effects:
  187. * None.
  188. *
  189. * Caveats:
  190. * None.
  191. */
  192. /*
  193. * PUChar GetPointer ()
  194. *
  195. * Functional Description:
  196. * This function retrieves the buffer being represented by
  197. * this object.
  198. *
  199. * Formal Parameters:
  200. * None.
  201. *
  202. * Return Value:
  203. * The buffer pointer.
  204. *
  205. * Side Effects:
  206. * None.
  207. *
  208. * Caveats:
  209. * None.
  210. */
  211. /*
  212. * int GetLockCount ()
  213. *
  214. * Functional Description:
  215. * This function retrieves the lock counter for the buffer being represented by
  216. * this object.
  217. *
  218. * Formal Parameters:
  219. * None.
  220. *
  221. * Return Value:
  222. * The buffer's current lock counter.
  223. *
  224. * Side Effects:
  225. * None.
  226. *
  227. * Caveats:
  228. * None.
  229. */
  230. /*
  231. * Void Lock ()
  232. *
  233. * Functional Description:
  234. * This function locks the buffer being represented by
  235. * this object.
  236. *
  237. * Formal Parameters:
  238. * None.
  239. *
  240. * Return Value:
  241. * None.
  242. *
  243. * Side Effects:
  244. * None.
  245. *
  246. * Caveats:
  247. * None.
  248. */
  249. /*
  250. * int Unlock ()
  251. *
  252. * Functional Description:
  253. * This function unlocks the buffer being represented by
  254. * this object.
  255. *
  256. * Formal Parameters:
  257. * None.
  258. *
  259. * Return Value:
  260. * The lock count after the unlock operation.
  261. *
  262. * Side Effects:
  263. * None.
  264. *
  265. * Caveats:
  266. * None.
  267. */
  268. #endif
  269.