cmMem: Added cmThPtrCAS() to allow mutlithread alloc.

This commit is contained in:
kpl 2012-11-23 21:29:30 -08:00
parent b7191065bb
commit fd40c2a213

39
cmMem.c
View File

@ -5,6 +5,7 @@
#include "cmMem.h" #include "cmMem.h"
#include "cmFloatTypes.h" #include "cmFloatTypes.h"
#include "cmMath.h" #include "cmMath.h"
#include "cmThread.h"
// Block layout // Block layout
// //
@ -431,14 +432,14 @@ bool cmMmIsValid( cmMmH_t h )
{ return h.h != NULL; } { return h.h != NULL; }
void* cmMmAllocate( void* cmMmAllocate(
cmMmH_t h, cmMmH_t h,
void* orgDataPtr, void* orgDataPtr,
unsigned newEleCnt, unsigned newEleCnt,
unsigned newEleByteCnt, unsigned newEleByteCnt,
enum cmMmAllocFlags_t flags, enum cmMmAllocFlags_t flags,
const char* fileName, const char* fileName,
const char* funcName, const char* funcName,
unsigned fileLine ) unsigned fileLine )
{ {
cmMm_t* p = _cmMmHandleToPtr(h); cmMm_t* p = _cmMmHandleToPtr(h);
unsigned newByteCnt = newEleCnt * newEleByteCnt; unsigned newByteCnt = newEleCnt * newEleByteCnt;
@ -449,8 +450,14 @@ void* cmMmAllocate(
{ {
cmErrMsg(&p->err,kOkMmRC,"Breakpoint for memory allocation id:%i.",p->nextId); 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 we are tracking changes
if( cmIsFlag(p->flags,kTrackMmFl) ) if( cmIsFlag(p->flags,kTrackMmFl) )
{ {
@ -470,23 +477,27 @@ void* cmMmAllocate(
return ndp; return ndp;
} }
cmThUIntIncr(&p->nextId,1);
// initialize the new tracking recd // initialize the new tracking recd
rp->uniqueId = p->nextId;
rp->dataPtr = ndp; rp->dataPtr = ndp;
rp->dataByteCnt = newByteCnt; rp->dataByteCnt = newByteCnt;
rp->fileLine = fileLine; rp->fileLine = fileLine;
rp->fileNameStr = fileName; rp->fileNameStr = fileName;
rp->funcNameStr = funcName; rp->funcNameStr = funcName;
rp->flags = 0; rp->flags = 0;
rp->linkPtr = p->listPtr; rp->uniqueId = p->nextId;
//printf("%i %i %s %i %s\n",rp->uniqueId,newByteCnt,funcName,fileLine,fileName); cmMmRecd_t *oldp, *newp;
do
p->listPtr = rp; {
oldp = p->listPtr;
newp = rp;
rp->linkPtr = p->listPtr;
}while(!cmThPtrCAS(&p->listPtr,oldp,newp));
assert( _cmMmCheckGuards(p,rp) == kOkMmRC ); assert( _cmMmCheckGuards(p,rp) == kOkMmRC );
++p->nextId;
} }
else // a reallocation occurred. else // a reallocation occurred.
if( orgDataPtr == ndp ) if( orgDataPtr == ndp )