tizen 2.3 release
[external/buxton.git] / src / shared / buxtonlist.h
1 /*
2  * This file is part of buxton.
3  *
4  * Copyright (C) 2014 Intel Corporation
5  *
6  * buxton is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as
8  * published by the Free Software Foundation; either version 2.1
9  * of the License, or (at your option) any later version.
10  */
11
12 #ifdef HAVE_CONFIG_H
13         #include "config.h"
14 #endif
15
16 #pragma once
17
18 #define _GNU_SOURCE
19 #include <stdlib.h>
20 #include <stdbool.h>
21 #include <stdint.h>
22
23 #define _cleanup_list_ __attribute__ ((cleanup(buxton_list_free)))
24 #define _cleanup_list_all_ __attribute__ ((cleanup(buxton_list_free_all)))
25
26 /**
27  * A singly-linked list
28  */
29 typedef struct BuxtonList {
30         void *data; /**<Data for this item in the list */
31         struct BuxtonList *next; /**<Next item in the list */
32         struct BuxtonList *tail; /**<Tail of the list */
33         uint64_t size; /**<Size of list */
34 } BuxtonList;
35
36 #define BUXTON_LIST_FOREACH(list, elem)                                 \
37         for ((elem) = (list); (elem) != NULL; (elem)=(elem)->next)
38
39
40 /**
41  * Append data to an existing list, or create a new list if NULL
42  *
43  * @note May return false if memory allocation errors exist
44  * @param list Pointer to BuxtonList pointer
45  * @param data Data pointer to store in the list
46  * @return a boolean value, indicating success of the operation
47  */
48 bool buxton_list_append(BuxtonList **list, void *data);
49
50 /*
51  * Prepend data to an existing list, or create a new list if NULL
52  * Much faster than append
53  *
54  * @note May return false if memory allocation errors exist
55  * @param list Pointer to BuxtonList pointer
56  * @param data Data pointer to store in the list
57  * @return a boolean value, indicating success of the operation
58  */
59 bool buxton_list_prepend(BuxtonList **list, void *data);
60
61 /**
62  * Remove the given element from the list
63  *
64  * @param list Pointer to a BuxtonList pointer
65  * @param data Remove item with the matching data pointer
66  * @param do_free Whether to free the item
67  * @return a boolean value, indicating success of the operation
68  */
69 bool buxton_list_remove(BuxtonList **list, void *data, bool do_free);
70
71 /**
72  * Free all items in the list
73  */
74 static inline void buxton_list_free(void *p)
75 {
76         BuxtonList *list = *(BuxtonList**)p;
77         if (!list) {
78                 return;
79         }
80         BuxtonList *elem, *node = NULL;
81         elem = list;
82         while (elem != NULL) {
83                 node = elem->next;
84                 free(elem);
85                 elem = node;
86         }
87 }
88
89 /**
90  * Free all items in the list and their items
91  */
92 static inline void buxton_list_free_all(void *p)
93 {
94         BuxtonList *list = *(BuxtonList**)p;
95         if (!list) {
96                 return;
97         }
98         BuxtonList *elem, *node = NULL;
99         elem = list;
100         while (elem != NULL) {
101                 free(elem->data);
102                 node = elem->next;
103                 free(elem);
104                 elem = node;
105         }
106 }
107
108 /*
109  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
110  *
111  * Local variables:
112  * c-basic-offset: 8
113  * tab-width: 8
114  * indent-tabs-mode: t
115  * End:
116  *
117  * vi: set shiftwidth=8 tabstop=8 noexpandtab:
118  * :indentSize=8:tabSize=8:noTabs=false:
119  */