improve output of die()
[rofl0r-jobflow.git] / sblist.h
blobd4c26eaf9765bdc65068d2a936eb0dcb84f95b61
1 #ifndef SBLIST_H
2 #define SBLIST_H
4 /* this file is part of libulz, as of commit 8ab361a27743aaf025323ee43b8b8876dc054fdd
5 modified for direct inclusion in tinyproxy, and for this purpose released under
6 the license of tinyproxy. */
9 #ifdef __cplusplus
10 extern "C" {
11 #endif
13 #include <stddef.h>
15 * simple buffer list.
17 * this thing here is basically a generic dynamic array
18 * will realloc after every blockitems inserts
19 * can store items of any size.
21 * so think of it as a by-value list, as opposed to a typical by-ref list.
22 * you typically use it by having some struct on the stack, and pass a pointer
23 * to sblist_add, which will copy the contents into its internal memory.
27 typedef struct {
28 size_t itemsize;
29 size_t blockitems;
30 size_t count;
31 size_t capa;
32 char* items;
33 } sblist;
35 #define sblist_getsize(X) ((X)->count)
36 #define sblist_get_count(X) ((X)->count)
37 #define sblist_empty(X) ((X)->count == 0)
39 /* for dynamic style */
40 sblist* sblist_new(size_t itemsize, size_t blockitems);
41 void sblist_free(sblist* l);
43 /*for static style*/
44 void sblist_init(sblist* l, size_t itemsize, size_t blockitems);
45 void sblist_free_items(sblist* l);
47 /* accessors */
48 void* sblist_get(sblist* l, size_t item);
49 /* returns 1 on success, 0 on OOM */
50 int sblist_add(sblist* l, void* item);
51 int sblist_set(sblist* l, void* item, size_t pos);
52 void sblist_delete(sblist* l, size_t item);
53 char* sblist_item_from_index(sblist* l, size_t idx);
54 int sblist_grow_if_needed(sblist* l);
55 int sblist_insert(sblist* l, void* item, size_t pos);
57 /* you can use sblist as a stack by using sblist_add()/sblist_pop() */
58 /* return last item in list and remove it. returns NULL if no items in list. */
59 void* sblist_pop(sblist *l);
61 /* same as sblist_add, but returns list index of new item, or -1 */
62 size_t sblist_addi(sblist* l, void* item);
63 void sblist_sort(sblist *l, int (*compar)(const void *, const void *));
64 /* insert element into presorted list, returns listindex of new entry or -1*/
65 size_t sblist_insert_sorted(sblist* l, void* o, int (*compar)(const void *, const void *));
67 #ifndef __COUNTER__
68 #define __COUNTER__ __LINE__
69 #endif
71 #define __sblist_concat_impl( x, y ) x##y
72 #define __sblist_macro_concat( x, y ) __sblist_concat_impl( x, y )
73 #define __sblist_iterator_name __sblist_macro_concat(sblist_iterator, __COUNTER__)
75 /* use with custom iterator variable */
76 #define sblist_iter_counter(LIST, ITER, PTR) \
77 for(size_t ITER = 0; (PTR = sblist_get(LIST, ITER)), ITER < sblist_getsize(LIST); ITER++)
79 /* use with custom iterator variable, which is predeclared */
80 #define sblist_iter_counter2(LIST, ITER, PTR) \
81 for(ITER = 0; (PTR = sblist_get(LIST, ITER)), ITER < sblist_getsize(LIST); ITER++)
83 /* use with custom iterator variable, which is predeclared and signed */
84 /* useful for a loop which can delete items from the list, and then decrease the iterator var. */
85 #define sblist_iter_counter2s(LIST, ITER, PTR) \
86 for(ITER = 0; (PTR = sblist_get(LIST, ITER)), ITER < (ssize_t) sblist_getsize(LIST); ITER++)
89 /* uses "magic" iterator variable */
90 #define sblist_iter(LIST, PTR) sblist_iter_counter(LIST, __sblist_iterator_name, PTR)
92 #ifdef __cplusplus
94 #endif
96 #endif