|
1 | 1 | #ifndef DL_INTRU2_H |
2 | 2 | #define DL_INTRU2_H |
3 | 3 |
|
4 | | -#include <stddef.h> |
| 4 | +#include <stddef.h> // for offsetof() |
5 | 5 |
|
6 | | -#define dl_container_of(ptr, type, member) ((type *)((char *)(ptr) - offsetof(type, member))) |
7 | | - |
8 | | -typedef struct dl_head_s { |
9 | | - struct dl_head_s *p[2]; |
| 6 | +typedef struct dl_head_s { // this struct can't be hidden |
| 7 | + struct dl_head_s *p[2]; // p[0] points the previous record; p[1] points to the next |
10 | 8 | } dl_head_t; |
11 | 9 |
|
| 10 | +/** |
| 11 | + * Given a pointer to a struct member, get the pointer to the struct |
| 12 | + * |
| 13 | + * @param ptr pointer to a struct member |
| 14 | + * @param type type of the struct |
| 15 | + * @param member name of the member in the struct |
| 16 | + * |
| 17 | + * @return pointer to the struct |
| 18 | + */ |
| 19 | +#define dl_container_of(ptr, type, member) ((type *)((char *)(ptr) - offsetof(type, member))) |
| 20 | + |
12 | 21 | #ifdef __cplusplus |
13 | 22 | extern "C" { |
14 | 23 | #endif |
15 | 24 |
|
| 25 | +/** |
| 26 | + * Push a record to the list |
| 27 | + * |
| 28 | + * A double-linked list is represented by two pointers: a head and a tail. For |
| 29 | + * an empty list, both of them are set to NULL pointers. As such, head[0] and |
| 30 | + * head[1] shall be set to NULL on the first call to this function. dl_push() |
| 31 | + * implements C++'s push_back() and push_front() at the same time. |
| 32 | + * |
| 33 | + * @param head head and tail of the list |
| 34 | + * @param p pointer to the element to add |
| 35 | + * @param dir direction: 0 for pushing from the front; 1 from the back |
| 36 | + */ |
16 | 37 | void dl_push(dl_head_t *head[2], dl_head_t *p, int dir); |
| 38 | + |
| 39 | +/** |
| 40 | + * Pop a record from the list |
| 41 | + * |
| 42 | + * @param head head and tail of the list |
| 43 | + * @param dir direction |
| 44 | + * |
| 45 | + * @return pointer to the record, which is removed from the list. If the list |
| 46 | + * is empty, NULL is returned. If the list only has one record, head[0] and |
| 47 | + * head[1] are set to NULL, indicating an empty list. |
| 48 | + */ |
17 | 49 | dl_head_t *dl_pop(dl_head_t *head[2], int dir); |
18 | 50 |
|
19 | 51 | #ifdef __cplusplus |
|
0 commit comments