2 #define _POSIX_C_SOURCE 200809L
7 #define MY_PAGE_SIZE 4096
9 sblist
* sblist_new(size_t itemsize
, size_t blockitems
) {
10 sblist
* ret
= (sblist
*) malloc(sizeof(sblist
));
11 sblist_init(ret
, itemsize
, blockitems
);
15 static void sblist_clear(sblist
* l
) {
21 void sblist_init(sblist
* l
, size_t itemsize
, size_t blockitems
) {
23 l
->blockitems
= blockitems
? blockitems
: MY_PAGE_SIZE
/ itemsize
;
24 l
->itemsize
= itemsize
;
29 void sblist_free_items(sblist
* l
) {
31 if(l
->items
) free(l
->items
);
36 void sblist_free(sblist
* l
) {
43 char* sblist_item_from_index(sblist
* l
, size_t idx
) {
44 return l
->items
+ (idx
* l
->itemsize
);
47 void* sblist_get(sblist
* l
, size_t item
) {
48 if(item
< l
->count
) return (void*) sblist_item_from_index(l
, item
);
52 int sblist_set(sblist
* l
, void* item
, size_t pos
) {
53 if(pos
>= l
->count
) return 0;
54 memcpy(sblist_item_from_index(l
, pos
), item
, l
->itemsize
);
58 int sblist_grow_if_needed(sblist
* l
) {
60 if(l
->count
== l
->capa
) {
61 temp
= realloc(l
->items
, (l
->capa
+ l
->blockitems
) * l
->itemsize
);
63 l
->capa
+= l
->blockitems
;
69 int sblist_add(sblist
* l
, void* item
) {
70 if(!sblist_grow_if_needed(l
)) return 0;
72 return sblist_set(l
, item
, l
->count
- 1);