Annotation of libaitio/src/sarray.c, revision 1.4.2.1
1.3 misho 1: /*************************************************************************
2: * (C) 2011 AITNET ltd - Sofia/Bulgaria - <misho@aitnet.org>
3: * by Michael Pounov <misho@elwix.org>
4: *
5: * $Author: misho $
1.4.2.1 ! misho 6: * $Id: sarray.c,v 1.4 2011/08/29 12:00:57 misho Exp $
1.3 misho 7: *
8: **************************************************************************
9: The ELWIX and AITNET software is distributed under the following
10: terms:
11:
12: All of the documentation and software included in the ELWIX and AITNET
13: Releases is copyrighted by ELWIX - Sofia/Bulgaria <info@elwix.org>
14:
15: Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
16: by Michael Pounov <misho@elwix.org>. All rights reserved.
17:
18: Redistribution and use in source and binary forms, with or without
19: modification, are permitted provided that the following conditions
20: are met:
21: 1. Redistributions of source code must retain the above copyright
22: notice, this list of conditions and the following disclaimer.
23: 2. Redistributions in binary form must reproduce the above copyright
24: notice, this list of conditions and the following disclaimer in the
25: documentation and/or other materials provided with the distribution.
26: 3. All advertising materials mentioning features or use of this software
27: must display the following acknowledgement:
28: This product includes software developed by Michael Pounov <misho@elwix.org>
29: ELWIX - Embedded LightWeight unIX and its contributors.
30: 4. Neither the name of AITNET nor the names of its contributors
31: may be used to endorse or promote products derived from this software
32: without specific prior written permission.
33:
34: THIS SOFTWARE IS PROVIDED BY AITNET AND CONTRIBUTORS ``AS IS'' AND
35: ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
36: IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
37: ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
38: FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
39: DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
40: OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
41: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
42: LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
43: OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44: SUCH DAMAGE.
45: */
1.2 misho 46: #include "global.h"
47:
48:
49: /*
50: * io_sarrInit() - Create and initialize dynamic split-order array
51: * @numItems = Number of Items
52: * @segLen = Length of segment
53: * return: NULL error, != NULL allocated memory for array
54: */
55: inline sarr_t *
56: io_sarrInit(int numItems, int segLen)
57: {
58: sarr_t *arr = NULL;
59:
60: if (segLen < 1)
61: return NULL;
62:
63: arr = malloc(sizeof(sarr_t));
64: if (!arr) {
65: LOGERR;
66: return NULL;
67: }
68:
69: arr->sarr_num = numItems;
70: arr->sarr_seg = segLen;
71: arr->sarr_siz = numItems / segLen + 1;
72: arr->sarr_data = calloc(arr->sarr_siz, sizeof(sarr_seg_t));
73: if (!arr->sarr_data) {
74: LOGERR;
75: free(arr);
76: return NULL;
77: } else
78: memset(arr->sarr_data, 0, arr->sarr_siz * sizeof(sarr_seg_t));
79:
80: return arr;
81: }
82:
83: /*
84: * io_sarrDestroy() - Free all data in dynamic split-order array and Destroy array
85: * @parr = Array
86: * return: none
87: */
88: inline void
89: io_sarrDestroy(sarr_t ** __restrict parr)
90: {
91: register int i;
92:
1.4 misho 93: if (!parr || !*parr)
1.2 misho 94: return;
95:
96: for (i = 0; i < (*parr)->sarr_siz; i++)
97: if ((*parr)->sarr_data[i]) {
98: free((*parr)->sarr_data[i]);
99: (*parr)->sarr_data[i] = NULL;
100: }
101:
102: if ((*parr)->sarr_data)
103: free((*parr)->sarr_data);
104: free(*parr);
105: *parr = NULL;
106: }
107:
108: /*
109: * io_sarrVacuum() - Vacuum dynamic split-order array, empty segments will be freed
110: * @arr = Array
111: * return: -1 error, >-1 freed segments
112: */
113: int
114: io_sarrVacuum(sarr_t * __restrict arr)
115: {
116: register int i, j;
117: int cx = 0;
118: sarr_seg_t seg;
119:
120: assert(arr);
121: if (!arr)
122: return -1;
123:
124: for (i = 0; i < arr->sarr_siz; i++)
125: if (arr->sarr_data[i]) {
126: for (j = 0, seg = arr->sarr_data[i]; j < arr->sarr_seg; j++)
127: if (seg[j])
128: break;
129: if (j == arr->sarr_seg) {
130: free(arr->sarr_data[i]);
131: arr->sarr_data[i] = NULL;
132: cx++;
133: }
134: }
135:
136: return cx;
137: }
138:
139: /*
140: * io_sarrGrow() - Grow/Shrink dynamic split-order array, Use with care when it shrink!!!
141: * @arr = Array
142: * @newNumItems = Number of Items
143: * return: -1 error, 0 ok
144: */
145: int
146: io_sarrGrow(sarr_t * __restrict arr, int newNumItems)
147: {
148: sarr_seg_t *data;
149: int seg, n = 0;
150: register int i;
151:
152: assert(arr);
153: if (!arr)
154: return -1;
155:
156: arr->sarr_num = newNumItems;
157: seg = newNumItems / arr->sarr_seg + 1;
158: if (arr->sarr_siz == seg)
159: return n;
160: if (arr->sarr_siz < seg)
161: n = seg - arr->sarr_siz;
162: else
163: for (i = seg; i < arr->sarr_siz; i++)
164: if (arr->sarr_data[i])
165: free(arr->sarr_data[i]);
166:
167: arr->sarr_siz = seg;
168: data = realloc(arr->sarr_data, arr->sarr_siz * sizeof(sarr_seg_t));
169: if (!data) {
170: LOGERR;
171: return -1;
172: } else
173: arr->sarr_data = data;
174: memset(arr->sarr_data + (arr->sarr_siz - n), 0, n * sizeof(sarr_seg_t));
175:
176: return 0;
177: }
178:
179: /*
180: * io_sarrGet() - Get element from dynamic split-order array
181: * @arr = Array
182: * @idx = Index (warning 1st element is at position 1)
183: * return: NULL not found, !=NULL element
184: */
185: inline void *
186: io_sarrGet(sarr_t * __restrict arr, u_int idx)
187: {
188: void *ret = NULL;
189: sarr_seg_t seg;
190:
191: assert(arr);
192: if (!arr || idx < 1 || arr->sarr_num < idx)
193: return ret;
194:
195: seg = arr->sarr_data[idx / arr->sarr_seg];
196: if (seg)
197: ret = seg[idx % arr->sarr_seg];
198:
199: return ret;
200: }
201:
202: /*
203: * io_sarrGet2() - Always get element from dynamic split-order array
204: * Function automatic grow array. Good use for Hash tables!
205: * @arr = Array
206: * @idx = Index (warning 1st element is at position 1)
207: * return: NULL not found, !=NULL element
208: */
209: void *
210: io_sarrGet2(sarr_t * __restrict arr, u_int idx)
211: {
212: assert(arr);
213: if (!arr || idx < 1)
214: return NULL;
215: if (arr->sarr_num < idx)
216: if (io_sarrGrow(arr, idx))
217: return NULL;
218: return io_sarrGet(arr, idx);
219: }
220:
221: /*
222: * io_sarrSet() - Set element to dynamic split-order array
223: * @arr = Array
224: * @idx = Index (warning 1st element is at position 1)
225: * @data = Value
226: * return: NULL error or empty, !=NULL old value in element
227: */
228: inline void *
229: io_sarrSet(sarr_t * __restrict arr, u_int idx, void *data)
230: {
231: void *ret = NULL;
232: sarr_seg_t seg;
233: register int pos;
234:
235: assert(arr);
236: if (!arr || idx < 1 || arr->sarr_num < idx)
237: return ret;
238:
239: seg = arr->sarr_data[idx / arr->sarr_seg];
240: if (!seg) {
241: seg = calloc(arr->sarr_seg, sizeof(void*));
242: if (!seg) {
243: LOGERR;
244: return ret;
245: } else
246: memset(seg, 0, arr->sarr_seg * sizeof(void*));
247: arr->sarr_data[idx / arr->sarr_seg] = seg;
248: }
249:
250: pos = idx % arr->sarr_seg;
251: ret = seg[pos];
252: seg[pos] = data;
253:
254: return ret;
255: }
1.4 misho 256:
257: /*
258: * io_sarr2array() - Convert from split-order array to dynamic array
259: * @sa = split array
260: * @sarrFree = after convert split array !=0 will be destroyed sarray
261: * return: NULL error or != NULL new array
262: */
263: array_t *
264: io_sarr2array(sarr_t ** __restrict sa, int sarrFree)
265: {
266: array_t *arr = NULL;
267: int el;
268: register int i;
269:
270: assert(sa && *sa);
271: if (!sa || !*sa)
272: return NULL;
273:
274: el = io_sarrSize(*sa);
275: arr = io_arrayInit(el);
276: if (!arr)
277: return NULL;
278:
279: for (i = 0; i < el; i++)
280: io_arraySet(arr, i, io_sarrGet(*sa, i + 1));
281:
282: if (sarrFree) {
283: free(*sa);
284: *sa = NULL;
285: }
286: return arr;
287: }
288:
289: /*
290: * io_array2sarr() - Convert from dynamic array to split-order array
291: * @a = array
292: * @segLen = Length of segment
293: * @arrFree = after convert array !=0 will be destroyed
294: * return: NULL error or != NULL new sarr
295: */
296: sarr_t *
297: io_array2sarr(array_t ** __restrict a, int segLen, int arrFree)
298: {
299: sarr_t *sa = NULL;
300: int el;
301: register int i;
302:
303: assert(a && *a);
304: if (!a || !*a)
305: return NULL;
306:
307: el = io_arraySize(*a);
308: sa = io_sarrInit(el, segLen);
309: if (!sa)
310: return NULL;
311:
312: for (i = 0; i < el; i++)
313: io_sarrSet(sa, i + 1, io_arrayGet(*a, i));
314:
315: if (arrFree) {
316: free(*a);
317: *a = NULL;
318: }
319: return sa;
320: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>