Browse Source

cmMem: Added cmThPtrCAS() to allow mutlithread alloc.

master
kpl 11 years ago
parent
commit
fd40c2a213
1 changed files with 25 additions and 14 deletions
  1. 25
    14
      cmMem.c

+ 25
- 14
cmMem.c View File

@@ -5,6 +5,7 @@
5 5
 #include "cmMem.h"
6 6
 #include "cmFloatTypes.h"
7 7
 #include "cmMath.h"
8
+#include "cmThread.h"
8 9
 
9 10
 // Block layout
10 11
 //
@@ -431,14 +432,14 @@ bool     cmMmIsValid( cmMmH_t h )
431 432
 { return h.h != NULL; }
432 433
 
433 434
 void* cmMmAllocate(     
434
-  cmMmH_t     h, 
435
-  void*       orgDataPtr, 
436
-  unsigned    newEleCnt,
437
-  unsigned    newEleByteCnt, 
438
-  enum cmMmAllocFlags_t    flags, 
439
-  const char* fileName, 
440
-  const char* funcName, 
441
-  unsigned    fileLine )
435
+  cmMmH_t               h, 
436
+  void*                 orgDataPtr, 
437
+  unsigned              newEleCnt,
438
+  unsigned              newEleByteCnt, 
439
+  enum cmMmAllocFlags_t flags, 
440
+  const char*           fileName, 
441
+  const char*           funcName, 
442
+  unsigned              fileLine )
442 443
 {
443 444
    cmMm_t*  p          = _cmMmHandleToPtr(h); 
444 445
    unsigned newByteCnt = newEleCnt * newEleByteCnt;
@@ -449,8 +450,14 @@ void* cmMmAllocate(
449 450
    {
450 451
      cmErrMsg(&p->err,kOkMmRC,"Breakpoint for memory allocation id:%i.",p->nextId);
451 452
    }
453
+
454
+   if( (long long)_cmMmDataToBasePtr(ndp,p->guardByteCnt) == 0x7fffed8d0b40 )
455
+   {
456
+     cmErrMsg(&p->err,kOkMmRC,"Breakpoint for memory allocation id:%i.",p->nextId);
457
+   }
452 458
    */
453 459
 
460
+
454 461
    // if we are tracking changes
455 462
    if( cmIsFlag(p->flags,kTrackMmFl) )
456 463
    {
@@ -470,23 +477,27 @@ void* cmMmAllocate(
470 477
          return ndp;
471 478
        }
472 479
 
480
+       cmThUIntIncr(&p->nextId,1);
481
+
473 482
        // initialize the new tracking recd
474
-       rp->uniqueId     = p->nextId;
475 483
        rp->dataPtr      = ndp;
476 484
        rp->dataByteCnt  = newByteCnt;
477 485
        rp->fileLine     = fileLine;
478 486
        rp->fileNameStr  = fileName;
479 487
        rp->funcNameStr  = funcName;
480 488
        rp->flags        = 0;
481
-       rp->linkPtr      = p->listPtr;
482
-
483
-       //printf("%i %i %s %i %s\n",rp->uniqueId,newByteCnt,funcName,fileLine,fileName);
489
+       rp->uniqueId     = p->nextId;
484 490
 
485
-       p->listPtr = rp;
491
+       cmMmRecd_t *oldp, *newp;
492
+       do
493
+       {
494
+         oldp         = p->listPtr;
495
+         newp         = rp;
496
+         rp->linkPtr  = p->listPtr;
497
+       }while(!cmThPtrCAS(&p->listPtr,oldp,newp));
486 498
 
487 499
        assert( _cmMmCheckGuards(p,rp) == kOkMmRC );
488 500
 
489
-       ++p->nextId;
490 501
      }
491 502
      else // a reallocation occurred.
492 503
        if( orgDataPtr == ndp )

Loading…
Cancel
Save