Soletta™ Framework
Framework for making IoT devices

Full online documentation | C API Index
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
sol-list.h
Go to the documentation of this file.
1 /*
2  * This file is part of the Soletta (TM) Project
3  *
4  * Copyright (C) 2015 Intel Corporation. All rights reserved.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
19 #pragma once
20 
21 #include <stdlib.h>
22 #include <stdbool.h>
23 #include <stddef.h>
24 
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28 
60 typedef struct sol_list {
61  struct sol_list *next;
62  struct sol_list *prev;
63 } sol_list;
64 
75 #define SOL_LIST_INIT(name) { &(name), &(name) }
76 
86 #define SOL_LIST_GET_CONTAINER(list, type, member) (type *)((char *)(list) - offsetof(type, member))
87 
97 #define SOL_LIST_FOREACH(list, itr) for (itr = (list)->next; itr != (list); itr = itr->next)
98 
111 #define SOL_LIST_FOREACH_SAFE(list, itr, itr_next) for (itr = (list)->next, itr_next = itr->next; itr != (list); itr = itr_next, itr_next = itr_next->next)
112 
118 static inline void
119 sol_list_init(struct sol_list *list)
120 {
121  list->next = list->prev = list;
122 }
123 
130 static inline void
131 sol_list_append(struct sol_list *list, struct sol_list *new_l)
132 {
133  new_l->next = list;
134  new_l->prev = list->prev;
135  list->prev->next = new_l;
136  list->prev = new_l;
137 }
138 
145 static inline void
146 sol_list_prepend(struct sol_list *list, struct sol_list *new_l)
147 {
148  new_l->prev = list;
149  new_l->next = list->next;
150  list->next->prev = new_l;
151  list->next = new_l;
152 }
153 
159 static inline void
161 {
162  list->next->prev = list->prev;
163  list->prev->next = list->next;
164 }
165 
173 static inline bool
175 {
176  return list->next == list;
177 }
178 
188 static inline void
189 sol_list_steal(struct sol_list *list, struct sol_list *new_head)
190 {
191  list->prev->next = new_head;
192  list->next->prev = new_head;
193  new_head->next = list->next;
194  new_head->prev = list->prev;
195  sol_list_init(list);
196 }
197 
202 #ifdef __cplusplus
203 }
204 #endif
struct sol_list sol_list
Structure to add list support to a type.
static void sol_list_prepend(struct sol_list *list, struct sol_list *new_l)
Attach an new node to the beginning of the list.
Definition: sol-list.h:146
struct sol_list * prev
Link to the previous node in the list.
Definition: sol-list.h:62
static bool sol_list_is_empty(struct sol_list *list)
Convenience function to check if the list is empty.
Definition: sol-list.h:174
Structure to add list support to a type.
Definition: sol-list.h:60
static void sol_list_remove(struct sol_list *list)
Remove the node pointed by list from the list.
Definition: sol-list.h:160
static void sol_list_steal(struct sol_list *list, struct sol_list *new_head)
Steals the list nodes from a list.
Definition: sol-list.h:189
static void sol_list_append(struct sol_list *list, struct sol_list *new_l)
Append an new node to the end of the list.
Definition: sol-list.h:131
struct sol_list * next
Link to the next node in the list.
Definition: sol-list.h:61
static void sol_list_init(struct sol_list *list)
Initializes a sol_list struct.
Definition: sol-list.h:119