summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: ec7449c)
raw | patch | inline | side by side (parent: ec7449c)
author | Sebastian Harl <sh@tokkee.org> | |
Sat, 23 Mar 2013 03:01:57 +0000 (20:01 -0700) | ||
committer | Sebastian Harl <sh@tokkee.org> | |
Sat, 23 Mar 2013 03:01:57 +0000 (20:01 -0700) |
A type is defined by its size and init/destroy functions. This is now used
when creating a new object (of that type) rather than passing all required
callbacks to the object create function.
when creating a new object (of that type) rather than passing all required
callbacks to the object create function.
src/core/object.c | patch | blob | history | |
src/core/plugin.c | patch | blob | history | |
src/core/store.c | patch | blob | history | |
src/include/core/object.h | patch | blob | history |
diff --git a/src/core/object.c b/src/core/object.c
index 5ebcff213d827fdb0404e8d7a505ae3c0fcfc9f4..8389c5194f89a1fb006007f234f6587c0143ddda 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
+};
+
/*
* 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 */
diff --git a/src/core/plugin.c b/src/core/plugin.c
index 5df7059e9c6412b1c49bfc49813563a534f7d031..431264266ea66df7b10eadbe5c1d425487b1dac8 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
+};
+
+static sdb_type_t sdb_plugin_collector_cb_type = {
+ sizeof(sdb_plugin_collector_cb_t),
+
+ sdb_plugin_cb_init,
+ sdb_plugin_cb_destroy
+};
+
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 +453,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 4e841bac0022040ca77cd5fedcfa55aab1f4e2a1..db57d2e5ba9506bc7adafba2a48a58263a1edca0 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 */
+/*
+ * private types
+ */
+
static int
sdb_host_init(sdb_object_t *obj, va_list ap)
{
free(SDB_SVC(obj)->_name);
} /* sdb_svc_destroy */
+static sdb_type_t sdb_host_type = {
+ sizeof(sdb_host_t),
+
+ sdb_host_init,
+ sdb_host_destroy
+};
+
+static sdb_type_t sdb_attr_type = {
+ sizeof(sdb_attribute_t),
+
+ sdb_attr_init,
+ sdb_attr_destroy
+};
+
+static sdb_type_t sdb_svc_type = {
+ sizeof(sdb_service_t),
+
+ sdb_svc_init,
+ sdb_svc_destroy
+};
+
/*
* 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);
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_attr_type, hostname, name, value);
if (! obj)
return NULL;
return SDB_ATTR(obj);
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_svc_type, hostname, name);
if (! obj)
return NULL;
return SDB_SVC(obj);
index f2992c79346c989eb7d31d2fad25665174aab57c..cab77d0816973ca242620c8320c22c8eca07682c 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 *);
};
-#define SDB_OBJECT_INIT { 1, NULL, 0 }
+#define SDB_TYPE_INIT { 0, NULL, NULL }
+
+struct sdb_object {
+ sdb_type_t type;
+ int ref_cnt;
+};
+#define SDB_OBJECT_INIT { SDB_TYPE_INIT, 1 }
typedef struct {
sdb_object_t super;
/*
* 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 *));