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.

288 lines
11 KiB

  1. //
  2. // Copyright (c) Microsoft Corporation 1993-1995
  3. //
  4. // mem.h
  5. //
  6. // Memory management functions.
  7. //
  8. // History:
  9. // 09-27-94 ScottH Partially taken from commctrl
  10. // 04-29-95 ScottH Taken from briefcase and cleaned up
  11. //
  12. #ifndef _MEM_H_
  13. #define _MEM_H_
  14. //
  15. // Memory routines
  16. //
  17. #ifdef WIN32
  18. //
  19. // These macros are used in our controls, that in 32 bits we simply call
  20. // LocalAlloc as to have the memory associated with the process that created
  21. // it and as such will be cleaned up if the process goes away.
  22. //
  23. LPVOID PUBLIC MemAlloc(HANDLE hheap, DWORD cb);
  24. LPVOID PUBLIC MemReAlloc(HANDLE hheap, LPVOID pb, DWORD cb);
  25. BOOL PUBLIC MemFree(HANDLE hheap, LPVOID pb);
  26. DWORD PUBLIC MemSize(HANDLE hheap, LPVOID pb);
  27. #else // WIN32
  28. // In 16 bit code we need the Allocs to go from our heap code as we do not
  29. // want to limit them to 64K of data. If we have some type of notification of
  30. // 16 bit application termination, We may want to see if we can
  31. // dedicate different heaps for different processes to cleanup...
  32. #define MemAlloc(hheap, cb) Alloc(cb) /* calls to verify heap exists */
  33. #define MemReAlloc(hheap, pb, cb) ReAlloc(pb, cb)
  34. #define MemFree(hheap, pb) Free(pb)
  35. #define MemSize(hheap, pb) GetSize((LPCVOID)pb)
  36. #endif // WIN32
  37. // Mem_Terminate() must be called before the app/dll is terminated.
  38. //
  39. void PUBLIC Mem_Terminate();
  40. //
  41. // Non-shared memory allocation
  42. //
  43. // void * GAlloc(DWORD cbBytes)
  44. //
  45. // Alloc a chunk of memory. Initialize to zero.
  46. //
  47. #define GAlloc(cbBytes) GlobalAlloc(GPTR, cbBytes)
  48. // void * GReAlloc(void * pv, DWORD cbNewSize)
  49. //
  50. // Realloc memory. If pv is NULL, then this function will do
  51. // an alloc for you. Initializes new portion to zero.
  52. //
  53. #define GReAlloc(pv, cbNewSize) GlobalReAlloc(pv, cbNewSize, GMEM_MOVEABLE | GMEM_ZEROINIT)
  54. // void GFree(void *pv)
  55. //
  56. // Free pv if it is nonzero.
  57. //
  58. #define GFree(pv) ((pv) ? GlobalFree(pv) : (void)0)
  59. // DWORD GGetSize(void *pv)
  60. //
  61. // Get the size of a block allocated by GAlloc()
  62. //
  63. #define GGetSize(pv) GlobalSize(pv)
  64. // type * GAllocType(type) (macro)
  65. //
  66. // Alloc some memory the size of <type> and return
  67. // pointer to <type>.
  68. //
  69. #define GAllocType(type) (type *)GAlloc(sizeof(type))
  70. // type * GAllocArray(type, DWORD cNum) (macro)
  71. //
  72. // Alloc an array of data the size of <type>. Returns
  73. // a pointer to <type>.
  74. //
  75. #define GAllocArray(type, cNum) (type *)GAlloc(sizeof(type) * (cNum))
  76. // type * GReAllocArray(type, void * pb, DWORD cNum);
  77. //
  78. // Realloc an array of <type>. Returns a pointer to
  79. // <type>. The returned pointer may differ from the
  80. // given <pb> parameter.
  81. //
  82. #define GReAllocArray(type, pb, cNum) (type *)GReAlloc(pb, sizeof(type) * (cNum))
  83. // (Re)allocates *ppszBuf and copies psz into *ppszBuf. If
  84. // *ppszBuf is NULL, this function allocates memory to hold
  85. // psz. If *ppszBuf is non-NULL, this function reallocates
  86. // memory to hold psz. If psz is NULL, this function frees
  87. // *ppszBuf.
  88. //
  89. // Returns TRUE if successful, FALSE if not.
  90. //
  91. BOOL PUBLIC GSetString(LPSTR * ppszBuf, LPCSTR psz);
  92. // This function is like GSetString except it concatentates
  93. // psz onto *ppszBuf.
  94. //
  95. BOOL PUBLIC GCatString(LPSTR * ppszBuf, LPCSTR psz);
  96. //
  97. // Shared memory allocation functions.
  98. //
  99. #ifndef NOSHAREDHEAP
  100. // PVOID SharedAlloc(DWORD cb);
  101. //
  102. // Alloc a chunk of memory. Initialize to zero.
  103. //
  104. PVOID PUBLIC SharedAlloc(DWORD cb);
  105. // PVOID SharedReAlloc(PVOID pv, DWORD cb);
  106. //
  107. // Realloc memory. If pv is NULL, then this function will do
  108. // an alloc for you. Initializes new portion to zero.
  109. //
  110. PVOID PUBLIC SharedReAlloc(PVOID pv, DWORD cb);
  111. // void SharedFree(PVOID pv);
  112. //
  113. // Free pv if it is nonzero.
  114. //
  115. void PUBLIC _SharedFree(PVOID pv);
  116. #define SharedFree(pv) ((pv) ? _SharedFree(pv) : (void)0)
  117. // DWORD SharedGetSize(PVOID pv);
  118. //
  119. // Get the size of a block allocated by SharedAlloc()
  120. //
  121. DWORD PUBLIC SharedGetSize(PVOID pv);
  122. // type * SharedAllocType(type); (macro)
  123. //
  124. // Alloc some memory the size of <type> and return
  125. // pointer to <type>.
  126. //
  127. #define SharedAllocType(type) (type *)SharedAlloc(sizeof(type))
  128. // type * SharedAllocArray(type, DWORD cNum); (macro)
  129. //
  130. // Alloc an array of data the size of <type>. Returns
  131. // a pointer to <type>.
  132. //
  133. #define SharedAllocArray(type, cNum) (type *)SharedAlloc(sizeof(type) * (cNum))
  134. // type * SharedReAllocArray(type, void * pb, DWORD cNum);
  135. //
  136. // Realloc an array of <type>. Returns a pointer to
  137. // <type>. The returned pointer may differ from the
  138. // given <pb> parameter.
  139. //
  140. #define SharedReAllocArray(type, pb, cNum) (type *)SharedReAlloc(pb, sizeof(type) * (cNum))
  141. // (Re)allocates *ppszBuf and copies psz into *ppszBuf. If
  142. // *ppszBuf is NULL, this function allocates memory to hold
  143. // psz. If *ppszBuf is non-NULL, this function reallocates
  144. // memory to hold psz. If psz is NULL, this function frees
  145. // *ppszBuf.
  146. //
  147. // Returns TRUE if successful, FALSE if not.
  148. //
  149. BOOL PUBLIC SharedSetString(LPSTR * ppszBuf, LPCSTR psz);
  150. #else // NOSHAREDHEAP
  151. #define SharedAlloc(cbBytes) GAlloc(cbBytes)
  152. #define SharedReAlloc(pv, cb) GReAlloc(pv, cb)
  153. #define SharedFree(pv) GFree(pv)
  154. #define SharedGetSize(pv) GGetSize(pv)
  155. #define SharedAllocType(type) (type *)SharedAlloc(sizeof(type))
  156. #define SharedAllocArray(type, cNum) (type *)SharedAlloc(sizeof(type) * (cNum))
  157. #define SharedReAllocArray(type, pb, cNum) (type *)SharedReAlloc(pb, sizeof(type) * (cNum))
  158. #define SharedSetString(ppszBuf, psz) GSetString(ppszBuf, psz)
  159. #endif // NOSHAREDHEAP
  160. #ifndef NODA
  161. //
  162. // Structure Array
  163. //
  164. #define SA_ERR ((DWORD)(-1))
  165. #define SA_APPEND NULL
  166. typedef struct _SA FAR * HSA;
  167. typedef HSA * PHSA;
  168. BOOL PUBLIC SACreateEx(PHSA phsa, DWORD cbItem, DWORD cItemGrow, HANDLE hheap, DWORD dwFlags);
  169. #define SACreate(phsa, cbItem, cItemGrow) SACreateEx(phsa, cbItem, cItemGrow, NULL, SAF_DEFAULT)
  170. // Flags for SACreate
  171. #define SAF_DEFAULT 0x0000
  172. #define SAF_SHARED 0x0001
  173. #define SAF_HEAP 0x0002
  174. typedef void (CALLBACK *PFNSAFREE)(LPVOID pv, LPARAM lParam);
  175. BOOL PUBLIC SADestroyEx(HSA hsa, PFNSAFREE pfnFree, LPARAM lParam);
  176. #define SADestroy(hsa) SADestroyEx(hsa, NULL, 0)
  177. BOOL PUBLIC SAGetItem(HSA hsa, DWORD iItem, LPVOID pitem);
  178. BOOL PUBLIC SAGetItemPtr(HSA hsa, DWORD iItem, LPVOID * ppv);
  179. BOOL PUBLIC SASetItem(HSA hsa, DWORD iItem, LPVOID pitem);
  180. BOOL PUBLIC SAInsertItem(HSA hsa, LPDWORD pindex, LPVOID pitem);
  181. BOOL PUBLIC SADeleteItem(HSA hsa, DWORD iItem);
  182. BOOL PUBLIC SADeleteAllItems(HSA hsa);
  183. #define SAGetCount(hsa) (*(DWORD FAR*)(hsa))
  184. //
  185. // Pointer Array
  186. //
  187. #define PA_ERR ((DWORD)(-1))
  188. #define PA_APPEND NULL
  189. typedef struct _PA FAR * HPA;
  190. typedef HPA * PHPA;
  191. BOOL PUBLIC PACreateEx(PHPA phpa, DWORD cItemGrow, HANDLE hheap, DWORD dwFlags);
  192. #define PACreate(phpa, cItemGrow) (PACreateEx(phpa, cItemGrow, NULL, PAF_DEFAULT))
  193. // Flags for PACreate
  194. #define PAF_DEFAULT 0x0000
  195. #define PAF_SHARED 0x0001
  196. #define PAF_HEAP 0x0002
  197. typedef void (CALLBACK *PFNPAFREE)(LPVOID pv, LPARAM lParam);
  198. BOOL PUBLIC PADestroyEx(HPA hpa, PFNPAFREE pfnFree, LPARAM lParam);
  199. #define PADestroy(hpa) PADestroyEx(hpa, NULL, 0)
  200. BOOL PUBLIC PAClone(PHPA phpa, HPA hpa);
  201. BOOL PUBLIC PAGetPtr(HPA hpa, DWORD i, LPVOID * ppv);
  202. BOOL PUBLIC PAGetPtrIndex(HPA hpa, LPVOID pv, LPDWORD pindex);
  203. BOOL PUBLIC PAGrow(HPA pdpa, DWORD cp);
  204. BOOL PUBLIC PASetPtr(HPA hpa, DWORD i, LPVOID p);
  205. BOOL PUBLIC PAInsertPtr(HPA hpa, LPDWORD pindex, LPVOID pv);
  206. LPVOID PUBLIC PADeletePtr(HPA hpa, DWORD i);
  207. BOOL PUBLIC PADeleteAllPtrsEx(HPA hpa, PFNPAFREE pfnFree, LPARAM lParam);
  208. #define PADeleteAllPtrs(hpa) PADeleteAllPtrsEx(hpa, NULL, 0)
  209. #define PAGetCount(hpa) (*(DWORD FAR*)(hpa))
  210. #define PAGetPtrPtr(hpa) (*((LPVOID FAR* FAR*)((BYTE FAR*)(hpa) + 2*sizeof(DWORD))))
  211. #define PAFastGetPtr(hpa, i) (PAGetPtrPtr(hpa)[i])
  212. typedef int (CALLBACK *PFNPACOMPARE)(LPVOID p1, LPVOID p2, LPARAM lParam);
  213. BOOL PUBLIC PASort(HPA hpa, PFNPACOMPARE pfnCompare, LPARAM lParam);
  214. // Search array. If PAS_SORTED, then array is assumed to be sorted
  215. // according to pfnCompare, and binary search algorithm is used.
  216. // Otherwise, linear search is used.
  217. //
  218. // Searching starts at iStart (0 to start search at beginning).
  219. //
  220. // PAS_INSERTBEFORE/AFTER govern what happens if an exact match is not
  221. // found. If neither are specified, this function returns -1 if no exact
  222. // match is found. Otherwise, the index of the item before or after the
  223. // closest (including exact) match is returned.
  224. //
  225. // Search option flags
  226. //
  227. #define PAS_SORTED 0x0001
  228. #define PAS_INSERTBEFORE 0x0002
  229. #define PAS_INSERTAFTER 0x0004
  230. DWORD PUBLIC PASearch(HPA hpa, LPVOID pFind, DWORD iStart,
  231. PFNPACOMPARE pfnCompare,
  232. LPARAM lParam, UINT options);
  233. #endif // NODA
  234. #endif // _MEM_H_