Contiki 2.5
list.h
Go to the documentation of this file.
1 /** \addtogroup lib
2  @{ */
3 /**
4  * \defgroup list Linked list library
5  *
6  * The linked list library provides a set of functions for
7  * manipulating linked lists.
8  *
9  * A linked list is made up of elements where the first element \b
10  * must be a pointer. This pointer is used by the linked list library
11  * to form lists of the elements.
12  *
13  * Lists are declared with the LIST() macro. The declaration specifies
14  * the name of the list that later is used with all list functions.
15  *
16  * Lists can be manipulated by inserting or removing elements from
17  * either sides of the list (list_push(), list_add(), list_pop(),
18  * list_chop()). A specified element can also be removed from inside a
19  * list with list_remove(). The head and tail of a list can be
20  * extracted using list_head() and list_tail(), respectively.
21  *
22  * @{
23  */
24 
25 /**
26  * \file
27  * Linked list manipulation routines.
28  * \author Adam Dunkels <adam@sics.se>
29  *
30  *
31  */
32 
33 
34 
35 /*
36  * Copyright (c) 2004, Swedish Institute of Computer Science.
37  * All rights reserved.
38  *
39  * Redistribution and use in source and binary forms, with or without
40  * modification, are permitted provided that the following conditions
41  * are met:
42  * 1. Redistributions of source code must retain the above copyright
43  * notice, this list of conditions and the following disclaimer.
44  * 2. Redistributions in binary form must reproduce the above copyright
45  * notice, this list of conditions and the following disclaimer in the
46  * documentation and/or other materials provided with the distribution.
47  * 3. Neither the name of the Institute nor the names of its contributors
48  * may be used to endorse or promote products derived from this software
49  * without specific prior written permission.
50  *
51  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
52  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
55  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61  * SUCH DAMAGE.
62  *
63  * This file is part of the Contiki operating system.
64  *
65  * Author: Adam Dunkels <adam@sics.se>
66  *
67  * $Id: list.h,v 1.5 2010/09/13 13:31:00 adamdunkels Exp $
68  */
69 #ifndef __LIST_H__
70 #define __LIST_H__
71 
72 #define LIST_CONCAT2(s1, s2) s1##s2
73 #define LIST_CONCAT(s1, s2) LIST_CONCAT2(s1, s2)
74 
75 /**
76  * Declare a linked list.
77  *
78  * This macro declares a linked list with the specified \c type. The
79  * type \b must be a structure (\c struct) with its first element
80  * being a pointer. This pointer is used by the linked list library to
81  * form the linked lists.
82  *
83  * The list variable is declared as static to make it easy to use in a
84  * single C module without unnecessarily exporting the name to other
85  * modules.
86  *
87  * \param name The name of the list.
88  */
89 #define LIST(name) \
90  static void *LIST_CONCAT(name,_list) = NULL; \
91  static list_t name = (list_t)&LIST_CONCAT(name,_list)
92 
93 /**
94  * Declare a linked list inside a structure declaraction.
95  *
96  * This macro declares a linked list with the specified \c type. The
97  * type \b must be a structure (\c struct) with its first element
98  * being a pointer. This pointer is used by the linked list library to
99  * form the linked lists.
100  *
101  * Internally, the list is defined as two items: the list itself and a
102  * pointer to the list. The pointer has the name of the parameter to
103  * the macro and the name of the list is a concatenation of the name
104  * and the suffix "_list". The pointer must point to the list for the
105  * list to work. Thus the list must be initialized before using.
106  *
107  * The list is initialized with the LIST_STRUCT_INIT() macro.
108  *
109  * \param name The name of the list.
110  */
111 #define LIST_STRUCT(name) \
112  void *LIST_CONCAT(name,_list); \
113  list_t name
114 
115 /**
116  * Initialize a linked list that is part of a structure.
117  *
118  * This macro sets up the internal pointers in a list that has been
119  * defined as part of a struct. This macro must be called before using
120  * the list.
121  *
122  * \param struct_ptr A pointer to the struct
123  * \param name The name of the list.
124  */
125 #define LIST_STRUCT_INIT(struct_ptr, name) \
126  do { \
127  (struct_ptr)->name = &((struct_ptr)->LIST_CONCAT(name,_list)); \
128  (struct_ptr)->LIST_CONCAT(name,_list) = NULL; \
129  list_init((struct_ptr)->name); \
130  } while(0)
131 
132 /**
133  * The linked list type.
134  *
135  */
136 typedef void ** list_t;
137 
138 void list_init(list_t list);
139 void * list_head(list_t list);
140 void * list_tail(list_t list);
141 void * list_pop (list_t list);
142 void list_push(list_t list, void *item);
143 
144 void * list_chop(list_t list);
145 
146 void list_add(list_t list, void *item);
147 void list_remove(list_t list, void *item);
148 
149 int list_length(list_t list);
150 
151 void list_copy(list_t dest, list_t src);
152 
153 void list_insert(list_t list, void *previtem, void *newitem);
154 
155 void * list_item_next(void *item);
156 
157 #endif /* __LIST_H__ */
158 
159 /** @} */
160 /** @} */