1 /* t_list -- tinydtls lists
2 *
3 * Copyright (C) 2012 Olaf Bergmann <bergmann@tzi.org>
4 *
5 * Permission is hereby granted, free of charge, to any person
6 * obtaining a copy of this software and associated documentation
7 * files (the "Software"), to deal in the Software without
8 * restriction, including without limitation the rights to use, copy,
9 * modify, merge, publish, distribute, sublicense, and/or sell copies
10 * of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 */
25
26 /**
27 * @file t_list.h
28 * @brief Wrappers for list structures and functions
29 */
30
31 #ifndef _DTLS_LIST_H_
32 #define _DTLS_LIST_H_
33
34 #ifndef WITH_CONTIKI
35 #include "uthash.h"
36 #include "utlist.h"
37
38 /* We define list structures and utility functions to be compatible
39 * with Contiki list structures. The Contiki list API is part of the
40 * Contiki operating system, and therefore the following licensing
41 * terms apply (taken from contiki/core/lib/list.h):
42 *
43 * Copyright (c) 2004, Swedish Institute of Computer Science.
44 * All rights reserved.
45 *
46 * Redistribution and use in source and binary forms, with or without
47 * modification, are permitted provided that the following conditions
48 * are met:
49 * 1. Redistributions of source code must retain the above copyright
50 * notice, this list of conditions and the following disclaimer.
51 * 2. Redistributions in binary form must reproduce the above copyright
52 * notice, this list of conditions and the following disclaimer in the
53 * documentation and/or other materials provided with the distribution.
54 * 3. Neither the name of the Institute nor the names of its contributors
55 * may be used to endorse or promote products derived from this software
56 * without specific prior written permission.
57 *
58 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
59 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
60 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
61 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
62 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
63 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
64 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
65 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
66 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
67 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68 * SUCH DAMAGE.
69 *
70 * This file is part of the Contiki operating system.
71 *
72 * Author: Adam Dunkels <adam@sics.se>
73 *
74 * $ Id: list.h,v 1.5 2010/09/13 13:31:00 adamdunkels Exp $
75 */
76
77 typedef void **list_t;
78 struct list {
79 struct list *next;
80 };
81
82 #define LIST_CONCAT(s1, s2) s1##s2
83
84 #define LIST_STRUCT(name) \
85 void *LIST_CONCAT(name, _list); \
86 list_t name
87
88 #define LIST_STRUCT_INIT(struct_ptr, name) { \
89 (struct_ptr)->name = &((struct_ptr)->LIST_CONCAT(name,_list)); \
90 (struct_ptr)->LIST_CONCAT(name,_list) = NULL; \
91 }
92
93 static inline void *
list_head(list_t the_list)94 list_head(list_t the_list) {
95 return *the_list;
96 }
97
98 static inline void
list_remove(list_t the_list,void * item)99 list_remove(list_t the_list, void *item) {
100 if (list_head(the_list))
101 LL_DELETE(*(struct list **)the_list, (struct list *)item);
102 }
103
104 static inline void
list_add(list_t the_list,void * item)105 list_add(list_t the_list, void *item) {
106 list_remove(the_list, item);
107 LL_APPEND(*(struct list **)the_list, (struct list *)item);
108 }
109
110 static inline void
list_push(list_t the_list,void * item)111 list_push(list_t the_list, void *item) {
112 LL_PREPEND(*(struct list **)the_list, (struct list *)item);
113 }
114
115 static inline void *
list_pop(list_t the_list)116 list_pop(list_t the_list) {
117 struct list *l;
118 l = (struct list*)*the_list;
119 if(l)
120 list_remove(the_list, l);
121
122 return l;
123 }
124
125 static inline void
list_insert(list_t the_list,void * previtem,void * newitem)126 list_insert(list_t the_list, void *previtem, void *newitem) {
127 if(previtem == NULL) {
128 list_push(the_list, newitem);
129 } else {
130 ((struct list *)newitem)->next = ((struct list *)previtem)->next;
131 ((struct list *)previtem)->next = (struct list*)newitem;
132 }
133 }
134
135 static inline void *
list_item_next(void * item)136 list_item_next(void *item)
137 {
138 return item == NULL? NULL: ((struct list *)item)->next;
139 }
140
141 #else /* WITH_CONTIKI */
142 #include "list.h"
143 #endif /* WITH_CONTIKI */
144
145 #endif /* _DTLS_LIST_H_ */
146
147