From d9b993fd5dc195e819a142b039d25d768d5c711c Mon Sep 17 00:00:00 2001 From: Sebastian Harl Date: Wed, 20 Aug 2014 20:17:05 -0700 Subject: [PATCH] timeseries: Added sdb_timeseries_create(). This function allocates a time-series object and pre-allocates the data array based on size information passed to the function. All memory is initialized to zero (except for the length information) such that the object contains valid (but mostly useless) information. --- src/core/timeseries.c | 42 +++++++++++++++++++++++++++++++++++ src/include/core/timeseries.h | 13 +++++++++++ 2 files changed, 55 insertions(+) diff --git a/src/core/timeseries.c b/src/core/timeseries.c index 06c321c..17166eb 100644 --- a/src/core/timeseries.c +++ b/src/core/timeseries.c @@ -33,11 +33,53 @@ #include "core/timeseries.h" #include +#include /* * public API */ +sdb_timeseries_t * +sdb_timeseries_create(size_t data_names_len, const char * const *data_names, + size_t data_len) +{ + sdb_timeseries_t *ts; + size_t i; + + ts = calloc(1, sizeof(*ts)); + if (! ts) + return NULL; + + ts->data = calloc(data_names_len, sizeof(*ts->data)); + if (! ts->data) { + sdb_timeseries_destroy(ts); + return NULL; + } + ts->data_names_len = data_names_len; + for (i = 0; i < data_names_len; ++i) { + ts->data[i] = calloc(data_len, sizeof(**ts->data)); + if (! ts->data[i]) { + sdb_timeseries_destroy(ts); + return NULL; + } + } + ts->data_len = data_len; + + ts->data_names = calloc(data_names_len, sizeof(*ts->data_names)); + if (! ts->data_names) { + sdb_timeseries_destroy(ts); + return NULL; + } + for (i = 0; i < data_names_len; ++i) { + ts->data_names[i] = strdup(data_names[i]); + if (! ts->data_names[i]) { + sdb_timeseries_destroy(ts); + return NULL; + } + } + return ts; +} /* sdb_timeseries_create */ + void sdb_timeseries_destroy(sdb_timeseries_t *ts) { diff --git a/src/include/core/timeseries.h b/src/include/core/timeseries.h index acfdaef..1a8e48e 100644 --- a/src/include/core/timeseries.h +++ b/src/include/core/timeseries.h @@ -70,6 +70,19 @@ typedef struct { sdb_time_t end; } sdb_timeseries_opts_t; +/* + * sdb_timeseries_create: + * Allocate a time-series object, pre-populating the data_names information + * and allocating the data field. + * + * Returns: + * - a newly allocated time-series object on success + * - NULL else + */ +sdb_timeseries_t * +sdb_timeseries_create(size_t data_names_len, const char * const *data_names, + size_t data_len); + /* * sdb_timeseries_destroy: * Destroy a time-series object, freeing all of its memory. -- 2.30.2