From cd24f3be4bcb388fd6b4ed48fc9a076ef0e1cee9 Mon Sep 17 00:00:00 2001 From: Gerd Hoffmann Date: Wed, 2 Oct 2013 07:00:13 +0200 Subject: [PATCH 1/8] Introduce and convert pmm code to use standard list helpers. RH-Author: Gerd Hoffmann Message-id: <1380697219-1860-2-git-send-email-kraxel@redhat.com> Patchwork-id: 54628 O-Subject: [RHEL-7 seabios PATCH 1/7] Introduce and convert pmm code to use standard list helpers. Bugzilla: 947051 RH-Acked-by: Laszlo Ersek RH-Acked-by: Bandan Das RH-Acked-by: Miroslav Rezanina From: Kevin O'Connor Signed-off-by: Kevin O'Connor (cherry picked from commit e097a75ef0de08ad6d8660c41efe10c1133f1865) [ rhel6: skip ppm.c changes ] Conflicts: src/pmm.c --- src/list.h | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/types.h | 3 +++ 2 files changed, 79 insertions(+) create mode 100644 src/list.h Signed-off-by: Miroslav Rezanina --- src/list.h | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/types.h | 3 ++ 2 files changed, 79 insertions(+), 0 deletions(-) create mode 100644 src/list.h diff --git a/src/list.h b/src/list.h new file mode 100644 index 0000000..db7e962 --- /dev/null +++ b/src/list.h @@ -0,0 +1,76 @@ +#ifndef __LIST_H +#define __LIST_H + +#include "types.h" // container_of + + +/**************************************************************** + * hlist - Double linked lists with a single pointer list head + ****************************************************************/ + +struct hlist_node { + struct hlist_node *next, **pprev; +}; + +struct hlist_head { + struct hlist_node *first; +}; + +static inline int +hlist_empty(const struct hlist_head *h) +{ + return !h->first; +} + +static inline void +hlist_del(struct hlist_node *n) +{ + struct hlist_node *next = n->next; + struct hlist_node **pprev = n->pprev; + *pprev = next; + if (next) + next->pprev = pprev; +} + +static inline void +hlist_add(struct hlist_node *n, struct hlist_node **pprev) +{ + struct hlist_node *next = *pprev; + n->pprev = pprev; + n->next = next; + if (next) + next->pprev = &n->next; + *pprev = n; +} + +static inline void +hlist_add_head(struct hlist_node *n, struct hlist_head *h) +{ + hlist_add(n, &h->first); +} + +static inline void +hlist_add_before(struct hlist_node *n, struct hlist_node *next) +{ + hlist_add(n, next->pprev); +} + +static inline void +hlist_add_after(struct hlist_node *n, struct hlist_node *prev) +{ + hlist_add(n, &prev->next); +} + +#define hlist_for_each_entry(pos, head, member) \ + for (pos = container_of((head)->first, typeof(*pos), member) \ + ; pos != container_of(NULL, typeof(*pos), member) \ + ; pos = container_of(pos->member.next, typeof(*pos), member)) + +#define hlist_for_each_entry_safe(pos, pprev, head, member) \ + for (pprev = &(head)->first \ + ; *pprev \ + && ({ pos=container_of((*pprev)->next, typeof(*pos), member); 1; }) \ + ; pprev = &(*pprev)->next) + + +#endif // list.h diff --git a/src/types.h b/src/types.h index 24b078e..e5ab4bf 100644 --- a/src/types.h +++ b/src/types.h @@ -121,6 +121,9 @@ extern void __force_link_error__only_in_16bit(void) __noreturn; #define container_of(ptr, type, member) ({ \ const typeof( ((type *)0)->member ) *__mptr = (ptr); \ (type *)( (char *)__mptr - offsetof(type,member) );}) +#define container_of_or_null(ptr, type, member) ({ \ + const typeof( ((type *)0)->member ) *___mptr = (ptr); \ + ___mptr ? container_of(___mptr, type, member) : NULL; }) #define likely(x) __builtin_expect(!!(x), 1) #define unlikely(x) __builtin_expect(!!(x), 0) -- 1.7.1