1
2
3
4
5
6
7
11
12
18
19
20
21
22
23
24
25
26
29
30
31
32
33
34
35
36
40
41
50
51
56
57
70
71
85
86
91
92
99
100
106
107
113
114
115
116
117
118
119
123
124
125
126
129
130
131
132
133
134
135
136
137
138
142
143
144
145
148
149
150
151
152
153
154
155
156
157
161
162
163
170
171
172
173
174
175
176
177
178
179
182
183
191
192
193
200
201
202
203
204
205
206
207
208
209
212
213
221
222
223
230
231
232
233
234
235
236
237
240
241
242
243
244
245
246
247
248
249
250
257
258
259
260
265
266
271
272
277
278
283
284
285
286
287
288
289
290
291
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#include "bt_common.h"
#include "osi/allocator.h"
#include "osi/list.h"
#include "osi/osi.h"
struct list_node_t {
struct list_node_t *next;
void *data;
}{ ... };
typedef struct list_t {
list_node_t *head;
list_node_t *tail;
size_t length;
list_free_cb free_cb;
}{ ... } list_t;
list_t *list_new_internal(list_free_cb callback)
{
list_t *list = (list_t *) osi_calloc(sizeof(list_t));
if (!list) {
return NULL;
}{...}
list->head = list->tail = NULL;
list->length = 0;
list->free_cb = callback;
return list;
}{ ... }
list_t *list_new(list_free_cb callback)
{
return list_new_internal(callback);
}{ ... }
void list_free(list_t *list)
{
if (!list) {
return;
}{...}
list_clear(list);
osi_free(list);
}{ ... }
bool list_is_empty(const list_t *list)
{
assert(list != NULL);
return (list->length == 0);
}{ ... }
bool list_contains(const list_t *list, const void *data)
{
assert(list != NULL);
assert(data != NULL);
for (const list_node_t *node = list_begin(list); node != list_end(list); node = list_next(node)) {
if (list_node(node) == data) {
return true;
}{...}
}{...}
return false;
}{ ... }
list_node_t *list_get_node(const list_t *list, const void *data)
{
assert(list != NULL);
assert(data != NULL);
list_node_t *p_node_ret = NULL;
for (list_node_t *node = list_begin(list); node != list_end(list); node = list_next(node)) {
if (list_node(node) == data) {
p_node_ret = node;
break;
}{...}
}{...}
return p_node_ret;
}{ ... }
size_t list_length(const list_t *list)
{
assert(list != NULL);
return list->length;
}{ ... }
void *list_front(const list_t *list)
{
assert(list != NULL);
assert(!list_is_empty(list));
return list->head->data;
}{ ... }
void *list_back(const list_t *list) {
assert(list != NULL);
assert(!list_is_empty(list));
return list->tail->data;
}{ ... }
list_node_t *list_back_node(const list_t *list) {
assert(list != NULL);
assert(!list_is_empty(list));
return list->tail;
}{ ... }
bool list_insert_after(list_t *list, list_node_t *prev_node, void *data) {
assert(list != NULL);
assert(prev_node != NULL);
assert(data != NULL);
list_node_t *node = (list_node_t *)osi_calloc(sizeof(list_node_t));
if (!node) {
OSI_TRACE_ERROR("%s osi_calloc failed.\n", __FUNCTION__ );
return false;
}{...}
node->next = prev_node->next;
node->data = data;
prev_node->next = node;
if (list->tail == prev_node) {
list->tail = node;
}{...}
++list->length;
return true;
}{ ... }
bool list_prepend(list_t *list, void *data)
{
assert(list != NULL);
assert(data != NULL);
list_node_t *node = (list_node_t *)osi_calloc(sizeof(list_node_t));
if (!node) {
OSI_TRACE_ERROR("%s osi_calloc failed.\n", __FUNCTION__ );
return false;
}{...}
node->next = list->head;
node->data = data;
list->head = node;
if (list->tail == NULL) {
list->tail = list->head;
}{...}
++list->length;
return true;
}{ ... }
bool list_append(list_t *list, void *data)
{
assert(list != NULL);
assert(data != NULL);
list_node_t *node = (list_node_t *)osi_calloc(sizeof(list_node_t));
if (!node) {
OSI_TRACE_ERROR("%s osi_calloc failed.\n", __FUNCTION__ );
return false;
}{...}
node->next = NULL;
node->data = data;
if (list->tail == NULL) {
list->head = node;
list->tail = node;
}{...} else {
list->tail->next = node;
list->tail = node;
}{...}
++list->length;
return true;
}{ ... }
bool list_remove(list_t *list, void *data)
{
assert(list != NULL);
assert(data != NULL);
if (list_is_empty(list)) {
return false;
}{...}
if (list->head->data == data) {
list_node_t *next = list_free_node(list, list->head);
if (list->tail == list->head) {
list->tail = next;
}{...}
list->head = next;
return true;
}{...}
for (list_node_t *prev = list->head, *node = list->head->next; node; prev = node, node = node->next)
if (node->data == data) {
prev->next = list_free_node(list, node);
if (list->tail == node) {
list->tail = prev;
}{...}
return true;
}{...}
return false;
}{ ... }
bool list_delete(list_t *list, void *data)
{
assert(list != NULL);
assert(data != NULL);
if (list_is_empty(list)) {
return false;
}{...}
if (list->head->data == data) {
list_node_t *next = list_delete_node(list, list->head);
if (list->tail == list->head) {
list->tail = next;
}{...}
list->head = next;
return true;
}{...}
for (list_node_t *prev = list->head, *node = list->head->next; node; prev = node, node = node->next)
if (node->data == data) {
prev->next = list_delete_node(list, node);
if (list->tail == node) {
list->tail = prev;
}{...}
return true;
}{...}
return false;
}{ ... }
void list_clear(list_t *list)
{
assert(list != NULL);
for (list_node_t *node = list->head; node; ) {
node = list_free_node(list, node);
}{...}
list->head = NULL;
list->tail = NULL;
list->length = 0;
}{ ... }
list_node_t *list_foreach(const list_t *list, list_iter_cb callback, void *context)
{
assert(list != NULL);
assert(callback != NULL);
for (list_node_t *node = list->head; node; ) {
list_node_t *next = node->next;
if (!callback(node->data, context)) {
return node;
}{...}
node = next;
}{...}
return NULL;
}{ ... }
list_node_t *list_begin(const list_t *list)
{
assert(list != NULL);
return list->head;
}{ ... }
list_node_t *list_end(UNUSED_ATTR const list_t *list)
{
assert(list != NULL);
return NULL;
}{ ... }
list_node_t *list_next(const list_node_t *node)
{
assert(node != NULL);
return node->next;
}{ ... }
void *list_node(const list_node_t *node)
{
assert(node != NULL);
return node->data;
}{ ... }
list_node_t *list_free_node(list_t *list, list_node_t *node)
{
assert(list != NULL);
assert(node != NULL);
list_node_t *next = node->next;
if (list->free_cb) {
list->free_cb(node->data);
}{...}
osi_free(node);
--list->length;
return next;
}{ ... }
list_node_t *list_delete_node(list_t *list, list_node_t *node)
{
assert(list != NULL);
assert(node != NULL);
list_node_t *next = node->next;
osi_free(node);
--list->length;
return next;
}{ ... }