-
Notifications
You must be signed in to change notification settings - Fork 1
/
ptrlist.c
92 lines (78 loc) · 1.96 KB
/
ptrlist.c
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include "common.h"
#include "ptrlist.h"
struct ptrlist *ptrlist_create()
{
struct ptrlist *list = malloc(sizeof(struct ptrlist));
memset(list, 0, sizeof(struct ptrlist));
list->count = 0;
list->size = 2;
list->data = calloc(list->size, sizeof(struct ptrlist_node *));
return list;
}
void ptrlist_free(struct ptrlist *list)
{
unsigned int i;
for(i = 0; i < list->count; i++)
{
if(list->free_func)
list->free_func(list->data[i]->ptr);
free(list->data[i]);
}
free(list->data);
free(list);
}
unsigned int ptrlist_add(struct ptrlist *list, int ptr_type, void *ptr)
{
struct ptrlist_node *node;
int pos;
if((pos = ptrlist_find(list, ptr)) != -1)
return pos;
if(list->count == list->size) // list is full, we need to allocate more memory
{
list->size <<= 1; // double size
list->data = realloc(list->data, list->size * sizeof(struct ptrlist_node *));
}
node = malloc(sizeof(struct ptrlist_node));
node->type = ptr_type;
node->ptr = ptr;
pos = list->count++;
list->data[pos] = node;
return pos;
}
int ptrlist_find(struct ptrlist *list, const void *ptr)
{
for(unsigned int i = 0; i < list->count; i++)
if(list->data[i]->ptr == ptr)
return i;
return -1;
}
void ptrlist_set_free_func(struct ptrlist *list, ptrlist_free_f *free_func)
{
list->free_func = free_func;
}
void ptrlist_clear(struct ptrlist *list)
{
for(unsigned int i = 0; i < list->count; i++)
{
if(list->free_func)
list->free_func(list->data[i]->ptr);
free(list->data[i]);
}
list->count = 0;
}
void ptrlist_del(struct ptrlist *list, unsigned int pos, unsigned int *pos_ptr)
{
assert(pos < list->count);
if(list->free_func)
list->free_func(list->data[pos]->ptr);
free(list->data[pos]);
list->data[pos] = list->data[--list->count]; // copy last element into empty position
if(pos_ptr != NULL && *pos_ptr == list->count)
*pos_ptr = pos;
}
void ptrlist_del_ptr(struct ptrlist *list, void *ptr)
{
int pos = ptrlist_find(list, ptr);
if(pos != -1)
ptrlist_del(list, pos, NULL);
}