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.7KB

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