libyang 6.4.3
libyang is YANG data modelling language parser and toolkit written (and providing API) in C.
Loading...
Searching...
No Matches
Sized Array API

Macros

#define LYA_ADD(ARRAY, EACTION)
 Allocate memory for a new item in a sized array.
 
#define LYA_ADD_ITEM(ARRAY, NEW_ITEM, EACTION)
 Allocate memory for a new item in a sized array.
 
#define LYA_COUNT(ARRAY)   (ARRAY ? LYA_COUNT_(ARRAY) : 0)
 Get the number of records in the ARRAY.
 
#define LYA_COUNT_(ARRAY)   (*((LYA_COUNT_T *)(ARRAY) - 1))
 Internal macro, do not use.
 
#define LYA_COUNT_T   uint64_t
 Type (i.e. size) of the sized array's size counter.
 
#define LYA_DECREMENT(ARRAY)    --LYA_COUNT_(ARRAY)
 Decrement the items counter of a sized array.
 
#define LYA_DECREMENT_FREE(ARRAY)
 Decrement the items counter of a sized array. Free and zero the whole array in case the count was decremented to 0.
 
#define LYA_FOR(ARRAY, INDEX)
 Helper macro to go through sized-arrays with a numeric iterator.
 
#define LYA_FOR_EACH(ARRAY, ITER)
 Helper macro to go through sized-arrays with a pointer iterator.
 
#define LYA_FREE(ARRAY)
 Free the space allocated for a sized array.
 
#define LYA_INCREMENT(ARRAY)    ++LYA_COUNT_(ARRAY)
 Increment the items counter of a sized array.
 
#define LYA_NEW(ARRAY, COUNT, EACTION)
 Allocate memory of a sized array or resize an existing array.
 
#define LYA_PREALLOC(ARRAY, COUNT, EACTION)
 Allocate memory of a sized array or resize an existing array.
 
#define LYA_PRI_COUNT_T   PRIu64
 Printing format specifier macro for LYA_COUNT_T values.
 
#define LYA_REMOVE_VALUE(ARRAY, VALUE)
 Remove a specific value from a sized array.
 

Detailed Description

Generic macros for LY array handling that includes the information about its size.

Macro Definition Documentation

◆ LYA_ADD

#define LYA_ADD (   ARRAY,
  EACTION 
)
Value:
LYA_PREALLOC(ARRAY, LYA_COUNT(ARRAY) + 1, EACTION); \
LYA_INCREMENT(ARRAY)
#define LYA_PREALLOC(ARRAY, COUNT, EACTION)
Allocate memory of a sized array or resize an existing array.
Definition ly_array.h:97
#define LYA_COUNT(ARRAY)
Get the number of records in the ARRAY.
Definition ly_array.h:78

Allocate memory for a new item in a sized array.

Updates the size information.

New item is zeroed.

Parameters
[in,out]ARRAYSized array to manipulate.
[in]EACTIONError action on memory allocation error, must include 'return' or 'goto' commands.

Definition at line 138 of file ly_array.h.

◆ LYA_ADD_ITEM

#define LYA_ADD_ITEM (   ARRAY,
  NEW_ITEM,
  EACTION 
)
Value:
LYA_ADD(ARRAY, EACTION); \
(NEW_ITEM) = &(ARRAY)[LYA_COUNT_(ARRAY) - 1]
#define LYA_COUNT_(ARRAY)
Internal macro, do not use.
Definition ly_array.h:83
#define LYA_ADD(ARRAY, EACTION)
Allocate memory for a new item in a sized array.
Definition ly_array.h:138

Allocate memory for a new item in a sized array.

Updates the size information.

New item is zeroed.

Parameters
[in,out]ARRAYSized array to manipulate.
[out]NEW_ITEMPointer to the new item.
[in]EACTIONError action on memory allocation error, must include 'return' or 'goto' commands.

Definition at line 153 of file ly_array.h.

◆ LYA_COUNT

#define LYA_COUNT (   ARRAY)    (ARRAY ? LYA_COUNT_(ARRAY) : 0)

Get the number of records in the ARRAY.

Parameters
[in]ARRAYSized array to manipulate.

Definition at line 78 of file ly_array.h.

◆ LYA_COUNT_

#define LYA_COUNT_ (   ARRAY)    (*((LYA_COUNT_T *)(ARRAY) - 1))

Internal macro, do not use.

Definition at line 83 of file ly_array.h.

◆ LYA_COUNT_T

#define LYA_COUNT_T   uint64_t

Type (i.e. size) of the sized array's size counter.

To print the value via a print format, use LYA_PRI_COUNT_T specifier.

Definition at line 38 of file ly_array.h.

◆ LYA_DECREMENT

#define LYA_DECREMENT (   ARRAY)     --LYA_COUNT_(ARRAY)

Decrement the items counter of a sized array.

Does not change the allocated memory used by the ARRAY. To do so, use LYA_PREALLOC.

Parameters
[in,out]ARRAYSized array to manipulate.

Definition at line 174 of file ly_array.h.

◆ LYA_DECREMENT_FREE

#define LYA_DECREMENT_FREE (   ARRAY)
Value:
LYA_DECREMENT(ARRAY); \
if (!LYA_COUNT(ARRAY)) { \
LYA_FREE(ARRAY); \
(ARRAY) = NULL; \
}
#define LYA_DECREMENT(ARRAY)
Decrement the items counter of a sized array.
Definition ly_array.h:174

