Browse Source

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

master
kevin 11 years ago
parent
commit
620af4450b
2 changed files with 35 additions and 5 deletions
  1. 28
    4
      cmArray.c
  2. 7
    1
      cmArray.h

+ 28
- 4
cmArray.c View File

@@ -28,11 +28,14 @@ cmAr_t* _cmArHandleToPtr( cmArrayH_t h )
28 28
 cmArRC_t _cmArFree( cmAr_t* p )
29 29
 {
30 30
   cmArRC_t rc = kOkArRC;
31
+  cmMemFree(p->base);
31 32
   cmMemFree(p);
32 33
   return rc;
33 34
 }
34 35
 
35
-cmArRC_t    cmArrayAlloc(  cmCtx_t* ctx, cmArrayH_t* hp, unsigned initCnt, unsigned expandCnt, unsigned eleByteCnt )
36
+
37
+
38
+cmArRC_t    cmArrayAlloc0(  cmCtx_t* ctx, cmArrayH_t* hp, unsigned eleByteCnt, unsigned initCnt, unsigned expandCnt )
36 39
 {
37 40
   cmArRC_t rc;
38 41
   if((rc = cmArrayRelease(hp)) != kOkArRC )
@@ -54,6 +57,10 @@ cmArRC_t    cmArrayAlloc(  cmCtx_t* ctx, cmArrayH_t* hp, unsigned initCnt, unsig
54 57
   return rc;
55 58
 }
56 59
 
60
+cmArRC_t    cmArrayAlloc(  cmCtx_t* ctx, cmArrayH_t* hp, unsigned eleByteCnt )
61
+{ return cmArrayAlloc0(ctx,hp,eleByteCnt,10,10); }
62
+
63
+
57 64
 cmArRC_t    cmArrayRelease(   cmArrayH_t* hp )
58 65
 {
59 66
   cmArRC_t rc = kOkArRC;
@@ -74,6 +81,18 @@ cmArRC_t    cmArrayRelease(   cmArrayH_t* hp )
74 81
 cmArRC_t    cmArrayIsValid(cmArrayH_t h )
75 82
 {  return h.h != NULL; }
76 83
 
84
+void    cmArraySetExpandCount( cmArrayH_t h, unsigned expandCnt )
85
+{
86
+  cmAr_t* p = _cmArHandleToPtr(h);
87
+  p->expand_cnt = expandCnt;
88
+}
89
+
90
+unsigned    cmArrayExpandCount( cmArrayH_t h )
91
+{
92
+  cmAr_t* p = _cmArHandleToPtr(h);
93
+  return p->expand_cnt;
94
+}
95
+
77 96
 unsigned    cmArrayCount(  cmArrayH_t h )
78 97
 {
79 98
   cmAr_t* p = _cmArHandleToPtr(h);
@@ -99,15 +118,20 @@ void*    _cmArraySet( cmAr_t* p, unsigned idx, const void* data, unsigned dataEl
99 118
   {
100 119
     unsigned add_cnt = (idx + dataEleCnt) - p->alloc_cnt;
101 120
 
102
-    p->alloc_cnt += ((add_cnt / p->expand_cnt) + 1) * p->expand_cnt;
121
+    if( add_cnt < p->expand_cnt )
122
+      add_cnt = p->expand_cnt;
123
+    else
124
+      add_cnt = ((add_cnt / p->expand_cnt) + 1) * p->expand_cnt;
125
+
126
+    p->alloc_cnt += add_cnt;
103 127
 
104
-    p->base = cmMemResizePZ(char,p->base,p->alloc_cnt);    
128
+    p->base = cmMemResizePZ(char,p->base,p->alloc_cnt*p->ele_byte_cnt);    
105 129
   }
106 130
 
107 131
   char* bp = p->base + (idx*p->ele_byte_cnt);
108 132
 
109 133
   if( data == NULL )
110
-    memset(bp, 0,  p->ele_byte_cnt * dataEleCnt );
134
+    memset(bp, 0,    p->ele_byte_cnt * dataEleCnt );
111 135
   else
112 136
     memcpy(bp, data, p->ele_byte_cnt * dataEleCnt );
113 137
     

+ 7
- 1
cmArray.h View File

@@ -16,9 +16,14 @@ enum
16 16
 
17 17
   extern cmArrayH_t cmArrayNullHandle;
18 18
 
19
-  cmArRC_t    cmArrayAlloc(  cmCtx_t* ctx, cmArrayH_t* hp, unsigned eleByteCnt, unsigned initCnt, unsigned expandCnt );
19
+  cmArRC_t    cmArrayAlloc0( cmCtx_t* ctx, cmArrayH_t* hp, unsigned eleByteCnt, unsigned initCnt, unsigned expandCnt );
20
+
21
+  // Defaults initCnt and expandCnt to 10.
22
+  cmArRC_t    cmArrayAlloc(    cmCtx_t* ctx, cmArrayH_t* hp, unsigned eleByteCnt );
20 23
   cmArRC_t    cmArrayRelease(cmArrayH_t* hp );
21 24
   cmArRC_t    cmArrayIsValid(cmArrayH_t h );
25
+  void        cmArraySetExpandCount( cmArrayH_t h, unsigned expandCnt );
26
+  unsigned    cmArrayExpandCount( cmArrayH_t h );
22 27
   unsigned    cmArrayCount(  cmArrayH_t h );
23 28
   cmArRC_t    cmArrayClear(  cmArrayH_t h, bool releaseFl );
24 29
   void*       cmArrayPush(   cmArrayH_t h, const void* data, unsigned dataEleCnt );
@@ -30,6 +35,7 @@ enum
30 35
 
31 36
 
32 37
 #define cmArrayPtr(t,h,i)     ((t*)cmArrayGet(h,i))
38
+#define cmArrayBase(t,h)     ((t*)cmArrayGet(h,0))
33 39
 #define cmArrayEle(t,h,i)    (*(t*)cmArrayGet(h,i))
34 40
 #define cmArrayClr(t,h,i)    ((t*)cmArraySet(h,i,NULL,1))
35 41
 #define cmArrayClrN(t,h,i,n) ((t*)cmArraySet(h,i,NULL,n))

Loading…
Cancel
Save