From a873701765e00228463e06795bdde21274e8fa74 Mon Sep 17 00:00:00 2001 From: Sebastian Harl Date: Wed, 27 May 2015 22:49:20 +0200 Subject: [PATCH] store: Let sdb_store_json_formatter_t inherit from sdb_object_t. --- src/core/store_exec.c | 12 ++++---- src/core/store_json.c | 55 ++++++++++++++++++++++------------- src/include/core/store.h | 3 ++ t/unit/core/store_json_test.c | 2 +- 4 files changed, 44 insertions(+), 28 deletions(-) diff --git a/src/core/store_exec.c b/src/core/store_exec.c index 436b36e..6a8e8e7 100644 --- a/src/core/store_exec.c +++ b/src/core/store_exec.c @@ -138,14 +138,14 @@ exec_fetch(sdb_strbuf_t *buf, sdb_strbuf_t *errbuf, int type, "%s %s.%s to JSON", SDB_STORE_TYPE_TO_NAME(type), hostname, name); sdb_strbuf_sprintf(errbuf, "Out of memory"); - free(f); + sdb_object_deref(SDB_OBJ(f)); sdb_object_deref(SDB_OBJ(obj)); return -1; } sdb_object_deref(SDB_OBJ(obj)); sdb_store_json_finish(f); - free(f); + sdb_object_deref(SDB_OBJ(f)); return SDB_CONNECTION_DATA; } /* exec_fetch */ @@ -173,12 +173,12 @@ exec_list(sdb_strbuf_t *buf, sdb_strbuf_t *errbuf, int type, sdb_log(SDB_LOG_ERR, "frontend: Failed to serialize " "store to JSON"); sdb_strbuf_sprintf(errbuf, "Out of memory"); - free(f); + sdb_object_deref(SDB_OBJ(f)); return -1; } sdb_store_json_finish(f); - free(f); + sdb_object_deref(SDB_OBJ(f)); return SDB_CONNECTION_DATA; } /* exec_list */ @@ -208,12 +208,12 @@ exec_lookup(sdb_strbuf_t *buf, sdb_strbuf_t *errbuf, int type, SDB_STORE_TYPE_TO_NAME(type)); sdb_strbuf_sprintf(errbuf, "Failed to lookup %ss", SDB_STORE_TYPE_TO_NAME(type)); - free(f); + sdb_object_deref(SDB_OBJ(f)); return -1; } sdb_store_json_finish(f); - free(f); + sdb_object_deref(SDB_OBJ(f)); return SDB_CONNECTION_DATA; } /* exec_lookup */ diff --git a/src/core/store_json.c b/src/core/store_json.c index 7362691..878cc4f 100644 --- a/src/core/store_json.c +++ b/src/core/store_json.c @@ -48,6 +48,9 @@ */ struct sdb_store_json_formatter { + sdb_object_t super; + + /* The string buffer to write to */ sdb_strbuf_t *buf; /* The context describes the state of the formatter through @@ -61,6 +64,35 @@ struct sdb_store_json_formatter { int type; int flags; }; +#define F(obj) ((sdb_store_json_formatter_t *)(obj)) + +static int +formatter_init(sdb_object_t *obj, va_list ap) +{ + sdb_store_json_formatter_t *f = F(obj); + + f->buf = va_arg(ap, sdb_strbuf_t *); + if (! f->buf) + return -1; + + f->type = va_arg(ap, int); + if ((f->type != SDB_HOST) && (f->type != SDB_SERVICE) && (f->type != SDB_METRIC)) + return -1; + + f->flags = va_arg(ap, int); + + f->context[0] = 0; + f->current = 0; + + f->current_host = NULL; + return 0; +} /* formatter_init */ + +static sdb_type_t formatter_type = { + /* size = */ sizeof(sdb_store_json_formatter_t), + /* init = */ formatter_init, + /* destroy = */ NULL, +}; /* * private helper functions @@ -169,27 +201,8 @@ json_emit(sdb_store_json_formatter_t *f, sdb_store_obj_t *obj) sdb_store_json_formatter_t * sdb_store_json_formatter(sdb_strbuf_t *buf, int type, int flags) { - sdb_store_json_formatter_t *f; - - if (! buf) - return NULL; - - if ((type != SDB_HOST) && (type != SDB_SERVICE) && (type != SDB_METRIC)) - return NULL; - - f = calloc(1, sizeof(*f)); - if (! f) - return NULL; - - f->buf = buf; - f->context[0] = 0; - f->current = 0; - - f->current_host = NULL; - - f->type = type; - f->flags = flags; - return f; + return F(sdb_object_create("json-formatter", formatter_type, + buf, type, flags)); } /* sdb_store_json_formatter */ int diff --git a/src/include/core/store.h b/src/include/core/store.h index 20ca047..289bc2f 100644 --- a/src/include/core/store.h +++ b/src/include/core/store.h @@ -144,6 +144,9 @@ typedef struct sdb_store_matcher sdb_store_matcher_t; /* * A JSON formatter converts stored objects into the JSON format. * See http://www.ietf.org/rfc/rfc4627.txt + * + * A JSON formatter object inherits from sdb_object_t and, thus, may safely be + * cast to a generic object. */ struct sdb_store_json_formatter; typedef struct sdb_store_json_formatter sdb_store_json_formatter_t; diff --git a/t/unit/core/store_json_test.c b/t/unit/core/store_json_test.c index baecf80..6be3c14 100644 --- a/t/unit/core/store_json_test.c +++ b/t/unit/core/store_json_test.c @@ -486,8 +486,8 @@ START_TEST(test_store_tojson) verify_json_output(buf, store_tojson_data[_i].expected); - free(f); sdb_object_deref(SDB_OBJ(filter)); + sdb_object_deref(SDB_OBJ(f)); sdb_strbuf_destroy(buf); } END_TEST -- 2.30.2