From fd40c2a213863580257f2b0f018f2e00a5a90f3a Mon Sep 17 00:00:00 2001 From: kpl Date: Fri, 23 Nov 2012 21:29:30 -0800 Subject: [PATCH] cmMem: Added cmThPtrCAS() to allow mutlithread alloc. --- cmMem.c | 39 +++++++++++++++++++++++++-------------- 1 file changed, 25 insertions(+), 14 deletions(-) diff --git a/cmMem.c b/cmMem.c index 1f0a42a..f9f1347 100644 --- a/cmMem.c +++ b/cmMem.c @@ -5,6 +5,7 @@ #include "cmMem.h" #include "cmFloatTypes.h" #include "cmMath.h" +#include "cmThread.h" // Block layout // @@ -431,14 +432,14 @@ bool cmMmIsValid( cmMmH_t h ) { return h.h != NULL; } void* cmMmAllocate( - cmMmH_t h, - void* orgDataPtr, - unsigned newEleCnt, - unsigned newEleByteCnt, - enum cmMmAllocFlags_t flags, - const char* fileName, - const char* funcName, - unsigned fileLine ) + cmMmH_t h, + void* orgDataPtr, + unsigned newEleCnt, + unsigned newEleByteCnt, + enum cmMmAllocFlags_t flags, + const char* fileName, + const char* funcName, + unsigned fileLine ) { cmMm_t* p = _cmMmHandleToPtr(h); unsigned newByteCnt = newEleCnt * newEleByteCnt; @@ -449,8 +450,14 @@ void* cmMmAllocate( { cmErrMsg(&p->err,kOkMmRC,"Breakpoint for memory allocation id:%i.",p->nextId); } + + if( (long long)_cmMmDataToBasePtr(ndp,p->guardByteCnt) == 0x7fffed8d0b40 ) + { + cmErrMsg(&p->err,kOkMmRC,"Breakpoint for memory allocation id:%i.",p->nextId); + } */ + // if we are tracking changes if( cmIsFlag(p->flags,kTrackMmFl) ) { @@ -470,23 +477,27 @@ void* cmMmAllocate( return ndp; } + cmThUIntIncr(&p->nextId,1); + // initialize the new tracking recd - rp->uniqueId = p->nextId; rp->dataPtr = ndp; rp->dataByteCnt = newByteCnt; rp->fileLine = fileLine; rp->fileNameStr = fileName; rp->funcNameStr = funcName; rp->flags = 0; - rp->linkPtr = p->listPtr; + rp->uniqueId = p->nextId; - //printf("%i %i %s %i %s\n",rp->uniqueId,newByteCnt,funcName,fileLine,fileName); - - p->listPtr = rp; + cmMmRecd_t *oldp, *newp; + do + { + oldp = p->listPtr; + newp = rp; + rp->linkPtr = p->listPtr; + }while(!cmThPtrCAS(&p->listPtr,oldp,newp)); assert( _cmMmCheckGuards(p,rp) == kOkMmRC ); - ++p->nextId; } else // a reallocation occurred. if( orgDataPtr == ndp )