libyang 6.4.6
libyang is YANG data modelling language parser and toolkit written (and providing API) in C.
Loading...
Searching...
No Matches
ly_array.h
Go to the documentation of this file.
1
15#ifndef LY_ARRAY_H_
16#define LY_ARRAY_H_
17
18#include <inttypes.h>
19#include <stdlib.h>
20
21#ifdef __cplusplus
22extern "C" {
23#endif
24
38#define LYA_COUNT_T uint64_t
39
43#define LYA_PRI_COUNT_T PRIu64
44
55#define LYA_FOR(ARRAY, INDEX) \
56 for (INDEX = 0; \
57 INDEX < LYA_COUNT(ARRAY); \
58 ++INDEX)
59
68#define LYA_FOR_EACH(ARRAY, ITER) \
69 for (ITER = ARRAY; \
70 (ARRAY) && ((LYA_COUNT_T)(ITER - ARRAY) < LYA_COUNT(ARRAY)); \
71 ++ITER)
72
78#define LYA_COUNT(ARRAY) (ARRAY ? LYA_COUNT_(ARRAY) : 0)
79
83#define LYA_COUNT_(ARRAY) (*((LYA_COUNT_T *)(ARRAY) - 1))
84
97#define LYA_PREALLOC(ARRAY, COUNT, EACTION) \
98 { \
99 LYA_COUNT_T lya_prealloc_orig = LYA_COUNT(ARRAY); \
100 LYA_COUNT_T lya_prealloc_count = (COUNT); \
101 if (lya_prealloc_count < lya_prealloc_orig) { \
102 lya_prealloc_count = lya_prealloc_orig; \
103 } \
104 void *lya_prealloc_mem = realloc((ARRAY) ? (LYA_COUNT_T *)(ARRAY) - 1 : NULL, \
105 sizeof(LYA_COUNT_T) + lya_prealloc_count * sizeof *(ARRAY)); \
106 if (!lya_prealloc_mem) { \
107 EACTION; \
108 } \
109 void *lya_prealloc_array = (LYA_COUNT_T *)lya_prealloc_mem + 1; \
110 memcpy(&(ARRAY), &lya_prealloc_array, sizeof(ARRAY)); \
111 LYA_COUNT_(ARRAY) = lya_prealloc_orig; \
112 memset((ARRAY) + lya_prealloc_orig, 0, (lya_prealloc_count - lya_prealloc_orig) * sizeof *(ARRAY)); \
113 }
114
126#define LYA_NEW(ARRAY, COUNT, EACTION) \
127 LYA_PREALLOC(ARRAY, COUNT, EACTION); \
128 LYA_COUNT_(ARRAY) = (COUNT)
129
140#define LYA_ADD(ARRAY, EACTION) \
141 LYA_PREALLOC(ARRAY, LYA_COUNT(ARRAY) + 1, EACTION); \
142 LYA_INCREMENT(ARRAY)
143
155#define LYA_ADD_ITEM(ARRAY, NEW_ITEM, EACTION) \
156 LYA_ADD(ARRAY, EACTION); \
157 (NEW_ITEM) = &(ARRAY)[LYA_COUNT_(ARRAY) - 1]
158
166#define LYA_INCREMENT(ARRAY) \
167 ++LYA_COUNT_(ARRAY)
168
176#define LYA_DECREMENT(ARRAY) \
177 --LYA_COUNT_(ARRAY)
178
185#define LYA_DECREMENT_FREE(ARRAY) \
186 LYA_DECREMENT(ARRAY); \
187 if (!LYA_COUNT(ARRAY)) { \
188 LYA_FREE(ARRAY); \
189 (ARRAY) = NULL; \
190 }
191
199#define LYA_FREE(ARRAY) \
200 if (ARRAY) { \
201 free((LYA_COUNT_T *)(ARRAY) - 1); \
202 }
203
210#define LYA_REMOVE_VALUE(ARRAY, VALUE) \
211 { \
212 LYA_COUNT_T u; \
213 LYA_FOR(ARRAY, u) { \
214 if ((ARRAY)[u] == VALUE) { \
215 if (u < LYA_COUNT(ARRAY) - 1) { \
216 memmove(&((ARRAY)[u]), &((ARRAY)[LYA_COUNT(ARRAY) - 1]), sizeof *(ARRAY)); \
217 } \
218 LYA_DECREMENT(ARRAY); \
219 break; \
220 } \
221 } \
222 }
223
228#ifdef __cplusplus
229}
230#endif
231
232#endif /* LY_ARRAY_H_ */