Return to sarray.c CVS log | Up to [ELWIX - Embedded LightWeight unIX -] / libaitio / src |
1.1.2.1 misho 1: #include "global.h"
2:
3:
4: /*
5: * io_sarrInit() - Create and initialize dynamic split-order array
6: * @numItems = Number of Items
7: * @segLen = Length of segment
8: * return: NULL error, != NULL allocated memory for array
9: */
10: inline sarr_t *
11: io_sarrInit(int numItems, int segLen)
12: {
13: sarr_t *arr = NULL;
14:
15: if (segLen < 1)
16: return NULL;
17:
18: arr = malloc(sizeof(sarr_t));
19: if (!arr) {
20: LOGERR;
21: return NULL;
22: }
23:
24: arr->sarr_num = numItems;
25: arr->sarr_seg = segLen;
1.1.2.2 misho 26: arr->sarr_siz = numItems / segLen + 1;
1.1.2.1 misho 27: arr->sarr_data = calloc(arr->sarr_siz, sizeof(sarr_seg_t));
28: if (!arr->sarr_data) {
29: LOGERR;
30: free(arr);
31: return NULL;
32: } else
33: memset(arr->sarr_data, 0, arr->sarr_siz * sizeof(sarr_seg_t));
34:
35: return arr;
36: }
37:
38: /*
39: * io_sarrDestroy() - Free all data in dynamic split-order array and Destroy array
40: * @parr = Array
41: * return: none
42: */
43: inline void
44: io_sarrDestroy(sarr_t ** __restrict parr)
45: {
46: register int i;
47:
48: assert(parr);
49: if (!parr)
50: return;
51:
52: for (i = 0; i < (*parr)->sarr_siz; i++)
53: if ((*parr)->sarr_data[i]) {
54: free((*parr)->sarr_data[i]);
55: (*parr)->sarr_data[i] = NULL;
56: }
57:
58: if ((*parr)->sarr_data)
59: free((*parr)->sarr_data);
60: free(*parr);
61: *parr = NULL;
62: }
63:
64: /*
65: * io_sarrVacuum() - Vacuum dynamic split-order array, empty segments will be freed
66: * @arr = Array
67: * return: -1 error, >-1 freed segments
68: */
69: int
70: io_sarrVacuum(sarr_t * __restrict arr)
71: {
72: register int i, j;
73: int cx = 0;
74: sarr_seg_t seg;
75:
76: assert(arr);
77: if (!arr)
78: return -1;
79:
80: for (i = 0; i < arr->sarr_siz; i++)
81: if (arr->sarr_data[i]) {
82: for (j = 0, seg = arr->sarr_data[i]; j < arr->sarr_seg; j++)
83: if (seg[j])
84: break;
85: if (j == arr->sarr_seg) {
86: free(arr->sarr_data[i]);
87: arr->sarr_data[i] = NULL;
88: cx++;
89: }
90: }
91:
92: return cx;
93: }
94:
1.1.2.2 misho 95: /*
96: * io_sarrGrow() - Grow/Shrink dynamic split-order array, Use with care when it shrink!!!
97: * @arr = Array
98: * @newNumItems = Number of Items
99: * return: -1 error, 0 ok
100: */
101: int
102: io_sarrGrow(sarr_t * __restrict arr, int newNumItems)
103: {
104: sarr_seg_t *data;
105: int seg, n = 0;
106: register int i;
107:
108: assert(arr);
109: if (!arr)
110: return -1;
111:
112: arr->sarr_num = newNumItems;
113: seg = newNumItems / arr->sarr_seg + 1;
114: if (arr->sarr_siz == seg)
115: return n;
116: if (arr->sarr_siz < seg)
117: n = seg - arr->sarr_siz;
118: else
119: for (i = seg; i < arr->sarr_siz; i++)
120: if (arr->sarr_data[i])
121: free(arr->sarr_data[i]);
122:
123: arr->sarr_siz = seg;
124: data = realloc(arr->sarr_data, arr->sarr_siz * sizeof(sarr_seg_t));
125: if (!data) {
126: LOGERR;
127: return -1;
128: } else
129: arr->sarr_data = data;
130: memset(arr->sarr_data + (arr->sarr_siz - n), 0, n * sizeof(sarr_seg_t));
131:
132: return 0;
133: }
134:
135: /*
136: * io_sarrGet() - Get element from dynamic split-order array
137: * @arr = Array
138: * @idx = Index (warning 1st element is at position 1)
139: * return: NULL not found, !=NULL element
140: */
141: inline void *
142: io_sarrGet(sarr_t * __restrict arr, u_int idx)
143: {
144: void *ret = NULL;
145: sarr_seg_t seg;
146:
147: assert(arr);
148: if (!arr || idx < 1 || arr->sarr_num < idx)
149: return ret;
150:
151: seg = arr->sarr_data[idx / arr->sarr_seg];
152: if (seg)
153: ret = seg[idx % arr->sarr_seg];
154:
155: return ret;
156: }
157:
158: /*
159: * io_sarrSet() - Set element to dynamic split-order array
160: * @arr = Array
161: * @idx = Index (warning 1st element is at position 1)
162: * @data = Value
163: * return: NULL error or empty, !=NULL old value in element
164: */
165: inline void *
166: io_sarrSet(sarr_t * __restrict arr, u_int idx, void *data)
167: {
168: void *ret = NULL;
169: sarr_seg_t seg;
170: register int pos;
171:
172: assert(arr);
173: if (!arr || idx < 1 || arr->sarr_num < idx)
174: return ret;
175:
176: seg = arr->sarr_data[idx / arr->sarr_seg];
1.1.2.3 misho 177: if (!seg) {
178: seg = calloc(arr->sarr_seg, sizeof(void*));
179: if (!seg) {
180: LOGERR;
181: return ret;
182: } else
183: memset(seg, 0, arr->sarr_seg * sizeof(void*));
1.1.2.4 ! misho 184: arr->sarr_data[idx / arr->sarr_seg] = seg;
1.1.2.2 misho 185: }
186:
1.1.2.3 misho 187: pos = idx % arr->sarr_seg;
188: ret = seg[pos];
189: seg[pos] = data;
190:
1.1.2.2 misho 191: return ret;
192: }