libcm is a C development framework with an emphasis on audio signal processing applications.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

cmMem.c 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771
  1. #include "cmPrefix.h"
  2. #include "cmGlobal.h"
  3. #include "cmRpt.h"
  4. #include "cmErr.h"
  5. #include "cmMem.h"
  6. #include "cmFloatTypes.h"
  7. #include "cmMath.h"
  8. #include "cmThread.h"
  9. // Block layout
  10. //
  11. // |a |b |c |d |e |f
  12. // |xx...xxx|yyyy|zzzz|gggggggg|ddd...ddd|gggggggg|
  13. //
  14. //
  15. // xxxx = alignment (prefix) bytes - size is given by yyyy (may contain up to alignByteCnt-1 bytes).
  16. // yyyy = count of bytes preceding yyyy
  17. // zzzz = count of data bytes
  18. // gggg = guard area (size is fixed by guardByteCnt arg. to cmMemInitialize())
  19. // dddd = data area
  20. //
  21. // d = e - guardByteCnt
  22. // c = d - sizeof(unsigned)
  23. // b = c - sizeof(unsigned)
  24. // a = b - yyyy
  25. //
  26. // Notes:
  27. // 1) The smallest allocation is dataByteCnt + (2*sizeof(unsigned)
  28. // 2) If allocByteCnt > 0 then the smallest allocation is allocByteCnt + (2*sizeof(unsigned)) + dataByteCnt.
  29. //
  30. #define _cmMmDataToByteCntPtr( dp, gbc ) (dp==NULL?NULL:(((char*)(dp))-(gbc)-sizeof(unsigned)))
  31. #define _cmMmDataToByteCnt( dp, gbc ) (dp==NULL?0 :(*(unsigned*)_cmMmDataToByteCntPtr(dp,gbc)))
  32. #define _cmMmDataToPrefixCntPtr( dp, gbc ) (dp==NULL?NULL:( _cmMmDataToByteCntPtr(dp,gbc) - sizeof(unsigned)))
  33. #define _cmMmDataToPrefixCnt( dp, gbc ) (dp==NULL?0 :(*(unsigned*)_cmMmDataToPrefixCntPtr(dp,gbc)))
  34. #define _cmMmDataToBasePtr( dp, gbc ) (dp==NULL?NULL:(_cmMmDataToPrefixCntPtr(dp,gbc) - _cmMmDataToPrefixCnt(dp,gbc)))
  35. #define _cmMmDataToTotalByteCnt( dp, gbc ) (dp==NULL?0 :(sizeof(unsigned) + (2*gbc) + _cmMmDataToByteCnt(dp,gbc) + (_cmMmDataToPrefixCnt(dp,gbc) + sizeof(unsigned))))
  36. #define _cmMmDataToPreGuardPtr( dp, gbc ) (dp==NULL?NULL:(((char*)(dp))-(gbc)))
  37. #define _cmMmDataToPostGuardPtr( dp, gbc ) (dp==NULL?NULL:(((char*)(dp))+_cmMmDataToByteCnt(dp,gbc)))
  38. enum
  39. {
  40. kFreedMmFl = 0x01,
  41. kDblFreeMmFl = 0x02
  42. };
  43. typedef struct cmMmRecd_str
  44. {
  45. unsigned uniqueId; //
  46. void* dataPtr; // dataPtr may be NULL if the assoc'd alloc request was for 0 bytes.
  47. unsigned dataByteCnt; // data area bytes on original allocation
  48. unsigned fileLine;
  49. char* fileNameStr;
  50. char* funcNameStr;
  51. unsigned flags;
  52. struct cmMmRecd_str* linkPtr;
  53. } cmMmRecd_t;
  54. typedef struct cmMmStr_str
  55. {
  56. struct cmMmStr_str* link;
  57. char* str;
  58. } cmMmStr_t;
  59. typedef struct
  60. {
  61. cmRpt_t* rpt;
  62. cmErr_t err;
  63. cmMmRecd_t* listPtr;
  64. unsigned nextId;
  65. unsigned alignByteCnt;
  66. unsigned guardByteCnt;
  67. cmAllocMmFunc_t allocFunc;
  68. cmFreeMmFunc_t freeFunc;
  69. void* funcArgPtr;
  70. char uninitChar;
  71. char freeChar;
  72. char guardChar;
  73. unsigned flags;
  74. cmMmStr_t* fnList;
  75. cmMmStr_t* funcList;
  76. } cmMm_t;
  77. cmMmH_t cmMmNullHandle = { NULL };
  78. cmMm_t* _cmMmHandleToPtr( cmMmH_t h )
  79. {
  80. assert(h.h != NULL );
  81. return (cmMm_t*)h.h;
  82. }
  83. cmMmRC_t _cmMmCheckGuards( cmMm_t* p, cmMmRecd_t* rp )
  84. {
  85. // it is possible that dataPtr is NULL if zero bytes was requested at allocation
  86. if( rp->dataPtr == NULL )
  87. return kOkMmRC;
  88. // check the pre data area guard space
  89. char* pp = _cmMmDataToPreGuardPtr( rp->dataPtr, p->guardByteCnt );
  90. char* ep = pp + p->guardByteCnt;
  91. for(;pp<ep; ++pp)
  92. if( *pp != p->guardChar )
  93. return kGuardCorruptMmRC;
  94. // check the post data area guard space
  95. pp = _cmMmDataToPostGuardPtr( rp->dataPtr, p->guardByteCnt );
  96. ep = pp + p->guardByteCnt;
  97. for(;pp<ep; ++pp)
  98. if( *pp != p->guardChar )
  99. return kGuardCorruptMmRC;
  100. // if this block was freed and the kFillFreedMmFl was set then check for write-after-free
  101. if( cmIsFlag(rp->flags,kFreedMmFl) && cmIsFlag(p->flags,kFillFreedMmFl) )
  102. {
  103. pp = rp->dataPtr;
  104. ep = pp + _cmMmDataToByteCnt(pp,p->guardByteCnt);
  105. for(; pp<ep; ++pp)
  106. if( *pp != p->freeChar )
  107. return kWriteAfterFreeMmRC;
  108. }
  109. return kOkMmRC;
  110. }
  111. cmMmRecd_t* _cmMmFindRecd( cmMm_t* p, const void* dataPtr )
  112. {
  113. cmMmRecd_t* rp = p->listPtr;
  114. while( rp != NULL )
  115. {
  116. if( rp->dataPtr == dataPtr )
  117. break;
  118. rp = rp->linkPtr;
  119. }
  120. return rp;
  121. }
  122. cmMmRC_t _cmMmFree( cmMm_t* p, void* dataPtr, cmMmRecd_t* rp )
  123. {
  124. cmMmRC_t rc = kOkMmRC;
  125. if( p->freeFunc(p->funcArgPtr, _cmMmDataToBasePtr(dataPtr,p->guardByteCnt)) == false )
  126. {
  127. if( rp == NULL )
  128. rc = cmErrMsg(&p->err,kFreeFailMmRC,"Memory free failed on data area at %p.",dataPtr);
  129. else
  130. rc = cmErrMsg(&p->err,kFreeFailMmRC,"Memory free failed on data area at %p. Allocated with id:%i at %s line:%i %s.",dataPtr, rp->uniqueId,rp->funcNameStr,rp->fileLine,rp->fileNameStr);
  131. }
  132. return rc;
  133. }
  134. cmMmRC_t _cmMmFreeP( cmMm_t* p, void* dataPtr )
  135. {
  136. if( dataPtr == NULL )
  137. return kOkMmRC;
  138. cmMmRecd_t* rp = p->listPtr;
  139. cmMmRC_t rc = kOkMmRC;
  140. // if we are tracking alloc's and free's
  141. if( cmIsFlag(p->flags,kTrackMmFl) )
  142. {
  143. // locate the tracking recd
  144. rp = _cmMmFindRecd(p,dataPtr);
  145. // if no tracking recd was found
  146. if( rp == NULL )
  147. return cmErrMsg(&p->err,kMissingRecdMmRC,"Unable to locate a tracking record associated with released data area pointer:%p.",dataPtr);
  148. /*
  149. if( rp->uniqueId == 59451 )
  150. {
  151. cmErrMsg(&p->err,kOkMmRC,"Breakpoint for memory free id:%i.",rp->uniqueId);
  152. }
  153. */
  154. // if this block was already freed then this is a double free
  155. if( cmIsFlag(rp->flags,kFreedMmFl) )
  156. rp->flags = cmSetFlag(rp->flags,kDblFreeMmFl);
  157. else
  158. // otherwise fill the freed block with the freeChar (if requested)
  159. if( cmIsFlag(p->flags,kFillFreedMmFl) )
  160. memset(dataPtr, p->freeChar, _cmMmDataToByteCnt(dataPtr,p->guardByteCnt));
  161. // mark the block as having been freed
  162. rp->flags = cmSetFlag(rp->flags,kFreedMmFl);
  163. }
  164. // if we are not deferring free to the finalize stage ... then free the memory here
  165. if( cmIsFlag(p->flags,kDeferFreeMmFl) == false)
  166. rc = _cmMmFree(p,dataPtr,rp);
  167. return rc;
  168. }
  169. void* _cmMmAllocate(cmMm_t* p, void* orgDataPtr, unsigned newByteCnt, unsigned flags )
  170. {
  171. unsigned abc = cmIsFlag(flags,kAlignMmFl) ? p->alignByteCnt : 0;
  172. unsigned gbc = p->guardByteCnt;
  173. char* bp = NULL;
  174. char* dp = NULL;
  175. char* idp = NULL;
  176. unsigned orgByteCnt = 0;
  177. unsigned prefixByteCnt = 0;
  178. // Determine the total allocation block size including the auxillary data.
  179. //
  180. // alignment bytes + data_byte_cnt + guard bytes + actual data
  181. unsigned ttlByteCnt = abc+sizeof(unsigned) + sizeof(unsigned) + (2*gbc) + newByteCnt;
  182. // if this is a reallocation
  183. if( orgDataPtr != NULL )
  184. {
  185. // asking to reallocate a block with zero bytes free's the original block and returns NULL
  186. // (this is in keeping with realloc() behaviour)
  187. if( newByteCnt == 0 )
  188. {
  189. _cmMmFreeP(p,orgDataPtr);
  190. return NULL;
  191. }
  192. // get the size of the currently allocated block
  193. orgByteCnt = _cmMmDataToByteCnt(orgDataPtr,gbc);
  194. // if the requested data area is <= the alloc'd data area
  195. if( newByteCnt <= orgByteCnt)
  196. {
  197. // if preservation was requested simply return with the original data area ptr
  198. if( cmIsFlag(flags,kPreserveMmFl) )
  199. return orgDataPtr;
  200. // otherwise initialze the data area
  201. dp = orgDataPtr;
  202. goto initLabel;
  203. }
  204. // expansion was requested
  205. }
  206. // if an empty data area was requested
  207. if( newByteCnt == 0 )
  208. return NULL;
  209. //
  210. // A new data block must be allocated
  211. //
  212. // allocate the memory block via a callback
  213. if((bp = p->allocFunc(p->funcArgPtr,ttlByteCnt)) == NULL )
  214. {
  215. cmErrMsg(&p->err,kAllocFailMmRC,"Attempt to allocate %i bytes failed.",ttlByteCnt);
  216. goto errLabel;
  217. }
  218. // make the data area offset: data_byte_cnt + guard bytes
  219. dp = bp + (2*sizeof(unsigned)) + gbc;
  220. if( abc )
  221. {
  222. unsigned long alignMask = abc - 1;
  223. // alignment requires an additional offset
  224. dp += abc;
  225. // align the data area to the specified boundary
  226. char* ndp = ((char*)((unsigned long)dp & (~alignMask)));
  227. prefixByteCnt = abc - (dp - ndp);
  228. dp = ndp;
  229. }
  230. // set the prefix byteCnt
  231. *(unsigned*)_cmMmDataToPrefixCntPtr(dp,gbc) = prefixByteCnt;
  232. // set the data area byte cnt
  233. *(unsigned*)_cmMmDataToByteCntPtr(dp,gbc) = newByteCnt;
  234. // uncomment this line to print memory layout information for each block
  235. //printf("prefix:%i prefix*:%p bytes:%i bytes*:%p base:%p==%p data:%p\n",_cmMmDataToPrefixCnt(dp,gbc),_cmMmDataToPrefixCntPtr(dp,gbc),_cmMmDataToByteCnt(dp,gbc),_cmMmDataToByteCntPtr(dp,gbc),_cmMmDataToBasePtr(dp,gbc),bp,dp);
  236. // set the guard bytes
  237. if( gbc )
  238. {
  239. void* gp = _cmMmDataToPreGuardPtr( dp,gbc);
  240. if( gp != NULL )
  241. memset(gp, p->guardChar, gbc);
  242. gp = _cmMmDataToPostGuardPtr( dp,gbc);
  243. if( gp != NULL )
  244. memset(gp, p->guardChar, gbc);
  245. }
  246. initLabel:
  247. //
  248. // initialize the new data area
  249. //
  250. idp = dp;
  251. // if this is a reallocation with expansion ...
  252. if( orgDataPtr != NULL && newByteCnt > orgByteCnt )
  253. {
  254. // ... and preservation was requested
  255. if( cmIsFlag(flags,kPreserveMmFl) )
  256. {
  257. // copy original data into the new block
  258. memcpy(idp,orgDataPtr,orgByteCnt);
  259. idp += orgByteCnt;
  260. newByteCnt -= orgByteCnt;
  261. }
  262. _cmMmFreeP(p,orgDataPtr); // free the original data block
  263. }
  264. // if zeroing was requested
  265. if( cmIsFlag(flags,kZeroMmFl))
  266. memset(idp,0,newByteCnt);
  267. else
  268. // if uninitialized data should be set to a known character
  269. if( cmIsFlag(p->flags,kFillUninitMmFl) )
  270. memset(idp,p->uninitChar,newByteCnt);
  271. return dp;
  272. errLabel:
  273. return NULL;
  274. }
  275. cmMmRC_t cmMmInitialize(
  276. cmMmH_t* hp,
  277. cmAllocMmFunc_t allocFunc,
  278. cmFreeMmFunc_t freeFunc,
  279. void* funcArgPtr,
  280. unsigned guardByteCnt,
  281. unsigned alignByteCnt,
  282. unsigned flags,
  283. cmRpt_t* rpt )
  284. {
  285. cmMm_t* p;
  286. cmErr_t err;
  287. cmErrSetup(&err,rpt,"Memory Manager");
  288. // validate alignByteCnt
  289. if(alignByteCnt>0 && cmIsPowerOfTwo(alignByteCnt)==false)
  290. return cmErrMsg(&err,kParamErrMmRC,"The 'alignByteCnt' parameter must be a power of two.");
  291. // allocate the main object
  292. if((p= calloc(1,sizeof(cmMm_t))) == NULL )
  293. return cmErrMsg(&err,kObjAllocFailMmRC,"Object allocation failed.");
  294. // setting kDeferFreeMmFl only makes sense when kTrackMmFl is also set
  295. if(cmIsFlag(flags,kTrackMmFl)==false && cmIsFlag(flags,kDeferFreeMmFl)==true)
  296. {
  297. cmErrMsg(&err,kParamErrMmRC,"The flag 'kDeferFreeMmFl' may only be set if the 'kTrackMmFl' is also set. 'kDeferFreeMmFl' is begin disabled.");
  298. flags = cmClrFlag(flags,kDeferFreeMmFl);
  299. }
  300. cmErrClone(&p->err,&err);
  301. p->rpt = rpt;
  302. p->alignByteCnt = alignByteCnt;
  303. p->guardByteCnt = guardByteCnt;
  304. p->allocFunc = allocFunc;
  305. p->freeFunc = freeFunc;
  306. p->funcArgPtr = funcArgPtr;
  307. p->uninitChar = 0x55; // non-zeroed data areas are automatically filled w/ this value on allocation
  308. p->freeChar = 0x33; // data areas are overwritten with this value on free
  309. p->guardChar = 0xAA; // guard areas are set with this value
  310. p->flags = flags;
  311. hp->h = p;
  312. return kOkMmRC;
  313. }
  314. void _cmMmFreeStrList( cmMmStr_t* cp )
  315. {
  316. while( cp!=NULL )
  317. {
  318. cmMmStr_t* np = cp->link;
  319. free(cp->str);
  320. free(cp);
  321. cp = np;
  322. }
  323. }
  324. cmMmRC_t cmMmFinalize( cmMmH_t* hp )
  325. {
  326. cmMm_t* p;
  327. cmMmRC_t rc = kOkMmRC;
  328. if( hp == NULL || cmMmIsValid(*hp)==false )
  329. return kOkMmRC;
  330. p = _cmMmHandleToPtr(*hp);
  331. cmMmRecd_t* rp = p->listPtr;
  332. while( rp != NULL )
  333. {
  334. cmMmRecd_t* tp = rp->linkPtr;
  335. cmMmRC_t rrc;
  336. if( cmIsFlag(p->flags,kDeferFreeMmFl) )
  337. if((rrc = _cmMmFree(p,rp->dataPtr,rp)) != kOkMmRC )
  338. rc = rrc;
  339. free(rp);
  340. rp = tp;
  341. }
  342. _cmMmFreeStrList(p->fnList);
  343. _cmMmFreeStrList(p->funcList);
  344. free(p);
  345. hp->h = NULL;
  346. return rc;
  347. }
  348. unsigned cmMmGuardByteCount( cmMmH_t h )
  349. {
  350. cmMm_t* p = _cmMmHandleToPtr(h);
  351. return p->guardByteCnt;
  352. }
  353. unsigned cmMmAlignByteCount( cmMmH_t h )
  354. {
  355. cmMm_t* p = _cmMmHandleToPtr(h);
  356. return p->alignByteCnt;
  357. }
  358. unsigned cmMmInitializeFlags( cmMmH_t h )
  359. {
  360. cmMm_t* p = _cmMmHandleToPtr(h);
  361. return p->flags;
  362. }
  363. bool cmMmIsValid( cmMmH_t h )
  364. { return h.h != NULL; }
  365. // Allocate and/or return a pointer to a stored string.
  366. char* _cmMmAllocStr( cmMmStr_t** listPtrPtr, const char* str )
  367. {
  368. if( str == NULL )
  369. str = "";
  370. cmMmStr_t* lp = *listPtrPtr; // get ptr to first recd in list
  371. cmMmStr_t* cp = lp; // init current ptr
  372. // find 'str' in the list
  373. for(; cp!=NULL; cp=cp->link)
  374. if( strcmp(cp->str,str) == 0 )
  375. break;
  376. // 'str' was not found - create a new string recd
  377. if( cp == NULL )
  378. {
  379. cp = calloc(1,sizeof(cmMmStr_t));
  380. cp->str = strdup(str);
  381. cp->link = lp;
  382. *listPtrPtr = cp;
  383. }
  384. return cp->str;
  385. }
  386. void* cmMmAllocate(
  387. cmMmH_t h,
  388. void* orgDataPtr,
  389. unsigned newEleCnt,
  390. unsigned newEleByteCnt,
  391. enum cmMmAllocFlags_t flags,
  392. const char* fileName,
  393. const char* funcName,
  394. unsigned fileLine )
  395. {
  396. cmMm_t* p = _cmMmHandleToPtr(h);
  397. unsigned newByteCnt = newEleCnt * newEleByteCnt;
  398. void* ndp = _cmMmAllocate(p,orgDataPtr,newByteCnt,flags);
  399. // if we are tracking changes
  400. if( cmIsFlag(p->flags,kTrackMmFl) )
  401. {
  402. //
  403. // NOTE: it is possible that ndp is NULL if newByteCnt == 0.
  404. //
  405. cmMmRecd_t* rp = NULL;
  406. // if a new memory block was allocated
  407. if( orgDataPtr == NULL || orgDataPtr != ndp )
  408. {
  409. // allocate a new tracking recd
  410. if( (rp = calloc(1,sizeof(cmMmRecd_t))) == NULL )
  411. {
  412. cmErrMsg(&p->err,kTrkAllocFailMmRC,"Unable to allocate a tracking record for %s line:%i %s.",funcName,fileLine,fileName);
  413. return ndp;
  414. }
  415. // p->nextId is incremented here
  416. cmThUIntIncr(&p->nextId,1);
  417. /*
  418. if( p->nextId == 2393 )
  419. {
  420. cmErrMsg(&p->err,kOkMmRC,"Breakpoint for memory allocation id:%i.",p->nextId);
  421. }
  422. */
  423. /*
  424. if( (long long)_cmMmDataToBasePtr(ndp,p->guardByteCnt) == 0x7fffed8d0b40 )
  425. {
  426. cmErrMsg(&p->err,kOkMmRC,"Breakpoint for memory allocation id:%i.",p->nextId);
  427. }
  428. */
  429. // initialize the new tracking recd
  430. rp->dataPtr = ndp;
  431. rp->dataByteCnt = newByteCnt;
  432. rp->fileLine = fileLine;
  433. rp->fileNameStr = _cmMmAllocStr( &p->fnList, fileName );
  434. rp->funcNameStr = _cmMmAllocStr( &p->funcList, funcName);
  435. rp->flags = 0;
  436. rp->uniqueId = p->nextId;
  437. cmMmRecd_t *oldp, *newp;
  438. do
  439. {
  440. oldp = p->listPtr;
  441. newp = rp;
  442. rp->linkPtr = p->listPtr;
  443. }while(!cmThPtrCAS(&p->listPtr,oldp,newp));
  444. assert( _cmMmCheckGuards(p,rp) == kOkMmRC );
  445. }
  446. else // a reallocation occurred.
  447. if( orgDataPtr == ndp )
  448. {
  449. if((rp = _cmMmFindRecd(p,orgDataPtr)) == NULL )
  450. cmErrMsg(&p->err,kMissingRecdMmRC,"Unable to locate a tracking record associated with reallocation data area pointer:%p.",orgDataPtr);
  451. }
  452. }
  453. return ndp;
  454. }
  455. cmMmRC_t cmMmFree( cmMmH_t h, void* dataPtr )
  456. {
  457. cmMm_t* p = _cmMmHandleToPtr(h);
  458. return _cmMmFreeP(p,dataPtr);
  459. }
  460. cmMmRC_t cmMmFreeDebug( cmMmH_t h, void* dataPtr, const char* fileName, const char* funcName, unsigned fileLine )
  461. {
  462. cmMmRC_t rc;
  463. if((rc = cmMmFree(h,dataPtr)) != kOkMmRC )
  464. cmErrMsg(&_cmMmHandleToPtr(h)->err,rc,"Memory free failed at %s() line:%i %s",funcName,fileLine,fileName);
  465. return rc;
  466. }
  467. cmMmRC_t cmMmFreePtr( cmMmH_t h, void** dataPtrPtr )
  468. {
  469. assert(dataPtrPtr != NULL );
  470. cmMmRC_t rc = cmMmFree(h,*dataPtrPtr);
  471. *dataPtrPtr = NULL;
  472. return rc;
  473. }
  474. cmMmRC_t cmMmFreePtrDebug( cmMmH_t h, void* dataPtr, const char* fileName, const char* funcName, unsigned fileLine )
  475. {
  476. cmMmRC_t rc;
  477. if((rc = cmMmFreePtr(h,dataPtr)) != kOkMmRC )
  478. cmErrMsg(&_cmMmHandleToPtr(h)->err,rc,"Memory free failed at %s() line:%i %s",funcName,fileLine,fileName);
  479. return rc;
  480. }
  481. unsigned cmMmByteCount( cmMmH_t h, const void* dataPtr )
  482. {
  483. cmMm_t* p = _cmMmHandleToPtr(h);
  484. return _cmMmDataToByteCnt(dataPtr,p->guardByteCnt);
  485. }
  486. unsigned cmMmDebugId( cmMmH_t h, const void* dataPtr)
  487. {
  488. cmMm_t* p = _cmMmHandleToPtr(h);
  489. const cmMmRecd_t* rp = NULL;
  490. // if we are tracking alloc's and free's
  491. if( cmIsFlag(p->flags,kTrackMmFl) )
  492. {
  493. // locate the tracking recd
  494. rp = _cmMmFindRecd(p,dataPtr);
  495. }
  496. return rp==NULL ? cmInvalidId : rp->uniqueId;
  497. }
  498. cmMmRC_t _cmMmError( cmMm_t* p, cmMmRC_t rc, const cmMmRecd_t* rp, const char* msg )
  499. {
  500. return cmErrMsg(&p->err,rc,"%s detected on block id:%i from %s() line:%i %s.",msg,rp->uniqueId,rp->funcNameStr,rp->fileLine,rp->fileNameStr);
  501. }
  502. cmMmRC_t _cmMmRecdPrint( cmMm_t* p, cmMmRecd_t* rp, cmMmRC_t rc )
  503. {
  504. void* dp = rp->dataPtr;
  505. unsigned gbc = p->guardByteCnt;
  506. char* lbl = NULL;
  507. switch( rc )
  508. {
  509. case kOkMmRC: lbl = "Ok "; break;
  510. case kLeakDetectedMmRC: lbl = "Memory Leak "; break;
  511. case kWriteAfterFreeMmRC: lbl = "Write After Free"; break;
  512. case kDblFreeDetectedMmRC: lbl = "Double Free "; break;
  513. case kGuardCorruptMmRC: lbl = "Guard Corrupt "; break;
  514. default:
  515. lbl = "Unknown Status ";
  516. }
  517. cmRptPrintf(p->err.rpt,"%s id:%5i data:%p : data:%5i prefix:%5i total:%5i base:%p : line=%5i %s %s\n",
  518. lbl,rp->uniqueId,rp->dataPtr,
  519. _cmMmDataToByteCnt(dp,gbc),
  520. _cmMmDataToPrefixCnt(dp,gbc),
  521. _cmMmDataToTotalByteCnt(dp,gbc),
  522. _cmMmDataToBasePtr(dp,gbc),
  523. rp->fileLine,rp->funcNameStr,rp->fileNameStr );
  524. return rc;
  525. }
  526. cmMmRC_t cmMmReport( cmMmH_t h, unsigned flags )
  527. {
  528. cmMm_t* p = _cmMmHandleToPtr(h);
  529. unsigned allocByteCnt = 0;
  530. unsigned ttlByteCnt = 0;
  531. unsigned dataFrByteCnt = 0;
  532. unsigned ttlFrByteCnt = 0;
  533. unsigned dataLkByteCnt = 0;
  534. unsigned ttlLkByteCnt = 0;
  535. unsigned allocBlkCnt = 0;
  536. unsigned freeBlkCnt = 0;
  537. unsigned leakBlkCnt = 0;
  538. cmMmRecd_t* rp = p->listPtr;
  539. cmMmRC_t ret_rc = kOkMmRC;
  540. for(; rp != NULL; rp = rp->linkPtr,++allocBlkCnt )
  541. {
  542. cmMmRC_t rc = kOkMmRC;
  543. unsigned blkDataByteCnt = _cmMmDataToByteCnt(rp->dataPtr,p->guardByteCnt);
  544. unsigned blkTtlByteCnt = _cmMmDataToTotalByteCnt(rp->dataPtr,p->guardByteCnt);
  545. allocByteCnt += blkDataByteCnt;
  546. ttlByteCnt += blkTtlByteCnt;
  547. // if this block was freed or never alloc'd
  548. if( cmIsFlag(rp->flags,kFreedMmFl) || rp->dataPtr == NULL )
  549. {
  550. ++freeBlkCnt;
  551. dataFrByteCnt += blkDataByteCnt;
  552. ttlFrByteCnt += blkTtlByteCnt;
  553. }
  554. else // if this block leaked
  555. {
  556. ++leakBlkCnt;
  557. dataLkByteCnt += blkDataByteCnt;
  558. ttlLkByteCnt += blkTtlByteCnt;
  559. if( cmIsFlag(flags,kIgnoreLeaksMmFl) == false )
  560. rc = _cmMmRecdPrint(p,rp,kLeakDetectedMmRC);
  561. }
  562. // if this block was double freed
  563. if( cmIsFlag(rp->flags,kDblFreeMmFl) )
  564. rc = _cmMmRecdPrint(p,rp,kDblFreeDetectedMmRC);
  565. // check for guard corruption and write-after-free
  566. cmMmRC_t t_rc = _cmMmCheckGuards(p,rp);
  567. switch( t_rc )
  568. {
  569. case kOkMmRC:
  570. break;
  571. case kGuardCorruptMmRC:
  572. rc = _cmMmRecdPrint(p,rp,t_rc);
  573. break;
  574. case kWriteAfterFreeMmRC:
  575. rc = _cmMmRecdPrint(p,rp,t_rc);
  576. break;
  577. default:
  578. assert(0);
  579. break;
  580. }
  581. if( rc != kOkMmRC )
  582. ret_rc = rc;
  583. else
  584. {
  585. if( cmIsFlag(flags,kIgnoreNormalMmFl)==false )
  586. _cmMmRecdPrint(p,rp,rc);
  587. }
  588. }
  589. if( p->listPtr != NULL && cmIsFlag(flags,kSuppressSummaryMmFl)==false )
  590. {
  591. cmRptPrintf(p->err.rpt,"Blocks Allocated:%i Freed:%i Leaked:%i\n",allocBlkCnt,freeBlkCnt,leakBlkCnt);
  592. cmRptPrintf(p->err.rpt,"Bytes Allocated: data=%i total=%i Freed: data=%i total=%i Leaked: data=%i total=%i\n",allocByteCnt,ttlByteCnt,dataFrByteCnt,ttlFrByteCnt,dataLkByteCnt,ttlLkByteCnt);
  593. }
  594. return ret_rc;
  595. }
  596. cmMmRC_t cmMmIsGuardCorrupt( cmMmH_t h, unsigned id )
  597. {
  598. cmMm_t* p = _cmMmHandleToPtr(h);
  599. cmMmRecd_t* rp = p->listPtr;
  600. while(rp != NULL )
  601. {
  602. if( rp->uniqueId == id )
  603. break;
  604. rp = rp->linkPtr;
  605. }
  606. if( rp == NULL )
  607. return cmErrMsg(&p->err,kMissingRecdMmRC,"Unable to locate the tracking record associated with id %i.",id);
  608. return _cmMmCheckGuards(p,rp);
  609. }
  610. cmMmRC_t cmMmCheckAllGuards( cmMmH_t h )
  611. {
  612. cmMm_t* p = _cmMmHandleToPtr(h);
  613. cmMmRecd_t* rp = p->listPtr;
  614. cmMmRC_t rc = kOkMmRC;
  615. while(rp != NULL )
  616. {
  617. if((rc = _cmMmCheckGuards(p,rp)) != kOkMmRC )
  618. rc = cmErrMsg(&p->err,rc,"A corrupt guard or 'write after free' was detected in the data area allocated with id:%i at %s (line:%i) %s.",rp->uniqueId,rp->funcNameStr,rp->fileLine,rp->fileNameStr);
  619. rp = rp->linkPtr;
  620. }
  621. return rc;
  622. }