Code

timeseries: Added sdb_timeseries_create().
authorSebastian Harl <sh@tokkee.org>
Thu, 21 Aug 2014 03:17:05 +0000 (20:17 -0700)
committerSebastian Harl <sh@tokkee.org>
Thu, 21 Aug 2014 03:17:05 +0000 (20:17 -0700)
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
src/include/core/timeseries.h

index 06c321c3f253198e47b57303bbcbe7b30df4d6ad..17166eb82a88a6bcef20765975ad69275f1de656 100644 (file)
 #include "core/timeseries.h"
 
 #include <stdlib.h>
+#include <string.h>
 
 /*
  * 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)
 {
index acfdaeff52094f9b37ff7d587ece03793d59a315..1a8e48e47dc4db49cfb243d825bd33910f9125f4 100644 (file)
@@ -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.