HAlloc.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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, bool 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 bool
  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. bool trace_memory);
  73. extern HLibExport bool 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(int max, char* file[], int32_t line[],
  104. INT4_8 size[], int32_t level[],
  105. INT4_8* num);
  106. extern HLibExport int HfprintfAllocInfo(FILE* stream);
  107. /* HPrepMem - prepares memory (allocates+frees memory) => more efficiency */
  108. extern HLibExport Herror HPrepMem(Hproc_handle proc_handle, size_t size,
  109. size_t* si_ze);
  110. /* HFreeUnusedTmpMem - free any unused temporary memory */
  111. extern HLibExport Herror HFreeUnusedTmpMem(Hproc_handle proc_handle);
  112. /*---------------------------------------------------------------------------*/
  113. /* Management of Free Usable Memory */
  114. /*---------------------------------------------------------------------------*/
  115. /* HXAlloc - allocate free usable memory */
  116. extern HLibExport Herror HXAlloc(Hproc_handle proc_handle, size_t size,
  117. void* void_p);
  118. /* HXAllocAlign - allocate free usable memory with given ptr alignment */
  119. extern HLibExport Herror HXAllocAlign(Hproc_handle proc_handle, size_t size,
  120. size_t alignement, void* void_p);
  121. /* HXAllocMemCheck - allocate free usable memory and collect test info */
  122. extern HLibExport Herror HXAllocMemCheck(Hproc_handle proc_handle, size_t size,
  123. const char* file, INT4_8 line,
  124. int32_t level, void* void_p);
  125. /* HXAllocAlignMemCheck - allocate free usable memory with given ptr alignment
  126. and collect test info */
  127. extern HLibExport Herror HXAllocAlignMemCheck(Hproc_handle proc_handle,
  128. size_t size, size_t alignment,
  129. const char* file, INT4_8 line,
  130. int32_t level, void* void_p);
  131. /* HXFree - give free (free usable) memory (insert it into cache) */
  132. extern HLibExport Herror HXFree(Hproc_handle proc_handle, void* mem);
  133. /* HXFree - give free (free usable) memory (insert it into cache) and test it*/
  134. extern HLibExport Herror HXFreeMemCheck(Hproc_handle proc_handle, void* mem,
  135. const char* file, INT4_8 line);
  136. /* HXRealloc - reallocate memory and test it (memory still available?) */
  137. extern HLibExport Herror HXRealloc(Hproc_handle proc_handle, void* ptr,
  138. size_t size, void* void_p, const char* file,
  139. INT4_8 line);
  140. /* HXReallocToGlobal - reallocate global/local memory and convert to global
  141. * memory */
  142. extern HLibExport Herror HXReallocToGlobal(Hproc_handle proc_handle, void* ptr,
  143. size_t size, void* void_p,
  144. const char* file, INT4_8 line);
  145. /* HXAllocRLNum - allocate permanent memory for specific region */
  146. extern HLibExport Herror HXAllocRLNum(Hproc_handle proc_handle,
  147. Hrlregion* H_RESTRICT* H_RESTRICT region,
  148. size_t num, const char* file,
  149. INT4_8 line);
  150. /* HXReallocRLNum - reallocate global memory for specific region */
  151. extern HLibExport Herror HXReallocRLNum(Hproc_handle proc_handle,
  152. Hrlregion* reg_old, size_t num_new,
  153. Hrlregion** reg_new, const char* file,
  154. INT4_8 line);
  155. /* HXReallocRLNumToGlobal - reallocate local or global memory for specific
  156. * region and convert it to global memory. */
  157. extern HLibExport Herror HXReallocRLNumToGlobal(Hproc_handle proc_handle,
  158. Hrlregion* reg_old,
  159. size_t num_new,
  160. Hrlregion** reg_new,
  161. const char* file, INT4_8 line);
  162. /* HXAllocRL - allocate permanent memory for new regions */
  163. extern HLibExport Herror HXAllocRL(Hproc_handle proc_handle,
  164. Hrlregion* H_RESTRICT* H_RESTRICT region,
  165. const char* file, INT4_8 line);
  166. /* HXFreeRL - give free (permanent) memory of a specific region */
  167. extern HLibExport Herror HXFreeRL(Hproc_handle proc_handle, Hrlregion* region,
  168. const char* file, INT4_8 line);
  169. /* HXAllocGeneral - allocate memory according to allocation bit */
  170. extern HLibExport Herror HXAllocGeneral(Hproc_handle ph, int type, size_t size,
  171. void* mem, const char* file,
  172. INT4_8 line);
  173. #define HAllocGeneral(ph, type, size, mem) \
  174. HXAllocGeneral(ph, type, size, (void*)mem, H__FILE__, H__LINE__)
  175. /* HXFreeGeneral - give free (free usable) memory (insert it into cache) */
  176. extern HLibExport Herror HXFreeGeneral(Hproc_handle proc_handle, void* mem);
  177. extern HLibExport Herror HXFreeGeneralMemCheck(Hproc_handle proc_handle,
  178. void* mem, char const* file,
  179. INT4_8 line);
  180. /* HXReallocGeneral - reallocate global/local memory and test it */
  181. extern HLibExport Herror HXReallocGeneral(Hproc_handle proc_handle, void* ptr,
  182. size_t size, void* void_p,
  183. const char* file, INT4_8 line);
  184. /* HXReallocRLNumGeneral - reallocate global/local memory for specific region*/
  185. extern HLibExport Herror HXReallocRLNumGeneral(Hproc_handle proc_handle,
  186. Hrlregion* reg_old,
  187. size_t num_new,
  188. Hrlregion** reg_new,
  189. const char* file, INT4_8 line);
  190. /* HXFreeRLGeneral - give free global/local/temp memory of a region */
  191. extern HLibExport Herror HXFreeRLGeneral(Hproc_handle proc_handle,
  192. Hrlregion* region, const char* file,
  193. INT4_8 line);
  194. /* HAllocImageCached - fetch image memory from cache */
  195. extern HLibExport void* HAllocImageCached(size_t size);
  196. /* HFreeImageCached - cache image memory */
  197. extern HLibExport void HFreeImageCached(void* image);
  198. /*---------------------------------------------------------------------------*/
  199. /* Management of Local Free Memory */
  200. /*---------------------------------------------------------------------------*/
  201. /* HXAllocLocal - allocate local memory (AllocLevel=H_LOCAL_ALLOC) */
  202. extern HLibExport Herror HXAllocLocal(Hproc_handle proc_handle, size_t size,
  203. const char* file, INT4_8 line,
  204. void* void_p);
  205. /* HXAllocLocalGC - allocate local memory with garbage collection without
  206. * warning if not explicitly freed in operator */
  207. extern HLibExport Herror HXAllocLocalGC(Hproc_handle proc_handle, size_t size,
  208. const char* file, INT4_8 line,
  209. void* void_p);
  210. /* HXAllocLocalAlign - allocate local memory with given ptr alignment
  211. (AllocLevel=H_LOCAL_ALLOC) */
  212. extern HLibExport Herror HXAllocLocalAlign(Hproc_handle proc_handle,
  213. size_t size, size_t alignment,
  214. const char* file, INT4_8 line,
  215. void* void_p);
  216. /* HXFreeLocal - give free local memory (insert it in cache) and test it */
  217. extern HLibExport Herror HXFreeLocal(Hproc_handle proc_handle, void* mem,
  218. const char* file, INT4_8 line);
  219. /* HXReallocLocal - reallocate local memory (AllocLevel=H_LOCAL_ALLOC) */
  220. extern HLibExport Herror HXReallocLocal(Hproc_handle proc_handle, void* ptr,
  221. size_t size, void* void_p,
  222. const char* file, INT4_8 line);
  223. /* HConvertLocalToGlobal - converts a local memory block to a global one */
  224. extern HLibExport Herror HConvertLocalToGlobal(Hproc_handle proc_handle,
  225. void* ptr);
  226. /* HXAllocRLNumLocal - allocate local memory for specific region */
  227. extern HLibExport Herror HXAllocRLNumLocal(
  228. Hproc_handle proc_handle, Hrlregion* H_RESTRICT* H_RESTRICT region,
  229. size_t num, const char* file, INT4_8 line);
  230. /* HXReallocRLNumLocal - reallocate local memory for specific region */
  231. extern HLibExport Herror HXReallocRLNumLocal(Hproc_handle proc_handle,
  232. Hrlregion* reg_old,
  233. size_t num_new,
  234. Hrlregion** reg_new,
  235. const char* file, INT4_8 line);
  236. /* HXAllocRLLocal - allocate local memory for new regions */
  237. extern HLibExport Herror HXAllocRLLocal(
  238. Hproc_handle proc_handle, Hrlregion* H_RESTRICT* H_RESTRICT region,
  239. const char* file, INT4_8 line);
  240. /* HXFreeRLLocal - give free (local) memory of a specific region */
  241. extern HLibExport Herror HXFreeRLLocal(Hproc_handle proc_handle,
  242. Hrlregion* region, const char* file,
  243. INT4_8 line);
  244. /* HMemClrLocal - give free the whole local memory (alloc. by HXAllocLocal) */
  245. extern HLibExport Herror HMemClrLocal(Hproc_handle proc_handle);
  246. /* HMemMoveLocal - transfers all local memory from a proc_handle to an other */
  247. extern HLibExport void HMemMoveLocal(Hproc_handle dest, Hproc_handle src);
  248. /*
  249. * HMemMoveLocalMem - transfer local memory object from one procedure handle
  250. * to another.
  251. */
  252. extern HLibExport Herror HMemMoveLocalMem(Hproc_handle dst, Hproc_handle src,
  253. void* mem);
  254. /* HFreeMemTTable - give free all temporary stack memory (of an instance) */
  255. extern HLibExport Herror HFreeMemTTable(Hproc_handle proc_handle);
  256. /* HXFreeAllTmp - give free all the allocated temporary memory */
  257. extern HLibExport Herror HXFreeAllTmp(Hproc_handle proc_handle,
  258. const char* file, INT4_8 line);
  259. /* HMemClrTTable - free (logically) all the allocated stack memory(MemTTable)*/
  260. extern HLibExport Herror HMemClrTTable(Hproc_handle proc_handle);
  261. /* HXAllocTmp - allocate temporary memory within a stack */
  262. extern HLibExport Herror HXAllocTmp(Hproc_handle proc_handle, void* ptr,
  263. size_t size, const char* file,
  264. INT4_8 line);
  265. /* HXAllocTmpAlign - allocate aligned temporary memory within a stack */
  266. extern HLibExport Herror HXAllocTmpAlign(Hproc_handle proc_handle, void* ptr,
  267. size_t size, size_t alignment,
  268. const char* file, INT4_8 line);
  269. /* HXAllocDomainImageTmp - allocate memory for temporary images regarding
  270. * input image width and domain height */
  271. extern HLibExport Herror HXAllocDomainImageTmp(
  272. Hproc_handle proc_handle, void* mem_ptr, void* img_ptr, Hrlregion* region,
  273. HIMGDIM width, HIMGDIM height, int bytes_per_pixel, HIMGDIM border_rows,
  274. HIMGCNT border_bytes, const char* file, INT4_8 line);
  275. /* HXAllocRLNumTmp - allocate temporary memory for specific region */
  276. extern HLibExport Herror HXAllocRLNumTmp(
  277. Hproc_handle proc_handle, Hrlregion* H_RESTRICT* H_RESTRICT region,
  278. size_t num, const char* file, INT4_8 line);
  279. /* HXAllocRLTmp - allocate temporary memory for new regions */
  280. extern HLibExport Herror HXAllocRLTmp(Hproc_handle proc_handle,
  281. Hrlregion* H_RESTRICT* H_RESTRICT region,
  282. const char* file, INT4_8 line);
  283. /* HXTestAllTmp - test all stacks for temporary memory of consistency */
  284. extern HLibExport Herror HXTestAllTmp(Hproc_handle proc_handle,
  285. const char* file, INT4_8 line);
  286. /* HXTestTmp - test the specified temporary memory block of consistency */
  287. extern HLibExport Herror HXTestTmp(void* ptr, const char* file, INT4_8 line,
  288. bool do_printf);
  289. /* HXFreeTmp - gives free the lastly allocated (!) stack memory */
  290. extern HLibExport Herror HXFreeTmp(Hproc_handle proc_handle, void* ptr,
  291. const char* file, INT4_8 line);
  292. /* HXFreeRLTmp - gives free temporary memory of a specific region */
  293. extern HLibExport Herror HXFreeRLTmp(Hproc_handle proc_handle,
  294. Hrlregion* region, const char* file,
  295. INT4_8 line);
  296. /* HXFreeNTmp - gives free the 'num' lastly allocated stack memory blocks */
  297. extern HLibExport Herror HXFreeNTmp(Hproc_handle proc_handle, int num,
  298. const char* file, INT4_8 line);
  299. /* HXFreeUpToTmp - give free stack memory up to a specified memory block,
  300. * including the specified memory block. */
  301. extern HLibExport Herror HXFreeUpToTmp(Hproc_handle proc_handle, void* ptr,
  302. const char* file, INT4_8 line);
  303. /* HXFreeUpToTmpExcl - give free stack memory up to a specified memory block,
  304. * excluding the specified memory block. */
  305. extern HLibExport Herror HXFreeUpToTmpExcl(Hproc_handle proc_handle, void* ptr,
  306. const char* file, INT4_8 line);
  307. /* HXGetLastTmp - get the last allocated memory block on the stack memory */
  308. extern HLibExport Herror HGetLastTmp(Hproc_handle proc_handle, void** last);
  309. /* HFreeUnusedStacks - gives free (physically) all currently unused stacks */
  310. extern HLibExport Herror HFreeUnusedStacks(Hproc_handle proc_handle);
  311. /* HlibAlloc
  312. * HlibFree
  313. * workarround for windows: under windows every DLL has its own memory heap
  314. * -> the free method must be called in the same DLL as the malloc
  315. *---------------------------------------------------------------------------*/
  316. #ifdef _WIN32
  317. extern HLibExport void* Hlib_malloc(size_t size);
  318. extern HLibExport void Hlib_free(void* mem);
  319. #else
  320. # define Hlib_malloc malloc
  321. # define Hlib_free free
  322. #endif
  323. #if defined(__cplusplus)
  324. }
  325. #endif
  326. #endif /* ifndef HALLOC_H */