1 /*******************************************************************
2 uLan Utilities Library - C library of basic reusable constructions
4 ul_gsacust.c - generic sorted arrays
6 (C) Copyright 2001 by Pavel Pisa - Originator
8 The uLan utilities library can be used, copied and modified under
10 - GPL - GNU General Public License
11 - LGPL - GNU Lesser General Public License
12 - MPL - Mozilla Public License
13 - and other licenses added by project originators
14 Code can be modified and re-distributed under any combination
15 of the above listed licenses. If contributor does not agree with
16 some of the licenses, he/she can delete appropriate line.
17 Warning, if you delete all lines, you are not allowed to
18 distribute source code and/or binaries utilizing code.
20 See files COPYING and README for details.
22 *******************************************************************/
25 #include "ul_utmalloc.h"
28 #define GSA_ALLOC_STEP 8
29 #define GSA_DEALLOC_STEP 32
32 gsa_cust_init_array_field(gsa_array_field_t *array)
41 gsa_cust_insert_at(gsa_array_field_t *array, void *item, unsigned where)
43 unsigned acnt=array->alloc_count;
44 unsigned cnt=array->count;
46 if(where>cnt) where=cnt;
47 if((cnt+1>=acnt)||!array->items)
49 if(!array->items || !acnt){
50 acnt=cnt+GSA_ALLOC_STEP;
51 items=malloc(acnt*sizeof(void*));
52 if(array->items && items)
53 memcpy(items,array->items,cnt*sizeof(void*));
55 if(acnt/4>GSA_ALLOC_STEP)
59 items=realloc(array->items,acnt*sizeof(void*));
62 array->alloc_count=acnt;
65 else items=array->items;
67 memmove(p+1,p,(char*)(items+cnt)-(char*)p);
74 gsa_cust_delete_at(gsa_array_field_t *array, unsigned indx)
76 unsigned acnt=array->alloc_count;
77 unsigned cnt=array->count;
78 void **items=array->items;
80 if(indx>=cnt) return -1;
82 p=malloc(cnt*sizeof(void*));
84 memcpy(p,items,cnt*sizeof(void*));
85 array->alloc_count=acnt=cnt;
91 memmove(p,p+1,(items+cnt-p)*sizeof(void *));
92 if(acnt-cnt>GSA_DEALLOC_STEP+GSA_ALLOC_STEP)
94 acnt-=GSA_DEALLOC_STEP;
95 items=realloc(array->items,acnt*sizeof(void*));
97 array->alloc_count=acnt;
105 gsa_cust_delete_all(gsa_array_field_t *array)
107 if(array->items && array->alloc_count)
111 array->alloc_count=0;