From 620af4450b235aac5390a9e997e06ea8ee0be7c9 Mon Sep 17 00:00:00 2001 From: kevin Date: Thu, 21 Mar 2013 11:04:40 -0700 Subject: [PATCH] cmArray.h/c: Many changes to complete initial development. --- cmArray.c | 32 ++++++++++++++++++++++++++++---- cmArray.h | 8 +++++++- 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/cmArray.c b/cmArray.c index 871c707..32ff6b5 100644 --- a/cmArray.c +++ b/cmArray.c @@ -28,11 +28,14 @@ cmAr_t* _cmArHandleToPtr( cmArrayH_t h ) cmArRC_t _cmArFree( cmAr_t* p ) { cmArRC_t rc = kOkArRC; + cmMemFree(p->base); cmMemFree(p); 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; if((rc = cmArrayRelease(hp)) != kOkArRC ) @@ -54,6 +57,10 @@ cmArRC_t cmArrayAlloc( cmCtx_t* ctx, cmArrayH_t* hp, unsigned initCnt, unsig 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 rc = kOkArRC; @@ -74,6 +81,18 @@ cmArRC_t cmArrayRelease( cmArrayH_t* hp ) cmArRC_t cmArrayIsValid(cmArrayH_t h ) { 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 ) { 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; - 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); if( data == NULL ) - memset(bp, 0, p->ele_byte_cnt * dataEleCnt ); + memset(bp, 0, p->ele_byte_cnt * dataEleCnt ); else memcpy(bp, data, p->ele_byte_cnt * dataEleCnt ); diff --git a/cmArray.h b/cmArray.h index 17fffe1..fe2d07c 100644 --- a/cmArray.h +++ b/cmArray.h @@ -16,9 +16,14 @@ enum 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 cmArrayIsValid(cmArrayH_t h ); + void cmArraySetExpandCount( cmArrayH_t h, unsigned expandCnt ); + unsigned cmArrayExpandCount( cmArrayH_t h ); unsigned cmArrayCount( cmArrayH_t h ); cmArRC_t cmArrayClear( cmArrayH_t h, bool releaseFl ); 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 cmArrayBase(t,h) ((t*)cmArrayGet(h,0)) #define cmArrayEle(t,h,i) (*(t*)cmArrayGet(h,i)) #define cmArrayClr(t,h,i) ((t*)cmArraySet(h,i,NULL,1)) #define cmArrayClrN(t,h,i,n) ((t*)cmArraySet(h,i,NULL,n))