Sfoglia il codice sorgente

cmMem.c : Added string tables for 'filename' and 'function name' strings

in the cmMmRecd_t record.

Previously the 'fileNameStr' and 'funcNameStr' were assumed to be stored
in static memory and therefore did not require duplication.  If the
strings however originated in a dynamic library then the string pointer
would be invalid after the library is unloaded.  Accessing these fields
would then result in a crash.
master
kpl 11 anni fa
parent
commit
42a9f19166
1 ha cambiato i file con 54 aggiunte e 5 eliminazioni
  1. 54
    5
      cmMem.c

+ 54
- 5
cmMem.c Vedi File

@@ -52,12 +52,18 @@ typedef struct cmMmRecd_str
52 52
   void*                dataPtr;      // dataPtr may be NULL if the assoc'd alloc request was for 0 bytes.
53 53
   unsigned             dataByteCnt;  // data area bytes on original allocation
54 54
   unsigned             fileLine;
55
-  const char*          fileNameStr;
56
-  const char*          funcNameStr;
55
+  char*                fileNameStr;
56
+  char*                funcNameStr;
57 57
   unsigned             flags;
58 58
   struct cmMmRecd_str* linkPtr;
59 59
 } cmMmRecd_t;
60 60
 
61
+typedef struct cmMmStr_str
62
+{
63
+  struct cmMmStr_str* link;
64
+  char*               str;
65
+} cmMmStr_t;
66
+
61 67
 typedef struct
62 68
 {
63 69
   cmRpt_t*        rpt;
@@ -73,6 +79,8 @@ typedef struct
73 79
   char            freeChar;
74 80
   char            guardChar; 
75 81
   unsigned        flags;
82
+  cmMmStr_t*      fnList;
83
+  cmMmStr_t*      funcList;
76 84
 } cmMm_t;
77 85
 
78 86
 cmMmH_t cmMmNullHandle = { NULL };
@@ -380,6 +388,18 @@ cmMmRC_t cmMmInitialize(
380 388
   return kOkMmRC;
381 389
 }
382 390
 
391
+void _cmMmFreeStrList( cmMmStr_t* cp )
392
+{
393
+  while( cp!=NULL )
394
+  {
395
+    cmMmStr_t* np = cp->link;
396
+    free(cp->str);
397
+    free(cp);
398
+    cp = np;
399
+  }
400
+
401
+}
402
+
383 403
 cmMmRC_t cmMmFinalize( cmMmH_t* hp )
384 404
 {
385 405
   cmMm_t* p;
@@ -404,6 +424,9 @@ cmMmRC_t cmMmFinalize( cmMmH_t* hp )
404 424
     rp = tp;
405 425
   }
406 426
 
427
+  _cmMmFreeStrList(p->fnList);
428
+  _cmMmFreeStrList(p->funcList);
429
+
407 430
   free(p);
408 431
   hp->h = NULL;
409 432
 
@@ -431,6 +454,32 @@ unsigned cmMmInitializeFlags( cmMmH_t h )
431 454
 bool     cmMmIsValid( cmMmH_t h )
432 455
 { return h.h != NULL; }
433 456
 
457
+// Allocate and/or return a pointer to a stored string.
458
+char* _cmMmAllocStr( cmMmStr_t** listPtrPtr, const char* str )
459
+{
460
+  if( str == NULL )
461
+    str = "";
462
+
463
+  cmMmStr_t* lp = *listPtrPtr; // get ptr to first recd in list
464
+  cmMmStr_t* cp = lp;          // init current ptr
465
+
466
+  // find 'str' in the list
467
+  for(; cp!=NULL; cp=cp->link)
468
+    if( strcmp(cp->str,str) == 0 )
469
+      break;
470
+
471
+  // 'str' was not found - create a new string recd
472
+  if( cp == NULL )
473
+  {
474
+    cp          =  calloc(1,sizeof(cmMmStr_t));
475
+    cp->str     = strdup(str);
476
+    cp->link    = lp;
477
+    *listPtrPtr = cp;
478
+  }
479
+
480
+  return cp->str;
481
+}
482
+
434 483
 void* cmMmAllocate(     
435 484
   cmMmH_t               h, 
436 485
   void*                 orgDataPtr, 
@@ -483,8 +532,8 @@ void* cmMmAllocate(
483 532
        rp->dataPtr      = ndp;
484 533
        rp->dataByteCnt  = newByteCnt;
485 534
        rp->fileLine     = fileLine;
486
-       rp->fileNameStr  = fileName;
487
-       rp->funcNameStr  = funcName;
535
+       rp->fileNameStr  = _cmMmAllocStr( &p->fnList, fileName ); 
536
+       rp->funcNameStr  = _cmMmAllocStr( &p->funcList, funcName);
488 537
        rp->flags        = 0;
489 538
        rp->uniqueId     = p->nextId;
490 539
 
@@ -587,7 +636,7 @@ cmMmRC_t _cmMmRecdPrint( cmMm_t* p, cmMmRecd_t* rp, cmMmRC_t rc )
587 636
       lbl = "Unknown Status  ";
588 637
   }
589 638
 
590
-  cmRptPrintf(p->err.rpt,"%s id:%5i data:%p : data:%5i prefix:%5i total:%5i base:%p : %5i %s %s\n",
639
+  cmRptPrintf(p->err.rpt,"%s id:%5i data:%p : data:%5i prefix:%5i total:%5i base:%p : line=%5i %s %s\n",
591 640
     lbl,rp->uniqueId,rp->dataPtr,
592 641
     _cmMmDataToByteCnt(dp,gbc),
593 642
     _cmMmDataToPrefixCnt(dp,gbc), 

Loading…
Annulla
Salva