Debian Codegraph @

c/src/core/object/object.c

raw
1 /*
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 */
21
22 #include <proton/object.h>
23
24 #include "core/memory.h"
25
26 #include <stdlib.h>
27 #include <assert.h>
28
29 #define pn_object_initialize NULL
30 #define pn_object_finalize NULL
31 #define pn_object_inspect NULL
32 uintptr_t pn_object_hashcode(void *object) { return (uintptr_t) object; }
33 intptr_t pn_object_compare(void *a, void *b) { return (intptr_t) a - (intptr_t) b; }
34
35 const pn_class_t PN_OBJECT[] = {PN_CLASS(pn_object)};
36
37 #define pn_void_initialize NULL
38 void *pn_void_new(const pn_class_t *clazz, size_t size) { return pni_mem_allocate(clazz, size); }
39 void pn_void_incref(void* p) {}
40 void pn_void_decref(void* p) {}
41 int pn_void_refcount(void *object) { return -1; }
42 #define pn_void_finalize NULL
43 static void pn_void_free(void *object) { pni_mem_deallocate(PN_VOID, object); }
44 static const pn_class_t *pn_void_reify(void *object) { return PN_VOID; }
45 uintptr_t pn_void_hashcode(void *object) { return (uintptr_t) object; }
46 intptr_t pn_void_compare(void *a, void *b) { return (intptr_t) a - (intptr_t) b; }
47 int pn_void_inspect(void *object, pn_string_t *dst) { return pn_string_addf(dst, "%p", object); }
48
49 const pn_class_t PN_VOID[] = {PN_METACLASS(pn_void)};
50
51 const char *pn_class_name(const pn_class_t *clazz)
52 {
53 return clazz->name;
54 }
55
56 pn_cid_t pn_class_id(const pn_class_t *clazz)
57 {
58 return clazz->cid;
59 }
60
61 void *pn_class_new(const pn_class_t *clazz, size_t size)
62 {
63 assert(clazz);
64 void *object = clazz->newinst(clazz, size);
65 if (clazz->initialize) {
66 clazz->initialize(object);
67 }
68 return object;
69 }
70
71 void *pn_class_incref(const pn_class_t *clazz, void *object)
72 {
73 assert(clazz);
74 if (object) {
75 clazz = clazz->reify(object);
76 clazz->incref(object);
77 }
78 return object;
79 }
80
81 int pn_class_refcount(const pn_class_t *clazz, void *object)
82 {
83 assert(clazz);
84 clazz = clazz->reify(object);
85 return clazz->refcount(object);
86 }
87
88 int pn_class_decref(const pn_class_t *clazz, void *object)
89 {
90 assert(clazz);
91
92 if (object) {
93 clazz = clazz->reify(object);
94 clazz->decref(object);
95 int rc = clazz->refcount(object);
96 if (rc == 0) {
97 if (clazz->finalize) {
98 clazz->finalize(object);
99 // check the refcount again in case the finalizer created a
100 // new reference
101 rc = clazz->refcount(object);
102 }
103 if (rc == 0) {
104 clazz->free(object);
105 return 0;
106 }
107 } else {
108 return rc;
109 }
110 }
111
112 return 0;
113 }
114
115 void pn_class_free(const pn_class_t *clazz, void *object)
116 {
117 assert(clazz);
118 if (object) {
119 clazz = clazz->reify(object);
120 int rc = clazz->refcount(object);
121 assert(rc == 1 || rc == -1);
122 if (rc == 1) {
123 rc = pn_class_decref(clazz, object);
124 assert(rc == 0);
125 } else {
126 if (clazz->finalize) {
127 clazz->finalize(object);
128 }
129 clazz->free(object);
130 }
131 }
132 }
133
134 const pn_class_t *pn_class_reify(const pn_class_t *clazz, void *object)
135 {
136 assert(clazz);
137 return clazz->reify(object);
138 }
139
140 uintptr_t pn_class_hashcode(const pn_class_t *clazz, void *object)
141 {
142 assert(clazz);
143
144 if (!object) return 0;
145
146 clazz = clazz->reify(object);
147
148 if (clazz->hashcode) {
149 return clazz->hashcode(object);
150 } else {
151 return (uintptr_t) object;
152 }
153 }
154
155 intptr_t pn_class_compare(const pn_class_t *clazz, void *a, void *b)
156 {
157 assert(clazz);
158
159 if (a == b) return 0;
160
161 clazz = clazz->reify(a);
162
163 if (a && b && clazz->compare) {
164 return clazz->compare(a, b);
165 } else {
166 return (intptr_t) a - (intptr_t) b;
167 }
168 }
169
170 bool pn_class_equals(const pn_class_t *clazz, void *a, void *b)
171 {
172 return pn_class_compare(clazz, a, b) == 0;
173 }
174
175 int pn_class_inspect(const pn_class_t *clazz, void *object, pn_string_t *dst)
176 {
177 assert(clazz);
178
179 clazz = clazz->reify(object);
180
181 if (!pn_string_get(dst)) {
182 pn_string_set(dst, "");
183 }
184
185 if (object && clazz->inspect) {
186 return clazz->inspect(object, dst);
187 }
188
189 const char *name = clazz->name ? clazz->name : "<anon>";
190
191 return pn_string_addf(dst, "%s<%p>", name, object);
192 }
193
194 typedef struct {
195 const pn_class_t *clazz;
196 int refcount;
197 } pni_head_t;
198
199 #define pni_head(PTR) \
200 (((pni_head_t *) (PTR)) - 1)
201
202 void *pn_object_new(const pn_class_t *clazz, size_t size)
203 {
204 void *object = NULL;
205 pni_head_t *head = (pni_head_t *) pni_mem_zallocate(clazz, sizeof(pni_head_t) + size);
206 if (head != NULL) {
207 object = head + 1;
208 head->clazz = clazz;
209 head->refcount = 1;
210 }
211 return object;
212 }
213
214 const pn_class_t *pn_object_reify(void *object)
215 {
216 if (object) {
217 return pni_head(object)->clazz;
218 } else {
219 return PN_OBJECT;
220 }
221 }
222
223 void pn_object_incref(void *object)
224 {
225 if (object) {
226 pni_head(object)->refcount++;
227 }
228 }
229
230 int pn_object_refcount(void *object)
231 {
232 assert(object);
233 return pni_head(object)->refcount;
234 }
235
236 void pn_object_decref(void *object)
237 {
238 pni_head_t *head = pni_head(object);
239 assert(head->refcount > 0);
240 head->refcount--;
241 }
242
243 void pn_object_free(void *object)
244 {
245 pni_head_t *head = pni_head(object);
246 pni_mem_deallocate(head->clazz, head);
247 }
248
249 void *pn_incref(void *object)
250 {
251 return pn_class_incref(PN_OBJECT, object);
252 }
253
254 int pn_decref(void *object)
255 {
256 return pn_class_decref(PN_OBJECT, object);
257 }
258
259 int pn_refcount(void *object)
260 {
261 return pn_class_refcount(PN_OBJECT, object);
262 }
263
264 void pn_free(void *object)
265 {
266 pn_class_free(PN_OBJECT, object);
267 }
268
269 const pn_class_t *pn_class(void *object)
270 {
271 return pn_class_reify(PN_OBJECT, object);
272 }
273
274 uintptr_t pn_hashcode(void *object)
275 {
276 return pn_class_hashcode(PN_OBJECT, object);
277 }
278
279 intptr_t pn_compare(void *a, void *b)
280 {
281 return pn_class_compare(PN_OBJECT, a, b);
282 }
283
284 bool pn_equals(void *a, void *b)
285 {
286 return !pn_compare(a, b);
287 }
288
289 int pn_inspect(void *object, pn_string_t *dst)
290 {
291 return pn_class_inspect(PN_OBJECT, object, dst);
292 }
293
294 #define pn_weakref_new NULL
295 #define pn_weakref_initialize NULL
296 #define pn_weakref_finalize NULL
297 #define pn_weakref_free NULL
298
299 static void pn_weakref_incref(void *object) {}
300 static void pn_weakref_decref(void *object) {}
301 static int pn_weakref_refcount(void *object) { return -1; }
302 static const pn_class_t *pn_weakref_reify(void *object) {
303 return PN_WEAKREF;
304 }
305 static uintptr_t pn_weakref_hashcode(void *object) {
306 return pn_hashcode(object);
307 }
308 static intptr_t pn_weakref_compare(void *a, void *b) {
309 return pn_compare(a, b);
310 }
311 static int pn_weakref_inspect(void *object, pn_string_t *dst) {
312 return pn_inspect(object, dst);
313 }
314
315 const pn_class_t PN_WEAKREF[] = {PN_METACLASS(pn_weakref)};