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