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 #include "tinydtls.h"
35
36 #ifndef WITH_CONTIKI
37 #include "uthash.h"
38 #include "utlist.h"
39
40 /* We define list structures and utility functions to be compatible
41 * with Contiki list structures. The Contiki list API is part of the
42 * Contiki operating system, and therefore the following licensing
43 * terms apply (taken from contiki/core/lib/list.h):
44 *
45 * Copyright (c) 2004, Swedish Institute of Computer Science.
46 * All rights reserved.
47 *
48 * Redistribution and use in source and binary forms, with or without
49 * modification, are permitted provided that the following conditions
50 * are met:
51 * 1. Redistributions of source code must retain the above copyright
52 * notice, this list of conditions and the following disclaimer.
53 * 2. Redistributions in binary form must reproduce the above copyright
54 * notice, this list of conditions and the following disclaimer in the
55 * documentation and/or other materials provided with the distribution.
56 * 3. Neither the name of the Institute nor the names of its contributors
57 * may be used to endorse or promote products derived from this software
58 * without specific prior written permission.
59 *
60 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
61 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
62 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
63 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
64 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
65 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
66 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
67 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
68 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
69 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
70 * SUCH DAMAGE.
71 *
72 * This file is part of the Contiki operating system.
73 *
74 * Author: Adam Dunkels <adam@sics.se>
75 *
76 * $ Id: list.h,v 1.5 2010/09/13 13:31:00 adamdunkels Exp $
77 */
78
79 typedef void **list_t;
80 struct list {
81 struct list *next;
82 };
83
84 #define LIST_CONCAT(s1, s2) s1##s2
85
86 #define LIST_STRUCT(name) \
87 void *LIST_CONCAT(name, _list); \
88 list_t name
89
90 #define LIST_STRUCT_INIT(struct_ptr, name) { \
91 (struct_ptr)->name = &((struct_ptr)->LIST_CONCAT(name,_list)); \
92 (struct_ptr)->LIST_CONCAT(name,_list) = NULL; \
93 }
94
95 static inline void *
list_head(list_t list)96 list_head(list_t list) {
97 return *list;
98 }
99
100 static inline void
list_remove(list_t list,void * item)101 list_remove(list_t list, void *item) {
102 LL_DELETE(*(struct list **)list, (struct list *)item);
103 }
104
105 static inline void
list_add(list_t list,void * item)106 list_add(list_t list, void *item) {
107 list_remove(list, item);
108 LL_APPEND(*(struct list **)list, (struct list *)item);
109 }
110
111 static inline void
list_push(list_t list,void * item)112 list_push(list_t list, void *item) {
113 LL_PREPEND(*(struct list **)list, (struct list *)item);
114 }
115
116 static inline void *
list_pop(list_t list)117 list_pop(list_t list) {
118 struct list *l;
119 l = *list;
120 if(l)
121 list_remove(list, l);
122
123 return l;
124 }
125
126 static inline void
list_insert(list_t list,void * previtem,void * newitem)127 list_insert(list_t list, void *previtem, void *newitem) {
128 if(previtem == NULL) {
129 list_push(list, newitem);
130 } else {
131 ((struct list *)newitem)->next = ((struct list *)previtem)->next;
132 ((struct list *)previtem)->next = newitem;
133 }
134 }
135
136 static inline void *
list_item_next(void * item)137 list_item_next(void *item)
138 {
139 return item == NULL? NULL: ((struct list *)item)->next;
140 }
141
142 #else /* WITH_CONTIKI */
143 #include "list.h"
144 #endif /* WITH_CONTIKI */
145
146 #endif /* _DTLS_LIST_H_ */
147
148