-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.h
47 lines (36 loc) · 1.15 KB
/
utils.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
#include <stdio.h>
#ifndef _UTILS_H
#define _UTILS_H
#define offset(TYPE, MEMBER) ( (size_t) &(((TYPE*)0)->MEMBER))
#define containerOf(ptr, type, member) ({ \
const typeof(((type*)0)->member) *__mptr = (ptr); \
(type*)((char*)__mptr - offset(type, member)); })
#define listEntry(ptr, type, member) \
containerOf(ptr, type, member)
#define list_for_each(pos, head) \
for(pos = (head)->prev; pos != (head); pos = pos->prev)
struct ListNode {
struct ListNode* prev;
struct ListNode* next;
};
static inline void initListNode(struct ListNode *head){
head->prev = head;
head->next = head;
}
static inline void addTailListNode(struct ListNode *head, struct ListNode *item){
item->next = head;
item->prev = head->prev;
head->prev->next = item;
head->prev = item;
};
static inline void addHeadListNode(struct ListNode *head, struct ListNode *item){
item->next = head->next;
item->prev = head->prev;
head->next->prev = item;
head->next = item;
};
static inline void removeListNode(struct ListNode* item){
item->prev->next = item->next;
item->next->prev = item->prev;
};
#endif