[sheepdog] [PATCH v2 2/2] split list_head into list_head and list_node
MORITA Kazutaka
morita.kazutaka at gmail.com
Tue Aug 20 03:01:46 CEST 2013
From: MORITA Kazutaka <morita.kazutaka at lab.ntt.co.jp>
Currently, we are abusing struct list_head for both list head and list
elements. This patch introduces struct list_node and uses it for a
list element to make list operations easier to understand. E.g. we
are using list_del_init() to remove a list element and list_empty() to
check the element is linked to a list. With this patch, we use
list_del() to remove an element and list_linked() to check the element
is linked.
Signed-off-by: MORITA Kazutaka <morita.kazutaka at lab.ntt.co.jp>
---
dog/farm/farm.c | 3 +-
dog/farm/object_tree.c | 2 +-
dog/trace.c | 5 +--
dog/treeview.c | 6 +--
include/list.h | 96 ++++++++++++++++++++++++++-------------------
include/work.h | 2 +-
lib/event.c | 2 +-
lib/logger.c | 2 +-
lib/util.c | 36 ++++++++---------
lib/work.c | 2 +-
sheep/cluster.h | 2 +-
sheep/cluster/corosync.c | 2 +-
sheep/cluster/shepherd.c | 4 +-
sheep/cluster/zookeeper.c | 2 +-
sheep/group.c | 2 +-
sheep/object_cache.c | 16 ++++----
sheep/object_list_cache.c | 2 +-
sheep/request.c | 3 --
sheep/sheep_priv.h | 6 +--
sheep/trace/trace.h | 2 +-
sheep/vdi.c | 2 +-
shepherd/shepherd.c | 8 +---
tests/unit/mock/mock.h | 2 +-
23 files changed, 106 insertions(+), 103 deletions(-)
diff --git a/dog/farm/farm.c b/dog/farm/farm.c
index 14f6278..ee47855 100644
--- a/dog/farm/farm.c
+++ b/dog/farm/farm.c
@@ -28,7 +28,7 @@ struct vdi_entry {
uint32_t vdi_id;
uint32_t snap_id;
uint8_t nr_copies;
- struct list_head list;
+ struct list_node list;
};
static LIST_HEAD(last_vdi_list);
@@ -62,7 +62,6 @@ static struct vdi_entry *new_vdi(const char *name, uint64_t vdi_size,
vdi->vdi_id = vdi_id;
vdi->snap_id = snap_id;
vdi->nr_copies = nr_copies;
- INIT_LIST_HEAD(&vdi->list);
return vdi;
}
diff --git a/dog/farm/object_tree.c b/dog/farm/object_tree.c
index 93ee9a1..53724e9 100644
--- a/dog/farm/object_tree.c
+++ b/dog/farm/object_tree.c
@@ -18,7 +18,7 @@ struct object_tree_entry {
uint64_t oid;
int nr_copies;
struct rb_node node;
- struct list_head list;
+ struct list_node list;
};
struct object_tree {
diff --git a/dog/trace.c b/dog/trace.c
index 162a935..9d44ebd 100644
--- a/dog/trace.c
+++ b/dog/trace.c
@@ -243,7 +243,7 @@ static int graph_cat(int argc, char **argv)
struct graph_stat_entry {
struct rb_node rb;
- struct list_head list;
+ struct list_node list;
char fname[TRACE_FNAME_LEN];
uint64_t duration;
uint16_t nr_calls;
@@ -293,7 +293,6 @@ static void prepare_stat_tree(struct trace_graph_item *item)
pstrcpy(new->fname, sizeof(new->fname), item->fname);
new->duration = item->return_time - item->entry_time;
new->nr_calls = 1;
- INIT_LIST_HEAD(&new->list);
if (stat_tree_insert(new)) {
free(new);
return;
@@ -314,7 +313,7 @@ static void stat_list_print(void)
}
}
-static int stat_list_cmp(void *priv, struct list_head *a, struct list_head *b)
+static int stat_list_cmp(void *priv, struct list_node *a, struct list_node *b)
{
struct graph_stat_entry *ga = container_of(a, struct graph_stat_entry,
list);
diff --git a/dog/treeview.c b/dog/treeview.c
index fe7c7e6..c5bad44 100644
--- a/dog/treeview.c
+++ b/dog/treeview.c
@@ -27,7 +27,7 @@ struct vdi_tree {
uint32_t pvid;
bool highlight;
struct list_head children;
- struct list_head siblings;
+ struct list_node siblings;
};
static int *width, *more;
@@ -158,8 +158,8 @@ static void _dump_tree(struct vdi_tree *current, int level, bool first, bool las
list_for_each_entry(vdi, ¤t->children, siblings) {
_dump_tree(vdi, level + 1,
- &vdi->siblings == current->children.next,
- vdi->siblings.next == ¤t->children);
+ &vdi->siblings == current->children.n.next,
+ vdi->siblings.next == ¤t->children.n);
}
}
diff --git a/include/list.h b/include/list.h
index e37b37e..392cc4b 100644
--- a/include/list.h
+++ b/include/list.h
@@ -4,53 +4,73 @@
/* taken from linux kernel */
#include <stddef.h>
+#include <stdbool.h>
#define container_of(ptr, type, member) ({ \
const typeof(((type *)0)->member) *__mptr = (ptr); \
(type *)((char *)__mptr - offsetof(type, member)); })
+struct list_node {
+ struct list_node *next;
+ struct list_node *prev;
+};
+
struct list_head {
- struct list_head *next, *prev;
+ struct list_node n;
};
-#define LIST_HEAD_INIT(name) { &(name), &(name) }
+#define LIST_HEAD_INIT(name) { { &(name.n), &(name.n) } }
+#define LIST_NODE_INIT { NULL, NULL }
#define LIST_HEAD(name) \
struct list_head name = LIST_HEAD_INIT(name)
+#define LIST_NODE(name) \
+ struct list_node name = LIST_NODE_INIT
static inline void INIT_LIST_HEAD(struct list_head *list)
{
- list->next = list;
- list->prev = list;
+ list->n.next = &list->n;
+ list->n.prev = &list->n;
+}
+
+static inline void INIT_LIST_NODE(struct list_node *list)
+{
+ list->next = NULL;
+ list->prev = NULL;
}
-#define list_first_entry(ptr, type, member) \
- list_entry((ptr)->next, type, member)
+#define list_first_entry(head, type, member) \
+ list_entry((head)->n.next, type, member)
-static inline int list_empty(const struct list_head *head)
+static inline bool list_empty(const struct list_head *head)
{
- return head->next == head;
+ return head->n.next == &head->n;
+}
+
+static inline bool list_linked(const struct list_node *node)
+{
+ return node->next != NULL;
}
#define list_entry(ptr, type, member) \
container_of(ptr, type, member)
#define list_for_each(pos, head) \
- pos = (head)->next; \
- for (typeof(pos) __n = pos->next; pos != (head); \
+ pos = (head)->n.next; \
+ for (typeof(pos) __n = pos->next; pos != &(head)->n; \
pos = __n, __n = pos->next)
#define list_for_each_entry(pos, head, member) \
- pos = list_entry((head)->next, typeof(*pos), member); \
+ pos = list_entry((head)->n.next, typeof(*pos), member); \
for (typeof(pos) __n = list_entry(pos->member.next, typeof(*pos), \
member); \
- &pos->member != (head); \
+ &pos->member != &(head)->n; \
pos = __n, __n = list_entry(__n->member.next, typeof(*__n), \
member))
-static inline void __list_add(struct list_head *new,
- struct list_head *prev,
- struct list_head *next)
+static inline void __list_add(struct list_node *new,
+ struct list_node *prev,
+ struct list_node *next)
{
next->prev = new;
new->next = next;
@@ -58,58 +78,52 @@ static inline void __list_add(struct list_head *new,
prev->next = new;
}
-static inline void list_add(struct list_head *new, struct list_head *head)
+static inline void list_add(struct list_node *new, struct list_head *head)
{
- __list_add(new, head, head->next);
+ __list_add(new, &head->n, head->n.next);
}
-static inline void list_add_tail(struct list_head *new, struct list_head *head)
+static inline void list_add_tail(struct list_node *new, struct list_head *head)
{
- __list_add(new, head->prev, head);
+ __list_add(new, head->n.prev, &head->n);
}
-static inline void __list_del(struct list_head *prev, struct list_head *next)
+static inline void __list_del(struct list_node *prev, struct list_node *next)
{
next->prev = prev;
prev->next = next;
}
-static inline void __list_del_entry(struct list_head *entry)
+static inline void __list_del_entry(struct list_node *entry)
{
__list_del(entry->prev, entry->next);
}
-static inline void list_del(struct list_head *entry)
+static inline void list_del(struct list_node *entry)
{
__list_del(entry->prev, entry->next);
entry->next = entry->prev = NULL;
}
-static inline void list_del_init(struct list_head *entry)
-{
- __list_del_entry(entry);
- INIT_LIST_HEAD(entry);
-}
-
-static inline void list_move(struct list_head *list, struct list_head *head)
+static inline void list_move(struct list_node *list, struct list_head *head)
{
__list_del_entry(list);
list_add(list, head);
}
-static inline void list_move_tail(struct list_head *list,
- struct list_head *head)
+static inline void list_move_tail(struct list_node *list,
+ struct list_head *head)
{
__list_del_entry(list);
list_add_tail(list, head);
}
static inline void __list_splice(const struct list_head *list,
- struct list_head *prev,
- struct list_head *next)
+ struct list_node *prev,
+ struct list_node *next)
{
- struct list_head *first = list->next;
- struct list_head *last = list->prev;
+ struct list_node *first = list->n.next;
+ struct list_node *last = list->n.prev;
first->prev = prev;
prev->next = first;
@@ -122,7 +136,7 @@ static inline void list_splice_init(struct list_head *list,
struct list_head *head)
{
if (!list_empty(list)) {
- __list_splice(list, head, head->next);
+ __list_splice(list, &head->n, head->n.next);
INIT_LIST_HEAD(list);
}
}
@@ -131,7 +145,7 @@ static inline void list_splice_tail_init(struct list_head *list,
struct list_head *head)
{
if (!list_empty(list)) {
- __list_splice(list, head->prev, head);
+ __list_splice(list, head->n.prev, &head->n);
INIT_LIST_HEAD(list);
}
}
@@ -158,12 +172,12 @@ static inline void INIT_HLIST_NODE(struct hlist_node *h)
h->pprev = NULL;
}
-static inline int hlist_unhashed(const struct hlist_node *h)
+static inline bool hlist_unhashed(const struct hlist_node *h)
{
return !h->pprev;
}
-static inline int hlist_empty(const struct hlist_head *h)
+static inline bool hlist_empty(const struct hlist_head *h)
{
return !h->first;
}
@@ -237,6 +251,6 @@ static inline void hlist_add_after(struct hlist_node *n,
pos = __n)
void list_sort(void *priv, struct list_head *head,
- int (*cmp)(void *priv, struct list_head *a,
- struct list_head *b));
+ int (*cmp)(void *priv, struct list_node *a,
+ struct list_node *b));
#endif /* __LIST_H__ */
diff --git a/include/work.h b/include/work.h
index 4dd419e..a5808b5 100644
--- a/include/work.h
+++ b/include/work.h
@@ -11,7 +11,7 @@ struct work;
typedef void (*work_func_t)(struct work *);
struct work {
- struct list_head w_list;
+ struct list_node w_list;
work_func_t fn;
work_func_t done;
};
diff --git a/lib/event.c b/lib/event.c
index d949d68..2143c5e 100644
--- a/lib/event.c
+++ b/lib/event.c
@@ -65,7 +65,7 @@ struct event_info {
event_handler_t handler;
int fd;
void *data;
- struct list_head ei_list;
+ struct list_node ei_list;
int prio;
};
diff --git a/lib/logger.c b/lib/logger.c
index 1f92b08..6c2f96f 100644
--- a/lib/logger.c
+++ b/lib/logger.c
@@ -98,7 +98,7 @@ struct logmsg {
struct log_format {
const char *name;
int (*formatter)(char *, size_t, const struct logmsg *);
- struct list_head list;
+ struct list_node list;
};
#define log_format_register(n, formatter_fn) \
diff --git a/lib/util.c b/lib/util.c
index 5454f0a..5c57449 100644
--- a/lib/util.c
+++ b/lib/util.c
@@ -614,12 +614,12 @@ end:
* to chaining of merge() calls: null-terminated, no reserved or
* sentinel head node, "prev" links not maintained.
*/
-static struct list_head *merge(void *priv,
- int (*cmp)(void *priv, struct list_head *a,
- struct list_head *b),
- struct list_head *a, struct list_head *b)
+static struct list_node *merge(void *priv,
+ int (*cmp)(void *priv, struct list_node *a,
+ struct list_node *b),
+ struct list_node *a, struct list_node *b)
{
- struct list_head head, *tail = &head;
+ struct list_node head, *tail = &head;
while (a && b) {
/* if equal, take 'a' -- important for sort stability */
@@ -645,12 +645,12 @@ static struct list_head *merge(void *priv,
*/
static void
merge_and_restore_back_links(void *priv,
- int (*cmp)(void *priv, struct list_head *a,
- struct list_head *b),
+ int (*cmp)(void *priv, struct list_node *a,
+ struct list_node *b),
struct list_head *head,
- struct list_head *a, struct list_head *b)
+ struct list_node *a, struct list_node *b)
{
- struct list_head *tail = head;
+ struct list_node *tail = &head->n;
while (a && b) {
/* if equal, take 'a' -- important for sort stability */
@@ -680,8 +680,8 @@ merge_and_restore_back_links(void *priv,
tail = tail->next;
} while (tail->next);
- tail->next = head;
- head->prev = tail;
+ tail->next = &head->n;
+ head->n.prev = tail;
}
/*
@@ -699,26 +699,26 @@ merge_and_restore_back_links(void *priv,
* ordering is to be preserved, @cmp must return 0.
*/
void list_sort(void *priv, struct list_head *head,
- int (*cmp)(void *priv, struct list_head *a,
- struct list_head *b))
+ int (*cmp)(void *priv, struct list_node *a,
+ struct list_node *b))
{
/* sorted partial lists -- last slot is a sentinel */
#define MAX_LIST_LENGTH_BITS 20
- struct list_head *part[MAX_LIST_LENGTH_BITS+1];
+ struct list_node *part[MAX_LIST_LENGTH_BITS+1];
int lev; /* index into part[] */
int max_lev = 0;
- struct list_head *list;
+ struct list_node *list;
if (list_empty(head))
return;
memset(part, 0, sizeof(part));
- head->prev->next = NULL;
- list = head->next;
+ head->n.prev->next = NULL;
+ list = head->n.next;
while (list) {
- struct list_head *cur = list;
+ struct list_node *cur = list;
list = list->next;
cur->next = NULL;
diff --git a/lib/work.c b/lib/work.c
index 56beb62..bf9252e 100644
--- a/lib/work.c
+++ b/lib/work.c
@@ -52,7 +52,7 @@ struct worker_info {
const char *name;
struct list_head finished_list;
- struct list_head worker_info_siblings;
+ struct list_node worker_info_siblings;
pthread_mutex_t finished_lock;
pthread_mutex_t startup_lock;
diff --git a/sheep/cluster.h b/sheep/cluster.h
index f493d51..a19a4de 100644
--- a/sheep/cluster.h
+++ b/sheep/cluster.h
@@ -112,7 +112,7 @@ struct cluster_driver {
*/
int (*update_node)(struct sd_node *);
- struct list_head list;
+ struct list_node list;
};
extern struct list_head cluster_drivers;
diff --git a/sheep/cluster/corosync.c b/sheep/cluster/corosync.c
index 7166c11..8c38cd7 100644
--- a/sheep/cluster/corosync.c
+++ b/sheep/cluster/corosync.c
@@ -76,7 +76,7 @@ struct corosync_event {
bool callbacked;
- struct list_head list;
+ struct list_node list;
};
struct corosync_message {
diff --git a/sheep/cluster/shepherd.c b/sheep/cluster/shepherd.c
index 6a2d205..f61f659 100644
--- a/sheep/cluster/shepherd.c
+++ b/sheep/cluster/shepherd.c
@@ -172,7 +172,7 @@ struct sph_event {
bool callbacked, removed;
- struct list_head event_list;
+ struct list_node event_list;
};
static LIST_HEAD(nonblocked_event_list);
@@ -241,8 +241,6 @@ static void push_sph_event(bool nonblock, struct sd_node *sender,
ev->removed = false;
ev->callbacked = false;
- INIT_LIST_HEAD(&ev->event_list);
-
if (nonblock)
list_add_tail(&ev->event_list, &nonblocked_event_list);
else
diff --git a/sheep/cluster/zookeeper.c b/sheep/cluster/zookeeper.c
index 8a9804d..475d0f6 100644
--- a/sheep/cluster/zookeeper.c
+++ b/sheep/cluster/zookeeper.c
@@ -51,7 +51,7 @@ enum zk_event_type {
};
struct zk_node {
- struct list_head list;
+ struct list_node list;
struct rb_node rb;
struct sd_node node;
bool callbacked;
diff --git a/sheep/group.c b/sheep/group.c
index 587e982..450c71d 100644
--- a/sheep/group.c
+++ b/sheep/group.c
@@ -13,7 +13,7 @@
struct node {
struct sd_node ent;
- struct list_head list;
+ struct list_node list;
};
struct get_vdis_work {
diff --git a/sheep/object_cache.c b/sheep/object_cache.c
index 4d22e28..51ecec7 100644
--- a/sheep/object_cache.c
+++ b/sheep/object_cache.c
@@ -44,8 +44,8 @@ struct object_cache_entry {
uint64_t bmap; /* Each bit represents one dirty block in object */
struct object_cache *oc; /* Object cache this entry belongs to */
struct rb_node node; /* For lru tree of object cache */
- struct list_head dirty_list; /* For dirty list of object cache */
- struct list_head lru_list; /* For lru list of object cache */
+ struct list_node dirty_list; /* For dirty list of object cache */
+ struct list_node lru_list; /* For lru list of object cache */
struct sd_lock lock; /* Entry lock */
};
@@ -276,7 +276,7 @@ static void del_from_dirty_list(struct object_cache_entry *entry)
{
struct object_cache *oc = entry->oc;
- list_del_init(&entry->dirty_list);
+ list_del(&entry->dirty_list);
uatomic_dec(&oc->dirty_count);
}
@@ -298,9 +298,9 @@ free_cache_entry(struct object_cache_entry *entry)
struct object_cache *oc = entry->oc;
rb_erase(&entry->node, &oc->lru_tree);
- list_del_init(&entry->lru_list);
+ list_del(&entry->lru_list);
oc->total_count--;
- if (!list_empty(&entry->dirty_list))
+ if (list_linked(&entry->dirty_list))
del_from_dirty_list(entry);
sd_destroy_lock(&entry->lock);
free(entry);
@@ -443,7 +443,7 @@ static int write_cache_object(struct object_cache_entry *entry, void *buf,
write_lock_cache(oc);
if (writeback) {
entry->bmap |= calc_object_bmap(oid, count, offset);
- if (list_empty(&entry->dirty_list))
+ if (!list_linked(&entry->dirty_list))
add_to_dirty_list(entry);
}
list_move_tail(&entry->lru_list, &oc->lru_head);
@@ -698,8 +698,8 @@ alloc_cache_entry(struct object_cache *oc, uint32_t idx)
entry->oc = oc;
entry->idx = idx;
sd_init_lock(&entry->lock);
- INIT_LIST_HEAD(&entry->dirty_list);
- INIT_LIST_HEAD(&entry->lru_list);
+ INIT_LIST_NODE(&entry->dirty_list);
+ INIT_LIST_NODE(&entry->lru_list);
return entry;
}
diff --git a/sheep/object_list_cache.c b/sheep/object_list_cache.c
index f572544..831edb5 100644
--- a/sheep/object_list_cache.c
+++ b/sheep/object_list_cache.c
@@ -15,7 +15,7 @@
struct objlist_cache_entry {
uint64_t oid;
- struct list_head list;
+ struct list_node list;
struct rb_node node;
};
diff --git a/sheep/request.c b/sheep/request.c
index 27ccb72..a79f648 100644
--- a/sheep/request.c
+++ b/sheep/request.c
@@ -413,8 +413,6 @@ static struct request *alloc_local_request(void *data, int data_length)
req->local = true;
- INIT_LIST_HEAD(&req->request_list);
-
refcount_set(&req->refcnt, 1);
return req;
@@ -482,7 +480,6 @@ static struct request *alloc_request(struct client_info *ci, int data_length)
}
}
- INIT_LIST_HEAD(&req->request_list);
refcount_set(&req->refcnt, 1);
uatomic_inc(&sys->nr_outstanding_reqs);
diff --git a/sheep/sheep_priv.h b/sheep/sheep_priv.h
index c38cb96..35c067e 100644
--- a/sheep/sheep_priv.h
+++ b/sheep/sheep_priv.h
@@ -93,8 +93,8 @@ struct request {
unsigned int data_length;
struct client_info *ci;
- struct list_head request_list;
- struct list_head pending_list;
+ struct list_node request_list;
+ struct list_node pending_list;
refcnt_t refcnt;
bool local;
@@ -187,7 +187,7 @@ struct vdi_state {
};
struct store_driver {
- struct list_head list;
+ struct list_node list;
const char *name;
int (*init)(void);
bool (*exist)(uint64_t oid);
diff --git a/sheep/trace/trace.h b/sheep/trace/trace.h
index 3c8a5b3..a50fa33 100644
--- a/sheep/trace/trace.h
+++ b/sheep/trace/trace.h
@@ -22,7 +22,7 @@ struct tracer {
/* internal use only */
uatomic_bool enabled;
- struct list_head list;
+ struct list_node list;
};
#define SD_MAX_STACK_DEPTH 1024
diff --git a/sheep/vdi.c b/sheep/vdi.c
index c957059..62bf0a8 100644
--- a/sheep/vdi.c
+++ b/sheep/vdi.c
@@ -579,7 +579,7 @@ struct deletion_work {
uint32_t done;
struct work work;
- struct list_head list;
+ struct list_node list;
struct request *req;
uint32_t vid;
diff --git a/shepherd/shepherd.c b/shepherd/shepherd.c
index 77c09fb..4ad4944 100644
--- a/shepherd/shepherd.c
+++ b/shepherd/shepherd.c
@@ -57,8 +57,8 @@ struct sheep {
enum sheep_state state;
- struct list_head sheep_list;
- struct list_head join_wait_list;
+ struct list_node sheep_list;
+ struct list_node join_wait_list;
};
static LIST_HEAD(sheep_list_head);
@@ -209,7 +209,6 @@ retry:
waiting = list_first_entry(&join_wait_queue,
struct sheep, join_wait_list);
list_del(&waiting->join_wait_list);
- INIT_LIST_HEAD(&waiting->join_wait_list);
memset(&snd, 0, sizeof(snd));
snd.type = SPH_SRV_MSG_JOIN_RETRY;
@@ -569,7 +568,6 @@ static void sheep_accept_handler(int fd, int events, void *data)
socklen_t len;
new_sheep = xzalloc(sizeof(struct sheep));
- INIT_LIST_HEAD(&new_sheep->sheep_list);
len = sizeof(struct sockaddr_in);
new_sheep->fd = accept(fd, (struct sockaddr *)&new_sheep->addr, &len);
@@ -592,8 +590,6 @@ static void sheep_accept_handler(int fd, int events, void *data)
list_add_tail(&new_sheep->sheep_list, &sheep_list_head);
new_sheep->state = SHEEP_STATE_CONNECTED;
- INIT_LIST_HEAD(&new_sheep->join_wait_list);
-
sd_info("accepted new sheep connection");
return;
diff --git a/tests/unit/mock/mock.h b/tests/unit/mock/mock.h
index 0bb1bee..9425c76 100644
--- a/tests/unit/mock/mock.h
+++ b/tests/unit/mock/mock.h
@@ -19,7 +19,7 @@
struct mock_method {
const char *name;
int nr_call;
- struct list_head list;
+ struct list_node list;
};
extern struct list_head mock_methods;
--
1.7.9.5
More information about the sheepdog
mailing list