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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #include "cmPrefix.h"
  2. #include "cmGlobal.h"
  3. #include "cmRpt.h"
  4. #include "cmMem.h"
  5. #include "cmMallocDebug.h"
  6. cmMmH_t _cmMdH = cmSTATIC_NULL_HANDLE;
  7. void* _cmMdAlloc(void* funcArgPtr, unsigned byteCnt)
  8. { return malloc(byteCnt); }
  9. bool _cmMdFree(void* funcArgPtr, void* ptr)
  10. { free(ptr); return true; }
  11. cmMmRC_t cmMdInitialize( unsigned guardByteCnt, unsigned alignByteCnt, unsigned flags, cmRpt_t* rptPtr )
  12. { return cmMmInitialize(&_cmMdH,_cmMdAlloc,_cmMdFree,NULL,guardByteCnt,alignByteCnt,flags,rptPtr); }
  13. cmMmRC_t cmMdFinalize()
  14. { return cmMmFinalize(&_cmMdH); }
  15. bool cmMdIsValid()
  16. { return _cmMdH.h != NULL; }
  17. unsigned cmMdGuardByteCount() { return cmMmGuardByteCount( _cmMdH); }
  18. unsigned cmMdAlignByteCount() { return cmMmAlignByteCount( _cmMdH); }
  19. unsigned cmMdInitializeFlags(){ return cmMmInitializeFlags(_cmMdH); }
  20. void* cmMdAllocate( void *orgDataPtr, unsigned eleCnt, unsigned eleByteCnt, unsigned flags)
  21. { return cmMmAllocate(_cmMdH,orgDataPtr,eleCnt,eleByteCnt,flags,NULL,NULL,0); }
  22. void* cmMdAllocateDebug( void* orgDataPtr, unsigned eleCnt, unsigned eleByteCnt, unsigned flags, const char* func, const char* fn, unsigned line)
  23. { return cmMmAllocate(_cmMdH,orgDataPtr,eleCnt,eleByteCnt,flags,fn,func,line); }
  24. void cmMdFree( void* p )
  25. { cmMmFree(_cmMdH,p); }
  26. void cmMdFreeDebug(void* p, const char* func, const char* fn, unsigned line )
  27. { cmMmFreeDebug(_cmMdH,p,fn,func,line); }
  28. void cmMdFreePtr( void* p )
  29. { cmMmFreePtr(_cmMdH,p); }
  30. void cmMdFreePtrDebug(void* p, const char* func, const char* fn, unsigned line )
  31. { cmMmFreePtrDebug(_cmMdH,p,fn,func,line); }
  32. cmChar_t* cmMdAllocStr( void* orgStrPtr, const cmChar_t* str, unsigned n, unsigned flags )
  33. {
  34. if( str==NULL)
  35. return NULL;
  36. //unsigned n = strlen(str)+1;
  37. n += 1;
  38. cmChar_t* cp = cmMdAllocate(orgStrPtr,n,sizeof(cmChar_t),flags);
  39. strncpy(cp,str,n);
  40. return cp;
  41. }
  42. cmChar_t* cmMdAllocStrDebug( void* orgStrPtr, const cmChar_t* str, unsigned n, unsigned flags, const char* func, const char* fn, unsigned line )
  43. {
  44. if( str==NULL)
  45. return NULL;
  46. n += 1;
  47. cmChar_t* cp = cmMdAllocateDebug((void*)orgStrPtr,n,sizeof(cmChar_t),flags,func,fn,line);
  48. strncpy(cp,str,n);
  49. cp[n-1] = 0;
  50. return cp;
  51. }
  52. cmMmRC_t cmMdIsGuardCorrupt( unsigned id )
  53. { return cmMmIsGuardCorrupt(_cmMdH,id); }
  54. cmMmRC_t cmMdCheckAllGuards( cmRpt_t* rpt )
  55. { return cmMmCheckAllGuards(_cmMdH); }
  56. unsigned cmMdDebugId( const void* dataPtr)
  57. { return cmMmDebugId(_cmMdH,dataPtr); }
  58. cmMmRC_t cmMdReport( unsigned mmFlags )
  59. { return cmMmReport(_cmMdH,mmFlags); }
  60. //! [cmMdExample]
  61. void cmMdTest( cmRpt_t* rpt )
  62. {
  63. bool memDebugFl = true;
  64. unsigned memGuardByteCnt = memDebugFl ? 0 : 8;
  65. unsigned memAlignByteCnt = 16;
  66. unsigned memFlags = memDebugFl ? kTrackMmFl | kDeferFreeMmFl | kFillUninitMmFl | kFillFreedMmFl : 0;
  67. // initialize the library
  68. cmMdInitialize( memGuardByteCnt, memAlignByteCnt, memFlags, rpt );
  69. // Allocate a block of 16 bytes of aligned and zeroed memory.
  70. void* d0p = cmMdAllocateDebug(NULL, 1, 16, kAlignMmFl | kZeroMmFl, __FUNCTION__, __FILE__, __LINE__ );
  71. // Allocate a block of 20 bytes of non-aligned, zeroed memory.
  72. unsigned* d1p = cmMdAllocateDebug(NULL, 1, 20, /*kAlignMmFl |*/ kZeroMmFl, __FUNCTION__, __FILE__, __LINE__ );
  73. unsigned i;
  74. // Intentionally overwrite the guard bytes by writing
  75. // 24 bytes where only 20 where allocated
  76. for(i=0; i<3; ++i)
  77. d1p[i] = i;
  78. // Print a report showing the state of each memory block.
  79. // This should show that d1p[] has been corrupted and
  80. // memory leaks because active blocks exist.
  81. cmMdReport( 0 );
  82. // Expand d1p[] preserving the existing 20 bytes.
  83. d1p = cmMdAllocateDebug(d1p, 1, 24, kPreserveMmFl | /*kAlignMmFl |*/ kZeroMmFl, __FUNCTION__, __FILE__, __LINE__ );
  84. // Print the contents of the expanded d1p[]
  85. for(i=0; i<3; ++i)
  86. printf("%i ",d1p[i]);
  87. printf("\n");
  88. // Free d0p[] and d1p[];
  89. cmMdFreeDebug(d0p, __FUNCTION__, __FILE__, __LINE__);
  90. cmMdFreeDebug(d1p, __FUNCTION__, __FILE__, __LINE__);
  91. // Perform a write after free on d0p[].
  92. *(unsigned*)d0p = 1;
  93. // Print another report showing to test write-after-free detection.
  94. cmMdReport( 0 );
  95. // Close the library.
  96. cmMdFinalize();
  97. }
  98. //! [cmMdExample]