forked from lukablurr/n2n_v3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathn2n_list.h
107 lines (69 loc) · 2.86 KB
/
n2n_list.h
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
100
101
102
103
104
105
106
107
/*
* n2n_list.h
*
* Created on: Aug 31, 2012
* Author: Costin Lupu
*/
#ifndef N2N_LIST_H_
#define N2N_LIST_H_
#include <stddef.h>
struct n2n_list
{
struct n2n_list *next;
};
#define CONTAINER_OF(ptr, type, member) ({ \
const typeof( ((type *)0)->member ) *__mptr = (ptr); \
(type *)( (char *)__mptr - offsetof(type, member) );})
#define LIST_ENTRY(ptr, type, member) \
CONTAINER_OF(ptr, type, member)
/*************************************/
#define LIST_FIRST(head) (head)->next
#define LIST_FOR_EACH(pos, head) \
for (pos = (head)->next; pos != NULL; pos = pos->next)
#define LIST_FOR_EACH_SAFE(pos, n, head) \
for (pos = (head)->next, n = pos->next; pos != NULL; pos = n, n = pos->next)
/*************************************/
#define LIST_FIRST_ENTRY(head, type, member) \
LIST_ENTRY((head)->next, type, member)
#define LIST_NEXT_ENTRY(ptr, member) \
LIST_ENTRY((ptr)->member.next, typeof(*ptr), list)
#define LIST_FOR_EACH_ENTRY(pos, head, member) \
for (pos = LIST_ENTRY((head)->next, typeof(*pos), member); \
pos != NULL; \
pos = LIST_ENTRY(pos->member.next, typeof(*pos), member))
#define LIST_FOR_EACH_ENTRY_SAFE(pos, n, head, member) \
for (pos = LIST_ENTRY((head)->next, typeof(*pos), member), \
n = LIST_ENTRY(pos->member.next, typeof(*pos), member); \
&pos->member != NULL; \
pos = n, n = LIST_ENTRY(n->member.next, typeof(*n), member))
/*************************************/
#define N2N_LIST_FIRST_ENTRY(head, type) \
LIST_FIRST_ENTRY(head, type, list)
#define N2N_LIST_NEXT_ENTRY(ptr) \
LIST_NEXT_ENTRY(ptr, list)
#define N2N_LIST_FOR_EACH_ENTRY(pos, head) \
LIST_FOR_EACH_ENTRY(pos, head, list)
#define N2N_LIST_FOR_EACH_ENTRY_SAFE(pos, n, head) \
LIST_FOR_EACH_ENTRY_SAFE(pos, n, head, list)
/*************************************/
typedef int (*cmp_func)(const void *a, const void *b);
void list_add(struct n2n_list *head, struct n2n_list *new);
size_t list_clear(struct n2n_list *head);
size_t list_size(const struct n2n_list *list);
void list_reverse(struct n2n_list *list);
void list_sort(struct n2n_list *list, cmp_func func);
static inline void list_init(struct n2n_list *list)
{
list->next = NULL;
}
static inline int list_empty(const struct n2n_list *head)
{
return (head->next == NULL);
}
/*************************************/
#include <stdio.h>
typedef struct n2n_list *(*read_entry_func)(FILE *f);
typedef void (*write_entry_func)(FILE *f, const void *entry);
int read_list_from_file(const char *filename, struct n2n_list *list, read_entry_func read_entry);
int write_list_to_file(const char *filename, struct n2n_list *list, write_entry_func write_entry);
#endif /* N2N_LIST_H_ */