1 #ifndef SBLIST_H 2 #define SBLIST_H 3 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. */ 7 8 9 #ifdef __cplusplus 10 extern "C" { 11 #endif 12 13 #include <stddef.h> 14 /* 15 * simple buffer list. 16 * 17 * this thing here is basically a generic dynamic array 18 * will realloc after every blockitems inserts 19 * can store items of any size. 20 * 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. 24 * 25 */ 26 27 typedef struct { 28 size_t itemsize; 29 size_t blockitems; 30 size_t count; 31 size_t capa; 32 char* items; 33 } sblist; 34 35 #define sblist_getsize(X) ((X)->count) 36 #define sblist_get_count(X) ((X)->count) 37 #define sblist_empty(X) ((X)->count == 0) 38 39 /* for dynamic style */ 40 sblist* sblist_new(size_t itemsize, size_t blockitems); 41 void sblist_free(sblist* l); 42 43 /*for static style*/ 44 void sblist_init(sblist* l, size_t itemsize, size_t blockitems); 45 void sblist_free_items(sblist* l); 46 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); 56 /* same as sblist_add, but returns list index of new item, or -1 */ 57 size_t sblist_addi(sblist* l, void* item); 58 void sblist_sort(sblist *l, int (*compar)(const void *, const void *)); 59 /* insert element into presorted list, returns listindex of new entry or -1*/ 60 size_t sblist_insert_sorted(sblist* l, void* o, int (*compar)(const void *, const void *)); 61 62 #ifndef __COUNTER__ 63 #define __COUNTER__ __LINE__ 64 #endif 65 66 #define __sblist_concat_impl( x, y ) x##y 67 #define __sblist_macro_concat( x, y ) __sblist_concat_impl( x, y ) 68 #define __sblist_iterator_name __sblist_macro_concat(sblist_iterator, __COUNTER__) 69 70 /* use with custom iterator variable */ 71 #define sblist_iter_counter(LIST, ITER, PTR) \ 72 for(size_t ITER = 0; (PTR = sblist_get(LIST, ITER)), ITER < sblist_getsize(LIST); ITER++) 73 74 /* use with custom iterator variable, which is predeclared */ 75 #define sblist_iter_counter2(LIST, ITER, PTR) \ 76 for(ITER = 0; (PTR = sblist_get(LIST, ITER)), ITER < sblist_getsize(LIST); ITER++) 77 78 /* use with custom iterator variable, which is predeclared and signed */ 79 /* useful for a loop which can delete items from the list, and then decrease the iterator var. */ 80 #define sblist_iter_counter2s(LIST, ITER, PTR) \ 81 for(ITER = 0; (PTR = sblist_get(LIST, ITER)), ITER < (ssize_t) sblist_getsize(LIST); ITER++) 82 83 84 /* uses "magic" iterator variable */ 85 #define sblist_iter(LIST, PTR) sblist_iter_counter(LIST, __sblist_iterator_name, PTR) 86 87 #ifdef __cplusplus 88 } 89 #endif 90 91 #endif 92 93