struct timeval t;
// Metrics
- td_metric* metrics;
unsigned int num_metrics;
+ td_metric metrics[];
};
static int td_metrics_valid_object(td_metrics* self, const char* object) {
td_source_unref(self->source);
if (self->ctx)
td_ctx_unref(self->ctx);
- if (self->metrics)
- free(self->metrics);
free(self);
}
int td_metrics_create(td_metrics** metrics, td_ctx* ctx, td_source* source, const char* object) {
+ unsigned int num_metrics = 0;
td_metrics* self = NULL;
int r;
+ // Fetch how many data sources there are
+ num_metrics = td_source_num_data_sources(source);
+
// Allocate some memory
- self = calloc(1, sizeof(*self));
+ self = calloc(1, sizeof(*self) + (num_metrics * sizeof(*self->metrics)));
if (!self)
return -errno;
}
}
+ // Store the number of data sources
+ self->num_metrics = num_metrics;
+
// Fetch the current time
r = gettimeofday(&self->t, NULL);
if (r < 0) {
goto ERROR;
}
- // Fetch the number of data sources
- self->num_metrics = td_source_num_data_sources(self->source);
-
- // Fail if there are no data sources
- if (!self->num_metrics) {
- ERROR(self->ctx, "Source %s has no data sources\n", td_source_name(self->source));
- r = -EINVAL;
- goto ERROR;
- }
-
- // Allocate space for the metrics
- self->metrics = calloc(self->num_metrics, sizeof(*self->metrics));
- if (!self->metrics) {
- ERROR(self->ctx, "Failed to allocate %u metric(s): %m\n", self->num_metrics);
- r = -errno;
- goto ERROR;
- }
-
// Return the pointer
*metrics = self;
return 0;