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.

285 lines
7.2 KiB

  1. /**********************************************************************/
  2. /** Microsoft LAN Manager **/
  3. /** Copyright(c) Microsoft Corp., 1990, 1991 **/
  4. /**********************************************************************/
  5. /*
  6. LOCLHEAP.hxx
  7. LM 2.1 Unlockable Heap and Element package
  8. This header file describes the HEAP_HANDLE and ELEMENT_HANDLE
  9. objects. These objects can be used to create unlockable memory
  10. buffers. In particular, HEAP_HANDLEs can be allocated while the
  11. shell is still starting up, and ELEMENT_HANDLEs can later be
  12. allocated within these heaps without any danger of being freed
  13. when the current application exits.
  14. Under Windows, a HEAP_HANDLE is a handle to a block of global
  15. memory to be accessed as a fixed heap, and an ELEMENT_HANDLE is
  16. a handle to a block of memory within a HEAP_HANDLE heap.
  17. The same interface is available to DOS/OS2 applications, but all
  18. operations are stubbed out or mapped to malloc/free.
  19. When an ELEMENT_HANDLE is locked, that handle and the HEAP_HANDLE
  20. heap containing it are locked and the pointer may be used freely.
  21. When an ELEMENT_HANDLE is not locked, its memory is free to float
  22. within its heap, and if a heap contains no locked elements, the
  23. heap can also float within global memory.
  24. These objects is used only by the UserProfile APIs (see uiprof.h),
  25. and are not recommended for new modules. The same functionality
  26. is available from MEM_MASTER (see heap.hxx). (Exception: the
  27. unlockable property is not yet available from MEM_MASTER, but
  28. can easily be made available.) The UserProfile APIs will
  29. probably be converted to MEM_MASTER for future releases, and
  30. this class will be eliminated.
  31. USAGE:
  32. //
  33. // Do not use these objects without first explicitly calling one
  34. // or the other form of Init().
  35. //
  36. #include <loclheap.hxx>
  37. HEAP_HANDLE heap;
  38. if (!heap.Init( 1024 ))
  39. error();
  40. ELEMENT_HANDLE element;
  41. if (!element.Init( heap, 128 ))
  42. error();
  43. LPSTR lpstr = element.Lock();
  44. // (... use lpstr ...)
  45. element.Unlock();
  46. element.Free();
  47. heap.Free();
  48. FILE HISTORY:
  49. jonn 26-Jan-1991 Created
  50. jonn 21-Mar-1991 Code review changes from 2/20/91 (attended
  51. by JonN, RustanL, ?)
  52. */
  53. #ifndef _LOCLHEAP_HXX_
  54. #define _LOCLHEAP_HXX_
  55. /* forward refs */
  56. DLL_CLASS ELEMENT_HANDLE;
  57. DLL_CLASS HEAP_HANDLE;
  58. /**********************************************************************
  59. NAME: HEAP_HANDLE
  60. SYNOPSIS: Heap in which to place ELEMENT_HANDLEs
  61. INTERFACE:
  62. Init()
  63. Initializes handle to empty status. Call only on
  64. uninitialized HEAP_HANDLEs.
  65. Init() (BOOL returning, taking args)
  66. Allocates new heap, by default moveable. Call
  67. only on uninitialized or null HEAP_HANDLEs. wBytes
  68. specifies the maximum heap size, wFlags specifies
  69. the Windows memory properties. This memory
  70. cannot be accessed directory, it can only be
  71. parcelled out into memory blocks via ELEMENT_HANDLEs.
  72. Free()
  73. Releases a heap, causing the handle to revert to null
  74. status. Call only on initialized HEAP_HANDLEs.
  75. Release all elements in the heap first.
  76. IsNull()
  77. Determines if a heap handle is null. Call only
  78. on initialized HEAP_HANDLEs.
  79. operator= ()
  80. Copies a HEAP_HANDLE (does NOT copy the
  81. underlying heap). Do not free a handle twice.
  82. PARENT:
  83. USES: ELEMENT_HANDLE
  84. CAVEATS: HEAP_HANDLEs and ELEMENT_HANDLEs do not have
  85. constructors or destructors, and therefore will not
  86. automatically initialize or destruct. They should be
  87. treated with the same care as Windows memory handles, e.g.
  88. - do not use before initializing
  89. - always check whether allocation succeeded
  90. - do not lock before allocating
  91. - always unlock after locking
  92. - leave unlocked when possible to avoid sandbarring memory
  93. - always free eventually
  94. - only free once
  95. Additionally,
  96. - always free the component ELEMENT_HANDLEs before freeing
  97. the HEAP_HANDLE.
  98. - Try to unlock all elements in a HEAP_HANDLE heap between
  99. messages, so that the heap can be unlocked and not become
  100. a sandbar in global memory.
  101. - Zero-length heaps are not allowed.
  102. - HEAP_HANDLE heaps cannot be resized.
  103. NOTES: We seperate out the Init call so that we can use global
  104. HEAP_HANDLE objects without worrying about the
  105. constructor-linker.
  106. HISTORY: JonN 26-Jan-1991 Created
  107. JonN 21-Mar-1991 Code review changes from 2/20/91 (attended
  108. by JonN, RustanL, ?)
  109. **********************************************************************/
  110. #ifdef WINDOWS
  111. #define HEAP_HANDLE_default GMEM_MOVEABLE
  112. #define ELEMENT_HANDLE_default LMEM_MOVEABLE
  113. #else
  114. #define HEAP_HANDLE_default 0
  115. #define ELEMENT_HANDLE_default 0
  116. #endif // WINDOWS
  117. DLL_CLASS HEAP_HANDLE
  118. {
  119. friend ELEMENT_HANDLE;
  120. private:
  121. HANDLE _handle;
  122. public:
  123. VOID Init();
  124. BOOL Init(WORD wBytes, WORD wFlags = HEAP_HANDLE_default);
  125. VOID Free();
  126. BOOL IsNull();
  127. // copies handle, not heap
  128. void operator=( const HEAP_HANDLE& source );
  129. };
  130. /**********************************************************************
  131. NAME: ELEMENT_HANDLE
  132. SYNOPSIS: Handles to memory blocks within a HEAP_HANDLE heap
  133. INTERFACE: Init()
  134. Initializes handle to empty status. Call only on
  135. uninitialized handles.
  136. Init() (BOOL returning, taking args)
  137. Allocates new memory block within a HEAP_HANDLE
  138. heap, by default moveable. Call only on
  139. uninitialized or null ELEMENT_HANDLEs. wBytes
  140. specifies the memory block size, wFlags specifies
  141. the Windows memory properties.
  142. Free()
  143. Releases an memory block, causing the handle to revert
  144. to null status. Call only on initialized handles.
  145. Lock()
  146. Lock a memory block. Returns a far pointer to the
  147. memory block.
  148. Unlock()
  149. Unlock a memory block. Do not use the pointer
  150. returned from Lock() after the memory block is
  151. unlocked.
  152. IsNull()
  153. Determines if a handle is null. Call only
  154. on initialized handles.
  155. operator= ()
  156. Copies an ELEMENT_HANDLE. Do not free a handle twice.
  157. PARENT:
  158. USES:
  159. CAVEATS: Zero-length memory blocks are not allowed (cannot pass
  160. Localinit API).
  161. Memory blocks cannot be resized.
  162. NOTES:
  163. HISTORY: JonN 26-Jan-1991 Created
  164. JonN 21-Mar-1991 Code review changes from 2/20/91 (attended
  165. by JonN, RustanL, ?)
  166. **********************************************************************/
  167. DLL_CLASS ELEMENT_HANDLE
  168. {
  169. private:
  170. //
  171. // This anonymous union is similar to that used in uibuffer.hxx.
  172. //
  173. #ifdef WINDOWS
  174. #define ELEMENT_HANDLE_win _win
  175. #define ELEMENT_HANDLE_os2 _very_obscure_name
  176. #else
  177. #define ELEMENT_HANDLE_win _very_obscure_name
  178. #define ELEMENT_HANDLE_os2 _os2
  179. #endif // WINDOWS
  180. union
  181. {
  182. struct {
  183. HANDLE _globalHandle;
  184. HANDLE _localHandle;
  185. } ELEMENT_HANDLE_win;
  186. struct {
  187. LPSTR _lpstr;
  188. } ELEMENT_HANDLE_os2;
  189. };
  190. public:
  191. VOID Init();
  192. BOOL Init(const HEAP_HANDLE &heap, WORD wBytes, WORD wFlags = ELEMENT_HANDLE_default);
  193. VOID Free();
  194. LPSTR Lock();
  195. VOID Unlock();
  196. BOOL IsNull();
  197. // copies handle, not element
  198. void operator=( const ELEMENT_HANDLE& source );
  199. };
  200. #endif // _LOCLHEAP_HXX_