aboutsummaryrefslogtreecommitdiff
path: root/src/utils/narray.c
blob: f2cf0ed98fd708e6bfc12b264ea1b8e07e9f70ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <utils/narray.h>

#ifdef CONFIG_IOE_NARRAY

void narray_add(void ***array, void *data) {
    if (*array == NULL) {
        *array = malloc(2 * sizeof(void *));
        (*array)[0] = data;
        (*array)[1] = NULL;
    } else {
        uint8_t size = narray_size(array);
        *array = realloc(*array, (size + 2) * sizeof(void *));
        (*array)[size] = data;
        (*array)[size + 1] = NULL;
    }
}

void narray_remove(void ***array, void *data) {
    uint8_t i = 0;
    while ((*array)[i] != NULL || (*array)[i] != data)
        i++;
    while ((*array)[i + 1] != NULL) {
        (*array)[i] = (*array)[i + 1];
        i++;
    }
    (*array)[i] = NULL;
    *array = realloc(*array, (i + 1) * sizeof(void *));
}

uint8_t narray_size(void ***array) {
    uint8_t size = 0;
    while ((*array)[size++] != NULL);
    return size;
}

#endif /* CONFIG_IOE_NARRAY */