libcm is a C development framework with an emphasis on audio signal processing applications.
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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