34#elif defined(__APPLE__)
35#include <mach-o/dyld.h>
57#define MAX_CLASS_IMAGES 8
95#if !defined(_SC_PAGESIZE)
115 if (GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS |
116 GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, (LPCSTR) address, &module)) {
121 if (dladdr(address, &
info)) {
122 return info.dli_fbase;
165 clazz->
next = __sync_lock_test_and_set(&
_classes, clazz);
178 object->clazz = clazz;
179 object->referenceCount = 1;
181 ident interface = clazz->interface;
196 if (c == clazz || clazz ==
_Object()) {
221 if (GetModuleFileNameA((HMODULE) handle, path,
sizeof(path))) {
224#elif defined(__APPLE__)
225 for (uint32_t i = 0; i < _dyld_image_count(); i++) {
230 ident image = dlopen(_dyld_get_image_name(i), RTLD_LAZY | RTLD_NOLOAD | RTLD_LOCAL);
237 if (image == handle) {
238 return (
ident) _dyld_get_image_header(i);
242 struct link_map *
map;
243 if (dlinfo(handle, RTLD_DI_LINKMAP, &
map) == 0 &&
map) {
273 Class *clazz = *link;
275 if (clazz->
image == image) {
297 if (strcmp(name, c->
def.
name) == 0) {
304 if (asprintf(&s,
"_%s", name) > 0) {
306 Class *(*archetype)(void) = NULL;
312 if (archetype == NULL) {
316 do_once(&once, { handle = dlopen(NULL, RTLD_LAZY); });
317 archetype = handle ? dlsym(handle, s) : NULL;
319 archetype = dlsym(RTLD_DEFAULT, s);
342 if (__sync_add_and_fetch(&object->referenceCount, -1) == 0) {
356 __sync_add_and_fetch(&object->referenceCount, 1);
static void map(Array *self, Functor functor, ident data)
static void dealloc(Object *self)
void addClassImage(ident handle)
Registers an image that provides Classes, e.g. a plugin.
static const ident imageForAddress(const ident address)
#define MAX_CLASS_IMAGES
The registered images providing Classes.
static ident _classImages[MAX_CLASS_IMAGES]
static void teardown(void)
Called atexit to teardown Objectively.
ident _alloc(Class *clazz)
Instantiate a type through the given Class.
static void setup(void)
Called when initializing Object to setup Objectively.
static ident imageForHandle(ident handle)
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.
static size_t _classImageCount
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).
ptrdiff_t interfaceOffset
The interface offset (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.
Object is the root Class of The Objectively Class hierarchy.
unsigned int magic
A header to allow introspection of Object types.