Code

Include config.h in source files.
[sysdb.git] / src / core / object.c
1 /*
2  * SysDB - src/core/object.c
3  * Copyright (C) 2012 Sebastian 'tokkee' Harl <sh@tokkee.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
19  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
28 #if HAVE_CONFIG_H
29 #       include "config.h"
30 #endif /* HAVE_CONFIG_H */
32 #include "core/object.h"
34 #include <assert.h>
36 #include <stdlib.h>
37 #include <string.h>
39 /*
40  * private types
41  */
43 static int
44 sdb_object_wrapper_init(sdb_object_t *obj, va_list ap)
45 {
46         void *data = va_arg(ap, void *);
47         void (*destructor)(void *) = va_arg(ap, void (*)(void *));
49         assert(obj);
51         SDB_OBJ_WRAPPER(obj)->data = data;
52         SDB_OBJ_WRAPPER(obj)->destructor = destructor;
53         return 0;
54 } /* sdb_object_wrapper_init */
56 static void
57 sdb_object_wrapper_destroy(sdb_object_t *obj)
58 {
59         if (! obj)
60                 return;
62         assert(obj->ref_cnt <= 0);
64         if (SDB_OBJ_WRAPPER(obj)->destructor && SDB_OBJ_WRAPPER(obj)->data)
65                 SDB_OBJ_WRAPPER(obj)->destructor(SDB_OBJ_WRAPPER(obj)->data);
66         SDB_OBJ_WRAPPER(obj)->data = NULL;
67 } /* sdb_object_wrapper_destroy */
69 static sdb_type_t sdb_object_wrapper_type = {
70         sizeof(sdb_object_wrapper_t),
72         sdb_object_wrapper_init,
73         sdb_object_wrapper_destroy
74 };
76 /*
77  * public API
78  */
80 sdb_object_t *
81 sdb_object_vcreate(const char *name, sdb_type_t type, va_list ap)
82 {
83         sdb_object_t *obj;
85         if (type.size <= sizeof(sdb_object_t))
86                 return NULL;
88         obj = malloc(type.size);
89         if (! obj)
90                 return NULL;
91         memset(obj, 0, type.size);
92         obj->type = type;
94         if (name) {
95                 obj->name = strdup(name);
96                 if (! obj->name) {
97                         obj->ref_cnt = 1;
98                         sdb_object_deref(obj);
99                         return NULL;
100                 }
101         }
103         if (type.init) {
104                 if (type.init(obj, ap)) {
105                         obj->ref_cnt = 1;
106                         sdb_object_deref(obj);
107                         return NULL;
108                 }
109         }
111         obj->ref_cnt = 1;
112         return obj;
113 } /* sdb_object_vcreate */
115 sdb_object_t *
116 sdb_object_create(const char *name, sdb_type_t type, ...)
118         sdb_object_t *obj;
119         va_list ap;
121         va_start(ap, type);
122         obj = sdb_object_vcreate(name, type, ap);
123         va_end(ap);
124         return obj;
125 } /* sdb_object_create */
127 sdb_object_t *
128 sdb_object_create_simple(const char *name, size_t size)
130         sdb_type_t t = { size, NULL, NULL };
131         return sdb_object_create(name, t);
132 } /* sdb_object_create_simple */
134 sdb_object_t *
135 sdb_object_create_wrapper(const char *name,
136                 void *data, void (*destructor)(void *))
138         return sdb_object_create(name, sdb_object_wrapper_type, data, destructor);
139 } /* sdb_object_create_wrapper */
141 void
142 sdb_object_deref(sdb_object_t *obj)
144         if (! obj)
145                 return;
147         --obj->ref_cnt;
148         if (obj->ref_cnt > 0)
149                 return;
151         if (obj->type.destroy)
152                 obj->type.destroy(obj);
154         if (obj->name)
155                 free(obj->name);
156         free(obj);
157 } /* sdb_object_deref */
159 void
160 sdb_object_ref(sdb_object_t *obj)
162         if (! obj)
163                 return;
164         assert(obj->ref_cnt > 0);
165         ++obj->ref_cnt;
166 } /* sdb_object_ref */
168 int
169 sdb_object_cmp_by_name(const sdb_object_t *o1, const sdb_object_t *o2)
171         if ((! o1) && (! o2))
172                 return 0;
173         else if (! o1)
174                 return -1;
175         else if (! o2)
176                 return 1;
178         return strcasecmp(o1->name, o2->name);
179 } /* sdb_object_cmp_by_name */
181 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */