|
@@ -0,0 +1,146 @@
|
|
1
|
+#include "cmGlobal.h"
|
|
2
|
+#include "cmRpt.h"
|
|
3
|
+#include "cmErr.h"
|
|
4
|
+#include "cmCtx.h"
|
|
5
|
+#include "cmMem.h"
|
|
6
|
+#include "cmMallocDebug.h"
|
|
7
|
+#include "cmArray.h"
|
|
8
|
+
|
|
9
|
+typedef struct
|
|
10
|
+{
|
|
11
|
+ cmErr_t err;
|
|
12
|
+ char* base;
|
|
13
|
+ unsigned expand_cnt;
|
|
14
|
+ unsigned alloc_cnt;
|
|
15
|
+ unsigned cur_cnt;
|
|
16
|
+ unsigned ele_byte_cnt;
|
|
17
|
+} cmAr_t;
|
|
18
|
+
|
|
19
|
+cmArrayH_t cmArrayNullHandle = cmSTATIC_NULL_HANDLE;
|
|
20
|
+
|
|
21
|
+cmAr_t* _cmArHandleToPtr( cmArrayH_t h )
|
|
22
|
+{
|
|
23
|
+ cmAr_t* p = (cmAr_t*)h.h;
|
|
24
|
+ assert(p!=NULL);
|
|
25
|
+ return p;
|
|
26
|
+}
|
|
27
|
+
|
|
28
|
+cmArRC_t _cmArFree( cmAr_t* p )
|
|
29
|
+{
|
|
30
|
+ cmArRC_t rc = kOkArRC;
|
|
31
|
+ cmMemFree(p);
|
|
32
|
+ return rc;
|
|
33
|
+}
|
|
34
|
+
|
|
35
|
+cmArRC_t cmArrayAlloc( cmCtx_t* ctx, cmArrayH_t* hp, unsigned initCnt, unsigned expandCnt, unsigned eleByteCnt )
|
|
36
|
+{
|
|
37
|
+ cmArRC_t rc;
|
|
38
|
+ if((rc = cmArrayRelease(hp)) != kOkArRC )
|
|
39
|
+ return rc;
|
|
40
|
+
|
|
41
|
+ cmAr_t* p = cmMemAllocZ(cmAr_t,1);
|
|
42
|
+ cmErrSetup(&p->err,&ctx->rpt,"Array");
|
|
43
|
+
|
|
44
|
+ p->alloc_cnt = initCnt * eleByteCnt;
|
|
45
|
+ p->expand_cnt = expandCnt;
|
|
46
|
+ p->cur_cnt = 0;
|
|
47
|
+ p->ele_byte_cnt = eleByteCnt;
|
|
48
|
+
|
|
49
|
+ if( p->alloc_cnt > 0 )
|
|
50
|
+ p->base = cmMemAllocZ(char,p->alloc_cnt);
|
|
51
|
+
|
|
52
|
+ hp->h = p;
|
|
53
|
+
|
|
54
|
+ return rc;
|
|
55
|
+}
|
|
56
|
+
|
|
57
|
+cmArRC_t cmArrayRelease( cmArrayH_t* hp )
|
|
58
|
+{
|
|
59
|
+ cmArRC_t rc = kOkArRC;
|
|
60
|
+
|
|
61
|
+ if(hp==NULL || cmArrayIsValid(*hp)==false )
|
|
62
|
+ return rc;
|
|
63
|
+
|
|
64
|
+ cmAr_t* p = _cmArHandleToPtr(*hp);
|
|
65
|
+
|
|
66
|
+ if((rc = _cmArFree(p)) != kOkArRC )
|
|
67
|
+ return rc;
|
|
68
|
+
|
|
69
|
+ hp->h = NULL;
|
|
70
|
+
|
|
71
|
+ return rc;
|
|
72
|
+}
|
|
73
|
+
|
|
74
|
+cmArRC_t cmArrayIsValid(cmArrayH_t h )
|
|
75
|
+{ return h.h != NULL; }
|
|
76
|
+
|
|
77
|
+unsigned cmArrayCount( cmArrayH_t h )
|
|
78
|
+{
|
|
79
|
+ cmAr_t* p = _cmArHandleToPtr(h);
|
|
80
|
+ return p->cur_cnt;
|
|
81
|
+}
|
|
82
|
+
|
|
83
|
+cmArRC_t cmArrayClear( cmArrayH_t h, bool releaseFl )
|
|
84
|
+{
|
|
85
|
+ cmAr_t* p = _cmArHandleToPtr(h);
|
|
86
|
+ if( releaseFl )
|
|
87
|
+ {
|
|
88
|
+ cmMemPtrFree(&p->base);
|
|
89
|
+ p->alloc_cnt = 0;
|
|
90
|
+ }
|
|
91
|
+
|
|
92
|
+ p->cur_cnt = 0;
|
|
93
|
+ return kOkArRC;
|
|
94
|
+}
|
|
95
|
+
|
|
96
|
+void* _cmArraySet( cmAr_t* p, unsigned idx, const void* data, unsigned dataEleCnt )
|
|
97
|
+{
|
|
98
|
+ if( idx+dataEleCnt > p->alloc_cnt )
|
|
99
|
+ {
|
|
100
|
+ unsigned add_cnt = (idx + dataEleCnt) - p->alloc_cnt;
|
|
101
|
+
|
|
102
|
+ p->alloc_cnt += ((add_cnt / p->expand_cnt) + 1) * p->expand_cnt;
|
|
103
|
+
|
|
104
|
+ p->base = cmMemResizePZ(char,p->base,p->alloc_cnt);
|
|
105
|
+ }
|
|
106
|
+
|
|
107
|
+ char* bp = p->base + (idx*p->ele_byte_cnt);
|
|
108
|
+
|
|
109
|
+ if( data == NULL )
|
|
110
|
+ memset(bp, 0, p->ele_byte_cnt * dataEleCnt );
|
|
111
|
+ else
|
|
112
|
+ memcpy(bp, data, p->ele_byte_cnt * dataEleCnt );
|
|
113
|
+
|
|
114
|
+ if( idx+dataEleCnt > p->cur_cnt )
|
|
115
|
+ p->cur_cnt = idx + dataEleCnt;
|
|
116
|
+
|
|
117
|
+ return bp;
|
|
118
|
+}
|
|
119
|
+
|
|
120
|
+void* cmArrayPush( cmArrayH_t h, const void* data, unsigned dataEleCnt )
|
|
121
|
+{
|
|
122
|
+ cmAr_t* p = _cmArHandleToPtr(h);
|
|
123
|
+ return _cmArraySet(p,p->cur_cnt,data,dataEleCnt);
|
|
124
|
+}
|
|
125
|
+
|
|
126
|
+cmArRC_t cmArrayPop( cmArrayH_t h, unsigned eleCnt )
|
|
127
|
+{
|
|
128
|
+ cmAr_t* p = _cmArHandleToPtr(h);
|
|
129
|
+ if( eleCnt > p->cur_cnt )
|
|
130
|
+ return cmErrMsg(&p->err,kUnderflowArRC,"Cannot pop %i element(s). Array contains %i element(s).",eleCnt,p->cur_cnt);
|
|
131
|
+ p->cur_cnt -= eleCnt;
|
|
132
|
+ return kOkArRC;
|
|
133
|
+}
|
|
134
|
+
|
|
135
|
+void* cmArraySet( cmArrayH_t h, unsigned index, const void* data, unsigned dataEleCnt )
|
|
136
|
+{
|
|
137
|
+ cmAr_t* p = _cmArHandleToPtr(h);
|
|
138
|
+ return _cmArraySet(p,index,data,dataEleCnt );
|
|
139
|
+}
|
|
140
|
+
|
|
141
|
+const void* cmArrayGet( cmArrayH_t h, unsigned index )
|
|
142
|
+{
|
|
143
|
+ cmAr_t* p = _cmArHandleToPtr(h);
|
|
144
|
+ return p->base + (index * p->ele_byte_cnt);
|
|
145
|
+}
|
|
146
|
+
|