SMRTHEAP.H 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848
  1. /* smrtheap.h -- SmartHeap (tm) public C header file
  2. * Professional Memory Management Library
  3. *
  4. * Copyright (C) 1991-1999 Compuware Corporation.
  5. * All Rights Reserved.
  6. *
  7. * No part of this source code may be copied, modified or reproduced
  8. * in any form without retaining the above copyright notice.
  9. * This source code, or source code derived from it, may not be redistributed
  10. * without express written permission of the author.
  11. *
  12. */
  13. #if !defined(_SMARTHEAP_H)
  14. #define _SMARTHEAP_H
  15. #include <limits.h>
  16. #include <stddef.h>
  17. #if !defined(macintosh) && !defined(THINK_C) && !defined(__MWERKS__) \
  18. && !defined(SHANSI) && UINT_MAX == 0xFFFFu \
  19. && (defined(_Windows) || defined(_WINDOWS) || defined(__WINDOWS__))
  20. #define MEM_WIN16
  21. #endif
  22. #if (UINT_MAX == 0xFFFFu) && (defined(MEM_WIN16) \
  23. || defined(MSDOS) || defined(__MSDOS__) || defined(__DOS__))
  24. /* 16-bit X86 */
  25. #if defined(SYS_DLL)
  26. #if defined(_MSC_VER) && _MSC_VER <= 600
  27. #define MEM_ENTRY _export _loadds far pascal
  28. #else
  29. #define MEM_ENTRY _export far pascal
  30. #endif
  31. #else
  32. #define MEM_ENTRY far pascal
  33. #endif
  34. #ifdef __WATCOMC__
  35. #define MEM_ENTRY_ANSI __far
  36. #else
  37. #define MEM_ENTRY_ANSI far cdecl
  38. #endif
  39. #define MEM_FAR far
  40. #if defined(MEM_WIN16)
  41. #define MEM_ENTRY2 _export far pascal
  42. #elif defined(DOS16M) || defined(DOSX286)
  43. #define MEM_ENTRY2 _export _loadds far pascal
  44. #endif
  45. #else /* not 16-bit X86 */
  46. #if defined(WIN32) || defined(_WIN32) || defined(__WIN32) \
  47. || defined(__WIN32__) || defined(__NT__)
  48. #define MEM_WIN32
  49. #if defined(_MSC_VER)
  50. #if defined(_SHI_Pool) && defined(SYS_DLL)
  51. #define MEM_ENTRY1 __declspec(dllexport)
  52. #define MEM_ENTRY4 __declspec(dllexport) extern
  53. #elif !defined(_SHI_Pool) && (defined(MEM_DEBUG) || defined(MEM_DLL))
  54. #define MEM_ENTRY1 __declspec(dllimport)
  55. #if defined(_M_IX86) || defined(_X86_)
  56. #define MemDefaultPool shi_MemDefaultPool
  57. #define MEM_ENTRY4 __declspec(dllimport)
  58. #endif
  59. #endif
  60. #endif
  61. #if (defined(_MT) || defined(__MT__)) && !defined(MEM_DEBUG)
  62. /* @@@ #define MEM_MT 1 */
  63. #endif
  64. #if !defined(_MSC_VER) || defined(_M_IX86) || defined(_X86_)
  65. #define MEM_ENTRY __stdcall
  66. #else
  67. #define MEM_ENTRY __cdecl /* for NT/RISC */
  68. #endif
  69. #ifndef __WATCOMC__
  70. #define MEM_ENTRY_ANSI __cdecl
  71. #endif
  72. #elif defined(__OS2__)
  73. #if defined(__BORLANDC__) || defined(__WATCOMC__)
  74. #if defined(SYS_DLL)
  75. #define MEM_ENTRY __export __syscall
  76. #else
  77. #define MEM_ENTRY __syscall
  78. #endif /* SYS_DLL */
  79. #ifdef __BORLANDC__
  80. #define MEM_ENTRY_ANSI __stdcall
  81. #endif
  82. #elif defined(__IBMC__) || defined(__IBMCPP__)
  83. #if defined(SYS_DLL) && 0
  84. #define MEM_ENTRY _Export _System
  85. #else
  86. #define MEM_ENTRY _System
  87. #endif
  88. #define MEM_ENTRY_ANSI _Optlink
  89. #define MEM_ENTRY3 MEM_ENTRY
  90. #define MEM_CALLBACK MEM_ENTRY3
  91. #define MEM_ENTRY2
  92. #endif
  93. #elif defined(__sun) || defined(__hpux) || defined(__osf__) || defined(sgi)
  94. #if defined(_REENTRANT) && !defined(MEM_DEBUG)
  95. /* @@@ #define MEM_MT 1 */
  96. #endif
  97. #elif defined(_AIX)
  98. #if defined(_THREAD_SAFE) && !defined(MEM_DEBUG)
  99. /* #define MEM_MT 1 */
  100. #endif
  101. #endif /* WIN32, OS2, UNIX */
  102. #if defined(__WATCOMC__) && defined(__SW_3S)
  103. /* Watcom stack calling convention */
  104. #ifndef __OS2__
  105. #ifdef __WINDOWS_386__
  106. #pragma aux syscall "*_" parm routine [eax ebx ecx edx fs gs] modify [eax];
  107. #else
  108. #pragma aux syscall "*_" parm routine [eax ebx ecx edx] modify [eax];
  109. #endif
  110. #ifndef MEM_ENTRY
  111. #define MEM_ENTRY __syscall
  112. #endif /* MEM_ENTRY */
  113. #endif
  114. #endif /* Watcom stack calling convention */
  115. #endif /* end of system-specific declarations */
  116. #ifndef MEM_ENTRY
  117. #define MEM_ENTRY
  118. #endif
  119. #ifndef MEM_ENTRY1
  120. #define MEM_ENTRY1
  121. #endif
  122. #ifndef MEM_ENTRY2
  123. #define MEM_ENTRY2 MEM_ENTRY
  124. #endif
  125. #ifndef MEM_ENTRY3
  126. #define MEM_ENTRY3
  127. #endif
  128. #ifndef MEM_ENTRY4
  129. #define MEM_ENTRY4 extern
  130. #endif
  131. #ifndef MEM_CALLBACK
  132. #define MEM_CALLBACK MEM_ENTRY2
  133. #endif
  134. #ifndef MEM_ENTRY_ANSI
  135. #define MEM_ENTRY_ANSI
  136. #endif
  137. #ifndef MEM_FAR
  138. #define MEM_FAR
  139. #endif
  140. #ifdef applec
  141. /* Macintosh: Apple MPW C/C++ passes char/short parms as longs (4 bytes),
  142. * whereas Symantec C/C++ for MPW passes these as words (2 bytes);
  143. * therefore, canonicalize all integer parms as 'int' for this platform.
  144. */
  145. #define MEM_USHORT unsigned
  146. #define MEM_UCHAR unsigned
  147. #else
  148. #define MEM_USHORT unsigned short
  149. #define MEM_UCHAR unsigned char
  150. #endif /* applec */
  151. #ifdef __cplusplus
  152. extern "C" {
  153. #endif
  154. #if !defined(MEM_DEBUG) || !(defined(MEM_WIN16) || defined(MEM_WIN32))
  155. #define SHI_MAJOR_VERSION 5
  156. #define SHI_MINOR_VERSION 0
  157. #define SHI_UPDATE_LEVEL 0
  158. #endif /* !(MEM_WIN16 || MEM_WIN32) */
  159. /*** Types ***/
  160. typedef int MEM_BOOL;
  161. /* Version Masks */
  162. typedef unsigned MEM_VERSION;
  163. #define MEM_MAJOR_VERSION(v) (((v) & 0xF000u) >> 12)
  164. #define MEM_MINOR_VERSION(v) (((v) & 0x0F00u) >> 8)
  165. #define MEM_UPDATE_VERSION(v) ((v) & 0x00FFu)
  166. /* Note: these types are struct's rather than integral types to facilitate
  167. * compile-time type-checking. MEM_POOL and MEM_HANDLE should be regarded
  168. * as black boxes, and treated just like handles.
  169. * You should not have any type casts to or from MEM_POOL or MEM_HANDLE;
  170. * nor should you dereference variables of type MEM_POOL or MEM_HANDLE
  171. * (unless you are using SmartHeap to replace NewHandle on the Mac, and
  172. * you have existing code that dereferences handles).
  173. */
  174. #ifdef _SHI_Pool
  175. typedef struct _SHI_Pool MEM_FAR *MEM_POOL;
  176. typedef struct _SHI_MovHandle MEM_FAR *MEM_HANDLE;
  177. #else
  178. #ifdef THINK_C
  179. typedef void *MEM_POOL;
  180. typedef void **MEM_HANDLE;
  181. #else
  182. typedef struct _SHI_Pool { int reserved; } MEM_FAR *MEM_POOL;
  183. typedef struct _SHI_MovHandle { int reserved; } MEM_FAR *MEM_HANDLE;
  184. #endif
  185. #endif
  186. /* Error codes: errorCode field of MEM_ERROR_INFO */
  187. typedef enum
  188. {
  189. MEM_NO_ERROR=0,
  190. MEM_INTERNAL_ERROR,
  191. MEM_OUT_OF_MEMORY,
  192. MEM_BLOCK_TOO_BIG,
  193. MEM_ALLOC_ZERO,
  194. MEM_RESIZE_FAILED,
  195. MEM_LOCK_ERROR,
  196. MEM_EXCEEDED_CEILING,
  197. MEM_TOO_MANY_PAGES,
  198. MEM_TOO_MANY_TASKS,
  199. MEM_BAD_MEM_POOL,
  200. MEM_BAD_BLOCK,
  201. MEM_BAD_FREE_BLOCK,
  202. MEM_BAD_HANDLE,
  203. MEM_BAD_POINTER,
  204. MEM_WRONG_TASK,
  205. MEM_NOT_FIXED_SIZE,
  206. MEM_BAD_FLAGS,
  207. #ifdef MEM_DEBUG
  208. MEM_BAD_BUFFER,
  209. MEM_DOUBLE_FREE,
  210. MEM_UNDERWRITE,
  211. MEM_OVERWRITE,
  212. MEM_FREE_BLOCK_WRITE,
  213. MEM_READONLY_MODIFIED,
  214. MEM_NOFREE,
  215. MEM_NOREALLOC,
  216. MEM_LEAKAGE,
  217. MEM_FREE_BLOCK_READ,
  218. MEM_UNINITIALIZED_READ,
  219. MEM_UNINITIALIZED_WRITE,
  220. MEM_OUT_OF_BOUNDS_READ,
  221. MEM_UNDERWRITE_STACK,
  222. MEM_OVERWRITE_STACK,
  223. MEM_FREE_STACK_READ,
  224. MEM_UNINITIALIZED_READ_STACK,
  225. MEM_UNINITIALIZED_WRITE_STACK,
  226. MEM_OUT_OF_BOUNDS_READ_STACK,
  227. MEM_LASTOK,
  228. MEM_BREAKPOINT,
  229. MEM_ERROR_CODE_COUNT,
  230. #endif /* MEM_DEBUG */
  231. MEM_ERROR_CODE_INT_MAX = INT_MAX /* to ensure enum is full int in size */
  232. } MEM_ERROR_CODE;
  233. /* HeapAgent Entry-Point API identifiers: errorAPI field of MEM_ERROR_INFO */
  234. typedef enum
  235. {
  236. MEM_NO_API,
  237. MEM_MEMVERSION,
  238. MEM_MEMREGISTERTASK,
  239. MEM_MEMUNREGISTERTASK,
  240. MEM_MEMPOOLINIT,
  241. MEM_MEMPOOLINITFS,
  242. MEM_MEMPOOLFREE,
  243. MEM_MEMPOOLSETPAGESIZE,
  244. MEM_MEMPOOLSETBLOCKSIZEFS,
  245. MEM_MEMPOOLSETFLOOR,
  246. MEM_MEMPOOLSETCEILING,
  247. MEM_MEMPOOLPREALLOCATE,
  248. MEM_MEMPOOLPREALLOCATEHANDLES,
  249. MEM_MEMPOOLSHRINK,
  250. MEM_MEMPOOLSIZE,
  251. MEM_MEMPOOLCOUNT,
  252. MEM_MEMPOOLINFO,
  253. MEM_MEMPOOLFIRST,
  254. MEM_MEMPOOLNEXT,
  255. MEM_MEMPOOLWALK,
  256. MEM_MEMPOOLCHECK,
  257. MEM_MEMALLOC,
  258. MEM_MEMREALLOC,
  259. MEM_MEMFREE,
  260. MEM_MEMLOCK,
  261. MEM_MEMUNLOCK,
  262. MEM_MEMFIX,
  263. MEM_MEMUNFIX,
  264. MEM_MEMLOCKCOUNT,
  265. MEM_MEMISMOVEABLE,
  266. MEM_MEMREFERENCE,
  267. MEM_MEMHANDLE,
  268. MEM_MEMSIZE,
  269. MEM_MEMALLOCPTR,
  270. MEM_MEMREALLOCPTR,
  271. MEM_MEMFREEPTR,
  272. MEM_MEMSIZEPTR,
  273. MEM_MEMCHECKPTR,
  274. MEM_MEMALLOCFS,
  275. MEM_MEMFREEFS,
  276. MEM_MEM_MALLOC,
  277. MEM_MEM_CALLOC,
  278. MEM_MEM_REALLOC,
  279. MEM_MEM_FREE,
  280. MEM_NEW,
  281. MEM_DELETE,
  282. MEM_DBGMEMPOOLSETCHECKFREQUENCY,
  283. MEM_DBGMEMPOOLDEFERFREEING,
  284. MEM_DBGMEMPOOLFREEDEFERRED,
  285. MEM_DBGMEMPROTECTPTR,
  286. MEM_DBGMEMREPORTLEAKAGE,
  287. MEM_MEMPOOLINITNAMEDSHARED,
  288. MEM_MEMPOOLINITNAMEDSHAREDEX,
  289. MEM_MEMPOOLATTACHSHARED,
  290. MEM_DBGMEMPOOLINFO,
  291. MEM_DBGMEMPTRINFO,
  292. MEM_DBGMEMSETTINGSINFO,
  293. MEM_DBGMEMCHECKPTR,
  294. MEM_DBGMEMPOOLSETNAME,
  295. MEM_DBGMEMPOOLSETDEFERQUEUELEN,
  296. MEM_DBGMEMFREEDEFERRED,
  297. MEM_DBGMEMCHECKALL,
  298. MEM_DBGMEMBREAKPOINT,
  299. MEM_MEMPOOLLOCK,
  300. MEM_MEMPOOLUNLOCK,
  301. MEM_MEMPOOLSETSMALLBLOCKSIZE,
  302. MEM_MEMSIZEREQUESTED,
  303. MEM_MSIZE,
  304. MEM_EXPAND,
  305. MEM_GETPROCESSHEAP,
  306. MEM_GETPROCESSHEAPS,
  307. MEM_GLOBALALLOC,
  308. MEM_GLOBALFLAGS,
  309. MEM_GLOBALFREE,
  310. MEM_GLOBALHANDLE,
  311. MEM_GLOBALLOCK,
  312. MEM_GLOBALREALLOC,
  313. MEM_GLOBALSIZE,
  314. MEM_GLOBALUNLOCK,
  315. MEM_HEAPALLOC,
  316. MEM_HEAPCOMPACT,
  317. MEM_HEAPCREATE,
  318. MEM_HEAPDESTROY,
  319. MEM_HEAPFREE,
  320. MEM_HEAPLOCK,
  321. MEM_HEAPREALLOC,
  322. MEM_HEAPSIZE,
  323. MEM_HEAPUNLOCK,
  324. MEM_HEAPVALIDATE,
  325. MEM_HEAPWALK,
  326. MEM_LOCALALLOC,
  327. MEM_LOCALFLAGS,
  328. MEM_LOCALFREE,
  329. MEM_LOCALHANDLE,
  330. MEM_LOCALLOCK,
  331. MEM_LOCALREALLOC,
  332. MEM_LOCALSIZE,
  333. MEM_LOCALUNLOCK,
  334. MEM_MEMPOOLINITREGION,
  335. MEM_TERMINATE,
  336. MEM_HEAPAGENT,
  337. MEM_USER_API,
  338. MEM_API_COUNT,
  339. MEM_API_INT_MAX = INT_MAX /* to ensure enum is full int in size */
  340. } MEM_API;
  341. #define MEM_MAXCALLSTACK 16 /* maximum number of call stack frames recorded */
  342. /* Error info, passed to error-handling callback routine */
  343. typedef struct _MEM_ERROR_INFO
  344. {
  345. MEM_ERROR_CODE errorCode; /* error code identifying type of error */
  346. MEM_POOL pool; /* pool in which error occurred, if known */
  347. /* all fields below this are valid only for debugging lib */
  348. /* the following seven fields identify the call where error detected */
  349. MEM_API errorAPI; /* fn ID of entry-point where error detected */
  350. MEM_POOL argPool; /* memory pool parameter, if applicable */
  351. void MEM_FAR *argPtr; /* memory pointer parameter, if applicable */
  352. void MEM_FAR *argBuf; /* result buffer parameter, if applicable */
  353. MEM_HANDLE argHandle; /* memory handle parameter, if applicable */
  354. unsigned long argSize; /* size parameter, if applicable */
  355. unsigned long argCount; /* count parameter, if applicable */
  356. unsigned argFlags; /* flags parameter, if applicable */
  357. /* the following two fields identify the app source file and line */
  358. const char MEM_FAR *file; /* app source file containing above call */
  359. int line; /* source line in above file */
  360. /* the following two fields identify call instance of error detection */
  361. unsigned long allocCount; /* enumeration of allocation since 1st alloc */
  362. unsigned long passCount; /* enumeration of call at at above file/line */
  363. unsigned checkpoint; /* group with which call has been tagged */
  364. /* the following fields, if non-NULL, points to the address where an
  365. overwrite was detected and another MEM_ERROR_INFO structure
  366. identifying where the corrupted object was first created, if known */
  367. void MEM_FAR *errorAlloc; /* ptr to beginning of alloc related to error */
  368. void MEM_FAR *corruptAddr;
  369. struct _MEM_ERROR_INFO MEM_FAR *objectCreationInfo;
  370. unsigned long threadID; /* ID of thread where error detected */
  371. unsigned long pid; /* ID of process where error detected */
  372. void MEM_FAR *callStack[MEM_MAXCALLSTACK];
  373. } MEM_ERROR_INFO;
  374. /* Error handling callback function */
  375. typedef MEM_BOOL (MEM_ENTRY2 * MEM_ENTRY3 MEM_ERROR_FN)
  376. (MEM_ERROR_INFO MEM_FAR *);
  377. /* Block Type: field of MEM_POOL_ENTRY, field of MEM_POOL_INFO,
  378. * parameter to MemPoolPreAllocate
  379. */
  380. typedef enum
  381. {
  382. MEM_FS_BLOCK = 0x0001u,
  383. MEM_VAR_MOVEABLE_BLOCK = 0x0002u,
  384. MEM_VAR_FIXED_BLOCK = 0x0004u,
  385. MEM_EXTERNAL_BLOCK = 0x0008u,
  386. MEM_BLOCK_TYPE_INT_MAX = INT_MAX /* to ensure enum is full int in size */
  387. } MEM_BLOCK_TYPE;
  388. typedef enum
  389. {
  390. MEM_SMALL_BLOCK_NONE,
  391. MEM_SMALL_BLOCK_SH3,
  392. MEM_SMALL_BLOCK_SH5,
  393. MEM_SMALL_BLOCK_INT_MAX = INT_MAX /* to ensure enum is full int in size */
  394. } MEM_SMALL_BLOCK_ALLOCATOR;
  395. /* Pool Entry: parameter to MemPoolWalk */
  396. typedef struct
  397. {
  398. void MEM_FAR *entry;
  399. MEM_POOL pool;
  400. MEM_BLOCK_TYPE type;
  401. MEM_BOOL isInUse;
  402. unsigned long size;
  403. MEM_HANDLE handle;
  404. unsigned lockCount;
  405. void MEM_FAR *reserved_ptr;
  406. } MEM_POOL_ENTRY;
  407. /* Pool Status: returned by MemPoolWalk, MemPoolFirst, MemPoolNext */
  408. typedef enum
  409. {
  410. MEM_POOL_OK = 1,
  411. MEM_POOL_CORRUPT = -1,
  412. MEM_POOL_CORRUPT_FATAL = -2,
  413. MEM_POOL_END = 0,
  414. MEM_POOL_STATUS_INT_MAX = INT_MAX /* to ensure enum is full int in size */
  415. } MEM_POOL_STATUS;
  416. /* Pointer Status: returned by MemCheckPtr */
  417. typedef enum
  418. {
  419. MEM_POINTER_OK = 1,
  420. MEM_POINTER_WILD = 0,
  421. MEM_POINTER_FREE = -1,
  422. MEM_POINTER_STATUS_INT_MAX = INT_MAX /* to ensure enum is full int */
  423. } MEM_POINTER_STATUS;
  424. /* Pool Info: parameter to MemPoolInfo, MemPoolFirst, MemPoolNext */
  425. typedef struct
  426. {
  427. MEM_POOL pool;
  428. MEM_BLOCK_TYPE type; /* disjunctive combination of block type flags */
  429. unsigned short blockSizeFS;
  430. unsigned short smallBlockSize;
  431. unsigned pageSize;
  432. unsigned long floor;
  433. unsigned long ceiling;
  434. unsigned flags;
  435. MEM_ERROR_FN errorFn;
  436. } MEM_POOL_INFO;
  437. /* Flags passed to MemAlloc, MemAllocPtr, MemReAlloc, MemReAllocPtr */
  438. #define MEM_FIXED 0x0000u /* fixed handle-based block */
  439. #define MEM_ZEROINIT 0x0001u /* == TRUE for SH 1.5 compatibility */
  440. #define MEM_MOVEABLE 0x0002u /* moveable handle-based block */
  441. #define MEM_RESIZEABLE 0x0004u /* reserve space above block */
  442. #define MEM_RESIZE_IN_PLACE 0x0008u /* do not move block (realloc) */
  443. #define MEM_NOGROW 0x0010u /* do not grow heap to satisfy request */
  444. #define MEM_NOEXTERNAL 0x0020u /* reserved for internal use */
  445. #define MEM_NOCOMPACT 0x0040u /* do not compact to satisfy request */
  446. #define MEM_NO_SERIALIZE 0x0080u /* do not serialize this request */
  447. #define MEM_HANDLEBASED 0x4000u /* for internal use */
  448. #define MEM_RESERVED 0x8000u /* for internal use */
  449. #define MEM_UNLOCK_FAILED USHRT_MAX
  450. /* Flags passed to MemPoolInit, MemPoolInitFS */
  451. #define MEM_POOL_SHARED 0x0001u /* == TRUE for SH 1.5 compatibility */
  452. #define MEM_POOL_SERIALIZE 0x0002u /* pool used in more than one thread */
  453. #define MEM_POOL_VIRTUAL_LOCK 0x0004u /* pool is locked in physical memory */
  454. #define MEM_POOL_ZEROINIT 0x0008u /* malloc/new from pool zero-inits */
  455. #define MEM_POOL_REGION 0x0010u /* store pool in user-supplied region*/
  456. #define MEM_POOL_DEFAULT 0x8000u /* pool with default characteristics */
  457. /* Default memory pool for C malloc, C++ new (for backwards compatibility) */
  458. #define MEM_DEFAULT_POOL MemDefaultPool
  459. /* define and initialize these variables at file scope to change defaults */
  460. extern unsigned short MemDefaultPoolBlockSizeFS;
  461. extern unsigned MemDefaultPoolPageSize;
  462. extern unsigned MemDefaultPoolFlags;
  463. /* define SmartHeap_malloc at file scope if you
  464. * are intentionally _NOT_ linking in the SmartHeap malloc definition
  465. * ditto for SmartHeap operator new, and fmalloc et al.
  466. */
  467. extern int SmartHeap_malloc;
  468. extern int SmartHeap_far_malloc;
  469. extern int SmartHeap_new;
  470. #define MEM_ERROR_RET ULONG_MAX
  471. #ifdef __cplusplus
  472. }
  473. #endif /* __cplusplus */
  474. #ifdef MEM_DEBUG
  475. #include "heapagnt.h"
  476. #endif
  477. #endif /* !defined(_SMARTHEAP_H) */
  478. #ifdef __cplusplus
  479. extern "C" {
  480. #endif
  481. #ifdef MEM_MT
  482. #ifdef MemDefaultPool
  483. #undef MemDefaultPool
  484. #endif
  485. #define MemDefaultPool shi_getThreadPool()
  486. #ifndef MemInitDefaultPool
  487. #define MemInitDefaultPool() shi_getThreadPool()
  488. #define MemFreeDefaultPool() shi_freeThreadPools()
  489. #endif
  490. MEM_ENTRY1 MEM_POOL MEM_ENTRY shi_getThreadPool(void);
  491. MEM_ENTRY1 MEM_BOOL MEM_ENTRY shi_freeThreadPools(void);
  492. #else /* MEM_MT */
  493. MEM_ENTRY4 MEM_POOL MemDefaultPool;
  494. MEM_POOL MEM_ENTRY MemInitDefaultPool(void);
  495. MEM_BOOL MEM_ENTRY MemFreeDefaultPool(void);
  496. #endif /* MEM_MT */
  497. #ifdef __cplusplus
  498. }
  499. #endif /* __cplusplus */
  500. /*** Function Prototypes ***/
  501. #ifndef _SMARTHEAP_PROT
  502. #define _SMARTHEAP_PROT
  503. #ifdef __cplusplus
  504. extern "C" {
  505. #endif
  506. #ifndef _shAPI
  507. #if defined(MEM_DEBUG) && !defined(SHI_NO_MEM_DEBUG)
  508. #define _shAPI(ret, name) MEM_ENTRY1 ret MEM_ENTRY _dbg ## name
  509. #else
  510. #define _shAPI(ret, name) MEM_ENTRY1 ret MEM_ENTRY name
  511. #endif
  512. #endif
  513. #ifndef _dbgARGS
  514. #if defined(MEM_DEBUG) && !defined(SHI_NO_MEM_DEBUG)
  515. #define _dbgARGS1 const char MEM_FAR *, int
  516. #define _dbgARGS , _dbgARGS1
  517. #else
  518. #define _dbgARGS1 void
  519. #define _dbgARGS
  520. #endif
  521. #endif
  522. /**** HOW TO READ SmartHeap PROTOTYPES ****
  523. * prototypes below have the follow syntax in order to support both debug
  524. * and non-debug APIs with single-source:
  525. *
  526. * _shiAPI(<return-type>, <API name>)([<parms>] _dbgARGS);
  527. *
  528. * the above translates to a C prototype as follows:
  529. *
  530. * <return-type> <API name>([<parms>]);
  531. */
  532. /* Library Version */
  533. MEM_ENTRY1 MEM_VERSION MEM_ENTRY MemVersion(void);
  534. /* Library Registration */
  535. _shAPI(MEM_BOOL, MemRegisterTask)(_dbgARGS1);
  536. _shAPI(MEM_BOOL, MemUnregisterTask)(_dbgARGS1);
  537. /* Process heap usage */
  538. MEM_ENTRY1 void MEM_ENTRY MemProcessSetGrowIncrement(unsigned long);
  539. /* Memory Pool Functions */
  540. _shAPI(MEM_POOL, MemPoolInit)(unsigned _dbgARGS);
  541. _shAPI(MEM_POOL, MemPoolInitFS)(MEM_USHORT, unsigned long,
  542. unsigned _dbgARGS);
  543. _shAPI(MEM_POOL, MemPoolInitRegion)(void MEM_FAR *,
  544. unsigned long size, unsigned _dbgARGS);
  545. _shAPI(MEM_POOL, MemPoolInitRegionEx)(void MEM_FAR *addr,
  546. unsigned long size, unsigned flags, void MEM_FAR *security _dbgARGS);
  547. _shAPI(MEM_POOL, MemPoolInitNamedShared)(const char MEM_FAR *,
  548. unsigned long size,unsigned _dbgARGS);
  549. _shAPI(MEM_POOL, MemPoolInitNamedSharedEx)(void MEM_FAR *addr,
  550. unsigned pidCount, unsigned long MEM_FAR *pids, void MEM_FAR *security,
  551. const char MEM_FAR *name, unsigned long size, unsigned flags _dbgARGS);
  552. _shAPI(MEM_POOL, MemPoolAttachShared)(MEM_POOL, const char MEM_FAR * _dbgARGS);
  553. _shAPI(MEM_BOOL, MemPoolFree)(MEM_POOL _dbgARGS);
  554. _shAPI(unsigned, MemPoolSetPageSize)(MEM_POOL, unsigned _dbgARGS);
  555. _shAPI(MEM_BOOL, MemPoolSetBlockSizeFS)(MEM_POOL, MEM_USHORT _dbgARGS);
  556. MEM_ENTRY1 unsigned long MEM_ENTRY MemPoolSetGrowIncrement(MEM_POOL,
  557. unsigned long);
  558. MEM_ENTRY1 MEM_BOOL MEM_ENTRY MemPoolSetSmallBlockAllocator(MEM_POOL, MEM_SMALL_BLOCK_ALLOCATOR);
  559. _shAPI(MEM_BOOL, MemPoolSetSmallBlockSize)(MEM_POOL, MEM_USHORT _dbgARGS);
  560. _shAPI(unsigned long, MemPoolSetFloor)(MEM_POOL, unsigned long _dbgARGS);
  561. _shAPI(unsigned long, MemPoolSetCeiling)(MEM_POOL, unsigned long _dbgARGS);
  562. _shAPI(unsigned long, MemPoolPreAllocate)(MEM_POOL, unsigned long,
  563. MEM_BLOCK_TYPE _dbgARGS);
  564. _shAPI(unsigned long, MemPoolPreAllocateHandles)(MEM_POOL,
  565. unsigned long _dbgARGS);
  566. _shAPI(unsigned long, MemPoolShrink)(MEM_POOL _dbgARGS);
  567. _shAPI(unsigned long, MemPoolSize)(MEM_POOL _dbgARGS);
  568. _shAPI(unsigned long, MemPoolCount)(MEM_POOL _dbgARGS);
  569. _shAPI(MEM_BOOL, MemPoolInfo)(MEM_POOL, void MEM_FAR *,
  570. MEM_POOL_INFO MEM_FAR* _dbgARGS);
  571. _shAPI(MEM_POOL_STATUS, MemPoolFirst)(MEM_POOL_INFO MEM_FAR *,
  572. MEM_BOOL _dbgARGS);
  573. _shAPI(MEM_POOL_STATUS,MemPoolNext)(MEM_POOL_INFO MEM_FAR*,MEM_BOOL _dbgARGS);
  574. _shAPI(MEM_POOL_STATUS,MemPoolWalk)(MEM_POOL,MEM_POOL_ENTRY MEM_FAR*_dbgARGS);
  575. _shAPI(MEM_BOOL, MemPoolCheck)(MEM_POOL _dbgARGS);
  576. _shAPI(MEM_BOOL, MemPoolLock)(MEM_POOL _dbgARGS);
  577. _shAPI(MEM_BOOL, MemPoolUnlock)(MEM_POOL _dbgARGS);
  578. /* Handle-based API for moveable memory within heap. */
  579. _shAPI(MEM_HANDLE, MemAlloc)(MEM_POOL, unsigned, unsigned long _dbgARGS);
  580. _shAPI(MEM_HANDLE, MemReAlloc)(MEM_HANDLE,unsigned long,unsigned _dbgARGS);
  581. _shAPI(MEM_BOOL, MemFree)(MEM_HANDLE _dbgARGS);
  582. _shAPI(void MEM_FAR *, MemLock)(MEM_HANDLE _dbgARGS);
  583. _shAPI(unsigned, MemUnlock)(MEM_HANDLE _dbgARGS);
  584. _shAPI(void MEM_FAR *, MemFix)(MEM_HANDLE _dbgARGS);
  585. _shAPI(unsigned, MemUnfix)(MEM_HANDLE _dbgARGS);
  586. _shAPI(unsigned, MemLockCount)(MEM_HANDLE _dbgARGS);
  587. #ifndef MemFlags
  588. #define MemFlags(mem) MemLockCount(mem)
  589. #endif
  590. _shAPI(MEM_BOOL, MemIsMoveable)(MEM_HANDLE _dbgARGS);
  591. _shAPI(unsigned long, MemSize)(MEM_HANDLE _dbgARGS);
  592. _shAPI(unsigned long, MemSizeRequested)(MEM_HANDLE _dbgARGS);
  593. _shAPI(MEM_HANDLE, MemHandle)(void MEM_FAR * _dbgARGS);
  594. #ifndef MEM_REFERENCE
  595. #ifdef MEM_DEBUG
  596. MEM_ENTRY1 void MEM_FAR * MEM_ENTRY _dbgMemReference(MEM_HANDLE,
  597. const char MEM_FAR *, int);
  598. #define MEM_REFERENCE(handle) \
  599. _dbgMemReference(handle, __FILE__, __LINE__)
  600. #else
  601. #define MEM_REFERENCE(handle) (*(void MEM_FAR * MEM_FAR *)handle)
  602. #endif
  603. #endif
  604. /* General Heap Allocator (returns direct pointer to memory) */
  605. _shAPI(void MEM_FAR*,MemAllocPtr)(MEM_POOL,unsigned long,unsigned _dbgARGS);
  606. _shAPI(void MEM_FAR *, MemReAllocPtr)(void MEM_FAR *, unsigned long,
  607. unsigned _dbgARGS);
  608. _shAPI(MEM_BOOL, MemFreePtr)(void MEM_FAR * _dbgARGS);
  609. _shAPI(unsigned long, MemSizePtr)(void MEM_FAR * _dbgARGS);
  610. _shAPI(MEM_POINTER_STATUS, MemCheckPtr)(MEM_POOL, void MEM_FAR * _dbgARGS);
  611. /* Fixed-Size Allocator */
  612. _shAPI(void MEM_FAR *, MemAllocFS)(MEM_POOL _dbgARGS);
  613. _shAPI(MEM_BOOL, MemFreeFS)(void MEM_FAR * _dbgARGS);
  614. /* Error Handling Functions */
  615. MEM_ENTRY1 MEM_ERROR_FN MEM_ENTRY MemSetErrorHandler(MEM_ERROR_FN);
  616. MEM_ENTRY1 MEM_BOOL MEM_ENTRY MemDefaultErrorHandler(MEM_ERROR_INFO MEM_FAR*);
  617. MEM_ENTRY1 void MEM_ENTRY MemErrorUnwind(void);
  618. #ifdef MEM_WIN32
  619. /* patching control */
  620. #ifndef MEM_PATCHING_DEFINED
  621. #define MEM_PATCHING_DEFINED
  622. typedef enum
  623. {
  624. MEM_PATCH_ALL = 0,
  625. MEM_SKIP_PATCHING_THIS_DLL = 1,
  626. MEM_DISABLE_SYSTEM_HEAP_PATCHING = 2,
  627. MEM_DISABLE_ALL_PATCHING = 4|2|1,
  628. MEM_PATCHING_INT_MAX = INT_MAX /* to ensure enum is full int in size */
  629. } MEM_PATCHING;
  630. #endif /* MEM_PATCHING_DEFINED */
  631. #ifdef _MSC_VER
  632. __declspec(dllexport)
  633. #endif
  634. MEM_PATCHING MEM_ENTRY MemSetPatching(const char ***skipDLLs);
  635. #endif /* MEM_WIN32 */
  636. /* Internal routines */
  637. MEM_ENTRY1 MEM_BOOL MEM_ENTRY _shi_enterCriticalSection(void);
  638. MEM_ENTRY1 void MEM_ENTRY _shi_leaveCriticalSection(void);
  639. MEM_BOOL shi_call_new_handler_msc(size_t, MEM_BOOL);
  640. /* Wrapper macros for debugging API */
  641. #ifndef _SHI_dbgMacros
  642. #ifdef MEM_DEBUG
  643. #define MemRegisterTask() _dbgMemRegisterTask(__FILE__, __LINE__)
  644. #define MemUnregisterTask() _dbgMemUnregisterTask(__FILE__, __LINE__)
  645. #define MemPoolInit(flags) _dbgMemPoolInit(flags, __FILE__, __LINE__)
  646. #define MemPoolInitFS(bs, bc, f) _dbgMemPoolInitFS(bs,bc,f,__FILE__,__LINE__)
  647. #define MemPoolInitRegion(addr, sz, f) \
  648. _dbgMemPoolInitRegion(addr, sz, f, __FILE__, __LINE__)
  649. #define MemPoolInitRegionEx(addr, sz, f) \
  650. _dbgMemPoolInitRegionEx(addr, sz, f, s, __FILE__, __LINE__)
  651. #define MemPoolInitNamedShared(nm, sz, f) \
  652. _dbgMemPoolInitNamedShared(nm, sz, f, __FILE__, __LINE__)
  653. #define MemPoolInitNamedSharedEx(a, c, p, sec, nm, sz, f) \
  654. _dbgMemPoolInitNamedSharedEx(a, c, p, sec, nm, sz, f, __FILE__, __LINE__)
  655. #define MemPoolAttachShared(p, n) \
  656. _dbgMemPoolAttachShared(p, n, __FILE__, __LINE__)
  657. #define MemPoolFree(pool) _dbgMemPoolFree(pool, __FILE__, __LINE__)
  658. #define MemPoolSetPageSize(p, s) _dbgMemPoolSetPageSize(p,s,__FILE__,__LINE__)
  659. #define MemPoolSetBlockSizeFS(p, s) \
  660. _dbgMemPoolSetBlockSizeFS(p, s, __FILE__, __LINE__)
  661. #define MemPoolSetSmallBlockSize(p, s) \
  662. _dbgMemPoolSetSmallBlockSize(p, s, __FILE__, __LINE__)
  663. #define MemPoolSetFloor(p, f) _dbgMemPoolSetFloor(p, f, __FILE__, __LINE__)
  664. #define MemPoolSetCeiling(p, c) _dbgMemPoolSetCeiling(p,c,__FILE__, __LINE__)
  665. #define MemPoolPreAllocate(p,s,t) \
  666. _dbgMemPoolPreAllocate(p,s,t,__FILE__, __LINE__)
  667. #define MemPoolPreAllocateHandles(p,h) \
  668. _dbgMemPoolPreAllocateHandles(p,h,__FILE__, __LINE__)
  669. #define MemPoolShrink(p) _dbgMemPoolShrink(p, __FILE__, __LINE__)
  670. #define MemPoolCheck(p) _dbgMemPoolCheck(p, __FILE__, __LINE__)
  671. #define MemPoolWalk(p, e) _dbgMemPoolWalk(p, e, __FILE__, __LINE__)
  672. #define MemPoolSize(p) _dbgMemPoolSize(p, __FILE__, __LINE__)
  673. #define MemPoolCount(p) _dbgMemPoolCount(p, __FILE__, __LINE__)
  674. #define MemPoolInfo(p,x,i) _dbgMemPoolInfo(p,x,i, __FILE__, __LINE__)
  675. #define MemPoolFirst(i, b) _dbgMemPoolFirst(i, b, __FILE__, __LINE__)
  676. #define MemPoolNext(i, b) _dbgMemPoolNext(i, b, __FILE__, __LINE__)
  677. #define MemPoolLock(p) _dbgMemPoolLock(p, __FILE__, __LINE__)
  678. #define MemPoolUnlock(p) _dbgMemPoolUnlock(p, __FILE__, __LINE__)
  679. #define MemAlloc(p, f, s) _dbgMemAlloc(p, f, s, __FILE__, __LINE__)
  680. #define MemReAlloc(h, s, f) _dbgMemReAlloc(h, s, f, __FILE__, __LINE__)
  681. #define MemFree(h) _dbgMemFree(h, __FILE__, __LINE__)
  682. #define MemLock(h) _dbgMemLock(h, __FILE__, __LINE__)
  683. #define MemUnlock(h) _dbgMemUnlock(h, __FILE__, __LINE__)
  684. #define MemFix(h) _dbgMemFix(h, __FILE__, __LINE__)
  685. #define MemUnfix(h) _dbgMemUnfix(h, __FILE__, __LINE__)
  686. #define MemSize(h) _dbgMemSize(h, __FILE__, __LINE__)
  687. #define MemSizeRequested(h) _dbgMemSizeRequested(h, __FILE__, __LINE__)
  688. #define MemLockCount(h) _dbgMemLockCount(h, __FILE__, __LINE__)
  689. #define MemIsMoveable(h) _dbgMemIsMoveable(h, __FILE__, __LINE__)
  690. #define MemHandle(p) _dbgMemHandle(p, __FILE__, __LINE__)
  691. #define MemAllocPtr(p, s, f) _dbgMemAllocPtr(p, s, f, __FILE__, __LINE__)
  692. #define MemReAllocPtr(p, s, f) _dbgMemReAllocPtr(p, s, f, __FILE__,__LINE__)
  693. #define MemFreePtr(p) _dbgMemFreePtr(p, __FILE__, __LINE__)
  694. #define MemSizePtr(p) _dbgMemSizePtr(p, __FILE__, __LINE__)
  695. #define MemCheckPtr(p, x) _dbgMemCheckPtr(p, x, __FILE__, __LINE__)
  696. #define MemAllocFS(p) _dbgMemAllocFS(p, __FILE__, __LINE__)
  697. #define MemFreeFS(p) _dbgMemFreeFS(p, __FILE__, __LINE__)
  698. #else /* MEM_DEBUG */
  699. /* MEM_DEBUG not defined: define dbgMemXXX as no-op macros
  700. * each macro returns "success" value when MEM_DEBUG not defined
  701. */
  702. #ifndef dbgMemBreakpoint
  703. #define dbgMemBreakpoint() ((void)0)
  704. #define dbgMemCheckAll() 1
  705. #define dbgMemCheckPtr(p, f, s) 1
  706. #define dbgMemDeferFreeing(b) 1
  707. #define dbgMemFormatCall(i, b, s) 0
  708. #define dbgMemFormatErrorInfo(i, b, s) 0
  709. #define dbgMemPoolDeferFreeing(p, b) 1
  710. #define dbgMemFreeDeferred() 1
  711. #define dbgMemPoolFreeDeferred(p) 1
  712. #define dbgMemPoolInfo(p, b) 1
  713. #define dbgMemPoolSetCheckFrequency(p, f) 1
  714. #define dbgMemPoolSetDeferQueueLen(p, b) 1
  715. #define dbgMemPoolSetName(p, n) 1
  716. #define dbgMemProtectPtr(p, f) 1
  717. #define dbgMemPtrInfo(p, b) 1
  718. #define dbgMemReallocMoves(b) 1
  719. #define dbgMemReportLeakage(p, c1, c2) 1
  720. #define dbgMemReportWrongTaskRef(b) 1
  721. #define dbgMemScheduleChecking(b, p, i) 1
  722. #define dbgMemSetCheckFrequency(f) 1
  723. #define dbgMemSetCheckpoint(c) 1
  724. #define dbgMemSetDefaultErrorOutput(x, f) 1
  725. #define dbgMemSetDeferQueueLen(l) 1
  726. #define dbgMemSetDeferSizeThreshold(s) 1
  727. #define dbgMemSetEntryHandler(f) 0
  728. #define dbgMemSetExitHandler(f) 0
  729. #define dbgMemSetFreeFill(c) 1
  730. #define dbgMemSetGuardFill(c) 1
  731. #define dbgMemSetGuardSize(s) 1
  732. #define dbgMemSetInUseFill(c) 1
  733. #define dbgMemSetCallstackChains(s) 1
  734. #define dbgMemSetStackChecking(s) 1
  735. #define dbgMemSetSafetyLevel(s) 1
  736. #define dbgMemSettingsInfo(b) 1
  737. #define dbgMemSuppressFreeFill(b) 1
  738. #define dbgMemTotalCount() 1
  739. #define dbgMemTotalSize() 1
  740. #define dbgMemWalkHeap(b) MEM_POOL_OK
  741. #endif /* dbgMemBreakpoint */
  742. #endif /* MEM_DEBUG */
  743. #endif /* _SHI_dbgMacros */
  744. #if defined(__WATCOMC__) && defined(__SW_3S)
  745. /* Watcom stack calling convention */
  746. #pragma aux MemDefaultPool "_*";
  747. #pragma aux MemDefaultPoolBlockSizeFS "_*";
  748. #pragma aux MemDefaultPoolPageSize "_*";
  749. #pragma aux MemDefaultPoolFlags "_*";
  750. #pragma aux SmartHeap_malloc "_*";
  751. #pragma aux SmartHeap_far_malloc "_*";
  752. #pragma aux SmartHeap_new "_*";
  753. #ifdef MEM_DEBUG
  754. #pragma aux dbgMemGuardSize "_*";
  755. #pragma aux dbgMemGuardFill "_*";
  756. #pragma aux dbgMemFreeFill "_*";
  757. #pragma aux dbgMemInUseFill "_*";
  758. #endif
  759. #endif
  760. #ifdef __cplusplus
  761. }
  762. #endif /* __cplusplus */
  763. #endif /* !defined(_SMARTHEAP_PROT) */