1 /** @file object.c Reference-counted object.
2
3 @authors Copyright (c) 2017 Jaakko Keränen <jaakko.keranen@iki.fi>
4
5 @par License
6
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9
10 1. Redistributions of source code must retain the above copyright notice, this
11 list of conditions and the following disclaimer.
12 2. Redistributions in binary form must reproduce the above copyright notice,
13 this list of conditions and the following disclaimer in the documentation
14 and/or other materials provided with the distribution.
15
16 <small>THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
20 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.</small>
26 */
27
28 #include "the_Foundation/object.h"
29 #include "the_Foundation/audience.h"
30
31 #include <stdio.h>
32 #include <stdlib.h>
33
34 #define iObjectSignature 0x4a424f69 // iOBJ
35
36 #if 0
37 # define iObjectDebug(...) iDebug(__VA_ARGS__)
38 #else
39 # define iObjectDebug(...)
40 #endif
41
42 #if !defined (NDEBUG)
43
44 static iAtomicInt totalCount_;
45
totalCount_Object(void)46 int totalCount_Object(void) {
47 return value_Atomic(&totalCount_);
48 }
49
checkSignature_Object(const iAnyObject * d)50 void checkSignature_Object(const iAnyObject *d) {
51 iAssert(d != NULL);
52 iAssert(((const iObject *) d)->__sig == iObjectSignature);
53 }
54
55 #else /* defined (NDEBUG) */
56
totalCount_Object(void)57 int totalCount_Object(void) {
58 /* Not counted in release build. */
59 return 0;
60 }
61
checkSignature_Object(const iAnyObject * d)62 void checkSignature_Object(const iAnyObject *d) {
63 /* Checks disabled in release build. */
64 iUnused(d);
65 }
66
67 #endif /* defined (NDEBUG) */
68
free_Object_(iObject * d)69 static void free_Object_(iObject *d) {
70 deinit_Object(d);
71 iObjectDebug("[Object] deleting %s %p\n", d->class->name, d);
72 #if !defined (NDEBUG)
73 d->__sig = 0xdeadbeef;
74 add_Atomic(&totalCount_, -1);
75 #endif
76 free(d);
77 }
78
new_Object(const iAnyClass * class)79 iAnyObject *new_Object(const iAnyClass *class) {
80 iAssert(class != NULL);
81 iAssert(((const iClass *) class)->size >= sizeof(iObject));
82 iObject *d = malloc(((const iClass *) class)->size);
83 set_Atomic(&d->refCount, 1);
84 d->classObj = class;
85 d->memberOf = NULL;
86 #if !defined (NDEBUG)
87 d->__sig = iObjectSignature;
88 add_Atomic(&totalCount_, 1);
89 #endif
90 iObjectDebug("[Object] constructed %s %p\n", d->class->name, d);
91 return d;
92 }
93
deinit_Object(iAnyObject * any)94 void deinit_Object(iAnyObject *any) {
95 iObject *d = (iObject *) any;
96 iAssertIsObject(d);
97 deinit_Class(d->classObj, d);
98 if (d->memberOf) {
99 delete_AudienceMember(d->memberOf);
100 d->memberOf = NULL;
101 }
102 }
103
ref_Object(const iAnyObject * any)104 iAnyObject *ref_Object(const iAnyObject *any) {
105 if (any) {
106 iObject *d = iConstCast(iObject *, any);
107 iAssertIsObject(d);
108 add_Atomic(&d->refCount, 1);
109 return d;
110 }
111 return NULL;
112 }
113
deref_Object(const iAnyObject * any)114 void deref_Object(const iAnyObject *any) {
115 if (any) {
116 iObject *d = iConstCast(iObject *, any);
117 iAssertIsObject(d);
118 iAssert(d->refCount > 0);
119 if (add_Atomic(&d->refCount, -1) == 1) {
120 free_Object_(d);
121 }
122 }
123 }
124
class_Object(const iAnyObject * d)125 const iClass *class_Object(const iAnyObject *d) {
126 if (d) {
127 iAssertIsObject(d);
128 return ((const iObject *) d)->classObj;
129 }
130 return NULL;
131 }
132
audienceMember_Object(const iAnyObject * any)133 iAudienceMember *audienceMember_Object(const iAnyObject *any) {
134 if (any) {
135 iObject *d = iConstCast(iObject *, any);
136 if (!d->memberOf) {
137 d->memberOf = new_AudienceMember(d);
138 }
139 return d->memberOf;
140 }
141 return NULL;
142 }
143
setUserData_Object(iAnyObject * d,void * user)144 void setUserData_Object(iAnyObject *d, void *user) {
145 if (d) {
146 iAssertIsObject(d);
147 ((iObject *) d)->user = user;
148 }
149 }
150
userData_Object(const iAnyObject * d)151 void *userData_Object(const iAnyObject *d) {
152 if (d) {
153 iAssertIsObject(d);
154 return ((const iObject *) d)->user;
155 }
156 return NULL;
157 }
158