Code

bcefadbc9911570e837867213d843179a6579de8
[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 #include "core/object.h"
30 #include <assert.h>
32 #include <stdlib.h>
33 #include <string.h>
35 /*
36  * private types
37  */
39 static int
40 sdb_object_wrapper_init(sdb_object_t *obj, va_list ap)
41 {
42         void *data = va_arg(ap, void *);
43         void (*destructor)(void *) = va_arg(ap, void (*)(void *));
45         assert(obj);
47         SDB_OBJ_WRAPPER(obj)->data = data;
48         SDB_OBJ_WRAPPER(obj)->destructor = destructor;
49         return 0;
50 } /* sdb_object_wrapper_init */
52 static void
53 sdb_object_wrapper_destroy(sdb_object_t *obj)
54 {
55         if (! obj)
56                 return;
58         assert(obj->ref_cnt <= 0);
60         if (SDB_OBJ_WRAPPER(obj)->destructor && SDB_OBJ_WRAPPER(obj)->data)
61                 SDB_OBJ_WRAPPER(obj)->destructor(SDB_OBJ_WRAPPER(obj)->data);
62         SDB_OBJ_WRAPPER(obj)->data = NULL;
63 } /* sdb_object_wrapper_destroy */
65 static sdb_type_t sdb_object_wrapper_type = {
66         sizeof(sdb_object_wrapper_t),
68         sdb_object_wrapper_init,
69         sdb_object_wrapper_destroy
70 };
72 /*
73  * public API
74  */
76 sdb_object_t *
77 sdb_object_vcreate(const char *name, sdb_type_t type, va_list ap)
78 {
79         sdb_object_t *obj;
81         if (type.size <= sizeof(sdb_object_t))
82                 return NULL;
84         obj = malloc(type.size);
85         if (! obj)
86                 return NULL;
87         memset(obj, 0, type.size);
88         obj->type = type;
90         if (name) {
91                 obj->name = strdup(name);
92                 if (! obj->name) {
93                         obj->ref_cnt = 1;
94                         sdb_object_deref(obj);
95                         return NULL;
96                 }
97         }
99         if (type.init) {
100                 if (type.init(obj, ap)) {
101                         obj->ref_cnt = 1;
102                         sdb_object_deref(obj);
103                         return NULL;
104                 }
105         }
107         obj->ref_cnt = 1;
108         return obj;
109 } /* sdb_object_vcreate */
111 sdb_object_t *
112 sdb_object_create(const char *name, sdb_type_t type, ...)
114         sdb_object_t *obj;
115         va_list ap;
117         va_start(ap, type);
118         obj = sdb_object_vcreate(name, type, ap);
119         va_end(ap);
120         return obj;
121 } /* sdb_object_create */
123 sdb_object_t *
124 sdb_object_create_simple(const char *name, size_t size)
126         sdb_type_t t = { size, NULL, NULL };
127         return sdb_object_create(name, t);
128 } /* sdb_object_create_simple */
130 sdb_object_t *
131 sdb_object_create_wrapper(const char *name,
132                 void *data, void (*destructor)(void *))
134         return sdb_object_create(name, sdb_object_wrapper_type, data, destructor);
135 } /* sdb_object_create_wrapper */
137 void
138 sdb_object_deref(sdb_object_t *obj)
140         if (! obj)
141                 return;
143         --obj->ref_cnt;
144         if (obj->ref_cnt > 0)
145                 return;
147         if (obj->type.destroy)
148                 obj->type.destroy(obj);
150         if (obj->name)
151                 free(obj->name);
152         free(obj);
153 } /* sdb_object_deref */
155 void
156 sdb_object_ref(sdb_object_t *obj)
158         if (! obj)
159                 return;
160         assert(obj->ref_cnt > 0);
161         ++obj->ref_cnt;
162 } /* sdb_object_ref */
164 int
165 sdb_object_cmp_by_name(const sdb_object_t *o1, const sdb_object_t *o2)
167         if ((! o1) && (! o2))
168                 return 0;
169         else if (! o1)
170                 return -1;
171         else if (! o2)
172                 return 1;
174         return strcasecmp(o1->name, o2->name);
175 } /* sdb_object_cmp_by_name */
177 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */