Code

Include strings.h instead of defining _BSD_SOURCE to get strcasecmp.
[sysdb.git] / src / core / object.c
index 0205ed588f1e58a8afd727a99443c8037add9604..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
@@ -78,13 +83,14 @@ sdb_object_vcreate(const char *name, sdb_type_t type, va_list ap)
 {
        sdb_object_t *obj;
 
-       if (type.size <= sizeof(sdb_object_t))
+       if (type.size < sizeof(sdb_object_t))
                return NULL;
 
        obj = malloc(type.size);
        if (! obj)
                return NULL;
        memset(obj, 0, type.size);
+       obj->type = type;
 
        if (name) {
                obj->name = strdup(name);
@@ -103,7 +109,6 @@ sdb_object_vcreate(const char *name, sdb_type_t type, va_list ap)
                }
        }
 
-       obj->type = type;
        obj->ref_cnt = 1;
        return obj;
 } /* sdb_object_vcreate */
@@ -120,6 +125,14 @@ sdb_object_create(const char *name, sdb_type_t type, ...)
        return obj;
 } /* sdb_object_create */
 
+sdb_object_t *
+sdb_object_create_simple(const char *name, size_t size,
+               void (*destructor)(sdb_object_t *))
+{
+       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 *))
@@ -137,6 +150,9 @@ 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);