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.

cmMallocDebug.h 9.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 cmMallocDebug_h
  4. #define cmMallocDebug_h
  5. #ifdef __cplusplus
  6. extern "C" {
  7. #endif
  8. //( { file_desc:"Implements an extended memory allocation and tracking manager." kw:[base] }
  9. //
  10. // cmMallocDebug is a wrapper to cmMem.h for calls to malloc() and free().
  11. // Most of the cmMdXXX() calls are directly associated with same named
  12. // functions in cmMem.h. In effect this library is an implementation of the
  13. // cmMm object where malloc() and free() are the callback functions
  14. // provided to cmMmInitialize().
  15. //
  16. //
  17. // Initialize the malloc debug manager. guardByteCnt, alignByteeCnt, flags, and rpt
  18. // are used to initialize an internal cmMm object. See cmMm for their semantics.
  19. cmMmRC_t cmMdInitialize( unsigned guardByteCnt, unsigned alignByteCnt, unsigned flags, cmRpt_t* rptPtr );
  20. // Finalize the malloc debug manager which was previously iniitalized via
  21. // a call to cmMdInitialize().
  22. cmMmRC_t cmMdFinalize();
  23. // Returns true if the malloc debug manager has been initialized.
  24. bool cmMdIsValid();
  25. unsigned cmMdGuardByteCount(); //< Guard byte count set in cmMdInitialize().
  26. unsigned cmMdAlignByteCount(); //< Byte alignment set in cmMdInitialize().
  27. unsigned cmMdInitializeFlags(); //< cmMdInitialize() configuration flags.
  28. // Allocate a block of memory in release compile mode. This function is generally called
  29. // via one of the cmMemXXX() macros.
  30. void* cmMdAllocate( void *orgDataPtr, unsigned eleCnt, unsigned eleByteCnt, unsigned flags);
  31. // Allocate a block of memory in debug compile mode. This function is generally called
  32. // via one of the cmMmXXX() macros.
  33. void* cmMdAllocateDebug( void* orgDataPtr, unsigned eleCnt, unsigned eleByteCnt, unsigned flags, const char* func, const char* fn, unsigned line);
  34. // Free a block of memory allocated through cmMdAllocate().
  35. void cmMdFree( void* p );
  36. // Free a block of memory allocated through cmMdAllocateDebug().
  37. void cmMdFreeDebug(void* p, const char* func, const char* fn, unsigned line );
  38. // Free a block of memory allocated through cmMdAllocateI() and set the
  39. // pointer variable to NULL. This function combines the act of releasing memory
  40. // and then setting the memory variable to NULL in order to indicate that the
  41. // variable no longer points to a valid data area.
  42. //
  43. // The following two function calls cmMdFree/Debug(p) internally and then
  44. // set *p=NULL. In almost all cases this is what we want to do when freeing
  45. // allocated memory since it will prevent the pointer from being accidently
  46. // reused.
  47. //
  48. // cmMdFreePtr( void* p ) would ideally be written as
  49. // cmMdFreePtr( void** p ) because it is really taking a pointer to a pointer.
  50. // void** however produces compiler warning because while any pointer will be
  51. // automatically cast to void* the same is not true of void** (because void**
  52. // is naturally more restrictive - asking for a pointer to the specific type void).
  53. void cmMdFreePtr( void* p );
  54. void cmMdFreePtrDebug(void* p, const char* func, const char* fn, unsigned line );
  55. // void cmMdFreePtr( void** p )
  56. // void cmMdFreePtrDebug( void** p, ... )
  57. // Allocate a block of memory and then copy a string into it. This function is generally
  58. // called by the release compile mode version of the cmMemXXXStr() macros.
  59. cmChar_t* cmMdAllocStr( void* orgStrPtr, const cmChar_t* str, unsigned strCharCnt, unsigned flags );
  60. // Allocate a block of memory and then copy a string into it. This function is generally
  61. // called by the debug compile mode version of the cmMemXXXStr() macros.
  62. cmChar_t* cmMdAllocStrDebug( void* orgStrPtr, const cmChar_t* str, unsigned strCharCnt, unsigned flags, const char* func, const char* fn, unsigned line );
  63. // Check if the guard bytes associated with a specified memory block are corrupt.
  64. // This call is implemented as a direct call the cmMmIsGuardCorrupt().
  65. cmMmRC_t cmMdIsGuardCorrupt( unsigned id );
  66. // Check if any of the currently allocated blocks contain corrupt guard bytes.
  67. // This call is implemented as a direct call the cmMmCheckAllGuards().
  68. cmMmRC_t cmMdCheckAllGuards();
  69. // Return the unique id associated with a address returned from cmMdAllocateXXX().
  70. unsigned cmMdDebugId( const void* dataPtr);
  71. // Report the status of the memory manager and all blocks.
  72. // This call is implemented as a direct call to cmMmReport().
  73. cmMmRC_t cmMdReport( unsigned mmFlags );
  74. // An example and test function for the cmMallocDebug manager.
  75. void cmMdTest( cmRpt_t* rpt );
  76. #if cmDEBUG_FL == 0
  77. // Memory Allocation and Release Macros:
  78. // Release compile mode memory macros generally used in place of direct calls
  79. // to cmMdAllocate() or cmMdFree().
  80. //
  81. #define cmMemAllocate( type, p, eleCnt, fl ) ((type*)cmMdAllocate( p, eleCnt, sizeof(type), fl ))
  82. #define cmMemMalloc( byteCnt ) cmMdAllocate( NULL, byteCnt, 1, kAlignMmFl)
  83. #define cmMemMallocZ( byteCnt ) cmMdAllocate( NULL, byteCnt, 1, kAlignMmFl | kZeroMmFl)
  84. #define cmMemAlloc( type, eleCnt ) ((type*)cmMdAllocate( NULL, eleCnt, sizeof(type), kAlignMmFl))
  85. #define cmMemAllocZ( type, eleCnt ) ((type*)cmMdAllocate( NULL, eleCnt, sizeof(type), kAlignMmFl | kZeroMmFl))
  86. #define cmMemAllocStr( str ) cmMdAllocStr( NULL, str, cmStringLen(str), kAlignMmFl )
  87. #define cmMemAllocStrN( str, charCnt ) cmMdAllocStr( NULL, str, charCnt, kAlignMmFl )
  88. #define cmMemResizeStr( p, str ) cmMdAllocStr( p, str, cmStringLen(str), kAlignMmFl )
  89. #define cmMemResizeStrN(p, str, charCnt ) cmMdAllocStr( p, str, charCnt, kAlignMmFl )
  90. #define cmMemResizeN( n, p, eleCnt ) (cmMdAllocate( p, eleCnt, n, kAlignMmFl))
  91. #define cmMemResizeNZ( n, p, eleCnt ) (cmMdAllocate( p, eleCnt, n, kAlignMmFl | kZeroMmFl ))
  92. #define cmMemResize( type, p, eleCnt ) ((type*)cmMdAllocate( p, eleCnt, sizeof(type), kAlignMmFl))
  93. #define cmMemResizeZ( type, p, eleCnt ) ((type*)cmMdAllocate( p, eleCnt, sizeof(type), kAlignMmFl | kZeroMmFl))
  94. #define cmMemResizeP( type, p, eleCnt ) ((type*)cmMdAllocate( p, eleCnt, sizeof(type), kAlignMmFl | kPreserveMmFl))
  95. #define cmMemResizePZ( type, p, eleCnt ) ((type*)cmMdAllocate( p, eleCnt, sizeof(type), kAlignMmFl | kZeroMmFl | kPreserveMmFl))
  96. #define cmMemFree( ptr ) cmMdFree( ptr )
  97. #define cmMemPtrFree( ptrPtr ) cmMdFreePtr(ptrPtr);
  98. #define cmIsPadCorrupt( id ) (kOkMmRC)
  99. #else
  100. // Memory Allocation and Release Macros:
  101. // These are debug compile mode memory allocation macros generally used in place of
  102. // direct calls to cmMdAllocateDebug() or cmMdFree().
  103. //
  104. //
  105. #define cmMemAllocate( type, p, eleCnt, pre, fl ) ((type*)cmMdAllocateDebug( p, eleCnt, sizeof(type), fl, __FUNCTION__, __FILE__, __LINE__ ))
  106. #define cmMemMalloc( byteCnt ) cmMdAllocateDebug( NULL, 1, byteCnt, kAlignMmFl, __FUNCTION__, __FILE__, __LINE__ )
  107. #define cmMemMallocZ( byteCnt ) cmMdAllocateDebug( NULL, 1, byteCnt, kAlignMmFl | kZeroMmFl, __FUNCTION__, __FILE__, __LINE__ )
  108. #define cmMemAlloc( type, eleCnt ) ((type*)cmMdAllocateDebug( NULL, eleCnt, sizeof(type), kAlignMmFl, __FUNCTION__, __FILE__, __LINE__ ))
  109. #define cmMemAllocZ( type, eleCnt ) ((type*)cmMdAllocateDebug( NULL, eleCnt, sizeof(type), kAlignMmFl | kZeroMmFl, __FUNCTION__, __FILE__, __LINE__ ))
  110. #define cmMemAllocStr( str ) (cmMdAllocStrDebug( NULL, str, cmStringLen(str), kAlignMmFl, __FUNCTION__, __FILE__, __LINE__ ))
  111. #define cmMemAllocStrN(str, charCnt ) (cmMdAllocStrDebug( NULL, str, charCnt, kAlignMmFl, __FUNCTION__, __FILE__, __LINE__ ))
  112. #define cmMemResizeStr(p, str ) (cmMdAllocStrDebug( p, str, cmStringLen(str), kAlignMmFl, __FUNCTION__, __FILE__, __LINE__ ))
  113. #define cmMemResizeStrN(p, str, charCnt ) (cmMdAllocStrDebug( p, str, charCnt, kAlignMmFl, __FUNCTION__, __FILE__, __LINE__ ))
  114. #define cmMemResizeN( n, p, eleCnt ) (cmMdAllocateDebug( p, eleCnt, n, kAlignMmFl | kZeroMmFl, __FUNCTION__, __FILE__, __LINE__ ))
  115. #define cmMemResizeNZ( n, p, eleCnt ) (cmMdAllocateDebug( p, eleCnt, n, kZeroMmFl, __FUNCTION__, __FILE__, __LINE__ ))
  116. #define cmMemResize( type, p, eleCnt ) ((type*)cmMdAllocateDebug( p, eleCnt, sizeof(type), kAlignMmFl, __FUNCTION__, __FILE__, __LINE__ ))
  117. #define cmMemResizeZ( type, p, eleCnt ) ((type*)cmMdAllocateDebug( p, eleCnt, sizeof(type), kAlignMmFl | kZeroMmFl, __FUNCTION__, __FILE__, __LINE__ ))
  118. #define cmMemResizeP( type, p, eleCnt ) ((type*)cmMdAllocateDebug( p, eleCnt, sizeof(type), kAlignMmFl | kPreserveMmFl, __FUNCTION__, __FILE__, __LINE__ ))
  119. #define cmMemResizePZ( type, p, eleCnt ) ((type*)cmMdAllocateDebug( p, eleCnt, sizeof(type), kAlignMmFl | kZeroMmFl | kPreserveMmFl, __FUNCTION__, __FILE__, __LINE__ ))
  120. #define cmMemFree( ptr ) cmMdFreeDebug( ptr, __FUNCTION__, __FILE__, __LINE__ )
  121. #define cmMemPtrFree( ptrPtr ) cmMdFreePtrDebug( (void**)ptrPtr, __FUNCTION__, __FILE__, __LINE__ )
  122. #define cmIsPadCorrupt( id ) cmMdIsPadCorrupt(id)
  123. // Reports corrupt blocks and returns false if any corrupt blocks are found
  124. #define cmCheckAllPads( file ) cmMdCheckAllPads(file)
  125. #endif
  126. //)
  127. #ifdef __cplusplus
  128. }
  129. #endif
  130. #endif