HAlloc.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. /*****************************************************************************
  2. * HAlloc.h
  3. *****************************************************************************
  4. *
  5. * Project: HALCON/libhalcon
  6. * Description: Memory allocation
  7. *
  8. * (c) 1996-2020 by MVTec Software GmbH
  9. * www.mvtec.com
  10. *
  11. *****************************************************************************
  12. *
  13. *
  14. */
  15. #ifndef HALLOC_H
  16. #define HALLOC_H
  17. #if defined(__cplusplus)
  18. extern "C" {
  19. #endif
  20. /*===========================================================================*/
  21. /* Defines */
  22. /*===========================================================================*/
  23. #define H_LOCAL_ALLOC -111 /* AllocLevel for local. mem. */
  24. #define H_GLOBAL_ALLOC -112 /* use global AllocLevel */
  25. #define H_TMP_ALLOC -113 /* use tmp alloc */
  26. /*****************************************************************************/
  27. /** HMemType: Memory types where containers can live in
  28. *****************************************************************************/
  29. /*
  30. * Note: for historical reasons we also have the HMEMtype enum, which we cannot
  31. * remove without breaking source code and binary compatibility. New code and
  32. * APIs should use the new type.
  33. */
  34. typedef enum
  35. {
  36. eMemTypeNone =
  37. 0, /**< Uninitialized. Must be 0, zeroing memory re-sets to this */
  38. eMemTypeTemp = 1, /**< Temporary Memory (no realloc possible) */
  39. eMemTypeLocal = 2, /**< Local memory */
  40. eMemTypeGlobal = 4 /**< Global memory */
  41. } HMemType;
  42. typedef enum
  43. {
  44. HMEMunknown = -1,
  45. HMEMtemp = 0,
  46. HMEMlocal,
  47. HMEMglobal
  48. } HMEMtype;
  49. /*===========================================================================*/
  50. /* Prototypes of Functions */
  51. /*===========================================================================*/
  52. /*---------------------------------------------------------------------------*/
  53. /* Test Memory Allocation */
  54. /*---------------------------------------------------------------------------*/
  55. /* HXTestPtrRange - tests memory (address) allocated with HAlloc/HAllocLocal */
  56. extern HLibExport Herror HXTestPtrRange(void* ptr, const char* file,
  57. INT4_8 line, HBOOL do_print);
  58. /* HXTestPtr - tests memory (address+size) allocated with HAlloc/HAllocLocal */
  59. extern HLibExport Herror HXTestPtr(void* ptr, const char* file, INT4_8 line);
  60. /* HTestMem - tests all the memory currently allocated by HAlloc/HAllocLocal */
  61. extern HLibExport Herror HTestMem(void);
  62. /*---------------------------------------------------------------------------*/
  63. /* Auxiliary Functions for Memory Management */
  64. /*---------------------------------------------------------------------------*/
  65. extern HLibExport HBOOL
  66. HTraceMemory; /* switch if memory statistics
  67. * should be collected (do not set this directly)*/
  68. /* set HMemoryTrace Flag to TRUE (efficient) / FALSE (debug information
  69. * available)
  70. * NOTE: this function is not thread save and has to be used exclusively */
  71. extern HLibExport Herror HSetTraceMemory(Hproc_handle proc_handle,
  72. HBOOL trace_memory);
  73. extern HLibExport HBOOL HGetTraceMemory(void);
  74. /* HTmpStackSize - return stack size for temporary memory blocks */
  75. extern HLibExport Herror HTmpStackSize(Hproc_handle proc_handle, INT4_8* size);
  76. /* HSetAllocLevel - set 'level' of memory allocation */
  77. extern HLibExport Herror HSetAllocLevel(INT4_8 level);
  78. /* HGetAllocLevel - read 'level' of memory allocation */
  79. extern HLibExport Herror HGetAllocLevel(INT4_8* level);
  80. /* HGetImageCacheSize - current size in bytes of all cached images */
  81. extern HLibExport Herror HGetImageCacheSize(Hproc_handle proc_handle,
  82. INT4_8* size);
  83. /* HGetImageCacheCapacity - maximum size in bytes of all cached images */
  84. extern HLibExport Herror HGetImageCacheCapacity(Hproc_handle proc_handle,
  85. INT4_8* size);
  86. /* HGetImageCacheCapacity - set maximum size in bytes of all cached images
  87. * 0 switches off image caching */
  88. extern HLibExport Herror HSetImageCacheCapacity(Hproc_handle proc_handle,
  89. INT4_8 size);
  90. /* HClearImageCache - free all cached memory blocks */
  91. extern HLibExport Herror HClearImageCache(Hproc_handle proc_handle);
  92. /* HMallocInfo - return the number of calls of clib malloc */
  93. extern HLibExport Herror HMallocInfo(INT4_8* mcalls);
  94. /* HMemInfo - retrieve informationen zaboutc current memory allocation */
  95. extern HLibExport Herror HMemInfo(Hproc_handle proc_handle, INT4_8* alloc_mem,
  96. INT4_8* free_mem, INT4_8* used_mem,
  97. INT4_8* alloc_call, INT4_8* free_call,
  98. double* hit_rate, double* small_rate,
  99. size_t* cache, size_t* stack);
  100. /* HNumMemElements - get number of currently allocated memory blocks */
  101. extern HLibExport Herror HNumMemElements(INT4_8* num);
  102. /* HAllocInfo - get line of call/size of all blocks with level >= AllocLevel */
  103. extern HLibExport Herror HAllocInfo(HINT max, char* file[], INT4 line[],
  104. INT4_8 size[], INT4 level[], INT4_8* num);
  105. extern HLibExport int HfprintfAllocInfo(FILE* stream);
  106. /* HPrepMem - prepares memory (allocates+frees memory) => more efficiency */
  107. extern HLibExport Herror HPrepMem(Hproc_handle proc_handle, size_t size,
  108. size_t* si_ze);
  109. /* HFreeUnusedTmpMem - free any unused temporary memory */
  110. extern HLibExport Herror HFreeUnusedTmpMem(Hproc_handle proc_handle);
  111. /*---------------------------------------------------------------------------*/
  112. /* Management of Free Usable Memory */
  113. /*---------------------------------------------------------------------------*/
  114. /* HXAlloc - allocate free usable memory */
  115. extern HLibExport Herror HXAlloc(Hproc_handle proc_handle, size_t size,
  116. void* void_p);
  117. /* HXAllocAlign - allocate free usable memory with given ptr alignment */
  118. extern HLibExport Herror HXAllocAlign(Hproc_handle proc_handle, size_t size,
  119. size_t alignement, void* void_p);
  120. /* HXAllocMemCheck - allocate free usable memory and collect test info */
  121. extern HLibExport Herror HXAllocMemCheck(Hproc_handle proc_handle, size_t size,
  122. const char* file, INT4_8 line,
  123. INT4 level, void* void_p);
  124. /* HXAllocAlignMemCheck - allocate free usable memory with given ptr alignment
  125. and collect test info */
  126. extern HLibExport Herror HXAllocAlignMemCheck(Hproc_handle proc_handle,
  127. size_t size, size_t alignment,
  128. const char* file, INT4_8 line,
  129. INT4 level, void* void_p);
  130. /* HXFree - give free (free usable) memory (insert it into cache) */
  131. extern HLibExport Herror HXFree(Hproc_handle proc_handle, void* mem);
  132. /* HXFree - give free (free usable) memory (insert it into cache) and test it*/
  133. extern HLibExport Herror HXFreeMemCheck(Hproc_handle proc_handle, void* mem,
  134. const char* file, INT4_8 line);
  135. /* HXRealloc - reallocate memory and test it (memory still available?) */
  136. extern HLibExport Herror HXRealloc(Hproc_handle proc_handle, void* ptr,
  137. size_t size, void* void_p, const char* file,
  138. INT4_8 line);
  139. /* HXReallocToGlobal - reallocate global/local memory and convert to global
  140. * memory */
  141. extern HLibExport Herror HXReallocToGlobal(Hproc_handle proc_handle, void* ptr,
  142. size_t size, void* void_p,
  143. const char* file, INT4_8 line);
  144. /* HXAllocRLNum - allocate permanent memory for specific region */
  145. extern HLibExport Herror HXAllocRLNum(Hproc_handle proc_handle,
  146. Hrlregion* H_RESTRICT* H_RESTRICT region,
  147. size_t num, const char* file,
  148. INT4_8 line);
  149. /* HXReallocRLNum - reallocate global memory for specific region */
  150. extern HLibExport Herror HXReallocRLNum(Hproc_handle proc_handle,
  151. Hrlregion* reg_old, size_t num_new,
  152. Hrlregion** reg_new, const char* file,
  153. INT4_8 line);
  154. /* HXReallocRLNumToGlobal - reallocate local or global memory for specific
  155. * region and convert it to global memory. */
  156. extern HLibExport Herror HXReallocRLNumToGlobal(Hproc_handle proc_handle,
  157. Hrlregion* reg_old,
  158. size_t num_new,
  159. Hrlregion** reg_new,
  160. const char* file, INT4_8 line);
  161. /* HXAllocRL - allocate permanent memory for new regions */
  162. extern HLibExport Herror HXAllocRL(Hproc_handle proc_handle,
  163. Hrlregion* H_RESTRICT* H_RESTRICT region,
  164. const char* file, INT4_8 line);
  165. /* HXFreeRL - give free (permanent) memory of a specific region */
  166. extern HLibExport Herror HXFreeRL(Hproc_handle proc_handle, Hrlregion* region,
  167. const char* file, INT4_8 line);
  168. /* HXAllocGeneral - allocate memory according to allocation bit */
  169. extern HLibExport Herror HXAllocGeneral(Hproc_handle ph, HINT type,
  170. size_t size, void* mem,
  171. const char* file, INT4_8 line);
  172. #define HAllocGeneral(ph, type, size, mem) \
  173. HXAllocGeneral(ph, type, size, (void*)mem, H__FILE__, H__LINE__)
  174. /* HXFreeGeneral - give free (free usable) memory (insert it into cache) */
  175. extern HLibExport Herror HXFreeGeneral(Hproc_handle proc_handle, void* mem);
  176. extern HLibExport Herror HXFreeGeneralMemCheck(Hproc_handle proc_handle,
  177. void* mem, char const* file,
  178. INT4_8 line);
  179. /* HXReallocGeneral - reallocate global/local memory and test it */
  180. extern HLibExport Herror HXReallocGeneral(Hproc_handle proc_handle, void* ptr,
  181. size_t size, void* void_p,
  182. const char* file, INT4_8 line);
  183. /* HXReallocRLNumGeneral - reallocate global/local memory for specific region*/
  184. extern HLibExport Herror HXReallocRLNumGeneral(Hproc_handle proc_handle,
  185. Hrlregion* reg_old,
  186. size_t num_new,
  187. Hrlregion** reg_new,
  188. const char* file, INT4_8 line);
  189. /* HXFreeRLGeneral - give free global/local/temp memory of a region */
  190. extern HLibExport Herror HXFreeRLGeneral(Hproc_handle proc_handle,
  191. Hrlregion* region, const char* file,
  192. INT4_8 line);
  193. /* HAllocImageCached - fetch image memory from cache */
  194. extern HLibExport void* HAllocImageCached(size_t size);
  195. /* HFreeImageCached - cache image memory */
  196. extern HLibExport void HFreeImageCached(void* image);
  197. /*---------------------------------------------------------------------------*/
  198. /* Management of Local Free Memory */
  199. /*---------------------------------------------------------------------------*/
  200. /* HXAllocLocal - allocate local memory (AllocLevel=H_LOCAL_ALLOC) */
  201. extern HLibExport Herror HXAllocLocal(Hproc_handle proc_handle, size_t size,
  202. const char* file, INT4_8 line,
  203. void* void_p);
  204. /* HXAllocLocalGC - allocate local memory with garbage collection without
  205. * warning if not explicitly freed in operator */
  206. extern HLibExport Herror HXAllocLocalGC(Hproc_handle proc_handle, size_t size,
  207. const char* file, INT4_8 line,
  208. void* void_p);
  209. /* HXAllocLocalAlign - allocate local memory with given ptr alignment
  210. (AllocLevel=H_LOCAL_ALLOC) */
  211. extern HLibExport Herror HXAllocLocalAlign(Hproc_handle proc_handle,
  212. size_t size, size_t alignment,
  213. const char* file, INT4_8 line,
  214. void* void_p);
  215. /* HXFreeLocal - give free local memory (insert it in cache) and test it */
  216. extern HLibExport Herror HXFreeLocal(Hproc_handle proc_handle, void* mem,
  217. const char* file, INT4_8 line);
  218. /* HXReallocLocal - reallocate local memory (AllocLevel=H_LOCAL_ALLOC) */
  219. extern HLibExport Herror HXReallocLocal(Hproc_handle proc_handle, void* ptr,
  220. size_t size, void* void_p,
  221. const char* file, INT4_8 line);
  222. /* HConvertLocalToGlobal - converts a local memory block to a global one */
  223. extern HLibExport Herror HConvertLocalToGlobal(Hproc_handle proc_handle,
  224. void* ptr);
  225. /* HXAllocRLNumLocal - allocate local memory for specific region */
  226. extern HLibExport Herror HXAllocRLNumLocal(
  227. Hproc_handle proc_handle, Hrlregion* H_RESTRICT* H_RESTRICT region,
  228. size_t num, const char* file, INT4_8 line);
  229. /* HXReallocRLNumLocal - reallocate local memory for specific region */
  230. extern HLibExport Herror HXReallocRLNumLocal(Hproc_handle proc_handle,
  231. Hrlregion* reg_old,
  232. size_t num_new,
  233. Hrlregion** reg_new,
  234. const char* file, INT4_8 line);
  235. /* HXAllocRLLocal - allocate local memory for new regions */
  236. extern HLibExport Herror HXAllocRLLocal(
  237. Hproc_handle proc_handle, Hrlregion* H_RESTRICT* H_RESTRICT region,
  238. const char* file, INT4_8 line);
  239. /* HXFreeRLLocal - give free (local) memory of a specific region */
  240. extern HLibExport Herror HXFreeRLLocal(Hproc_handle proc_handle,
  241. Hrlregion* region, const char* file,
  242. INT4_8 line);
  243. /* HMemClrLocal - give free the whole local memory (alloc. by HXAllocLocal) */
  244. extern HLibExport Herror HMemClrLocal(Hproc_handle proc_handle);
  245. /* HMemMoveLocal - transfers all local memory from a proc_handle to an other */
  246. extern HLibExport void HMemMoveLocal(Hproc_handle dest, Hproc_handle src);
  247. /*
  248. * HMemMoveLocalMem - transfer local memory object from one procedure handle
  249. * to another.
  250. */
  251. extern HLibExport Herror HMemMoveLocalMem(Hproc_handle dst, Hproc_handle src,
  252. void* mem);
  253. /* HFreeMemTTable - give free all temporary stack memory (of an instance) */
  254. extern HLibExport Herror HFreeMemTTable(Hproc_handle proc_handle);
  255. /* HXFreeAllTmp - give free all the allocated temporary memory */
  256. extern HLibExport Herror HXFreeAllTmp(Hproc_handle proc_handle,
  257. const char* file, INT4_8 line);
  258. /* HMemClrTTable - free (logically) all the allocated stack memory(MemTTable)*/
  259. extern HLibExport Herror HMemClrTTable(Hproc_handle proc_handle);
  260. /* HXAllocTmp - allocate temporary memory within a stack */
  261. extern HLibExport Herror HXAllocTmp(Hproc_handle proc_handle, void* ptr,
  262. size_t size, const char* file,
  263. INT4_8 line);
  264. /* HXAllocTmpAlign - allocate aligned temporary memory within a stack */
  265. extern HLibExport Herror HXAllocTmpAlign(Hproc_handle proc_handle, void* ptr,
  266. size_t size, size_t alignment,
  267. const char* file, INT4_8 line);
  268. /* HXAllocDomainImageTmp - allocate memory for temporary images regarding
  269. * input image width and domain height */
  270. extern HLibExport Herror HXAllocDomainImageTmp(
  271. Hproc_handle proc_handle, void* mem_ptr, void* img_ptr, Hrlregion* region,
  272. HIMGDIM width, HIMGDIM height, HINT byte_per_pixel, HIMGDIM border_rows,
  273. HIMGCNT border_bytes, const char* file, INT4_8 line);
  274. /* HXAllocRLNumTmp - allocate temporary memory for specific region */
  275. extern HLibExport Herror HXAllocRLNumTmp(
  276. Hproc_handle proc_handle, Hrlregion* H_RESTRICT* H_RESTRICT region,
  277. size_t num, const char* file, INT4_8 line);
  278. /* HXAllocRLTmp - allocate temporary memory for new regions */
  279. extern HLibExport Herror HXAllocRLTmp(Hproc_handle proc_handle,
  280. Hrlregion* H_RESTRICT* H_RESTRICT region,
  281. const char* file, INT4_8 line);
  282. /* HXTestAllTmp - test all stacks for temporary memory of consistency */
  283. extern HLibExport Herror HXTestAllTmp(Hproc_handle proc_handle,
  284. const char* file, INT4_8 line);
  285. /* HXTestTmp - test the specified temporary memory block of consistency */
  286. extern HLibExport Herror HXTestTmp(void* ptr, const char* file, INT4_8 line,
  287. HBOOL do_printf);
  288. /* HXFreeTmp - gives free the lastly allocated (!) stack memory */
  289. extern HLibExport Herror HXFreeTmp(Hproc_handle proc_handle, void* ptr,
  290. const char* file, INT4_8 line);
  291. /* HXFreeRLTmp - gives free temporary memory of a specific region */
  292. extern HLibExport Herror HXFreeRLTmp(Hproc_handle proc_handle,
  293. Hrlregion* region, const char* file,
  294. INT4_8 line);
  295. /* HXFreeNTmp - gives free the 'num' lastly allocated stack memory blocks */
  296. extern HLibExport Herror HXFreeNTmp(Hproc_handle proc_handle, HINT num,
  297. const char* file, INT4_8 line);
  298. /* HXFreeUpToTmp - give free stack memory up to a specified memory block,
  299. * including the specified memory block. */
  300. extern HLibExport Herror HXFreeUpToTmp(Hproc_handle proc_handle, void* ptr,
  301. const char* file, INT4_8 line);
  302. /* HXFreeUpToTmpExcl - give free stack memory up to a specified memory block,
  303. * excluding the specified memory block. */
  304. extern HLibExport Herror HXFreeUpToTmpExcl(Hproc_handle proc_handle, void* ptr,
  305. const char* file, INT4_8 line);
  306. /* HXGetLastTmp - get the last allocated memory block on the stack memory */
  307. extern HLibExport Herror HGetLastTmp(Hproc_handle proc_handle, void** last);
  308. /* HFreeUnusedStacks - gives free (physically) all currently unused stacks */
  309. extern HLibExport Herror HFreeUnusedStacks(Hproc_handle proc_handle);
  310. /* HlibAlloc
  311. * HlibFree
  312. * workarround for windows: under windows every DLL has its own memory heap
  313. * -> the free method must be called in the same DLL as the malloc
  314. *---------------------------------------------------------------------------*/
  315. #ifdef _WIN32
  316. extern HLibExport void* Hlib_malloc(size_t size);
  317. extern HLibExport void Hlib_free(void* mem);
  318. #else
  319. # define Hlib_malloc malloc
  320. # define Hlib_free free
  321. #endif
  322. #if defined(__cplusplus)
  323. }
  324. #endif
  325. #endif /* ifndef HALLOC_H */