Code

Include strings.h instead of defining _BSD_SOURCE to get strcasecmp.
[sysdb.git] / src / core / object.c
index e9825b1d84cc122499600521a432e4bb162f24c8..dfa06551cb62c1353b59d827ea5333c8e7f30187 100644 (file)
  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+#if HAVE_CONFIG_H
+#      include "config.h"
+#endif /* HAVE_CONFIG_H */
+
 #include "core/object.h"
 
 #include <assert.h>
 
 #include <stdlib.h>
 #include <string.h>
+#include <strings.h>
 
 /*
  * private types
@@ -66,8 +71,7 @@ static sdb_type_t sdb_object_wrapper_type = {
        sizeof(sdb_object_wrapper_t),
 
        sdb_object_wrapper_init,
-       sdb_object_wrapper_destroy,
-       /* clone = */ NULL
+       sdb_object_wrapper_destroy
 };
 
 /*
@@ -75,41 +79,65 @@ static sdb_type_t sdb_object_wrapper_type = {
  */
 
 sdb_object_t *
-sdb_object_create(sdb_type_t type, ...)
+sdb_object_vcreate(const char *name, sdb_type_t type, va_list ap)
 {
        sdb_object_t *obj;
 
-       if (type.size <= 0)
+       if (type.size < sizeof(sdb_object_t))
                return NULL;
 
        obj = malloc(type.size);
        if (! obj)
                return NULL;
-       memset(obj, 0, sizeof(*obj));
+       memset(obj, 0, type.size);
+       obj->type = type;
 
-       if (type.init) {
-               va_list ap;
-               va_start(ap, type);
+       if (name) {
+               obj->name = strdup(name);
+               if (! obj->name) {
+                       obj->ref_cnt = 1;
+                       sdb_object_deref(obj);
+                       return NULL;
+               }
+       }
 
+       if (type.init) {
                if (type.init(obj, ap)) {
                        obj->ref_cnt = 1;
                        sdb_object_deref(obj);
-                       va_end(ap);
                        return NULL;
                }
-
-               va_end(ap);
        }
 
-       obj->type = type;
        obj->ref_cnt = 1;
        return obj;
+} /* sdb_object_vcreate */
+
+sdb_object_t *
+sdb_object_create(const char *name, sdb_type_t type, ...)
+{
+       sdb_object_t *obj;
+       va_list ap;
+
+       va_start(ap, type);
+       obj = sdb_object_vcreate(name, type, ap);
+       va_end(ap);
+       return obj;
 } /* sdb_object_create */
 
 sdb_object_t *
-sdb_object_create_wrapper(void *data, void (*destructor)(void *))
+sdb_object_create_simple(const char *name, size_t size,
+               void (*destructor)(sdb_object_t *))
 {
-       return sdb_object_create(sdb_object_wrapper_type, data, destructor);
+       sdb_type_t t = { size, NULL, destructor };
+       return sdb_object_create(name, t);
+} /* sdb_object_create_simple */
+
+sdb_object_t *
+sdb_object_create_wrapper(const char *name,
+               void *data, void (*destructor)(void *))
+{
+       return sdb_object_create(name, sdb_object_wrapper_type, data, destructor);
 } /* sdb_object_create_wrapper */
 
 void
@@ -122,9 +150,14 @@ sdb_object_deref(sdb_object_t *obj)
        if (obj->ref_cnt > 0)
                return;
 
+       /* we'd access free'd memory in case ref_cnt < 0 */
+       assert(! obj->ref_cnt);
+
        if (obj->type.destroy)
                obj->type.destroy(obj);
 
+       if (obj->name)
+               free(obj->name);
        free(obj);
 } /* sdb_object_deref */
 
@@ -137,5 +170,18 @@ sdb_object_ref(sdb_object_t *obj)
        ++obj->ref_cnt;
 } /* sdb_object_ref */
 
+int
+sdb_object_cmp_by_name(const sdb_object_t *o1, const sdb_object_t *o2)
+{
+       if ((! o1) && (! o2))
+               return 0;
+       else if (! o1)
+               return -1;
+       else if (! o2)
+               return 1;
+
+       return strcasecmp(o1->name, o2->name);
+} /* sdb_object_cmp_by_name */
+
 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */