summaryrefslogtreecommitdiffstats
path: root/src/common/impl/FFlist.c
blob: eddd0fa464c7758b856d9cbd55a3055a58e2082f (plain) (blame)
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
#include "common/FFlist.h"

#include <stdlib.h>
#include <string.h>

void* ffListAdd(FFlist* list, uint32_t elementSize) {
    if (list->length == list->capacity) {
        ffListReserve(list, elementSize, list->capacity == 0 ? FF_LIST_DEFAULT_ALLOC : list->capacity * 2);
    }

    ++list->length;
    return ffListGet(list, elementSize, list->length - 1);
}

bool ffListShift(FFlist* list, uint32_t elementSize, void* __restrict result) {
    if (list->length == 0) {
        return false;
    }

    memcpy(result, list->data, elementSize);
    memmove(list->data, list->data + elementSize, (size_t) elementSize * (list->length - 1));
    --list->length;
    return true;
}

bool ffListPop(FFlist* list, uint32_t elementSize, void* __restrict result) {
    if (list->length == 0) {
        return false;
    }

    memcpy(result, ffListGet(list, elementSize, list->length - 1), elementSize);
    --list->length;
    return true;
}