hamnet70/impl/src/var_array.c

52 lines
999 B
C
Raw Normal View History

2024-05-30 11:00:34 +02:00
#include <errno.h>
#include <string.h>
#include "logger.h"
#include "var_array.h"
bool var_array_cf_init(var_array_cf_t *array)
{
array->used = 0;
array->allocated = 64;
array->symbols = reallocarray(NULL, array->allocated, sizeof(float complex));
return (array->symbols != NULL);
}
void var_array_cf_free(var_array_cf_t *array)
{
if(array->symbols) {
free(array->symbols);
array->symbols = NULL;
}
array->used = 0;
array->allocated = 0;
}
void var_array_cf_clear(var_array_cf_t *array)
{
array->used = 0;
}
bool var_array_cf_append(var_array_cf_t *array, float complex symbol)
{
if(array->used == array->allocated) {
size_t new_size = 2 * array->allocated;
float complex *new_symbols = reallocarray(array->symbols, new_size, sizeof(float complex));
if(!new_symbols) {
2024-05-30 11:00:34 +02:00
LOG(LVL_ERR, "reallocarray: %s", strerror(errno));
return false;
}
array->allocated = new_size;
array->symbols = new_symbols;
}
array->symbols[array->used++] = symbol;
return true;
}