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.
This commit is contained in:
adamdunkels 2010-09-13 13:31:00 +00:00
parent 3be23eb3fc
commit 3ae774c91a

View file

@ -64,7 +64,7 @@
*
* Author: Adam Dunkels <adam@sics.se>
*
* $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.
*