author | Sebastian Harl <sh@tokkee.org> | |
Mon, 1 Apr 2013 18:09:28 +0000 (20:09 +0200) | ||
committer | Sebastian Harl <sh@tokkee.org> | |
Mon, 1 Apr 2013 18:09:28 +0000 (20:09 +0200) |
diff --git a/src/core/object.c b/src/core/object.c
index 5ebcff213d827fdb0404e8d7a505ae3c0fcfc9f4..65b256f908675018647e7bc41e9cda6458997ea0 100644 (file)
--- a/src/core/object.c
+++ b/src/core/object.c
#include <string.h>
/*
- * private helper functions
+ * private types
*/
static int
SDB_OBJ_WRAPPER(obj)->data = NULL;
} /* sdb_object_wrapper_destroy */
+static sdb_type_t sdb_object_wrapper_type = {
+ sizeof(sdb_object_wrapper_t),
+
+ sdb_object_wrapper_init,
+ sdb_object_wrapper_destroy,
+ /* clone = */ NULL
+};
+
/*
* public API
*/
sdb_object_t *
-sdb_object_create(size_t size, int (*init)(sdb_object_t *, va_list),
- void (*destructor)(sdb_object_t *), ...)
+sdb_object_create(sdb_type_t type, ...)
{
sdb_object_t *obj;
- obj = malloc(size);
+ if (type.size <= 0)
+ return NULL;
+
+ obj = malloc(type.size);
if (! obj)
return NULL;
memset(obj, 0, sizeof(*obj));
- if (init) {
+ if (type.init) {
va_list ap;
- va_start(ap, destructor);
+ va_start(ap, type);
- if (init(obj, ap)) {
+ if (type.init(obj, ap)) {
obj->ref_cnt = 1;
sdb_object_deref(obj);
va_end(ap);
va_end(ap);
}
+ obj->type = type;
obj->ref_cnt = 1;
- obj->destructor = destructor;
- obj->size = size;
return obj;
} /* sdb_object_create */
sdb_object_t *
sdb_object_create_wrapper(void *data, void (*destructor)(void *))
{
- return sdb_object_create(sizeof(sdb_object_wrapper_t),
- sdb_object_wrapper_init, sdb_object_wrapper_destroy,
- data, destructor);
+ return sdb_object_create(sdb_object_wrapper_type, data, destructor);
} /* sdb_object_create_wrapper */
void
if (obj->ref_cnt > 0)
return;
- if (obj->destructor)
- obj->destructor(obj);
+ if (obj->type.destroy)
+ obj->type.destroy(obj);
free(obj);
} /* sdb_object_deref */
++obj->ref_cnt;
} /* sdb_object_ref */
+sdb_object_t *
+sdb_object_clone(const sdb_object_t *obj)
+{
+ if ((! obj) || (! obj->type.clone))
+ return NULL;
+ return obj->type.clone(obj);
+} /* sdb_object_clone */
+
/* vim: set tw=78 sw=4 ts=4 noexpandtab : */
diff --git a/src/core/plugin.c b/src/core/plugin.c
index 5fdaca0db885b9ba3c0870ac301cd13c17017602..53bba9e74bfc6bcb95522b36ab5c478035e733dc 100644 (file)
--- a/src/core/plugin.c
+++ b/src/core/plugin.c
return SDB_PLUGIN_CB(obj);
} /* sdb_plugin_find_by_name */
+/*
+ * private types
+ */
+
static int
sdb_plugin_cb_init(sdb_object_t *obj, va_list ap)
{
sdb_object_deref(SDB_PLUGIN_CB(obj)->cb_user_data);
} /* sdb_plugin_cb_destroy */
+static sdb_type_t sdb_plugin_cb_type = {
+ sizeof(sdb_plugin_cb_t),
+
+ sdb_plugin_cb_init,
+ sdb_plugin_cb_destroy,
+ /* clone = */ NULL
+};
+
+static sdb_type_t sdb_plugin_collector_cb_type = {
+ sizeof(sdb_plugin_collector_cb_t),
+
+ sdb_plugin_cb_init,
+ sdb_plugin_cb_destroy,
+ /* clone = */ NULL
+};
+
static int
sdb_plugin_add_callback(sdb_llist_t **list, const char *type,
const char *name, void *callback, sdb_object_t *user_data)
if (! *list)
return -1;
- obj = sdb_object_create(sizeof(sdb_plugin_cb_t), sdb_plugin_cb_init,
- sdb_plugin_cb_destroy, list, type, name, callback, user_data);
+ obj = sdb_object_create(sdb_plugin_cb_type,
+ list, type, name, callback, user_data);
if (! obj)
return -1;
@@ -435,8 +455,7 @@ sdb_plugin_register_collector(const char *name, sdb_plugin_collector_cb callback
if (! collector_list)
return -1;
- obj = sdb_object_create(sizeof(sdb_plugin_collector_cb_t),
- sdb_plugin_cb_init, sdb_plugin_cb_destroy,
+ obj = sdb_object_create(sdb_plugin_collector_cb_type,
&collector_list, "collector", name, callback, user_data);
if (! obj)
return -1;
diff --git a/src/core/store.c b/src/core/store.c
index 714a0b39ed18161b07b6bdd3bd856da335abd04d..4ec9f44f204fc5be018595cbc6a61ce950e7d6c2 100644 (file)
--- a/src/core/store.c
+++ b/src/core/store.c
return strcasecmp(obj->name, lookup->obj_name);
} /* sdb_cmp_store_obj_with_name */
+/*
+ * public types
+ */
+
static int
sdb_host_init(sdb_object_t *obj, va_list ap)
{
sdb_llist_destroy(SDB_HOST(obj)->services);
} /* sdb_host_destroy */
+static sdb_object_t *
+sdb_host_do_clone(const sdb_object_t *obj)
+{
+ const sdb_host_t *host = (const sdb_host_t *)obj;
+ sdb_host_t *new;
+
+ new = sdb_host_create(host->_name);
+ if (! new)
+ return NULL;
+
+ /* make sure these are initialized; else sdb_object_deref() might access
+ * arbitrary memory in case of an error */
+ new->services = new->attributes = NULL;
+
+ if (host->attributes) {
+ new->attributes = sdb_llist_clone(host->attributes);
+ if (! new->attributes) {
+ sdb_object_deref(SDB_OBJ(new));
+ return NULL;
+ }
+ }
+
+ new->_last_update = host->_last_update;
+ if (host->services) {
+ new->services = sdb_llist_clone(host->services);
+ if (! new->services) {
+ sdb_object_deref(SDB_OBJ(new));
+ return NULL;
+ }
+ }
+ return SDB_OBJ(new);
+} /* sdb_host_do_clone */
+
static int
sdb_attr_init(sdb_object_t *obj, va_list ap)
{
free(SDB_ATTR(obj)->attr_value);
} /* sdb_attr_destroy */
+static sdb_object_t *
+sdb_attr_clone(const sdb_object_t *obj)
+{
+ const sdb_attribute_t *attr = (const sdb_attribute_t *)obj;
+ sdb_attribute_t *new;
+
+ new = sdb_attribute_create(attr->hostname,
+ attr->_name, attr->attr_value);
+ if (! new)
+ return NULL;
+
+ new->_last_update = attr->_last_update;
+ return SDB_OBJ(new);
+} /* sdb_attr_clone */
+
static int
sdb_svc_init(sdb_object_t *obj, va_list ap)
{
free(SDB_SVC(obj)->_name);
} /* sdb_svc_destroy */
+static sdb_object_t *
+sdb_svc_clone(const sdb_object_t *obj)
+{
+ const sdb_service_t *svc = (const sdb_service_t *)obj;
+ sdb_service_t *new;
+
+ new = sdb_service_create(svc->hostname, svc->_name);
+ if (! new)
+ return NULL;
+
+ new->_last_update = svc->_last_update;
+ return SDB_OBJ(new);
+} /* sdb_svc_clone */
+
+const sdb_type_t sdb_host_type = {
+ sizeof(sdb_host_t),
+
+ sdb_host_init,
+ sdb_host_destroy,
+ sdb_host_do_clone
+};
+
+const sdb_type_t sdb_attribute_type = {
+ sizeof(sdb_attribute_t),
+
+ sdb_attr_init,
+ sdb_attr_destroy,
+ sdb_attr_clone
+};
+
+const sdb_type_t sdb_service_type = {
+ sizeof(sdb_service_t),
+
+ sdb_svc_init,
+ sdb_svc_destroy,
+ sdb_svc_clone
+};
+
/*
* public API
*/
if (! name)
return NULL;
- obj = sdb_object_create(sizeof(sdb_host_t), sdb_host_init,
- sdb_host_destroy, name);
+ obj = sdb_object_create(sdb_host_type, name);
if (! obj)
return NULL;
return SDB_HOST(obj);
} /* sdb_host_create */
-sdb_host_t *
-sdb_host_clone(const sdb_host_t *host)
-{
- sdb_host_t *new;
-
- new = sdb_host_create(host->_name);
- if (! new)
- return NULL;
-
- /* make sure these are initialized; else sdb_object_deref() might access
- * arbitrary memory in case of an error */
- new->services = new->attributes = NULL;
-
- if (host->attributes) {
- new->attributes = sdb_llist_clone(host->attributes);
- if (! new->attributes) {
- sdb_object_deref(SDB_OBJ(new));
- return NULL;
- }
- }
-
- new->_last_update = host->_last_update;
- if (host->services) {
- new->services = sdb_llist_clone(host->services);
- if (! new->services) {
- sdb_object_deref(SDB_OBJ(new));
- return NULL;
- }
- }
- return new;
-} /* sdb_host_clone */
-
int
sdb_store_host(const sdb_host_t *host)
{
}
}
else {
- sdb_host_t *new = sdb_host_clone(host);
+ sdb_host_t *new = SDB_HOST(sdb_object_clone(SDB_CONST_OBJ(host)));
if (! new) {
char errbuf[1024];
sdb_log(SDB_LOG_ERR, "store: Failed to clone host object: %s",
if ((! hostname) || (! name) || (! value))
return NULL;
- obj = sdb_object_create(sizeof(sdb_attribute_t), sdb_attr_init,
- sdb_attr_destroy, hostname, name, value);
+ obj = sdb_object_create(sdb_attribute_type, hostname, name, value);
if (! obj)
return NULL;
return SDB_ATTR(obj);
} /* sdb_attribute_create */
-sdb_attribute_t *
-sdb_attribute_clone(const sdb_attribute_t *attr)
-{
- sdb_attribute_t *new;
-
- new = sdb_attribute_create(attr->hostname,
- attr->_name, attr->attr_value);
- if (! new)
- return NULL;
-
- new->_last_update = attr->_last_update;
- return new;
-} /* sdb_attribute_clone */
-
int
sdb_store_attribute(const sdb_attribute_t *attr)
{
}
}
else {
- sdb_attribute_t *new = sdb_attribute_clone(attr);
+ sdb_attribute_t *new = SDB_ATTR(sdb_object_clone(SDB_CONST_OBJ(attr)));
if (! new) {
char errbuf[1024];
sdb_log(SDB_LOG_ERR, "store: Failed to clone attribute "
if ((! hostname) || (! name))
return NULL;
- obj = sdb_object_create(sizeof(sdb_service_t), sdb_svc_init,
- sdb_svc_destroy, hostname, name);
+ obj = sdb_object_create(sdb_service_type, hostname, name);
if (! obj)
return NULL;
return SDB_SVC(obj);
} /* sdb_service_create */
-sdb_service_t *
-sdb_service_clone(const sdb_service_t *svc)
-{
- sdb_service_t *new;
-
- new = sdb_service_create(svc->hostname, svc->_name);
- if (! new)
- return NULL;
-
- new->_last_update = svc->_last_update;
- return new;
-} /* sdb_service_clone */
-
int
sdb_store_service(const sdb_service_t *svc)
{
}
}
else {
- sdb_service_t *new = sdb_service_clone(svc);
+ sdb_service_t *new = SDB_SVC(sdb_object_clone(SDB_CONST_OBJ(svc)));
if (! new) {
char errbuf[1024];
sdb_log(SDB_LOG_ERR, "store: Failed to clone service "
index f2992c79346c989eb7d31d2fad25665174aab57c..2f466104c86ee10c3973dc964ce3e80469b29f3c 100644 (file)
extern "C" {
#endif
+struct sdb_type;
+typedef struct sdb_type sdb_type_t;
+
struct sdb_object;
typedef struct sdb_object sdb_object_t;
-struct sdb_object {
- int ref_cnt;
- void (*destructor)(sdb_object_t *);
+struct sdb_type {
size_t size;
+
+ int (*init)(sdb_object_t *, va_list);
+ void (*destroy)(sdb_object_t *);
+ sdb_object_t *(*clone)(const sdb_object_t *);
};
-#define SDB_OBJECT_INIT { 1, NULL, 0 }
+#define SDB_TYPE_INIT { 0, NULL, NULL, NULL }
+
+struct sdb_object {
+ sdb_type_t type;
+ int ref_cnt;
+};
+#define SDB_OBJECT_INIT { SDB_TYPE_INIT, 1 }
+#define SDB_OBJECT_TYPED_INIT(t) { (t), 1 }
typedef struct {
sdb_object_t super;
} sdb_object_wrapper_t;
#define SDB_OBJ(obj) ((sdb_object_t *)(obj))
+#define SDB_CONST_OBJ(obj) ((const sdb_object_t *)(obj))
#define SDB_OBJ_WRAPPER(obj) ((sdb_object_wrapper_t *)(obj))
+#define SDB_CONST_OBJ_WRAPPER(obj) ((const sdb_object_wrapper_t *)(obj))
/*
* sdb_object_create:
- * Allocates a new sdb_object_t of the specified 'size'. The object will be
+ * Allocates a new sdb_object_t of the specified 'type'. The object will be
* initialized to zero and then passed on to the 'init' function (if
- * specified). If specified, the 'destructor' will be called, when the
+ * specified). If specified, the 'destroy' callback will be called, when the
* reference count drops to zero and before freeing the memory allocated by
* the object itself.
*
- * If the init function fails (returns a non-zero value), the object will be
- * destructed and destroyed.
+ * The init function will be called with the remaining arguments passed to
+ * sdb_object_create. If the init function fails (returns a non-zero value),
+ * the object will be destructed and destroyed.
*
* The reference count of the new object will be 1.
*
* - NULL on error
*/
sdb_object_t *
-sdb_object_create(size_t size, int (*init)(sdb_object_t *, va_list),
- void (*destructor)(sdb_object_t *), ...);
+sdb_object_create(sdb_type_t type, ...);
/*
* sdb_object_create_wrapper:
* Create a new sdb_object_t wrapping some arbitrary other object.
+ *
+ * Creation and initialization of the wrapped object needs to happen outside
+ * of the SysDB object system.
*/
sdb_object_t *
sdb_object_create_wrapper(void *data, void (*destructor)(void *));
void
sdb_object_ref(sdb_object_t *obj);
+/*
+ * sdb_object_clone:
+ * Clone an existing object using its type's 'clone' callback. The callback is
+ * responsible for correctly initializing a new object (which may be done
+ * using the object create function or the object's type's init function).
+ *
+ * Returns:
+ * - the cloned object on success
+ * - NULL on error or if no clone callback is available
+ */
+sdb_object_t *
+sdb_object_clone(const sdb_object_t *obj);
+
#ifdef __cplusplus
} /* extern "C" */
#endif
index 6bda96783763f2e0234a5eb089857ed40acc23ff..7cdfbf0576671a2e119f2fa78d28201deb77680d 100644 (file)
--- a/src/include/core/store.h
+++ b/src/include/core/store.h
extern "C" {
#endif
+extern const sdb_type_t sdb_host_type;
+extern const sdb_type_t sdb_attribute_type;
+extern const sdb_type_t sdb_service_type;
+
typedef struct {
sdb_object_t parent;
sdb_time_t last_update;
char *name;
} sdb_store_obj_t;
-#define SDB_STORE_OBJ_INIT { SDB_OBJECT_INIT, 0, NULL }
+#define SDB_STORE_OBJ_INIT(t) { SDB_OBJECT_TYPED_INIT(t), 0, NULL }
#define SDB_STORE_OBJ(obj) ((sdb_store_obj_t *)(obj))
+#define SDB_CONST_STORE_OBJ(obj) ((const sdb_store_obj_t *)(obj))
typedef struct {
sdb_store_obj_t parent;
char *hostname;
} sdb_service_t;
-#define SDB_SVC_INIT { SDB_STORE_OBJ_INIT, NULL }
+#define SDB_SVC_INIT { SDB_STORE_OBJ_INIT(sdb_service_type), NULL }
#define SDB_SVC(obj) ((sdb_service_t *)(obj))
+#define SDB_CONST_SVC(obj) ((const sdb_service_t *)(obj))
typedef struct {
sdb_store_obj_t parent;
char *attr_value;
char *hostname;
} sdb_attribute_t;
-#define SDB_ATTR_INIT { SDB_STORE_OBJ_INIT, NULL, NULL }
+#define SDB_ATTR_INIT { SDB_STORE_OBJ_INIT(sdb_attribute_type), NULL, NULL }
#define SDB_ATTR(obj) ((sdb_attribute_t *)(obj))
+#define SDB_CONST_ATTR(obj) ((const sdb_attribute_t *)(obj))
typedef struct {
sdb_store_obj_t parent;
sdb_llist_t *attributes;
sdb_llist_t *services;
} sdb_host_t;
-#define SDB_HOST_INIT { SDB_STORE_OBJ_INIT, NULL, NULL }
+#define SDB_HOST_INIT { SDB_STORE_OBJ_INIT(sdb_host_type), NULL, NULL }
#define SDB_HOST(obj) ((sdb_host_t *)(obj))
+#define SDB_CONST_HOST(obj) ((const sdb_host_t *)(obj))
/* shortcuts for accessing the sdb_store_obj_t attributes of inheriting
* objects */
sdb_host_t *
sdb_host_create(const char *name);
-sdb_host_t *
-sdb_host_clone(const sdb_host_t *host);
-
/*
* sdb_store_host:
* Add/update a host in the store. If the host, identified by its name,
sdb_attribute_create(const char *hostname,
const char *name, const char *value);
-sdb_attribute_t *
-sdb_attribute_clone(const sdb_attribute_t *attr);
-
/*
* sdb_store_attribute:
* Add/update a host's attribute in the store. If the attribute, identified by
sdb_service_t *
sdb_service_create(const char *hostname, const char *name);
-sdb_service_t *
-sdb_service_clone(const sdb_service_t *svc);
-
/*
* sdb_store_service:
* Add/update a store in the store. If the service, identified by its name,