Files
fastfetch/src/util/FFlist.c
T
李通洲 737b36e799 FFstrbuf / FFlist: add some improvements
1. make some short fns inlinable
2. simplify implementation
3. fix possible crashes with non-allocated strbuf
4. make the default init fn allocates no memory
5. add more tests
2022-09-24 22:18:48 +08:00

44 lines
1.1 KiB
C

#include "FFlist.h"
#include <stdlib.h>
void ffListInitA(FFlist* list, uint32_t elementSize, uint32_t capacity)
{
list->elementSize = elementSize;
list->capacity = capacity;
list->length = 0;
list->data = capacity == 0 ? NULL : malloc((size_t)list->capacity * list->elementSize);
}
void* ffListAdd(FFlist* list)
{
if(list->length == list->capacity)
{
list->capacity = list->capacity == 0 ? FF_LIST_DEFAULT_ALLOC : list->capacity * 2;
// realloc(NULL, newSize) is same as malloc(newSize)
list->data = realloc(list->data, (size_t)list->capacity * list->elementSize);
}
++list->length;
return ffListGet(list, list->length - 1);
}
uint32_t ffListFirstIndexComp(const FFlist* list, void* compElement, bool(*compFunc)(const void*, const void*))
{
for(uint32_t i = 0; i < list->length; i++)
{
if(compFunc(ffListGet(list, i), compElement))
return i;
}
return list->length;
}
void ffListDestroy(FFlist* list)
{
//Avoid free-after-use. These 3 assignments are cheap so don't remove them
list->capacity = list->length = 0;
free(list->data);
list->data = NULL;
}