forked from andbof/yastg
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathptrlist.c
More file actions
99 lines (79 loc) · 1.57 KB
/
ptrlist.c
File metadata and controls
99 lines (79 loc) · 1.57 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
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
93
94
95
96
97
98
99
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include "common.h"
#include "list.h"
#include "log.h"
#include "ptrlist.h"
#include "sector.h"
int ptrlist_init(struct ptrlist *l)
{
memset(l, 0, sizeof(*l));
INIT_LIST_HEAD(&l->list);
return 0;
}
void ptrlist_free(struct ptrlist *l)
{
assert(l != NULL);
struct ptrlist *e;
struct list_head *p, *q;
list_for_each_safe(p, q, &l->list) {
e = list_entry(p, struct ptrlist, list);
list_del(p);
free(e);
}
}
int ptrlist_push(struct ptrlist *l, void *e)
{
struct ptrlist *nl;
assert(l != NULL);
nl = malloc(sizeof(*nl));
if (!nl)
return -1;
ptrlist_init(nl);
nl->data = e;
list_add_tail(&nl->list, &l->list);
l->len++;
return 0;
}
void* ptrlist_pull(struct ptrlist * const l)
{
struct ptrlist *e;
void *data;
assert(l != NULL);
if (list_empty(&l->list))
return NULL;
e = list_entry(l->list.next, struct ptrlist, list);
list_del_init(&e->list);
l->len--;
data = e->data;
free(e);
return data;
}
struct ptrlist* ptrlist_get(const struct ptrlist * const l, const unsigned long n)
{
assert(l != NULL);
struct list_head *ptr;
unsigned long i = 0;
list_for_each(ptr, &l->list) {
if (i == n)
break;
i++;
}
return list_entry(ptr, struct ptrlist, list);
}
void* ptrlist_entry(const struct ptrlist * const l, const unsigned long n)
{
return ptrlist_get(l, n)->data;
}
void ptrlist_rm(struct ptrlist *l, const unsigned long n)
{
struct ptrlist *e;
assert(l != NULL);
assert(!list_empty(&l->list));
e = ptrlist_get(l, n);
list_del(&e->list);
free(e);
l->len--;
}