cmArray.h/c: Many changes to complete initial development.

This commit is contained in:
kevin 2013-03-21 11:04:40 -07:00
parent d085271de4
commit 620af4450b
2 changed files with 35 additions and 5 deletions

View File

@ -28,11 +28,14 @@ cmAr_t* _cmArHandleToPtr( cmArrayH_t h )
cmArRC_t _cmArFree( cmAr_t* p ) cmArRC_t _cmArFree( cmAr_t* p )
{ {
cmArRC_t rc = kOkArRC; cmArRC_t rc = kOkArRC;
cmMemFree(p->base);
cmMemFree(p); cmMemFree(p);
return rc; return rc;
} }
cmArRC_t cmArrayAlloc( cmCtx_t* ctx, cmArrayH_t* hp, unsigned initCnt, unsigned expandCnt, unsigned eleByteCnt )
cmArRC_t cmArrayAlloc0( cmCtx_t* ctx, cmArrayH_t* hp, unsigned eleByteCnt, unsigned initCnt, unsigned expandCnt )
{ {
cmArRC_t rc; cmArRC_t rc;
if((rc = cmArrayRelease(hp)) != kOkArRC ) if((rc = cmArrayRelease(hp)) != kOkArRC )
@ -54,6 +57,10 @@ cmArRC_t cmArrayAlloc( cmCtx_t* ctx, cmArrayH_t* hp, unsigned initCnt, unsig
return rc; return rc;
} }
cmArRC_t cmArrayAlloc( cmCtx_t* ctx, cmArrayH_t* hp, unsigned eleByteCnt )
{ return cmArrayAlloc0(ctx,hp,eleByteCnt,10,10); }
cmArRC_t cmArrayRelease( cmArrayH_t* hp ) cmArRC_t cmArrayRelease( cmArrayH_t* hp )
{ {
cmArRC_t rc = kOkArRC; cmArRC_t rc = kOkArRC;
@ -74,6 +81,18 @@ cmArRC_t cmArrayRelease( cmArrayH_t* hp )
cmArRC_t cmArrayIsValid(cmArrayH_t h ) cmArRC_t cmArrayIsValid(cmArrayH_t h )
{ return h.h != NULL; } { return h.h != NULL; }
void cmArraySetExpandCount( cmArrayH_t h, unsigned expandCnt )
{
cmAr_t* p = _cmArHandleToPtr(h);
p->expand_cnt = expandCnt;
}
unsigned cmArrayExpandCount( cmArrayH_t h )
{
cmAr_t* p = _cmArHandleToPtr(h);
return p->expand_cnt;
}
unsigned cmArrayCount( cmArrayH_t h ) unsigned cmArrayCount( cmArrayH_t h )
{ {
cmAr_t* p = _cmArHandleToPtr(h); cmAr_t* p = _cmArHandleToPtr(h);
@ -99,15 +118,20 @@ void* _cmArraySet( cmAr_t* p, unsigned idx, const void* data, unsigned dataEl
{ {
unsigned add_cnt = (idx + dataEleCnt) - p->alloc_cnt; unsigned add_cnt = (idx + dataEleCnt) - p->alloc_cnt;
p->alloc_cnt += ((add_cnt / p->expand_cnt) + 1) * p->expand_cnt; if( add_cnt < p->expand_cnt )
add_cnt = p->expand_cnt;
else
add_cnt = ((add_cnt / p->expand_cnt) + 1) * p->expand_cnt;
p->base = cmMemResizePZ(char,p->base,p->alloc_cnt); p->alloc_cnt += add_cnt;
p->base = cmMemResizePZ(char,p->base,p->alloc_cnt*p->ele_byte_cnt);
} }
char* bp = p->base + (idx*p->ele_byte_cnt); char* bp = p->base + (idx*p->ele_byte_cnt);
if( data == NULL ) if( data == NULL )
memset(bp, 0, p->ele_byte_cnt * dataEleCnt ); memset(bp, 0, p->ele_byte_cnt * dataEleCnt );
else else
memcpy(bp, data, p->ele_byte_cnt * dataEleCnt ); memcpy(bp, data, p->ele_byte_cnt * dataEleCnt );

View File

@ -16,9 +16,14 @@ enum
extern cmArrayH_t cmArrayNullHandle; extern cmArrayH_t cmArrayNullHandle;
cmArRC_t cmArrayAlloc( cmCtx_t* ctx, cmArrayH_t* hp, unsigned eleByteCnt, unsigned initCnt, unsigned expandCnt ); cmArRC_t cmArrayAlloc0( cmCtx_t* ctx, cmArrayH_t* hp, unsigned eleByteCnt, unsigned initCnt, unsigned expandCnt );
// Defaults initCnt and expandCnt to 10.
cmArRC_t cmArrayAlloc( cmCtx_t* ctx, cmArrayH_t* hp, unsigned eleByteCnt );
cmArRC_t cmArrayRelease(cmArrayH_t* hp ); cmArRC_t cmArrayRelease(cmArrayH_t* hp );
cmArRC_t cmArrayIsValid(cmArrayH_t h ); cmArRC_t cmArrayIsValid(cmArrayH_t h );
void cmArraySetExpandCount( cmArrayH_t h, unsigned expandCnt );
unsigned cmArrayExpandCount( cmArrayH_t h );
unsigned cmArrayCount( cmArrayH_t h ); unsigned cmArrayCount( cmArrayH_t h );
cmArRC_t cmArrayClear( cmArrayH_t h, bool releaseFl ); cmArRC_t cmArrayClear( cmArrayH_t h, bool releaseFl );
void* cmArrayPush( cmArrayH_t h, const void* data, unsigned dataEleCnt ); void* cmArrayPush( cmArrayH_t h, const void* data, unsigned dataEleCnt );
@ -30,6 +35,7 @@ enum
#define cmArrayPtr(t,h,i) ((t*)cmArrayGet(h,i)) #define cmArrayPtr(t,h,i) ((t*)cmArrayGet(h,i))
#define cmArrayBase(t,h) ((t*)cmArrayGet(h,0))
#define cmArrayEle(t,h,i) (*(t*)cmArrayGet(h,i)) #define cmArrayEle(t,h,i) (*(t*)cmArrayGet(h,i))
#define cmArrayClr(t,h,i) ((t*)cmArraySet(h,i,NULL,1)) #define cmArrayClr(t,h,i) ((t*)cmArraySet(h,i,NULL,1))
#define cmArrayClrN(t,h,i,n) ((t*)cmArraySet(h,i,NULL,n)) #define cmArrayClrN(t,h,i,n) ((t*)cmArraySet(h,i,NULL,n))