]> rtime.felk.cvut.cz Git - ulut.git/blob - ulut/ul_gsacust.c
Fix compilation on GCC 4.7 for ARM ABI.
[ulut.git] / ulut / ul_gsacust.c
1 /*******************************************************************
2   uLan Utilities Library - C library of basic reusable constructions
3
4   ul_gsacust.c  - generic sorted arrays
5
6   (C) Copyright 2001 by Pavel Pisa - Originator
7
8   The uLan utilities library can be used, copied and modified under
9   next licenses
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.
19   
20   See files COPYING and README for details.
21
22  *******************************************************************/
23
24 #include <string.h>
25 #include "ul_utmalloc.h"
26 #include "ul_gsa.h"
27
28 #define GSA_ALLOC_STEP 8
29 #define GSA_DEALLOC_STEP 32
30
31 void
32 gsa_cust_init_array_field(gsa_array_field_t *array)
33 {
34   array->count=0;
35   array->alloc_count=0;
36   array->items=NULL;
37 }
38
39
40 int
41 gsa_cust_insert_at(gsa_array_field_t *array, void *item, unsigned where)
42 {
43   unsigned acnt=array->alloc_count;
44   unsigned cnt=array->count;
45   void **items, **p;
46   if(where>cnt) where=cnt;
47   if((cnt+1>=acnt)||!array->items)
48   {
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*));
54     }else{
55       if(acnt/4>GSA_ALLOC_STEP)
56         acnt+=acnt/4;
57       else
58         acnt+=GSA_ALLOC_STEP;
59       items=realloc(array->items,acnt*sizeof(void*));
60     }
61     if(!items) return -1;
62     array->alloc_count=acnt;
63     array->items=items;
64   }
65   else items=array->items;
66   p=items+where;
67   memmove(p+1,p,(char*)(items+cnt)-(char*)p);
68   array->count=cnt+1;
69   *p=item;
70   return 0;
71 }
72
73 int
74 gsa_cust_delete_at(gsa_array_field_t *array, unsigned indx)
75 {
76   unsigned acnt=array->alloc_count;
77   unsigned cnt=array->count;
78   void **items=array->items;
79   void **p;
80   if(indx>=cnt) return -1;
81   if(cnt && !acnt){
82     p=malloc(cnt*sizeof(void*));
83     if(p){
84       memcpy(p,items,cnt*sizeof(void*));
85       array->alloc_count=acnt=cnt;
86       array->items=items=p;
87     }
88   }
89   p=items+indx;
90   array->count=--cnt;
91   memmove(p,p+1,(items+cnt-p)*sizeof(void *));
92   if(acnt-cnt>GSA_DEALLOC_STEP+GSA_ALLOC_STEP)
93   {
94     acnt-=GSA_DEALLOC_STEP;
95     items=realloc(array->items,acnt*sizeof(void*));
96     if(items){
97       array->alloc_count=acnt;
98       array->items=items;
99     }
100   }
101   return 0;
102 }
103
104 void
105 gsa_cust_delete_all(gsa_array_field_t *array)
106 {
107   if(array->items && array->alloc_count)
108     free(array->items);
109   array->items=NULL;
110   array->count=0;
111   array->alloc_count=0;
112 }