소스 검색

cmArray.h/c Initial commit.

master
kevin 11 년 전
부모
커밋
c7abab867e
2개의 변경된 파일187개의 추가작업 그리고 0개의 파일을 삭제
  1. 146
    0
      cmArray.c
  2. 41
    0
      cmArray.h

+ 146
- 0
cmArray.c 파일 보기

@@ -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
+

+ 41
- 0
cmArray.h 파일 보기

@@ -0,0 +1,41 @@
1
+#ifndef cmArray_h
2
+#define cmArray_h
3
+
4
+#ifdef __cplusplus
5
+extern "C" {
6
+#endif
7
+
8
+enum
9
+{
10
+  kOkArRC = cmOkRC,
11
+  kUnderflowArRC
12
+};
13
+
14
+  typedef cmRC_t     cmArRC_t;
15
+  typedef cmHandle_t cmArrayH_t;
16
+
17
+  extern cmArrayH_t cmArrayNullHandle;
18
+
19
+  cmArRC_t    cmArrayAlloc(  cmCtx_t* ctx, cmArrayH_t* hp, unsigned eleByteCnt, unsigned initCnt, unsigned expandCnt );
20
+  cmArRC_t    cmArrayRelease(cmArrayH_t* hp );
21
+  cmArRC_t    cmArrayIsValid(cmArrayH_t h );
22
+  unsigned    cmArrayCount(  cmArrayH_t h );
23
+  cmArRC_t    cmArrayClear(  cmArrayH_t h, bool releaseFl );
24
+  void*       cmArrayPush(   cmArrayH_t h, const void* data, unsigned dataEleCnt );
25
+  cmArRC_t    cmArrayPop(    cmArrayH_t h, unsigned eleCnt );
26
+  // If 'data' is NULL then array[idx:idx+dataEleCnt] is zeroed.
27
+  void*       cmArraySet(    cmArrayH_t h, unsigned index, const void* data, unsigned dataEleCnt );
28
+  const void* cmArrayGet(    cmArrayH_t h, unsigned index );
29
+
30
+
31
+
32
+#define cmArrayPtr(t,h,i)     ((t*)cmArrayGet(h,i))
33
+#define cmArrayEle(t,h,i)    (*(t*)cmArrayGet(h,i))
34
+#define cmArrayClr(t,h,i)    ((t*)cmArraySet(h,i,NULL,1))
35
+#define cmArrayClrN(t,h,i,n) ((t*)cmArraySet(h,i,NULL,n))
36
+
37
+#ifdef __cplusplus
38
+}
39
+#endif
40
+
41
+#endif

Loading…
취소
저장