Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 63 lines (53 sloc) 1.318 kB
143463d @rofl0r first
authored
1 #include <stdlib.h>
2 #include <string.h>
3 #include "../../include/sblist.h"
4
5 sblist* sblist_new(size_t itemsize, size_t blockitems) {
6 sblist* ret = (sblist*) malloc(sizeof(sblist));
7 sblist_init(ret, itemsize, blockitems);
8 return ret;
9 }
10
11 static void sblist_clear(sblist* l) {
12 l->items = NULL;
13 l->capa = 0;
14 l->count = 0;
15 }
16
17 void sblist_init(sblist* l, size_t itemsize, size_t blockitems) {
18 if(l) {
19 l->blockitems = blockitems;
20 l->itemsize = itemsize;
21 sblist_clear(l);
22 }
23 }
24
25 void sblist_free_items(sblist* l) {
26 if(l) {
27 if(l->items) free(l->items);
28 sblist_clear(l);
29 }
30 }
31
32 void sblist_free(sblist* l) {
33 sblist_free_items(l);
34 free(l);
35 }
36
37 static char* sblist_item_from_index(sblist* l, size_t idx) {
38 return l->items + (idx * l->itemsize);
39 }
40
41 void* sblist_get(sblist* l, size_t item) {
42 if(item < l->count) return (void*) sblist_item_from_index(l, item);
43 return NULL;
44 }
45
46 int sblist_set(sblist* l, void* item, size_t pos) {
47 if(pos >= l->count) return 0;
48 memcpy(sblist_item_from_index(l, pos), item, l->itemsize);
49 return 1;
50 }
51
52 int sblist_add(sblist* l, void* item) {
53 char* temp;
54 if(l->count == l->capa) {
55 temp = realloc(l->items, (l->capa + l->blockitems) * l->itemsize);
56 if(!temp) return 0;
57 l->capa += l->blockitems;
58 l->items = temp;
59 }
60 l->count++;
61 return sblist_set(l, item, l->count - 1);
62 }
Something went wrong with that request. Please try again.