mirror of
https://github.com/fastfetch-cli/fastfetch.git
synced 2026-10-30 09:33:39 +01:00
737b36e799
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
44 lines
1.1 KiB
C
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;
|
|
}
|