-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdl-intru2.h
55 lines (47 loc) · 1.56 KB
/
dl-intru2.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
#ifndef DL_INTRU2_H
#define DL_INTRU2_H
#include <stddef.h> // for offsetof()
typedef struct dl_head_s { // this struct can't be hidden
struct dl_head_s *p[2]; // p[0] points the previous record; p[1] points to the next
} dl_head_t;
/**
* Given a pointer to a struct member, get the pointer to the struct
*
* @param ptr pointer to a struct member
* @param type type of the struct
* @param member name of the member in the struct
*
* @return pointer to the struct
*/
#define dl_container_of(ptr, type, member) ((type *)((char *)(ptr) - offsetof(type, member)))
#ifdef __cplusplus
extern "C" {
#endif
/**
* Push a record to the list
*
* A double-linked list is represented by two pointers: a head and a tail. For
* an empty list, both of them are set to NULL pointers. As such, head[0] and
* head[1] shall be set to NULL on the first call to this function. dl_push()
* implements C++'s push_back() and push_front() at the same time.
*
* @param head head and tail of the list
* @param p pointer to the element to add
* @param dir direction: 0 for pushing from the front; 1 from the back
*/
void dl_push(dl_head_t *head[2], dl_head_t *p, int dir);
/**
* Pop a record from the list
*
* @param head head and tail of the list
* @param dir direction
*
* @return pointer to the record, which is removed from the list. If the list
* is empty, NULL is returned. If the list only has one record, head[0] and
* head[1] are set to NULL, indicating an empty list.
*/
dl_head_t *dl_pop(dl_head_t *head[2], int dir);
#ifdef __cplusplus
}
#endif
#endif