Decrement the items counter of a sized array. Free and zero the whole array in case the count was decremented to 0.

Parameters
[in,out]ARRAYSized array to manipulate.

Definition at line 183 of file ly_array.h.

◆ LYA_FOR

#define LYA_FOR (   ARRAY,
  INDEX 
)
Value:
for (INDEX = 0; \
INDEX < LYA_COUNT(ARRAY); \
++INDEX)

Helper macro to go through sized-arrays with a numeric iterator.

Use with opening curly bracket ({).

The item on the current INDEX in the ARRAY can be accessed in a standard C way as ARRAY[INDEX].

Parameters
[in]ARRAYSized array to manipulate.
[out]INDEXVariable for the iterating index of the item being processed in each loop.

Definition at line 55 of file ly_array.h.

◆ LYA_FOR_EACH

#define LYA_FOR_EACH (   ARRAY,
  ITER 
)
Value:
for (ITER = ARRAY; \
(ARRAY) && ((LYA_COUNT_T)(ITER - ARRAY) < LYA_COUNT(ARRAY)); \
++ITER)
#define LYA_COUNT_T
Type (i.e. size) of the sized array's size counter.
Definition ly_array.h:38

Helper macro to go through sized-arrays with a pointer iterator.

Use with opening curly bracket ({).

Parameters
[in]ARRAYSized array to manipulate.
[out]ITERIterating pointer to the item being processed in each loop.

Definition at line 68 of file ly_array.h.

◆ LYA_FREE

#define LYA_FREE (   ARRAY)
Value:
if (ARRAY) { \
free((LYA_COUNT_T *)(ARRAY) - 1); \
}

Free the space allocated for a sized array.

The items inside the array are not freed.

Parameters
[in,out]ARRAYSized array to manipulate.

Definition at line 197 of file ly_array.h.

◆ LYA_INCREMENT

#define LYA_INCREMENT (   ARRAY)     ++LYA_COUNT_(ARRAY)

Increment the items counter of a sized array.

Does not change the allocated memory used by the ARRAY. To do so, use LYA_PREALLOC.

Parameters
[in,out]ARRAYSized array to manipulate.

Definition at line 164 of file ly_array.h.

◆ LYA_NEW

#define LYA_NEW (   ARRAY,
  COUNT,
  EACTION 
)
Value:
LYA_PREALLOC(ARRAY, COUNT, EACTION); \
LYA_COUNT_(ARRAY) = (COUNT)

Allocate memory of a sized array or resize an existing array.

Updates the size information unlike LYA_PREALLOC.

Any new memory is zeroed.

Parameters
[in,out]ARRAYSized array to manipulate.
[in]COUNTNew count (size) of the items in the array.
[in]EACTIONError action on memory allocation error, must include 'return' or 'goto' commands.

Definition at line 124 of file ly_array.h.

◆ LYA_PREALLOC

#define LYA_PREALLOC (   ARRAY,
  COUNT,
  EACTION 
)
Value:
{ \
LYA_COUNT_T orig_count = LYA_COUNT(ARRAY); \
void *mem = realloc((ARRAY) ? (LYA_COUNT_T *)(ARRAY) - 1 : NULL, \
sizeof(LYA_COUNT_T) + (orig_count + COUNT) * sizeof *(ARRAY)); \
if (!mem) { \
EACTION; \
} \
void *new_array = (LYA_COUNT_T *)mem + 1; \
memcpy(&(ARRAY), &new_array, sizeof(ARRAY)); \
LYA_COUNT_(ARRAY) = orig_count; \
if ((COUNT) > orig_count) { \
memset((ARRAY) + orig_count, 0, ((COUNT) - orig_count) * sizeof *(ARRAY)); \
} \
}

Allocate memory of a sized array or resize an existing array.

Does not set the size information, it is supposed to be incremented via LYA_INCREMENT when the items are filled.

Any new memory is zeroed.

Parameters
[in,out]ARRAYSized array to manipulate.
[in]COUNTNew count (size) of the items in the array.
[in]EACTIONError action on memory allocation error, must include 'return' or 'goto' commands.

Definition at line 97 of file ly_array.h.

◆ LYA_PRI_COUNT_T

#define LYA_PRI_COUNT_T   PRIu64

Printing format specifier macro for LYA_COUNT_T values.

Definition at line 43 of file ly_array.h.

◆ LYA_REMOVE_VALUE

#define LYA_REMOVE_VALUE (   ARRAY,
  VALUE 
)
Value:
{ \
LYA_FOR(ARRAY, u) { \
if ((ARRAY)[u] == VALUE) { \
if (u < LYA_COUNT(ARRAY) - 1) { \
memmove(&((ARRAY)[u]), &((ARRAY)[LYA_COUNT(ARRAY) - 1]), sizeof *(ARRAY)); \
} \
LYA_DECREMENT(ARRAY); \
break; \
} \
} \
}

Remove a specific value from a sized array.

Parameters
[in,out]ARRAYSized array to manipulate.
[in]VALUEValue to remove, only the first occurence is removed.

Definition at line 208 of file ly_array.h.