Install json_object_private.h file
[platform/upstream/json-c.git] / arraylist.c
1 /*
2  * $Id: arraylist.c,v 1.4 2006/01/26 02:16:28 mclark Exp $
3  *
4  * Copyright (c) 2004, 2005 Metaparadigm Pte. Ltd.
5  * Michael Clark <michael@metaparadigm.com>
6  *
7  * This library is free software; you can redistribute it and/or modify
8  * it under the terms of the MIT license. See COPYING for details.
9  *
10  */
11
12 #include "config.h"
13
14 #include <limits.h>
15
16 #ifdef STDC_HEADERS
17 # include <stdlib.h>
18 # include <string.h>
19 #endif /* STDC_HEADERS */
20
21 #if defined(HAVE_STRINGS_H) && !defined(_STRING_H) && !defined(__USE_BSD)
22 # include <strings.h>
23 #endif /* HAVE_STRINGS_H */
24
25 #ifndef SIZE_T_MAX
26 #if SIZEOF_SIZE_T == SIZEOF_INT
27 #define SIZE_T_MAX UINT_MAX
28 #elif SIZEOF_SIZE_T == SIZEOF_LONG
29 #define SIZE_T_MAX ULONG_MAX
30 #elif SIZEOF_SIZE_T == SIZEOF_LONG_LONG
31 #define SIZE_T_MAX ULLONG_MAX
32 #else
33 #error Unable to determine size of size_t
34 #endif
35 #endif
36
37 #include "arraylist.h"
38
39 struct array_list*
40 array_list_new(array_list_free_fn *free_fn)
41 {
42   struct array_list *arr;
43
44   arr = (struct array_list*)calloc(1, sizeof(struct array_list));
45   if(!arr) return NULL;
46   arr->size = ARRAY_LIST_DEFAULT_SIZE;
47   arr->length = 0;
48   arr->free_fn = free_fn;
49   if(!(arr->array = (void**)calloc(sizeof(void*), arr->size))) {
50     free(arr);
51     return NULL;
52   }
53   return arr;
54 }
55
56 extern void
57 array_list_free(struct array_list *arr)
58 {
59   size_t i;
60   for(i = 0; i < arr->length; i++)
61     if(arr->array[i]) arr->free_fn(arr->array[i]);
62   free(arr->array);
63   free(arr);
64 }
65
66 void*
67 array_list_get_idx(struct array_list *arr, size_t i)
68 {
69   if(i >= arr->length) return NULL;
70   return arr->array[i];
71 }
72
73 static int array_list_expand_internal(struct array_list *arr, size_t max)
74 {
75   void *t;
76   size_t new_size;
77
78   if(max < arr->size) return 0;
79   /* Avoid undefined behaviour on size_t overflow */
80   if( arr->size >= SIZE_T_MAX / 2 )
81     new_size = max;
82   else
83   {
84     new_size = arr->size << 1;
85     if (new_size < max)
86       new_size = max;
87   }
88   if (new_size > (~((size_t)0)) / sizeof(void*)) return -1;
89   if (!(t = realloc(arr->array, new_size*sizeof(void*)))) return -1;
90   arr->array = (void**)t;
91   (void)memset(arr->array + arr->size, 0, (new_size-arr->size)*sizeof(void*));
92   arr->size = new_size;
93   return 0;
94 }
95
96 int
97 array_list_put_idx(struct array_list *arr, size_t idx, void *data)
98 {
99   if (idx > SIZE_T_MAX - 1 ) return -1;
100   if(array_list_expand_internal(arr, idx+1)) return -1;
101   if(idx < arr->length && arr->array[idx])
102     arr->free_fn(arr->array[idx]);
103   arr->array[idx] = data;
104   if(arr->length <= idx) arr->length = idx + 1;
105   return 0;
106 }
107
108 int
109 array_list_add(struct array_list *arr, void *data)
110 {
111   return array_list_put_idx(arr, arr->length, data);
112 }
113
114 void
115 array_list_sort(struct array_list *arr, int(*sort_fn)(const void *, const void *))
116 {
117   qsort(arr->array, arr->length, sizeof(arr->array[0]), sort_fn);
118 }
119
120 void* array_list_bsearch(const void **key, struct array_list *arr,
121                 int (*sort_fn)(const void *, const void *))
122 {
123         return bsearch(key, arr->array, arr->length, sizeof(arr->array[0]),
124                         sort_fn);
125 }
126
127 size_t
128 array_list_length(struct array_list *arr)
129 {
130   return arr->length;
131 }
132
133 int
134 array_list_del_idx( struct array_list *arr, size_t idx, size_t count )
135 {
136         size_t i, stop;
137
138         stop = idx + count;
139         if ( idx >= arr->length || stop > arr->length ) return -1;
140         for ( i = idx; i < stop; ++i ) {
141                 if ( arr->array[i] ) arr->free_fn( arr->array[i] );
142         }
143         memmove( arr->array + idx, arr->array + stop, (arr->length - stop) * sizeof(void*) );
144         arr->length -= count;
145         return 0;
146 }