| 1 |
/* linked lists based on offsets, suitable for shared memory |
| 2 |
* Copyright (C) 2012 Red Hat Inc. |
| 3 |
* |
| 4 |
* This file is part of systemtap, and is free software. You can |
| 5 |
* redistribute it and/or modify it under the terms of the GNU General |
| 6 |
* Public License (GPL); either version 2, or (at your option) any |
| 7 |
* later version. |
| 8 |
*/ |
| 9 |
|
| 10 |
#ifndef _OFFSET_LIST_H |
| 11 |
#define _OFFSET_LIST_H |
| 12 |
|
| 13 |
#include "offptr.h" |
| 14 |
|
| 15 |
|
| 16 |
struct olist_head { |
| 17 |
offptr_t onext, oprev; |
| 18 |
}; |
| 19 |
DEFINE_OFFPTR_GETSET(olist, struct olist_head, struct olist_head, onext); |
| 20 |
DEFINE_OFFPTR_GETSET(olist, struct olist_head, struct olist_head, oprev); |
| 21 |
|
| 22 |
static inline void INIT_OLIST_HEAD(struct olist_head *list) |
| 23 |
{ |
| 24 |
olist_set_onext(list, list); |
| 25 |
olist_set_oprev(list, list); |
| 26 |
} |
| 27 |
|
| 28 |
static inline void __olist_add(struct olist_head *new, |
| 29 |
struct olist_head *prev, |
| 30 |
struct olist_head *next) |
| 31 |
{ |
| 32 |
olist_set_oprev(next, new); |
| 33 |
olist_set_onext(new, next); |
| 34 |
olist_set_oprev(new, prev); |
| 35 |
olist_set_onext(prev, new); |
| 36 |
} |
| 37 |
|
| 38 |
static inline void olist_add(struct olist_head *new, struct olist_head *head) |
| 39 |
{ |
| 40 |
__olist_add(new, head, olist_onext(head)); |
| 41 |
} |
| 42 |
|
| 43 |
static inline void olist_add_tail(struct olist_head *new, struct olist_head *head) |
| 44 |
{ |
| 45 |
__olist_add(new, olist_oprev(head), head); |
| 46 |
} |
| 47 |
|
| 48 |
static inline void __olist_del(struct olist_head * prev, struct olist_head * next) |
| 49 |
{ |
| 50 |
olist_set_oprev(next, prev); |
| 51 |
olist_set_onext(prev, next); |
| 52 |
} |
| 53 |
|
| 54 |
static inline void __olist_del_entry(struct olist_head *entry) |
| 55 |
{ |
| 56 |
__olist_del(olist_oprev(entry), olist_onext(entry)); |
| 57 |
} |
| 58 |
|
| 59 |
static inline void olist_del(struct olist_head *entry) |
| 60 |
{ |
| 61 |
__olist_del(olist_oprev(entry), olist_onext(entry)); |
| 62 |
olist_set_onext(entry, LIST_POISON1); |
| 63 |
olist_set_oprev(entry, LIST_POISON2); |
| 64 |
} |
| 65 |
|
| 66 |
static inline void olist_move_tail(struct olist_head *list, struct olist_head *head) |
| 67 |
{ |
| 68 |
__olist_del_entry(list); |
| 69 |
olist_add_tail(list, head); |
| 70 |
} |
| 71 |
|
| 72 |
static inline int olist_empty(struct olist_head *head) |
| 73 |
{ |
| 74 |
return olist_onext(head) == head; |
| 75 |
} |
| 76 |
|
| 77 |
#define olist_entry(ptr, type, member) \ |
| 78 |
container_of(ptr, type, member) |
| 79 |
|
| 80 |
#define olist_for_each_safe(pos, n, head) \ |
| 81 |
for (pos = olist_onext(head), n = olist_onext(pos); \ |
| 82 |
pos != (head); pos = n, n = olist_onext(pos)) |
| 83 |
|
| 84 |
#define olist_for_each_entry(pos, head, member) \ |
| 85 |
for (pos = olist_entry(olist_onext(head), typeof(*pos), member); \ |
| 86 |
&pos->member != (head); \ |
| 87 |
pos = olist_entry(olist_onext(&pos->member), typeof(*pos), member)) |
| 88 |
|
| 89 |
#define olist_for_each_entry_safe(pos, n, head, member) \ |
| 90 |
for (pos = olist_entry(olist_onext(head), typeof(*pos), member), \ |
| 91 |
n = olist_entry(olist_onext(&pos->member), typeof(*pos), member); \ |
| 92 |
&pos->member != (head); \ |
| 93 |
pos = n, n = olist_entry(olist_onext(&n->member), typeof(*n), member)) |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
struct ohlist_head { |
| 98 |
offptr_t ofirst; |
| 99 |
}; |
| 100 |
struct ohlist_node { |
| 101 |
offptr_t onext, opprev; |
| 102 |
}; |
| 103 |
DEFINE_OFFPTR_GETSET(ohlist, struct ohlist_head, struct ohlist_node, ofirst); |
| 104 |
DEFINE_OFFPTR_GETSET(ohlist, struct ohlist_node, struct ohlist_node, onext); |
| 105 |
DEFINE_OFFPTR_GETSET(ohlist, struct ohlist_node, offptr_t, opprev); |
| 106 |
|
| 107 |
static inline void INIT_OHLIST_HEAD(struct ohlist_head *head) |
| 108 |
{ |
| 109 |
ohlist_set_ofirst(head, NULL); |
| 110 |
} |
| 111 |
|
| 112 |
static inline void INIT_OHLIST_NODE(struct ohlist_node *node) |
| 113 |
{ |
| 114 |
ohlist_set_onext(node, NULL); |
| 115 |
ohlist_set_opprev(node, NULL); |
| 116 |
} |
| 117 |
|
| 118 |
static inline int ohlist_unhashed(struct ohlist_node *node) |
| 119 |
{ |
| 120 |
return !ohlist_opprev(node); |
| 121 |
} |
| 122 |
|
| 123 |
static inline void __ohlist_del(struct ohlist_node *node) |
| 124 |
{ |
| 125 |
struct ohlist_node *next = ohlist_onext(node); |
| 126 |
offptr_t *pprev = ohlist_opprev(node); |
| 127 |
offptr_set(pprev, next); |
| 128 |
if (next) |
| 129 |
ohlist_set_opprev(next, pprev); |
| 130 |
} |
| 131 |
|
| 132 |
static inline void ohlist_del_init(struct ohlist_node *node) |
| 133 |
{ |
| 134 |
if (!ohlist_unhashed(node)) { |
| 135 |
__ohlist_del(node); |
| 136 |
INIT_OHLIST_NODE(node); |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
static inline void ohlist_add_head(struct ohlist_node *node, struct ohlist_head *head) |
| 141 |
{ |
| 142 |
struct ohlist_node *first = ohlist_ofirst(head); |
| 143 |
ohlist_set_onext(node, first); |
| 144 |
if (first) |
| 145 |
ohlist_set_opprev(first, &node->onext); |
| 146 |
ohlist_set_ofirst(head, node); |
| 147 |
ohlist_set_opprev(node, &head->ofirst); |
| 148 |
} |
| 149 |
|
| 150 |
#define ohlist_entry(ptr, type, member) \ |
| 151 |
container_of(ptr, type, member) |
| 152 |
|
| 153 |
#define ohlist_for_each(pos, head) \ |
| 154 |
for (pos = ohlist_ofirst(head); pos ; pos = ohlist_onext(pos)) |
| 155 |
|
| 156 |
#define ohlist_for_each_entry(tpos, pos, head, member) \ |
| 157 |
for (pos = ohlist_ofirst(head); \ |
| 158 |
pos && ({ tpos = ohlist_entry(pos, typeof(*tpos), member); 1;}); \ |
| 159 |
pos = ohlist_onext(pos)) |
| 160 |
|
| 161 |
|
| 162 |
#endif /* _OFFSET_LIST_H */ |