2 #define _POSIX_C_SOURCE 200809L
8 #warning "your C library sucks."
12 sblist
* sblist_new(size_t itemsize
, size_t blockitems
) {
13 sblist
* ret
= (sblist
*) malloc(sizeof(sblist
));
14 sblist_init(ret
, itemsize
, blockitems
);
18 static void sblist_clear(sblist
* l
) {
24 void sblist_init(sblist
* l
, size_t itemsize
, size_t blockitems
) {
26 l
->blockitems
= blockitems
? blockitems
: PAGE_SIZE
/ itemsize
;
27 l
->itemsize
= itemsize
;
32 void sblist_free_items(sblist
* l
) {
34 if(l
->items
) free(l
->items
);
39 void sblist_free(sblist
* l
) {
46 char* sblist_item_from_index(sblist
* l
, size_t idx
) {
47 return l
->items
+ (idx
* l
->itemsize
);
50 void* sblist_get(sblist
* l
, size_t item
) {
51 if(item
< l
->count
) return (void*) sblist_item_from_index(l
, item
);
55 int sblist_set(sblist
* l
, void* item
, size_t pos
) {
56 if(pos
>= l
->count
) return 0;
57 memcpy(sblist_item_from_index(l
, pos
), item
, l
->itemsize
);
61 int sblist_grow_if_needed(sblist
* l
) {
63 if(l
->count
== l
->capa
) {
64 temp
= realloc(l
->items
, (l
->capa
+ l
->blockitems
) * l
->itemsize
);
66 l
->capa
+= l
->blockitems
;
72 int sblist_add(sblist
* l
, void* item
) {
73 if(!sblist_grow_if_needed(l
)) return 0;
75 return sblist_set(l
, item
, l
->count
- 1);