Code

parser: Let the TIMESERIES command accept optional data-source names.
[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>
38 #include <strings.h>
40 /*
41  * private types
42  */
44 static int
45 sdb_object_wrapper_init(sdb_object_t *obj, va_list ap)
46 {
47         void *data = va_arg(ap, void *);
48         void (*destructor)(void *) = va_arg(ap, void (*)(void *));
50         assert(obj);
52         SDB_OBJ_WRAPPER(obj)->data = data;
53         SDB_OBJ_WRAPPER(obj)->destructor = destructor;
54         return 0;
55 } /* sdb_object_wrapper_init */
57 static void
58 sdb_object_wrapper_destroy(sdb_object_t *obj)
59 {
60         if (! obj)
61                 return;
63         assert(obj->ref_cnt <= 0);
65         if (SDB_OBJ_WRAPPER(obj)->destructor && SDB_OBJ_WRAPPER(obj)->data)
66                 SDB_OBJ_WRAPPER(obj)->destructor(SDB_OBJ_WRAPPER(obj)->data);
67         SDB_OBJ_WRAPPER(obj)->data = NULL;
68 } /* sdb_object_wrapper_destroy */
70 static sdb_type_t sdb_object_wrapper_type = {
71         sizeof(sdb_object_wrapper_t),
73         sdb_object_wrapper_init,
74         sdb_object_wrapper_destroy
75 };
77 /*
78  * public API
79  */
81 sdb_object_t *
82 sdb_object_vcreate(const char *name, sdb_type_t type, va_list ap)
83 {
84         sdb_object_t *obj;
86         if (type.size < sizeof(sdb_object_t))
87                 return NULL;
89         obj = malloc(type.size);
90         if (! obj)
91                 return NULL;
92         memset(obj, 0, type.size);
93         obj->type = type;
95         if (name) {
96                 obj->name = strdup(name);
97                 if (! obj->name) {
98                         obj->ref_cnt = 1;
99                         sdb_object_deref(obj);
100                         return NULL;
101                 }
102         }
104         if (type.init) {
105                 if (type.init(obj, ap)) {
106                         obj->ref_cnt = 1;
107                         sdb_object_deref(obj);
108                         return NULL;
109                 }
110         }
112         obj->ref_cnt = 1;
113         return obj;
114 } /* sdb_object_vcreate */
116 sdb_object_t *
117 sdb_object_create(const char *name, sdb_type_t type, ...)
119         sdb_object_t *obj;
120         va_list ap;
122         va_start(ap, type);
123         obj = sdb_object_vcreate(name, type, ap);
124         va_end(ap);
125         return obj;
126 } /* sdb_object_create */
128 sdb_object_t *
129 sdb_object_create_simple(const char *name, size_t size,
130                 void (*destructor)(sdb_object_t *))
132         sdb_type_t t = { size, NULL, destructor };
133         return sdb_object_create(name, t);
134 } /* sdb_object_create_simple */
136 sdb_object_t *
137 sdb_object_create_wrapper(const char *name,
138                 void *data, void (*destructor)(void *))
140         return sdb_object_create(name, sdb_object_wrapper_type, data, destructor);
141 } /* sdb_object_create_wrapper */
143 void
144 sdb_object_deref(sdb_object_t *obj)
146         if (! obj)
147                 return;
149         --obj->ref_cnt;
150         if (obj->ref_cnt > 0)
151                 return;
153         /* we'd access free'd memory in case ref_cnt < 0 */
154         assert(! obj->ref_cnt);
156         if (obj->type.destroy)
157                 obj->type.destroy(obj);
159         if (obj->name)
160                 free(obj->name);
161         free(obj);
162 } /* sdb_object_deref */
164 void
165 sdb_object_ref(sdb_object_t *obj)
167         if (! obj)
168                 return;
169         assert(obj->ref_cnt > 0);
170         ++obj->ref_cnt;
171 } /* sdb_object_ref */
173 int
174 sdb_object_cmp_by_name(const sdb_object_t *o1, const sdb_object_t *o2)
176         if ((! o1) && (! o2))
177                 return 0;
178         else if (! o1)
179                 return -1;
180         else if (! o2)
181                 return 1;
183         return strcasecmp(o1->name, o2->name);
184 } /* sdb_object_cmp_by_name */
186 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */