libcm is a C development framework with an emphasis on audio signal processing applications.
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

cmMallocDebug.c 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. cp[n-1] = 0;
  41. return cp;
  42. }
  43. cmChar_t* cmMdAllocStrDebug( void* orgStrPtr, const cmChar_t* str, unsigned n, unsigned flags, const char* func, const char* fn, unsigned line )
  44. {
  45. if( str==NULL)
  46. return NULL;
  47. n += 1;
  48. cmChar_t* cp = cmMdAllocateDebug((void*)orgStrPtr,n,sizeof(cmChar_t),flags,func,fn,line);
  49. strncpy(cp,str,n);
  50. cp[n-1] = 0;
  51. return cp;
  52. }
  53. cmMmRC_t cmMdIsGuardCorrupt( unsigned id )
  54. { return cmMmIsGuardCorrupt(_cmMdH,id); }
  55. cmMmRC_t cmMdCheckAllGuards( cmRpt_t* rpt )
  56. { return cmMmCheckAllGuards(_cmMdH); }
  57. unsigned cmMdDebugId( const void* dataPtr)
  58. { return cmMmDebugId(_cmMdH,dataPtr); }
  59. cmMmRC_t cmMdReport( unsigned mmFlags )
  60. { return cmMmReport(_cmMdH,mmFlags); }
  61. //! [cmMdExample]
  62. void cmMdTest( cmRpt_t* rpt )
  63. {
  64. bool memDebugFl = true;
  65. unsigned memGuardByteCnt = memDebugFl ? 0 : 8;
  66. unsigned memAlignByteCnt = 16;
  67. unsigned memFlags = memDebugFl ? kTrackMmFl | kDeferFreeMmFl | kFillUninitMmFl | kFillFreedMmFl : 0;
  68. // initialize the library
  69. cmMdInitialize( memGuardByteCnt, memAlignByteCnt, memFlags, rpt );
  70. // Allocate a block of 16 bytes of aligned and zeroed memory.
  71. void* d0p = cmMdAllocateDebug(NULL, 1, 16, kAlignMmFl | kZeroMmFl, __FUNCTION__, __FILE__, __LINE__ );
  72. // Allocate a block of 20 bytes of non-aligned, zeroed memory.
  73. unsigned* d1p = cmMdAllocateDebug(NULL, 1, 20, /*kAlignMmFl |*/ kZeroMmFl, __FUNCTION__, __FILE__, __LINE__ );
  74. unsigned i;
  75. // Intentionally overwrite the guard bytes by writing
  76. // 24 bytes where only 20 where allocated
  77. for(i=0; i<3; ++i)
  78. d1p[i] = i;
  79. // Print a report showing the state of each memory block.
  80. // This should show that d1p[] has been corrupted and
  81. // memory leaks because active blocks exist.
  82. cmMdReport( 0 );
  83. // Expand d1p[] preserving the existing 20 bytes.
  84. d1p = cmMdAllocateDebug(d1p, 1, 24, kPreserveMmFl | /*kAlignMmFl |*/ kZeroMmFl, __FUNCTION__, __FILE__, __LINE__ );
  85. // Print the contents of the expanded d1p[]
  86. for(i=0; i<3; ++i)
  87. printf("%i ",d1p[i]);
  88. printf("\n");
  89. // Free d0p[] and d1p[];
  90. cmMdFreeDebug(d0p, __FUNCTION__, __FILE__, __LINE__);
  91. cmMdFreeDebug(d1p, __FUNCTION__, __FILE__, __LINE__);
  92. // Perform a write after free on d0p[].
  93. *(unsigned*)d0p = 1;
  94. // Print another report showing to test write-after-free detection.
  95. cmMdReport( 0 );
  96. // Close the library.
  97. cmMdFinalize();
  98. }
  99. //! [cmMdExample]