libyang 6.4.3
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 orig_count = LYA_COUNT(ARRAY); \
100 void *mem = realloc((ARRAY) ? (LYA_COUNT_T *)(ARRAY) - 1 : NULL, \
101 sizeof(LYA_COUNT_T) + (orig_count + COUNT) * sizeof *(ARRAY)); \
102 if (!mem) { \
103 EACTION; \
104 } \
105 void *new_array = (LYA_COUNT_T *)mem + 1; \
106 memcpy(&(ARRAY), &new_array, sizeof(ARRAY)); \
107 LYA_COUNT_(ARRAY) = orig_count; \
108 if ((COUNT) > orig_count) { \
109 memset((ARRAY) + orig_count, 0, ((COUNT) - orig_count) * sizeof *(ARRAY)); \
110 } \
111 }
112
124#define LYA_NEW(ARRAY, COUNT, EACTION) \
125 LYA_PREALLOC(ARRAY, COUNT, EACTION); \
126 LYA_COUNT_(ARRAY) = (COUNT)
127
138#define LYA_ADD(ARRAY, EACTION) \
139 LYA_PREALLOC(ARRAY, LYA_COUNT(ARRAY) + 1, EACTION); \
140 LYA_INCREMENT(ARRAY)
141
153#define LYA_ADD_ITEM(ARRAY, NEW_ITEM, EACTION) \
154 LYA_ADD(ARRAY, EACTION); \
155 (NEW_ITEM) = &(ARRAY)[LYA_COUNT_(ARRAY) - 1]
156
164#define LYA_INCREMENT(ARRAY) \
165 ++LYA_COUNT_(ARRAY)
166
174#define LYA_DECREMENT(ARRAY) \
175 --LYA_COUNT_(ARRAY)
176
183#define LYA_DECREMENT_FREE(ARRAY) \
184 LYA_DECREMENT(ARRAY); \
185 if (!LYA_COUNT(ARRAY)) { \
186 LYA_FREE(ARRAY); \
187 (ARRAY) = NULL; \
188 }
189
197#define LYA_FREE(ARRAY) \
198 if (ARRAY) { \
199 free((LYA_COUNT_T *)(ARRAY) - 1); \
200 }
201
208#define LYA_REMOVE_VALUE(ARRAY, VALUE) \
209 { \
210 LYA_COUNT_T u; \
211 LYA_FOR(ARRAY, u) { \
212 if ((ARRAY)[u] == VALUE) { \
213 if (u < LYA_COUNT(ARRAY) - 1) { \
214 memmove(&((ARRAY)[u]), &((ARRAY)[LYA_COUNT(ARRAY) - 1]), sizeof *(ARRAY)); \
215 } \
216 LYA_DECREMENT(ARRAY); \
217 break; \
218 } \
219 } \
220 }
221
226#ifdef __cplusplus
227}
228#endif
229
230#endif /* LY_ARRAY_H_ */