-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy patharray.c
268 lines (244 loc) · 6.28 KB
/
array.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/*
* soliloque-server, an open source implementation of the TeamSpeak protocol.
* Copyright (C) 2009 Hugo Camboulive <hugo.camboulive AT gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <assert.h>
#include <strings.h>
#include <math.h>
#include <limits.h>
#include <errno.h>
#include "array.h"
#include "log.h"
#include "compat.h"
/**
* Find the next available slot in the array.
*
* @param a the array
*
* @return the first slot available, or -1 if the array is already full
*/
static int ar_next_available(const struct array *a)
{
size_t i;
for (i = 0 ; i < a->total_slots ; i++) {
if(a->array[i] == NULL)
return (int)i;
}
return -1;
}
/**
* Grow an array to twice its current size
*
* @param a the array that needs to be grown
*/
static int ar_grow(struct array *a)
{
size_t old_size, new_size;
void **tmp_alloc;
if (a == NULL || a->array == NULL) {
logger(LOG_WARN, "ar_grow : passed array is not allocated.");
return 0;
}
if (a->total_slots < a->max_slots) {
old_size = a->total_slots;
new_size = MIN(a->total_slots * 2, a->max_slots);
a->total_slots = new_size;
tmp_alloc = (void **)realloc(a->array, sizeof(void *) * (a->total_slots));
if (tmp_alloc == NULL) {
logger(LOG_ERR, "ar_grow, realloc failed : %s", strerror(errno));
return 0;
}
a->array = tmp_alloc;
/* realloc does not set to zero!! */
bzero(a->array + old_size, (a->total_slots - old_size) * sizeof(void *));
return AR_OK;
}
return 0;
}
/**
* Insert an element into the array, resizing if needed
*
* @param a the array
* @param elem a generic pointer to an element
*
* @return 0 on failure, 1 on success
*/
int ar_insert(struct array *a, void *elem)
{
int i;
int err;
pthread_mutex_lock(&a->lock);
if (a->used_slots == a->total_slots) {
err = ar_grow(a);
if (err != AR_OK) {
logger(LOG_ERR, "Could not grow array any further. Insertion impossible.");
pthread_mutex_unlock(&a->lock);
return 0;
}
}
i = ar_next_available(a);
if (i != -1) {
a->array[i] = elem;
a->used_slots++;
pthread_mutex_unlock(&a->lock);
return AR_OK;
}
pthread_mutex_unlock(&a->lock);
return 0;
}
/**
* Create a new empty array of a given size.
*
* @param size the initial size of the array
*
* @return the allocated array
*/
struct array *ar_new(size_t size)
{
struct array *a;
a = (struct array *)calloc(1, sizeof(struct array));
if (a == NULL) {
logger(LOG_ERR, "ar_new, calloc failed : %s", strerror(errno));
return NULL;
}
a->total_slots = size;
a->used_slots = 0;
a->max_slots = (size_t) - 1;
a->array = (void **)calloc(size, sizeof(void *));
if (a->array == NULL) {
logger(LOG_ERR, "ar_new, a->array calloc failed : %s", strerror(errno));
free(a);
return NULL;
}
pthread_mutex_init(&a->lock, NULL);
return a;
}
/**
* Remove the entry at the given index in the array
* and shrinks the array if necessary.
* Shrinking is not implemented yet.
*
* @param a the array
* @param index the index of the element that has to be removed
*/
static void ar_remove_index(struct array *a, size_t idx)
{
if (a == NULL || a->array == NULL) {
logger(LOG_WARN, "ar_remove_index, passed array is unallocated.");
return;
}
if (a->array[idx] != NULL) {
a->array[idx] = NULL; /* clear the pointer */
a->used_slots--;
}
return;
/* Add some table shrinking logic
* if too many slots are unused */
}
/**
* Remove the element from the array
* and shrinks the array if necessary.
* This function is a wrapper around ar_remove_index.
*
* @param a the array
* @param el a generic pointer to an element
*/
void ar_remove(struct array *a, void *el)
{
size_t i;
char found = 0;
pthread_mutex_lock(&a->lock);
for (i=0 ; i < a->total_slots ; i++) {
if (a->array[i] == el) {
ar_remove_index(a, i);
found = 1;
}
}
pthread_mutex_unlock(&a->lock);
if (found == 0)
logger(LOG_ERR, "ar_remove : pointer 0x%x was not found in our array.\n", el);
}
/**
* Retrieves a given number of elements, starting at a given index
* and put it into the array.
*
* @param a the array we will be picking elements from
* @param max_elem the maximum number of elements we will take
* @param start_at the number of element before the ones we will pick
* @param res the array we will put elements in
*
* @return the number of elements that were put into res (0 if none)
*/
int ar_get_n_elems_start_at(struct array *a, int max_elem, size_t start_at, void **res)
{
size_t i=0;
size_t nb_elem = 0;
int el_counter = 0;
pthread_mutex_lock(&a->lock);
if (a->used_slots <= start_at) {
pthread_mutex_unlock(&a->lock);
return 0;
}
for (i=0 ; i < a->total_slots ; i++) {
if (a->array[i] != NULL) {
if (nb_elem >= start_at && nb_elem < (start_at + max_elem)) {
res[nb_elem - start_at] = a->array[i];
el_counter++;
}
nb_elem++;
}
}
pthread_mutex_unlock(&a->lock);
return el_counter;
}
int ar_free(struct array *a)
{
size_t i;
pthread_mutex_lock(&a->lock);
/* if the array is not allocated, we cannot free it */
if (a == NULL || a->array == NULL) {
logger(LOG_ERR, "ar_free : Trying to free an unallocated array.");
pthread_mutex_unlock(&a->lock);
return 0;
}
/* if the array is not empty, we cannot free it */
for (i = 0 ; i < a->total_slots ; i++) {
if (a->array[i] != NULL) {
logger(LOG_ERR, "ar_free : Trying to free an array that is not empty.");
pthread_mutex_unlock(&a->lock);
return 0;
}
}
free(a->array);
pthread_mutex_unlock(&a->lock);
pthread_mutex_destroy(&a->lock);
free(a);
return AR_OK;
}
int ar_has(struct array *a, void *el)
{
size_t iter;
void *tmp_el;
ar_each(void *, tmp_el, iter, a)
if (tmp_el == el)
return 1;
ar_end_each;
return 0;
}