117#if !defined(_SC_PAGESIZE)
137 if (GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS |
138 GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, (LPCSTR) address, &module)) {
143 if (dladdr(address, &
info)) {
144 return info.dli_fbase;
206 object->clazz = clazz;
207 object->referenceCount = 1;
219 if (c == clazz || clazz ==
_Object()) {
238 fprintf(stderr,
"%s: no image contains %p\n", __func__, address);
245 classImage->
handle = handle;
246 classImage->
image = image;
250 classImage->
next = __atomic_load_n(&
_images, __ATOMIC_RELAXED);
251 while (!__atomic_compare_exchange_n(&
_images, &classImage->
next, classImage, 1,
252 __ATOMIC_RELEASE, __ATOMIC_RELAXED)) ;
273 if (__atomic_load_n(&i->handle, __ATOMIC_ACQUIRE) == handle) {
275 __atomic_store_n(&i->handle, NULL, __ATOMIC_RELEASE);
281 fprintf(stderr,
"%s: %p was never registered\n", __func__, handle);
287 Class *clazz = *classes;
289 if (clazz->
image == image) {
290 *classes = clazz->
next;
293 classes = &clazz->
next;
307 if (strcmp(name, c->
def.
name) == 0) {
320 if (asprintf(&s,
"_%s", name) > 0) {
322 Class *(*archetype)(void) = NULL;
325 i && archetype == NULL; i = i->
next) {
327 ident handle = __atomic_load_n(&i->handle, __ATOMIC_ACQUIRE);
329 archetype = dlsym(handle, s);
333 if (archetype == NULL) {
337 do_once(&once, { handle = dlopen(NULL, RTLD_LAZY); });
338 archetype = handle ? dlsym(handle, s) : NULL;
340 archetype = dlsym(RTLD_DEFAULT, s);
363 if (__atomic_fetch_sub(&object->referenceCount, 1, __ATOMIC_RELEASE) == 1) {
364 __atomic_thread_fence(__ATOMIC_ACQUIRE);
380 unsigned int referenceCount = __atomic_load_n(&object->referenceCount, __ATOMIC_RELAXED);
382 if (referenceCount == 0) {
383 fprintf(stderr,
"%s: %p is being deallocated\n", __func__,
object);
386 }
while (!__atomic_compare_exchange_n(&object->referenceCount, &referenceCount,
387 referenceCount + 1, 1, __ATOMIC_RELAXED, __ATOMIC_RELAXED));
static void dealloc(Object *self)
void addClassImage(ident handle, const ident address)
Registers an image that provides Classes, e.g. a plugin.
static const ident imageForAddress(const ident address)
static void teardown(void)
Called atexit to teardown Objectively.
ident _alloc(Class *clazz)
Instantiate a type through the given Class.
static pthread_mutex_t _classesLock
Guards the structure of _classes. MUST NOT be held across dlsym, dlopen, or a Class initializer,...
static void setup(void)
Called when initializing Object to setup Objectively.
static ClassImage * _images
The registered images providing Classes, most recently added first. Published atomically rather than ...
Class * classForName(const char *name)
ident release(ident obj)
Atomically decrement the given Object's reference count. If the resulting reference count is 0,...
Class * _initialize(const ClassDef *def)
Initializes the given Class.
void removeClassImage(ident handle)
Unregisters an image, and every Class it declared.
ident retain(ident obj)
Atomically increment the given Object's reference count.
ident _cast(const Class *clazz, const ident obj)
Perform a type-checking cast.
Classes describe the state and behavior of an Objectively type.
#define cast(type, obj)
Safely cast obj to type.
#define OBJECTIVELY_MAGIC
The header value identifying Objectively types.
static void info(const Log *self, const char *fmt,...)
Object is the root Class of The Objectively Class hierarchy.
static Unicode next(StringReader *self, StringReaderMode mode)
void * ident
The identity type, similar to Objective-C id.
#define do_once(once, block)
Executes the given block at most one time.
ClassDefs are passed to _initialize via an archetype to initialize a Class.
size_t instanceSize
The instance size (required).
size_t interfaceSize
The interface size (required).
Class * superclass
The superclass (required). e.g. _Object().
void(* destroy)(Class *clazz)
The Class destructor (optional). This method is run for initialized Classes when your application exi...
const char * name
The Class name (required).
void(* initialize)(Class *clazz)
The Class initializer (optional).
The runtime representation of a Class.
ident image
The base address of the image that declared this Class.
ClassDef def
The Class definition.
Class * next
Provides chaining of initialized Classes.
ident interface
The interface of the Class.
A registered image: the handle the application holds, and the base address that the Classes it declar...
Object is the root Class of The Objectively Class hierarchy.
unsigned int magic
A header to allow introspection of Object types.