Code

object.h: Added SC_OBJECT_WRAPPER_STATIC().
[sysdb.git] / src / include / core / object.h
1 /*
2  * syscollector - src/include/core/object.h
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 #ifndef SC_CORE_OBJECT_H
29 #define SC_CORE_OBJECT_H 1
31 #include <stdarg.h>
32 #include <stddef.h>
34 #ifdef __cplusplus
35 extern "C" {
36 #endif
38 struct sc_object;
39 typedef struct sc_object sc_object_t;
41 struct sc_object {
42         int    ref_cnt;
43         void (*destructor)(sc_object_t *);
44         size_t size;
45 };
46 #define SC_OBJECT_INIT { 1, NULL, 0 }
48 typedef struct {
49         sc_object_t super;
50         void *data;
51         void (*destructor)(void *);
52 } sc_object_wrapper_t;
54 #define SC_OBJ(obj) ((sc_object_t *)(obj))
55 #define SC_OBJ_WRAPPER(obj) ((sc_object_wrapper_t *)(obj))
57 /*
58  * sc_object_create:
59  * Allocates a new sc_object_t of the specified 'size'. The object will be
60  * initialized to zero and then passed on to the 'init' function (if
61  * specified). If specified, the 'destructor' will be called, when the
62  * reference count drops to zero and before freeing the memory allocated by
63  * the object itself.
64  *
65  * If the init function fails (returns a non-zero value), the object will be
66  * destructed and destroyed.
67  *
68  * The reference count of the new object will be 1.
69  *
70  * Returns:
71  *  - the newly allocated object
72  *  - NULL on error
73  */
74 sc_object_t *
75 sc_object_create(size_t size, int (*init)(sc_object_t *, va_list),
76                 void (*destructor)(sc_object_t *), ...);
78 /*
79  * sc_object_create_wrapper:
80  * Create a new sc_object_t wrapping some arbitrary other object.
81  */
82 sc_object_t *
83 sc_object_create_wrapper(void *data, void (*destructor)(void *));
85 #define SC_OBJECT_WRAPPER_STATIC(obj, destructor) \
86         { SC_OBJECT_INIT, (obj), (destructor) }
88 /*
89  * sc_object_deref:
90  * Dereference the object and free the allocated memory in case the ref-count
91  * drops to zero. In case a 'destructor' had been registered with the object,
92  * it will be called before freeing the memory.
93  */
94 void
95 sc_object_deref(sc_object_t *obj);
97 /*
98  * sc_object_ref:
99  * Take ownership of the specified object, that is, increment the reference
100  * count by one.
101  */
102 void
103 sc_object_ref(sc_object_t *obj);
105 #ifdef __cplusplus
106 } /* extern "C" */
107 #endif
109 #endif /* ! SC_CORE_OBJECT_H */
111 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */