libcm is a C development framework with an emphasis on audio signal processing applications.
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.

cmLinkedHeap.h 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. //| Copyright: (C) 2009-2020 Kevin Larke <contact AT larke DOT org>
  2. //| License: GNU GPL version 3.0 or above. See the accompanying LICENSE file.
  3. #ifndef cmLinkedHeap_h
  4. #define cmLinkedHeap_h
  5. #ifdef __cplusplus
  6. extern "C" {
  7. #endif
  8. //( { file_desc:"Implements a block based memory heap manager." kw:[base]}
  9. //
  10. // There are two advantages to using this memory manager over the cmMallocDebug
  11. // manager. It alleviates memory fragmentation by pre-allocating large blocks of memory
  12. // which are then used to fullfill many small memory requests. Second it can
  13. // act as a garbage collector by releasing all the memory it is managing at once.
  14. // This can reduce code complexity by eliminating for a class instance to release internally
  15. // allocated objects.
  16. //)
  17. //(
  18. typedef cmHandle_t cmLHeapH_t;
  19. extern cmLHeapH_t cmLHeapNullHandle;
  20. cmLHeapH_t cmLHeapCreate( unsigned dfltBlockByteCnt, cmCtx_t* ctx );
  21. void cmLHeapDestroy( cmLHeapH_t* hp );
  22. void* cmLHeapAllocate( cmLHeapH_t h, void* orgDataPtr, unsigned eleCnt, unsigned eleByteCnt, unsigned flags, const char* fileStr, const char* funcStr, unsigned fileLine );
  23. cmChar_t* cmLHeapAllocStr( cmLHeapH_t h, void* orgDataPtr, const cmChar_t* str, unsigned charCnt, unsigned flags, const char* fileStr, const char* funcStr, unsigned fileLine );
  24. // If ptr==NULL the function returns with no side effect.
  25. void cmLHeapFree( cmLHeapH_t h, void* ptr );
  26. void cmLHeapFreeDebug( cmLHeapH_t h, void* dataPtr, const char* fileName, const char* funcName, unsigned fileLine );
  27. // If *ptr==NULL the function returns with no side effect.
  28. void cmLHeapFreePtr( cmLHeapH_t h, void** ptrPtr );
  29. void cmLHeapFreePtrDebug( cmLHeapH_t h, void** dataPtrPtr, const char* fileName, const char* funcName, unsigned fileLine );
  30. bool cmLHeapIsValid( cmLHeapH_t h );
  31. // Return the values set in call to cmLHeapCreate()
  32. unsigned cmLHeapDefaultBlockByteCount( cmLHeapH_t h );
  33. unsigned cmLHeapGuardByteCount( cmLHeapH_t h );
  34. unsigned cmLHeapAlignByteCount( cmLHeapH_t h );
  35. unsigned cmLHeapInitializeFlags( cmLHeapH_t h );
  36. // If releaseFl==false then marks all internal memory blocks as empty but does not actually
  37. // release the associated memory, otherwise releases all memory blocks.
  38. void cmLHeapClear( cmLHeapH_t h, bool releaseFl );
  39. // Return true if 'ptr' points into a linked heap block.
  40. bool cmLHeapIsPtrInHeap( cmLHeapH_t h, const void* ptr );
  41. // mmFlags take the same values as the flags parameter to cmMmReport().
  42. cmMmRC_t cmLHeapReportErrors( cmLHeapH_t h, unsigned mmFlags );
  43. void cmLHeapReport( cmLHeapH_t h );
  44. void cmLHeapTest();
  45. #if cmDEBUG_FL == 0
  46. #define cmLHeapAlloc( h, byteCnt ) cmLHeapAllocate(h,NULL,1,byteCnt, kAlignMmFl, NULL,NULL,0)
  47. #define cmLHeapAllocZ(h, byteCnt ) cmLHeapAllocate(h,NULL,1,byteCnt, kAlignMmFl | kZeroMmFl, NULL,NULL,0)
  48. #define cmLhAlloc(h,t,n) ((t*)cmLHeapAllocate(h,NULL,n,sizeof(t), kAlignMmFl, NULL,NULL,0))
  49. #define cmLhAllocZ(h,t,n) ((t*)cmLHeapAllocate(h,NULL,n,sizeof(t), kAlignMmFl | kZeroMmFl, NULL,NULL,0))
  50. #define cmLhResizeN( h,t,p,n) ((t*)cmLHeapAllocate(h,p, n,sizeof(t), kAlignMmFl, NULL,NULL,0))
  51. #define cmLhResizeNZ(h,t,p,n) ((t*)cmLHeapAllocate(h,p, n,sizeof(t), kAlignMmFl | kZeroMmFl, NULL,NULL,0))
  52. #define cmLhAllocStr( h, str ) cmLHeapAllocStr( h, NULL, str, cmStringLen(str), kAlignMmFl, NULL,NULL,0 )
  53. #define cmLhAllocStrN( h, str, n ) cmLHeapAllocStr( h, NULL, str, n, kAlignMmFl, NULL,NULL,0 )
  54. #define cmLhResizeStr( h, p, str ) cmLHeapAllocStr( h, p, str, cmStringLen(str), kAlignMmFl, NULL,NULL,0 )
  55. #define cmLhResizeStrN( h, p, str, n ) cmLHeapAllocStr( h, p, str, n, kAlignMmFl, NULL,NULL,0 )
  56. #define cmLhFree( h, p ) cmLHeapFree( h, p )
  57. #define cmLhFreePtr(h, p ) cmLHeapFreePtr( h, p )
  58. #else
  59. #define cmLHeapAlloc( h, byteCnt ) cmLHeapAllocate(h,NULL,1,byteCnt, kAlignMmFl, __FILE__,__FUNCTION__,__LINE__)
  60. #define cmLHeapAllocZ(h, byteCnt ) cmLHeapAllocate(h,NULL,1,byteCnt, kAlignMmFl | kZeroMmFl, __FILE__,__FUNCTION__,__LINE__)
  61. #define cmLhAlloc(h,t,n) ((t*)cmLHeapAllocate(h,NULL,n,sizeof(t), kAlignMmFl, __FILE__,__FUNCTION__,__LINE__))
  62. #define cmLhAllocZ(h,t,n) ((t*)cmLHeapAllocate(h,NULL,n,sizeof(t), kAlignMmFl | kZeroMmFl, __FILE__,__FUNCTION__,__LINE__))
  63. #define cmLhResizeN( h,t,p,n) ((t*)cmLHeapAllocate(h,p, n,sizeof(t), kAlignMmFl, __FILE__,__FUNCTION__,__LINE__))
  64. #define cmLhResizeNZ(h,t,p,n) ((t*)cmLHeapAllocate(h,p, n,sizeof(t), kAlignMmFl | kZeroMmFl, __FILE__,__FUNCTION__,__LINE__))
  65. #define cmLhAllocStr( h, str ) cmLHeapAllocStr( h, NULL, str, cmStringLen(str), kAlignMmFl, __FILE__,__FUNCTION__,__LINE__ )
  66. #define cmLhAllocStrN( h, str, n ) cmLHeapAllocStr( h, NULL, str, n, kAlignMmFl, __FILE__,__FUNCTION__,__LINE__ )
  67. #define cmLhResizeStr( h, p, str ) cmLHeapAllocStr( h, p, str, cmStringLen(str), kAlignMmFl, __FILE__,__FUNCTION__,__LINE__ )
  68. #define cmLhResizeStrN( h, p, str, n ) cmLHeapAllocStr( h, p, str, n, kAlignMmFl, __FILE__,__FUNCTION__,__LINE__ )
  69. #define cmLhFree( h, p ) cmLHeapFreeDebug( h, p, __FILE__,__FUNCTION__,__LINE__ )
  70. #define cmLhFreePtr(h, p ) cmLHeapFreePtrDebug( h, p, __FILE__,__FUNCTION__,__LINE__ )
  71. #endif
  72. //)
  73. #ifdef __cplusplus
  74. }
  75. #endif
  76. #endif