-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsblist.c
More file actions
58 lines (44 loc) · 1.23 KB
/
sblist.c
File metadata and controls
58 lines (44 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include "sblist.h"
sblist *sblist_create(size_t itemsize, size_t blockitems)
{
sblist *ret;
if((ret = (sblist *)malloc(sizeof(sblist))) == 0) { log_fatal("Out of memory!"); }
memset(ret,0,sizeof(ret));
ret->max_count = blockitems;
ret->blockitems = blockitems;
ret->itemsize = itemsize;
if((ret->items = malloc(itemsize * blockitems)) == 0) { log_fatal("Out of memory!"); }
return ret;
}
void sblist_free(sblist *l)
{
if(l->items == NULL)
free(l->items);
memset(l,0,sizeof(l));
}
void *sblist_get(sblist *l, size_t n)
{
if(n > l->count) { log_fatal_with_backtrace("out of list"); }
return (l->items + (n * l->itemsize));
}
void sblist_set(sblist *l, void *item, size_t pos)
{
memcpy(sblist_get(l,pos),item,l->itemsize);
}
void sblist_add(sblist *l, void *item)
{
char *tmp;
if(l->count == l->max_count)
{
if((tmp = realloc(l->items,(l->max_count + l->blockitems) * l->itemsize)) == 0) { log_fatal_with_backtrace("Out of memory!"); }
l->max_count += l->blockitems;
l->items = tmp;
}
l->count++;
sblist_set(l,item,l->count - 1);
}
void sblist_delete(sblist *l, size_t n)
{
memset(sblist_get(l,n),0,l->itemsize);
l->count--;
}