auth-once mode: prevent same addr from being added multiple times
[rofl0r-microsocks.git] / sblist.h
blobf0d184649451338501fd8caffef28ad153f0782a
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 microsocks. */
8 #ifdef __cplusplus
9 extern "C" {
10 #endif
12 #include <stddef.h>
14 * simple buffer list.
16 * this thing here is basically a generic dynamic array
17 * will realloc after every blockitems inserts
18 * can store items of any size.
20 * so think of it as a by-value list, as opposed to a typical by-ref list.
21 * you typically use it by having some struct on the stack, and pass a pointer
22 * to sblist_add, which will copy the contents into its internal memory.
26 typedef struct {
27 size_t itemsize;
28 size_t blockitems;
29 size_t count;
30 size_t capa;
31 char* items;
32 } sblist;
34 #define sblist_getsize(X) ((X)->count)
35 #define sblist_get_count(X) ((X)->count)
36 #define sblist_empty(X) ((X)->count == 0)
38 // for dynamic style
39 sblist* sblist_new(size_t itemsize, size_t blockitems);
40 void sblist_free(sblist* l);
42 //for static style
43 void sblist_init(sblist* l, size_t itemsize, size_t blockitems);
44 void sblist_free_items(sblist* l);
46 // accessors
47 void* sblist_get(sblist* l, size_t item);
48 // returns 1 on success, 0 on OOM
49 int sblist_add(sblist* l, void* item);
50 int sblist_set(sblist* l, void* item, size_t pos);
51 void sblist_delete(sblist* l, size_t item);
52 char* sblist_item_from_index(sblist* l, size_t idx);
53 int sblist_grow_if_needed(sblist* l);
54 int sblist_insert(sblist* l, void* item, size_t pos);
55 /* same as sblist_add, but returns list index of new item, or -1 */
56 size_t sblist_addi(sblist* l, void* item);
57 void sblist_sort(sblist *l, int (*compar)(const void *, const void *));
58 /* insert element into presorted list, returns listindex of new entry or -1*/
59 size_t sblist_insert_sorted(sblist* l, void* o, int (*compar)(const void *, const void *));
61 #ifndef __COUNTER__
62 #define __COUNTER__ __LINE__
63 #endif
65 #define __sblist_concat_impl( x, y ) x##y
66 #define __sblist_macro_concat( x, y ) __sblist_concat_impl( x, y )
67 #define __sblist_iterator_name __sblist_macro_concat(sblist_iterator, __COUNTER__)
69 // use with custom iterator variable
70 #define sblist_iter_counter(LIST, ITER, PTR) \
71 for(size_t ITER = 0; (PTR = sblist_get(LIST, ITER)), ITER < sblist_getsize(LIST); ITER++)
73 // use with custom iterator variable, which is predeclared
74 #define sblist_iter_counter2(LIST, ITER, PTR) \
75 for(ITER = 0; (PTR = sblist_get(LIST, ITER)), ITER < sblist_getsize(LIST); ITER++)
77 // use with custom iterator variable, which is predeclared and signed
78 // useful for a loop which can delete items from the list, and then decrease the iterator var.
79 #define sblist_iter_counter2s(LIST, ITER, PTR) \
80 for(ITER = 0; (PTR = sblist_get(LIST, ITER)), ITER < (ssize_t) sblist_getsize(LIST); ITER++)
83 // uses "magic" iterator variable
84 #define sblist_iter(LIST, PTR) sblist_iter_counter(LIST, __sblist_iterator_name, PTR)
86 #ifdef __cplusplus
88 #endif
90 #pragma RcB2 DEP "sblist.c" "sblist_delete.c"
92 #endif