From patchwork Mon Feb 16 06:57:15 2009 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geoff Levand X-Patchwork-Id: 23205 X-Patchwork-Delegate: jk@ozlabs.org Return-Path: X-Original-To: patchwork-incoming@ozlabs.org Delivered-To: patchwork-incoming@ozlabs.org Received: from ozlabs.org (localhost [127.0.0.1]) by ozlabs.org (Postfix) with ESMTP id CAFF3DDF91 for ; Mon, 16 Feb 2009 18:03:51 +1100 (EST) X-Original-To: cbe-oss-dev@ozlabs.org Delivered-To: cbe-oss-dev@ozlabs.org Received: from hera.kernel.org (hera.kernel.org [140.211.167.34]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 28CFFDDE17; Mon, 16 Feb 2009 18:03:00 +1100 (EST) Received: from hera.kernel.org (IDENT:U2FsdGVkX18vhf73LmrpwJqaBCYVnzrJSNhwZ7kqSZU@localhost [127.0.0.1]) by hera.kernel.org (8.14.2/8.14.2) with ESMTP id n1G72tdC021685 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Mon, 16 Feb 2009 07:02:55 GMT Received: (from geoff@localhost) by hera.kernel.org (8.14.2/8.13.1/Submit) id n1G72sXM021684; Mon, 16 Feb 2009 07:02:54 GMT Message-Id: <20090216065712.814757320@am.sony.com> References: <20090216065712.595147007@am.sony.com> In-reply-to: <20090216065712.595147007@am.sony.com> User-Agent: quilt/0.46-1 Date: Sun, 15 Feb 2009 22:57:15 -0800 From: Geoff Levand To: Jeremy Kerr Content-Disposition: inline; filename=list-debug-signatures.diff X-Virus-Scanned: ClamAV 0.93.3/8994/Sun Feb 15 23:37:25 2009 on hera.kernel.org X-Virus-Status: Clean X-Spam-Status: No, score=-2.5 required=5.0 tests=AWL,BAYES_00, UNPARSEABLE_RELAY autolearn=ham version=3.2.5 X-Spam-Checker-Version: SpamAssassin 3.2.5 (2008-06-10) on hera.kernel.org X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.0 (hera.kernel.org [127.0.0.1]); Mon, 16 Feb 2009 07:02:56 +0000 (UTC) Cc: cbe-oss-dev@ozlabs.org Subject: [Cbe-oss-dev] [patch 3/8] petitboot: Add list debugging support X-BeenThere: cbe-oss-dev@ozlabs.org X-Mailman-Version: 2.1.11 Precedence: list List-Id: Discussion about Open Source Software for the Cell Broadband Engine List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: cbe-oss-dev-bounces+patchwork-incoming=ozlabs.org@ozlabs.org Errors-To: cbe-oss-dev-bounces+patchwork-incoming=ozlabs.org@ozlabs.org Add list and list item signature checking when the DEBUG preprocessor macro is defined. The signatures are checked during most list operations. Signed-off-by: Geoff Levand --- lib/list/list.c | 8 ++-- lib/list/list.h | 101 +++++++++++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 94 insertions(+), 15 deletions(-) --- a/lib/list/list.c +++ b/lib/list/list.c @@ -1,13 +1,13 @@ #include "list/list.h" -void list_init(struct list *list) +void list_init_nc(struct list *list) { list->head.next = &list->head; list->head.prev = &list->head; } -void list_insert_before(struct list_item *next, struct list_item *new) +void list_insert_before_nc(struct list_item *next, struct list_item *new) { new->next = next; new->prev = next->prev; @@ -15,7 +15,7 @@ void list_insert_before(struct list_item next->prev = new; } -void list_insert_after(struct list_item *prev, struct list_item *new) +void list_insert_after_nc(struct list_item *prev, struct list_item *new) { new->next = prev->next; new->prev = prev; @@ -23,7 +23,7 @@ void list_insert_after(struct list_item prev->next = new; } -void list_remove(struct list_item *item) +void list_remove_nc(struct list_item *item) { item->next->prev = item->prev; item->prev->next = item->next; --- a/lib/list/list.h +++ b/lib/list/list.h @@ -1,14 +1,43 @@ #ifndef _LIST_H #define _LIST_H +#define DEBUG + +#if defined(DEBUG) +# undef NDEBUG +#endif + +#include + +enum list_sig { + list_sig = 1111, + list_item_sig = 2222, + list_removed_sig = -3333, +}; + struct list_item { +#if defined(DEBUG) + enum list_sig i_sig; +#endif struct list_item *prev, *next; }; struct list { +#if defined(DEBUG) + enum list_sig l_sig; +#endif struct list_item head; }; +#if defined(DEBUG) +# define list_item_check(_i) assert((_i)->i_sig == list_item_sig) +# define list_check(_l) (assert((_l)->l_sig == list_sig), \ + list_item_check(&(_l)->head)) +#else +static inline void list_item_check(struct list_item *i) {} +static inline void list_check(struct list *l) {} +#endif + #ifndef container_of #define container_of(ptr, type, member) ({ \ const typeof( ((type *)0)->member ) *__mptr = (ptr); \ @@ -20,31 +49,81 @@ struct list { #endif #define list_for_each(list, pos) \ - for (pos = (list)->head.next; pos != ((list)->head); pos = pos->next) + for (list_check(list), \ + pos = (list)->head.next; pos != ((list)->head); \ + pos = pos->next, list_item_check(pos)) #define list_entry(ptr, type, member) \ container_of(ptr, type, member) #define list_for_each_entry(list, pos, member) \ - for (pos = list_entry((list)->head.next, typeof(*pos), member); \ + for (list_check(list), \ + pos = list_entry((list)->head.next, typeof(*pos), member); \ &pos->member != &(list)->head; \ - pos = list_entry(pos->member.next, typeof(*pos), member)) + pos = list_entry(pos->member.next, typeof(*pos), member), \ + list_item_check(&pos->member)) + +#define list_continue_each_entry(list, pos, member) \ + for (list_check(list); &pos->member != &(list)->head; \ + pos = list_entry(pos->member.next, typeof(*pos), member), \ + list_item_check(&pos->member)) + +/* No-check versions */ + +void list_init_nc(struct list *list); +void list_insert_before_nc(struct list_item *next, struct list_item *new); +void list_insert_after_nc(struct list_item *prev, struct list_item *new); +void list_remove_nc(struct list_item *item); + +/* Checked versions */ + +static inline void list_init(struct list *list) +{ +#if defined(DEBUG) + list->l_sig = list_sig; + list->head.i_sig = list_item_sig; +#endif + list_init_nc(list); +} -#define list_continue_each_entry(_list, _pos, _member) \ - for (; &_pos->_member != &(_list)->head; \ - _pos = list_entry(_pos->_member.next, typeof(*_pos), _member)) - -void list_init(struct list *list); -void list_insert_before(struct list_item *next, struct list_item *new); -void list_insert_after(struct list_item *prev, struct list_item *new); -void list_remove(struct list_item *item); +static inline void list_insert_before(struct list_item *next, + struct list_item *new) +{ + list_item_check(next); +#if defined(DEBUG) + new->i_sig = list_item_sig; +#endif + list_insert_before_nc(next, new); +} + +static inline void list_insert_after(struct list_item *prev, + struct list_item *new) +{ + list_item_check(prev); +#if defined(DEBUG) + new->i_sig = list_item_sig; +#endif + list_insert_after_nc(prev, new); +} + +static inline void list_remove(struct list_item *item) +{ + list_item_check(item); + list_remove_nc(item); +#if defined(DEBUG) + item->i_sig = list_removed_sig; +#endif +} static inline void list_add(struct list *list, struct list_item *new) { + list_check(list); list_insert_after(&list->head, new); } + static inline void list_add_tail(struct list *list, struct list_item *new) { + list_check(list); list_insert_before(&list->head, new); }