Objectively 1.0.0
Ultra-lightweight object oriented framework for GNU C.
Class.c
Go to the documentation of this file.
1/*
2 * Objectively: Ultra-lightweight object oriented framework for GNU C.
3 * Copyright (C) 2014 Jay Dolan <jay@jaydolan.com>
4 *
5 * This software is provided 'as-is', without any express or implied
6 * warranty. In no event will the authors be held liable for any damages
7 * arising from the use of this software.
8 *
9 * Permission is granted to anyone to use this software for any purpose,
10 * including commercial applications, and to alter it and redistribute it
11 * freely, subject to the following restrictions:
12 *
13 * 1. The origin of this software must not be misrepresented; you must not
14 * claim that you wrote the original software. If you use this software
15 * in a product, an acknowledgment in the product documentation would be
16 * appreciated but is not required.
17 *
18 * 2. Altered source versions must be plainly marked as such, and must not be
19 * misrepresented as being the original software.
20 *
21 * 3. This notice may not be removed or altered from any source distribution.
22 */
23
24#include "Config.h"
25
26#include <assert.h>
27#include <dlfcn.h>
28#include <stdlib.h>
29#include <stdio.h>
30#include <string.h>
31
32#if HAVE_UNISTD_H
33#include <unistd.h>
34#endif
35
36#include "Class.h"
37#include "Object.h"
38
39size_t _pageSize;
40
43
47static void teardown(void) {
48 Class *c;
49
50 c = _classes;
51 while (c) {
52 if (c->def.destroy) {
53 c->def.destroy(c);
54 }
55
56 c = c->next;
57 }
58
59 c = _classes;
60 while (c) {
61
62 Class *next = c->next;
63
64 free(c->interface);
65 free(c);
66
67 c = next;
68 }
69
70 if (_handle) {
71 dlclose(_handle);
72 }
73}
74
78static void setup(void) {
79
80 _classes = NULL;
81
82#if !defined(_SC_PAGESIZE)
83 _pageSize = 4096;
84#else
85 _pageSize = sysconf(_SC_PAGESIZE);
86#endif
87
88 atexit(teardown);
89}
90
92
93 static Once once;
94 do_once(&once, setup());
95
96 assert(def);
97 assert(def->name);
98 assert(def->instanceSize);
99 assert(def->interfaceSize);
100 assert(def->interfaceOffset);
101
102 Class *clazz = calloc(1, sizeof(Class));
103 assert(clazz);
104
105 clazz->def = *def;
106
107 clazz->interface = calloc(1, def->interfaceSize);
108 assert(clazz->interface);
109
110 Class *superclass = clazz->def.superclass;
111 if (superclass) {
112
113 assert(superclass->def.instanceSize <= def->instanceSize);
114 assert(superclass->def.interfaceSize <= def->interfaceSize);
115
116 memcpy(clazz->interface, superclass->interface, superclass->def.interfaceSize);
117 }
118
119 if (clazz->def.initialize) {
120 clazz->def.initialize(clazz);
121 }
122
123 clazz->next = __sync_lock_test_and_set(&_classes, clazz);
124
125 return clazz;
126}
127
129
130 ident obj = calloc(1, clazz->def.instanceSize);
131 assert(obj);
132
133 Object *object = (Object *) obj;
134
135 object->clazz = clazz;
136 object->referenceCount = 1;
137
138 ident interface = clazz->interface;
139 do {
140 *(ident *) (obj + clazz->def.interfaceOffset) = interface;
141 } while ((clazz = clazz->def.superclass));
142
143 return obj;
144}
145
146ident _cast(Class *clazz, const ident obj) {
147
148 if (obj) {
149 const Class *c = ((Object *) obj)->clazz;
150 while (c) {
151
152 // as a special case, we optimize for _Object
153 if (c == clazz || clazz == _Object()) {
154 break;
155 }
156
157 c = c->def.superclass;
158 }
159 assert(c);
160 }
161
162 return (ident) obj;
163}
164
165Class *classForName(const char *name) {
166
167 if (name) {
168 Class *c = _classes;
169 while (c) {
170 if (strcmp(name, c->def.name) == 0) {
171 return c;
172 }
173 c = c->next;
174 }
175
176 char *s;
177 if (asprintf(&s, "_%s", name) > 0) {
178 static Once once;
179
180 do_once(&once, _handle = dlopen(NULL, 0));
181
182 Class *clazz = NULL;
183 Class *(*archetype)(void) = dlsym(_handle, s);
184 if (archetype) {
185 clazz = archetype();
186 }
187
188 free(s);
189 return clazz;
190 }
191 }
192
193 return NULL;
194}
195
197
198 if (obj) {
199 Object *object = cast(Object, obj);
200
201 assert(object);
202
203 if (__sync_add_and_fetch(&object->referenceCount, -1) == 0) {
204 $(object, dealloc);
205 }
206 }
207
208 return NULL;
209}
210
212
213 Object *object = cast(Object, obj);
214
215 assert(object);
216
217 __sync_add_and_fetch(&object->referenceCount, 1);
218
219 return obj;
220}
static ident _handle
Definition: Class.c:42
ident _alloc(Class *clazz)
Instantiate a type through the given Class.
Definition: Class.c:128
size_t _pageSize
Definition: Class.c:39
Class * classForName(const char *name)
Definition: Class.c:165
ident _cast(Class *clazz, const ident obj)
Perform a type-checking cast.
Definition: Class.c:146
ident release(ident obj)
Atomically decrement the given Object's reference count. If the resulting reference count is 0,...
Definition: Class.c:196
Class * _initialize(const ClassDef *def)
Initializes the given Class.
Definition: Class.c:91
static Class * _classes
Definition: Class.c:41
ident retain(ident obj)
Atomically increment the given Object's reference count.
Definition: Class.c:211
Classes describe the state and behavior of an Objectively type.
#define obj
#define cast(type, obj)
Safely cast obj to type.
Definition: Class.h:165
Object is the root Class of The Objectively Class hierarchy.
Immutable UTF-8 strings.
void * ident
The identity type, similar to Objective-C id.
Definition: Types.h:49
long Once
The Once type.
Definition: Once.h:37
#define do_once(once, block)
Executes the given block at most one time.
Definition: Once.h:43
ClassDefs are passed to _initialize via an archetype to initialize a Class.
Definition: Class.h:41
size_t instanceSize
The instance size (required).
Definition: Class.h:69
size_t interfaceSize
The interface size (required).
Definition: Class.h:79
ptrdiff_t interfaceOffset
The interface offset (required).
Definition: Class.h:74
Class * superclass
The superclass (required). e.g. _Object().
Definition: Class.h:89
void(* destroy)(Class *clazz)
The Class destructor (optional). This method is run for initialized Classes when your application exi...
Definition: Class.h:47
const char * name
The Class name (required).
Definition: Class.h:84
void(* initialize)(Class *clazz)
The Class initializer (optional).
Definition: Class.h:64
The runtime representation of a Class.
Definition: Class.h:95
ClassDef def
The Class definition.
Definition: Class.h:100
Class * next
Provides chaining of initialized Classes.
Definition: Class.h:110
ident interface
The interface of the Class.
Definition: Class.h:105
Object is the root Class of The Objectively Class hierarchy.
Definition: Object.h:46
Class * _Object(void)
The Object archetype.
Definition: Object.c:136
void dealloc(Object *self)
Frees all resources held by this Object.
Definition: Array.c:50
Unicode next(StringReader *self)
void setup(URLSessionTask *)
Sets up this task.
void teardown(URLSessionTask *)
Tears down this task.