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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
|
/*
* Copyright (C) 2014 Tobias Brunner
* Hochschule fuer Technik Rapperswil
*
* Copyright (C) 2013 Martin Willi
* Copyright (C) 2013 revosec AG
*
* 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 2 of the License, or (at your
* option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
*
* 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.
*/
#define _GNU_SOURCE /* for qsort_r() */
#include <stdlib.h>
#include "array.h"
#ifndef HAVE_QSORT_R
#include <threading/thread_value.h>
#endif
/**
* Data is an allocated block, with potentially unused head and tail:
*
* "esize" each (or sizeof(void*) if esize = 0)
* /-\ /-\ /-\ /-\ /-\ /-\
*
* +---------------+-------------------------------+---------------+
* | h | e | a | d | e | l | e | m | e | n | t | s | t | a | i | l |
* +---------------+-------------------------------+---------------+
*
* \--------------/ \-----------------------------/ \-------------/
* unused used unused
* "head" "count" "tail"
*
*/
struct array_t {
/** number of elements currently in array (not counting head/tail) */
uint32_t count;
/** size of each element, 0 for a pointer based array */
uint16_t esize;
/** allocated but unused elements at array front */
uint8_t head;
/** allocated but unused elements at array end */
uint8_t tail;
/** array elements */
void *data;
};
#ifndef HAVE_QSORT_R
/* store data to replicate qsort_r in thread local storage */
static thread_value_t *sort_data;
#endif
/** maximum number of unused head/tail elements before cleanup */
#define ARRAY_MAX_UNUSED 32
/**
* Get the actual size of a number of elements
*/
static size_t get_size(array_t *array, uint32_t num)
{
if (array->esize)
{
return array->esize * num;
}
return sizeof(void*) * num;
}
/**
* Increase allocated but unused tail room to at least "room"
*/
static void make_tail_room(array_t *array, uint8_t room)
{
if (array->tail < room)
{
array->data = realloc(array->data,
get_size(array, array->count + array->head + room));
array->tail = room;
}
}
/**
* Increase allocated but unused head room to at least "room"
*/
static void make_head_room(array_t *array, uint8_t room)
{
if (array->head < room)
{
uint8_t increase = room - array->head;
array->data = realloc(array->data,
get_size(array, array->count + array->tail + room));
memmove(array->data + get_size(array, increase), array->data,
get_size(array, array->count + array->tail + array->head));
array->head = room;
}
}
/**
* Make space for an item at index using tail room
*/
static void insert_tail(array_t *array, int idx)
{
make_tail_room(array, 1);
/* move up all elements after idx by one */
memmove(array->data + get_size(array, array->head + idx + 1),
array->data + get_size(array, array->head + idx),
get_size(array, array->count - idx));
array->tail--;
array->count++;
}
/**
* Make space for an item at index using head room
*/
static void insert_head(array_t *array, int idx)
{
make_head_room(array, 1);
/* move down all elements before idx by one */
memmove(array->data + get_size(array, array->head - 1),
array->data + get_size(array, array->head),
get_size(array, idx));
array->head--;
array->count++;
}
/**
* Remove an item, increase tail
*/
static void remove_tail(array_t *array, int idx)
{
/* move all items after idx one down */
memmove(array->data + get_size(array, idx + array->head),
array->data + get_size(array, idx + array->head + 1),
get_size(array, array->count - 1 - idx));
array->count--;
array->tail++;
}
/**
* Remove an item, increase head
*/
static void remove_head(array_t *array, int idx)
{
/* move all items before idx one up */
memmove(array->data + get_size(array, array->head + 1),
array->data + get_size(array, array->head), get_size(array, idx));
array->count--;
array->head++;
}
array_t *array_create(u_int esize, uint8_t reserve)
{
array_t *array;
INIT(array,
.esize = esize,
.tail = reserve,
);
if (array->tail)
{
array->data = malloc(get_size(array, array->tail));
}
return array;
}
int array_count(array_t *array)
{
if (array)
{
return array->count;
}
return 0;
}
void array_compress(array_t *array)
{
if (array)
{
uint32_t tail;
tail = array->tail;
if (array->head)
{
memmove(array->data, array->data + get_size(array, array->head),
get_size(array, array->count + array->tail));
tail += array->head;
array->head = 0;
}
if (tail)
{
array->data = realloc(array->data, get_size(array, array->count));
array->tail = 0;
}
}
}
typedef struct {
/** public enumerator interface */
enumerator_t public;
/** enumerated array */
array_t *array;
/** current index +1, initialized at 0 */
int idx;
} array_enumerator_t;
METHOD(enumerator_t, enumerate, bool,
array_enumerator_t *this, va_list args)
{
void *pos, **out;
VA_ARGS_VGET(args, out);
if (this->idx >= this->array->count)
{
return FALSE;
}
pos = this->array->data +
get_size(this->array, this->idx + this->array->head);
if (this->array->esize)
{
/* for element based arrays we return a pointer to the element */
*out = pos;
}
else
{
/* for pointer based arrays we return the pointer directly */
*out = *(void**)pos;
}
this->idx++;
return TRUE;
}
enumerator_t* array_create_enumerator(array_t *array)
{
array_enumerator_t *enumerator;
if (!array)
{
return enumerator_create_empty();
}
INIT(enumerator,
.public = {
.enumerate = enumerator_enumerate_default,
.venumerate = _enumerate,
.destroy = (void*)free,
},
.array = array,
);
return &enumerator->public;
}
void array_remove_at(array_t *array, enumerator_t *public)
{
array_enumerator_t *enumerator = (array_enumerator_t*)public;
if (enumerator->idx)
{
array_remove(array, --enumerator->idx, NULL);
}
}
void array_insert_create(array_t **array, int idx, void *ptr)
{
if (*array == NULL)
{
*array = array_create(0, 0);
}
array_insert(*array, idx, ptr);
}
void array_insert_create_value(array_t **array, u_int esize,
int idx, void *val)
{
if (*array == NULL)
{
*array = array_create(esize, 0);
}
array_insert(*array, idx, val);
}
void array_insert_enumerator(array_t *array, int idx, enumerator_t *enumerator)
{
void *ptr;
while (enumerator->enumerate(enumerator, &ptr))
{
array_insert(array, idx, ptr);
}
enumerator->destroy(enumerator);
}
void array_insert(array_t *array, int idx, void *data)
{
if (idx < 0 || idx <= array_count(array))
{
void *pos;
if (idx < 0)
{
idx = array_count(array);
}
if (array->head && !array->tail)
{
insert_head(array, idx);
}
else if (array->tail && !array->head)
{
insert_tail(array, idx);
}
else if (idx > array_count(array) / 2)
{
insert_tail(array, idx);
}
else
{
insert_head(array, idx);
}
pos = array->data + get_size(array, array->head + idx);
if (array->esize)
{
memcpy(pos, data, get_size(array, 1));
}
else
{
/* pointer based array, copy pointer value */
*(void**)pos = data;
}
}
}
bool array_get(array_t *array, int idx, void *data)
{
if (!array)
{
return FALSE;
}
if (idx >= 0 && idx >= array_count(array))
{
return FALSE;
}
if (idx < 0)
{
if (array_count(array) == 0)
{
return FALSE;
}
idx = array_count(array) - 1;
}
if (data)
{
memcpy(data, array->data + get_size(array, array->head + idx),
get_size(array, 1));
}
return TRUE;
}
bool array_remove(array_t *array, int idx, void *data)
{
if (!array_get(array, idx, data))
{
return FALSE;
}
if (idx < 0)
{
idx = array_count(array) - 1;
}
if (idx > array_count(array) / 2)
{
remove_tail(array, idx);
}
else
{
remove_head(array, idx);
}
if (array->head + array->tail > ARRAY_MAX_UNUSED)
{
array_compress(array);
}
return TRUE;
}
typedef struct {
/** the array */
array_t *array;
/** comparison function */
int (*cmp)(const void*,const void*,void*);
/** optional user arg */
void *arg;
} sort_data_t;
#ifdef HAVE_QSORT_R_GNU
static int compare_elements(const void *a, const void *b, void *arg)
#elif defined(HAVE_QSORT_R_BSD)
static int compare_elements(void *arg, const void *a, const void *b)
#else /* !HAVE_QSORT_R */
static int compare_elements(const void *a, const void *b)
#endif
{
#ifdef HAVE_QSORT_R
sort_data_t *data = (sort_data_t*)arg;
#else
sort_data_t *data = sort_data->get(sort_data);
#endif
if (data->array->esize)
{
return data->cmp(a, b, data->arg);
}
return data->cmp(*(void**)a, *(void**)b, data->arg);
}
void array_sort(array_t *array, int (*cmp)(const void*,const void*,void*),
void *user)
{
if (array)
{
sort_data_t data = {
.array = array,
.cmp = cmp,
.arg = user,
};
void *start;
start = array->data + get_size(array, array->head);
#ifdef HAVE_QSORT_R_GNU
qsort_r(start, array->count, get_size(array, 1), compare_elements,
&data);
#elif defined(HAVE_QSORT_R_BSD)
qsort_r(start, array->count, get_size(array, 1), &data,
compare_elements);
#else /* !HAVE_QSORT_R */
sort_data->set(sort_data, &data);
qsort(start, array->count, get_size(array, 1), compare_elements);
#endif
}
}
typedef struct {
/** the array */
array_t *array;
/** the key */
const void *key;
/** comparison function */
int (*cmp)(const void*,const void*);
} bsearch_data_t;
static int search_elements(const void *a, const void *b)
{
bsearch_data_t *data = (bsearch_data_t*)a;
if (data->array->esize)
{
return data->cmp(data->key, b);
}
return data->cmp(data->key, *(void**)b);
}
int array_bsearch(array_t *array, const void *key,
int (*cmp)(const void*,const void*), void *out)
{
int idx = -1;
if (array)
{
bsearch_data_t data = {
.array = array,
.key = key,
.cmp = cmp,
};
void *start, *item;
start = array->data + get_size(array, array->head);
item = bsearch(&data, start, array->count, get_size(array, 1),
search_elements);
if (item)
{
if (out)
{
memcpy(out, item, get_size(array, 1));
}
idx = (item - start) / get_size(array, 1);
}
}
return idx;
}
void array_invoke(array_t *array, array_callback_t cb, void *user)
{
if (array)
{
void *obj;
int i;
for (i = array->head; i < array->count + array->head; i++)
{
obj = array->data + get_size(array, i);
if (!array->esize)
{
/* dereference if we store store pointers */
obj = *(void**)obj;
}
cb(obj, i - array->head, user);
}
}
}
void array_invoke_offset(array_t *array, size_t offset)
{
if (array)
{
void (*method)(void *data);
void *obj;
int i;
for (i = array->head; i < array->count + array->head; i++)
{
obj = array->data + get_size(array, i);
if (!array->esize)
{
/* dereference if we store store pointers */
obj = *(void**)obj;
}
method = *(void**)(obj + offset);
method(obj);
}
}
}
void array_destroy(array_t *array)
{
if (array)
{
free(array->data);
free(array);
}
}
void array_destroy_function(array_t *array, array_callback_t cb, void *user)
{
array_invoke(array, cb, user);
array_destroy(array);
}
void array_destroy_offset(array_t *array, size_t offset)
{
array_invoke_offset(array, offset);
array_destroy(array);
}
void arrays_init()
{
#ifndef HAVE_QSORT_R
sort_data = thread_value_create(NULL);
#endif
}
void arrays_deinit()
{
#ifndef HAVE_QSORT_R
sort_data->destroy(sort_data);
#endif
}
|