Contiki 2.6
|
00001 /** \addtogroup lib 00002 @{ */ 00003 /** 00004 * \defgroup list Linked list library 00005 * 00006 * The linked list library provides a set of functions for 00007 * manipulating linked lists. 00008 * 00009 * A linked list is made up of elements where the first element \b 00010 * must be a pointer. This pointer is used by the linked list library 00011 * to form lists of the elements. 00012 * 00013 * Lists are declared with the LIST() macro. The declaration specifies 00014 * the name of the list that later is used with all list functions. 00015 * 00016 * Lists can be manipulated by inserting or removing elements from 00017 * either sides of the list (list_push(), list_add(), list_pop(), 00018 * list_chop()). A specified element can also be removed from inside a 00019 * list with list_remove(). The head and tail of a list can be 00020 * extracted using list_head() and list_tail(), respectively. 00021 * 00022 * @{ 00023 */ 00024 00025 /** 00026 * \file 00027 * Linked list manipulation routines. 00028 * \author Adam Dunkels <adam@sics.se> 00029 * 00030 * 00031 */ 00032 00033 00034 00035 /* 00036 * Copyright (c) 2004, Swedish Institute of Computer Science. 00037 * All rights reserved. 00038 * 00039 * Redistribution and use in source and binary forms, with or without 00040 * modification, are permitted provided that the following conditions 00041 * are met: 00042 * 1. Redistributions of source code must retain the above copyright 00043 * notice, this list of conditions and the following disclaimer. 00044 * 2. Redistributions in binary form must reproduce the above copyright 00045 * notice, this list of conditions and the following disclaimer in the 00046 * documentation and/or other materials provided with the distribution. 00047 * 3. Neither the name of the Institute nor the names of its contributors 00048 * may be used to endorse or promote products derived from this software 00049 * without specific prior written permission. 00050 * 00051 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 00052 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00053 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00054 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 00055 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00056 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 00057 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 00058 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00059 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 00060 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 00061 * SUCH DAMAGE. 00062 * 00063 * This file is part of the Contiki operating system. 00064 * 00065 * Author: Adam Dunkels <adam@sics.se> 00066 * 00067 * $Id: list.h,v 1.5 2010/09/13 13:31:00 adamdunkels Exp $ 00068 */ 00069 #ifndef __LIST_H__ 00070 #define __LIST_H__ 00071 00072 #define LIST_CONCAT2(s1, s2) s1##s2 00073 #define LIST_CONCAT(s1, s2) LIST_CONCAT2(s1, s2) 00074 00075 /** 00076 * Declare a linked list. 00077 * 00078 * This macro declares a linked list with the specified \c type. The 00079 * type \b must be a structure (\c struct) with its first element 00080 * being a pointer. This pointer is used by the linked list library to 00081 * form the linked lists. 00082 * 00083 * The list variable is declared as static to make it easy to use in a 00084 * single C module without unnecessarily exporting the name to other 00085 * modules. 00086 * 00087 * \param name The name of the list. 00088 */ 00089 #define LIST(name) \ 00090 static void *LIST_CONCAT(name,_list) = NULL; \ 00091 static list_t name = (list_t)&LIST_CONCAT(name,_list) 00092 00093 /** 00094 * Declare a linked list inside a structure declaraction. 00095 * 00096 * This macro declares a linked list with the specified \c type. The 00097 * type \b must be a structure (\c struct) with its first element 00098 * being a pointer. This pointer is used by the linked list library to 00099 * form the linked lists. 00100 * 00101 * Internally, the list is defined as two items: the list itself and a 00102 * pointer to the list. The pointer has the name of the parameter to 00103 * the macro and the name of the list is a concatenation of the name 00104 * and the suffix "_list". The pointer must point to the list for the 00105 * list to work. Thus the list must be initialized before using. 00106 * 00107 * The list is initialized with the LIST_STRUCT_INIT() macro. 00108 * 00109 * \param name The name of the list. 00110 */ 00111 #define LIST_STRUCT(name) \ 00112 void *LIST_CONCAT(name,_list); \ 00113 list_t name 00114 00115 /** 00116 * Initialize a linked list that is part of a structure. 00117 * 00118 * This macro sets up the internal pointers in a list that has been 00119 * defined as part of a struct. This macro must be called before using 00120 * the list. 00121 * 00122 * \param struct_ptr A pointer to the struct 00123 * \param name The name of the list. 00124 */ 00125 #define LIST_STRUCT_INIT(struct_ptr, name) \ 00126 do { \ 00127 (struct_ptr)->name = &((struct_ptr)->LIST_CONCAT(name,_list)); \ 00128 (struct_ptr)->LIST_CONCAT(name,_list) = NULL; \ 00129 list_init((struct_ptr)->name); \ 00130 } while(0) 00131 00132 /** 00133 * The linked list type. 00134 * 00135 */ 00136 typedef void ** list_t; 00137 00138 void list_init(list_t list); 00139 void * list_head(list_t list); 00140 void * list_tail(list_t list); 00141 void * list_pop (list_t list); 00142 void list_push(list_t list, void *item); 00143 00144 void * list_chop(list_t list); 00145 00146 void list_add(list_t list, void *item); 00147 void list_remove(list_t list, void *item); 00148 00149 int list_length(list_t list); 00150 00151 void list_copy(list_t dest, list_t src); 00152 00153 void list_insert(list_t list, void *previtem, void *newitem); 00154 00155 void * list_item_next(void *item); 00156 00157 #endif /* __LIST_H__ */ 00158 00159 /** @} */ 00160 /** @} */