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, ...)
117 {
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,
129 void (*destructor)(sdb_object_t *))
130 {
131 sdb_type_t t = { size, NULL, destructor };
132 return sdb_object_create(name, t);
133 } /* sdb_object_create_simple */
135 sdb_object_t *
136 sdb_object_create_wrapper(const char *name,
137 void *data, void (*destructor)(void *))
138 {
139 return sdb_object_create(name, sdb_object_wrapper_type, data, destructor);
140 } /* sdb_object_create_wrapper */
142 void
143 sdb_object_deref(sdb_object_t *obj)
144 {
145 if (! obj)
146 return;
148 --obj->ref_cnt;
149 if (obj->ref_cnt > 0)
150 return;
152 /* we'd access free'd memory in case ref_cnt < 0 */
153 assert(! obj->ref_cnt);
155 if (obj->type.destroy)
156 obj->type.destroy(obj);
158 if (obj->name)
159 free(obj->name);
160 free(obj);
161 } /* sdb_object_deref */
163 void
164 sdb_object_ref(sdb_object_t *obj)
165 {
166 if (! obj)
167 return;
168 assert(obj->ref_cnt > 0);
169 ++obj->ref_cnt;
170 } /* sdb_object_ref */
172 int
173 sdb_object_cmp_by_name(const sdb_object_t *o1, const sdb_object_t *o2)
174 {
175 if ((! o1) && (! o2))
176 return 0;
177 else if (! o1)
178 return -1;
179 else if (! o2)
180 return 1;
182 return strcasecmp(o1->name, o2->name);
183 } /* sdb_object_cmp_by_name */
185 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */