MediaProcessors
llist.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017, 2018 Rafael Antoniello
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of copyright holders nor the names of its
14  * contributors may be used to endorse or promote products derived
15  * from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18  * “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS OR CONTRIBUTORS
21  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
36 #ifndef UTILS_SRC_LLIST_H_
37 #define UTILS_SRC_LLIST_H_
38 
39 #include <sys/types.h>
40 #include <inttypes.h>
41 
42 /* **** Definitions **** */
43 
44 typedef struct llist_s llist_t;
45 
49 typedef struct llist_s {
50  void *data;
51  llist_t *next;
52 } llist_t;
53 
54 /* **** Prototypes **** */
55 
66 int llist_push(llist_t** ref_llist_head, void *data);
67 
77 void* llist_pop(llist_t** ref_llist_head);
78 
86 int llist_len(const llist_t *llist_head);
87 
94 void* llist_get_nth(const llist_t *llist_head, int index);
95 
107 int llist_insert_nth(llist_t **ref_llist_head, int index, void *data);
108 
121 #define LLIST_DUPLICATE(ref_llist_dst, llist_src, node_dup_fxn, \
122  node_dup_fxn_t, ret_code) \
123 do {\
124  int i;\
125  llist_t **_ref_llist_dst= ref_llist_dst;\
126  const llist_t *_llist_src= llist_src;\
127  node_dup_fxn_t*(*_node_dup_fxn)(const node_dup_fxn_t*)= node_dup_fxn;\
128 \
129  ret_code= STAT_ERROR;\
130 \
131  CHECK_DO(_ref_llist_dst!= NULL, break);\
132  CHECK_DO(_node_dup_fxn!= NULL, break);\
133  if(_llist_src== NULL) {\
134  ret_code= STAT_SUCCESS;\
135  break;\
136  }\
137 \
138  for(i= 0;; i++) {\
139  void *node, *node_copy;\
140 \
141  node= llist_get_nth(_llist_src, i);\
142  if(node== NULL) {\
143  ret_code= STAT_SUCCESS;\
144  break;\
145  }\
146 \
147  node_copy= _node_dup_fxn((const node_dup_fxn_t*)node);\
148  CHECK_DO(node_copy!= NULL, break);\
149 \
150 CHECK_DO(\
151  llist_insert_nth(_ref_llist_dst, i, node_copy)== STAT_SUCCESS,\
152  break);\
153  }\
154 } while(0);
155 
165 #define LLIST_RELEASE(ref_llist_head, node_release_fxn, node_type) \
166 while((*ref_llist_head)!= NULL) {\
167  void (*_node_release_fxn)(node_type**)= node_release_fxn;\
168  node_type *node= (node_type*)llist_pop(ref_llist_head);\
169  if(node!= NULL) _node_release_fxn(&node);\
170  ASSERT(node== NULL);\
171 }
172 
173 #endif /* UTILS_SRC_LLIST_H_ */
int llist_push(llist_t **ref_llist_head, void *data)
Definition: llist.c:57
Definition: llist.h:49
int llist_len(const llist_t *llist_head)
Definition: llist.c:95
void * llist_pop(llist_t **ref_llist_head)
Definition: llist.c:75
void * llist_get_nth(const llist_t *llist_head, int index)
Definition: llist.c:113
struct llist_s llist_t
Definition: llist.h:44
int llist_insert_nth(llist_t **ref_llist_head, int index, void *data)
Definition: llist.c:137