From 3ae774c91adde0867f9b83af1f659702d8891447 Mon Sep 17 00:00:00 2001 From: adamdunkels Date: Mon, 13 Sep 2010 13:31:00 +0000 Subject: [PATCH] Added a way to for specifying lists as parts of structs. These lists must be initialized with a special LIST_STRUCT_INIT() call before using. --- core/lib/list.h | 41 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/core/lib/list.h b/core/lib/list.h index f98364be3..ae5f3a134 100644 --- a/core/lib/list.h +++ b/core/lib/list.h @@ -64,7 +64,7 @@ * * Author: Adam Dunkels * - * $Id: list.h,v 1.4 2010/06/15 18:54:27 adamdunkels Exp $ + * $Id: list.h,v 1.5 2010/09/13 13:31:00 adamdunkels Exp $ */ #ifndef __LIST_H__ #define __LIST_H__ @@ -90,6 +90,45 @@ static void *LIST_CONCAT(name,_list) = NULL; \ static list_t name = (list_t)&LIST_CONCAT(name,_list) +/** + * Declare a linked list inside a structure declaraction. + * + * This macro declares a linked list with the specified \c type. The + * type \b must be a structure (\c struct) with its first element + * being a pointer. This pointer is used by the linked list library to + * form the linked lists. + * + * Internally, the list is defined as two items: the list itself and a + * pointer to the list. The pointer has the name of the parameter to + * the macro and the name of the list is a concatenation of the name + * and the suffix "_list". The pointer must point to the list for the + * list to work. Thus the list must be initialized before using. + * + * The list is initialized with the LIST_STRUCT_INIT() macro. + * + * \param name The name of the list. + */ +#define LIST_STRUCT(name) \ + void *LIST_CONCAT(name,_list); \ + list_t name + +/** + * Initialize a linked list that is part of a structure. + * + * This macro sets up the internal pointers in a list that has been + * defined as part of a struct. This macro must be called before using + * the list. + * + * \param struct_ptr A pointer to the struct + * \param name The name of the list. + */ +#define LIST_STRUCT_INIT(struct_ptr, name) \ + do { \ + (struct_ptr)->name = &((struct_ptr)->LIST_CONCAT(name,_list)); \ + (struct_ptr)->LIST_CONCAT(name,_list) = NULL; \ + list_init((struct_ptr)->name); \ + } while(0) + /** * The linked list type. *