From: Sebastian Harl Date: Thu, 21 Aug 2014 03:17:05 +0000 (-0700) Subject: timeseries: Added sdb_timeseries_create(). X-Git-Tag: sysdb-0.4.0~21 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=d9b993fd5dc195e819a142b039d25d768d5c711c;p=sysdb.git 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. --- 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.