Imported Upstream version 1.1.1
[platform/upstream/iotivity.git] / resource / csdk / connectivity / lib / libcoap-4.1.1 / t_list.h
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 {
80     struct list *next;
81 };
82
83 #define LIST_CONCAT(s1, s2) s1##s2
84
85 #define LIST_STRUCT(name)           \
86   void *LIST_CONCAT(name, _list);       \
87   list_t name
88
89 #define LIST_STRUCT_INIT(struct_ptr, name)  {               \
90     (struct_ptr)->name = &((struct_ptr)->LIST_CONCAT(name,_list));  \
91     (struct_ptr)->LIST_CONCAT(name,_list) = NULL;           \
92   }
93
94 static inline void *
95 list_head(list_t the_list)
96 {
97     return *the_list;
98 }
99
100 static inline void list_remove(list_t the_list, void *item)
101 {
102     if (list_head(the_list))
103         LL_DELETE(*(struct list **)the_list, (struct list *)item);
104 }
105
106 static inline void list_add(list_t the_list, void *item)
107 {
108     list_remove(the_list, item);
109     LL_APPEND(*(struct list **)the_list, (struct list *)item);
110 }
111
112 static inline void list_push(list_t the_list, void *item)
113 {
114     LL_PREPEND(*(struct list **)the_list, (struct list *)item);
115 }
116
117 static inline void *
118 list_pop(list_t the_list)
119 {
120     struct list *l;
121     l = (struct list*) *the_list;
122     if (l)
123         list_remove(the_list, l);
124
125     return l;
126 }
127
128 static inline void list_insert(list_t the_list, void *previtem, void *newitem)
129 {
130     if (previtem == NULL)
131     {
132         list_push(the_list, newitem);
133     }
134     else
135     {
136         ((struct list *) newitem)->next = ((struct list *) previtem)->next;
137         ((struct list *) previtem)->next = (struct list*) newitem;
138     }
139 }
140
141 static inline void *
142 list_item_next(void *item)
143 {
144     return item == NULL ? NULL : ((struct list *) item)->next;
145 }
146
147 #else /* WITH_CONTIKI */
148 #include "list.h"
149 #endif /* WITH_CONTIKI */
150
151 #endif /* _DTLS_LIST_H_ */
152