2010-06-03 15:17:12 +00:00
|
|
|
#include <stdio.h>
|
2010-08-15 22:30:32 +00:00
|
|
|
#include <stdlib.h>
|
2010-06-03 15:17:12 +00:00
|
|
|
#include "objc/runtime.h"
|
2010-05-16 21:32:43 +00:00
|
|
|
#include "sarray2.h"
|
2010-06-03 15:17:12 +00:00
|
|
|
#include "selector.h"
|
2010-05-16 21:32:43 +00:00
|
|
|
#include "class.h"
|
|
|
|
#include "lock.h"
|
|
|
|
#include "method_list.h"
|
2010-06-03 15:17:12 +00:00
|
|
|
#include "slot_pool.h"
|
|
|
|
#include "dtable.h"
|
2011-04-14 12:25:17 +00:00
|
|
|
#include "visibility.h"
|
2010-05-16 21:32:43 +00:00
|
|
|
|
2011-04-14 12:25:17 +00:00
|
|
|
PRIVATE dtable_t __objc_uninstalled_dtable;
|
2010-05-16 21:32:43 +00:00
|
|
|
|
2010-05-19 12:56:46 +00:00
|
|
|
/** Head of the list of temporary dtables. Protected by initialize_lock. */
|
2011-04-14 12:25:17 +00:00
|
|
|
PRIVATE InitializingDtable *temporary_dtables;
|
|
|
|
PRIVATE mutex_t initialize_lock;
|
2010-05-16 21:32:43 +00:00
|
|
|
static uint32_t dtable_depth = 8;
|
|
|
|
|
|
|
|
|
|
|
|
static void collectMethodsForMethodListToSparseArray(
|
|
|
|
struct objc_method_list *list,
|
|
|
|
SparseArray *sarray)
|
|
|
|
{
|
|
|
|
if (NULL != list->next)
|
|
|
|
{
|
|
|
|
collectMethodsForMethodListToSparseArray(list->next, sarray);
|
|
|
|
}
|
|
|
|
for (unsigned i=0 ; i<list->count ; i++)
|
|
|
|
{
|
2010-06-06 21:11:25 +00:00
|
|
|
//fprintf(stderr, "Adding method %s (%d)\n", sel_getName(list->methods[i].selector), PTR_TO_IDX(list->methods[i].selector->name));
|
2011-03-15 17:27:26 +00:00
|
|
|
SparseArrayInsert(sarray, list->methods[i].selector->index,
|
2010-05-16 21:32:43 +00:00
|
|
|
(void*)&list->methods[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-15 22:30:32 +00:00
|
|
|
|
|
|
|
#ifdef __OBJC_LOW_MEMORY__
|
|
|
|
|
|
|
|
struct objc_dtable
|
|
|
|
{
|
|
|
|
struct cache_line
|
|
|
|
{
|
|
|
|
uint32_t idx;
|
|
|
|
uint32_t version;
|
|
|
|
struct objc_slot *slot;
|
|
|
|
} cache[8];
|
|
|
|
Class cls;
|
|
|
|
struct slots_list
|
|
|
|
{
|
|
|
|
uint32_t idx;
|
|
|
|
struct objc_slot *slot;
|
|
|
|
} *slots;
|
|
|
|
int slot_count;
|
|
|
|
int slot_size;
|
|
|
|
mutex_t lock;
|
|
|
|
};
|
|
|
|
|
2011-04-14 12:25:17 +00:00
|
|
|
PRIVATE void __objc_init_dispatch_tables ()
|
2010-08-15 22:30:32 +00:00
|
|
|
{
|
|
|
|
INIT_LOCK(initialize_lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
Class class_getSuperclass(Class);
|
|
|
|
|
2011-04-14 12:25:17 +00:00
|
|
|
PRIVATE void __objc_update_dispatch_table_for_class(Class cls)
|
2010-08-15 22:30:32 +00:00
|
|
|
{
|
|
|
|
static BOOL warned = NO;
|
|
|
|
if (!warned)
|
|
|
|
{
|
|
|
|
fprintf(stderr,
|
|
|
|
"Warning: Calling deprecated private ObjC runtime function %s\n", __func__);
|
|
|
|
warned = YES;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static dtable_t create_dtable_for_class(Class class)
|
|
|
|
{
|
|
|
|
// Don't create a dtable for a class that already has one
|
|
|
|
if (classHasDtable(class)) { return dtable_for_class(class); }
|
|
|
|
|
|
|
|
LOCK_UNTIL_RETURN(__objc_runtime_mutex);
|
|
|
|
|
|
|
|
// Make sure that another thread didn't create the dtable while we were
|
|
|
|
// waiting on the lock.
|
|
|
|
if (classHasDtable(class)) { return dtable_for_class(class); }
|
|
|
|
|
|
|
|
Class super = class_getSuperclass(class);
|
|
|
|
|
|
|
|
/* Allocate dtable if necessary */
|
|
|
|
dtable_t dtable = calloc(1, sizeof(struct objc_dtable));
|
|
|
|
dtable->cls = class;
|
|
|
|
INIT_LOCK(dtable->lock);
|
|
|
|
|
|
|
|
return dtable;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-04-14 12:25:17 +00:00
|
|
|
PRIVATE void objc_resize_dtables(uint32_t newSize)
|
2010-08-15 22:30:32 +00:00
|
|
|
{
|
|
|
|
if (1<<dtable_depth > newSize) { return; }
|
|
|
|
dtable_depth <<= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define HASH_UID(uid) ((uid >> 2) & 7)
|
|
|
|
|
|
|
|
static struct objc_slot* check_cache(dtable_t dtable, uint32_t uid)
|
|
|
|
{
|
|
|
|
int i = HASH_UID(uid);
|
|
|
|
volatile struct cache_line *cache = &dtable->cache[i];
|
|
|
|
int32_t initial_idx = cache->idx;
|
|
|
|
|
|
|
|
if (initial_idx != uid)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct objc_slot *slot;
|
|
|
|
int32_t idx;
|
|
|
|
int32_t version;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
initial_idx = cache->idx;
|
|
|
|
version = cache->version;
|
|
|
|
slot = cache->slot;
|
|
|
|
__sync_synchronize();
|
|
|
|
idx = cache->idx;
|
|
|
|
} while (idx != initial_idx);
|
|
|
|
|
|
|
|
return (idx == uid) && (slot->version == version) ? slot : NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct slots_list *find_slot(uint32_t uid,
|
|
|
|
struct slots_list *slots, int slot_count)
|
|
|
|
{
|
|
|
|
if (slot_count == 0) { return NULL; }
|
|
|
|
int idx = slot_count >> 1;
|
|
|
|
struct slots_list *slot = &slots[idx];
|
|
|
|
if (slot_count == 1)
|
|
|
|
{
|
|
|
|
if (slot->idx == uid)
|
|
|
|
{
|
|
|
|
return slot;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (slot->idx > uid)
|
|
|
|
{
|
|
|
|
return find_slot(uid, slots, idx);
|
|
|
|
}
|
|
|
|
if (slot->idx < uid)
|
|
|
|
{
|
|
|
|
return find_slot(uid, slots+idx, slot_count - idx);
|
|
|
|
}
|
|
|
|
if (slot->idx == uid)
|
|
|
|
{
|
|
|
|
return slot;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int slot_cmp(const void *l, const void *r)
|
|
|
|
{
|
|
|
|
return (((struct slots_list*)l)->idx - ((struct slots_list*)r)->idx);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void insert_slot(dtable_t dtable, struct objc_slot *slot, uint32_t idx)
|
|
|
|
{
|
|
|
|
if (dtable->slot_size == dtable->slot_count)
|
|
|
|
{
|
|
|
|
dtable->slot_size += 16;
|
|
|
|
dtable->slots = realloc(dtable->slots, dtable->slot_size *
|
|
|
|
sizeof(struct slots_list));
|
|
|
|
assert(NULL != dtable->slots && "Out of memory!");
|
|
|
|
}
|
|
|
|
dtable->slots[dtable->slot_count].slot = slot;
|
|
|
|
dtable->slots[dtable->slot_count++].idx = idx;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void update_dtable(dtable_t dtable)
|
|
|
|
{
|
|
|
|
//fprintf(stderr, "Updating dtable for %s! %d\n", dtable->cls->name, dtable_depth);
|
|
|
|
Class cls = dtable->cls;
|
|
|
|
|
|
|
|
if (NULL == cls->methods) { return; }
|
|
|
|
|
|
|
|
SparseArray *methods = SparseArrayNewWithDepth(dtable_depth);
|
|
|
|
collectMethodsForMethodListToSparseArray((void*)cls->methods, methods);
|
|
|
|
|
|
|
|
if (NULL == dtable->slots)
|
|
|
|
{
|
|
|
|
dtable->slots = calloc(sizeof(struct slots_list), 16);
|
|
|
|
dtable->slot_size = 16;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t old_slot_count = dtable->slot_count;
|
|
|
|
struct objc_method *m;
|
|
|
|
uint32_t idx = 0;
|
|
|
|
while ((m = SparseArrayNext(methods, &idx)))
|
|
|
|
{
|
2011-03-15 17:27:26 +00:00
|
|
|
uint32_t idx = m->selector->index;
|
2010-08-15 22:30:32 +00:00
|
|
|
struct slots_list *s = find_slot(idx, dtable->slots, old_slot_count);
|
|
|
|
if (NULL != s)
|
|
|
|
{
|
|
|
|
s->slot->method = m->imp;
|
|
|
|
s->slot->version++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
struct objc_slot *slot = new_slot_for_method_in_class(m, cls);
|
|
|
|
insert_slot(dtable, slot, idx);
|
|
|
|
if (Nil != cls->super_class)
|
|
|
|
{
|
|
|
|
slot = objc_dtable_lookup(dtable_for_class(cls->super_class), idx);
|
|
|
|
if (NULL != slot)
|
|
|
|
{
|
|
|
|
slot->version++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mergesort(dtable->slots, dtable->slot_count, sizeof(struct slots_list),
|
|
|
|
slot_cmp);
|
|
|
|
SparseArrayDestroy(methods);
|
|
|
|
}
|
|
|
|
|
2011-04-14 12:25:17 +00:00
|
|
|
PRIVATE void objc_update_dtable_for_class(Class cls)
|
2010-08-15 22:30:32 +00:00
|
|
|
{
|
|
|
|
dtable_t dtable = dtable_for_class(cls);
|
|
|
|
// Be lazy about constructing the slot list - don't do it unless we actually
|
|
|
|
// need to access it
|
|
|
|
if ((NULL == dtable) || (NULL == dtable->slots)) { return; }
|
|
|
|
|
|
|
|
LOCK_UNTIL_RETURN(&dtable->lock);
|
|
|
|
|
|
|
|
update_dtable(dtable);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-04-14 12:25:17 +00:00
|
|
|
PRIVATE struct objc_slot* objc_dtable_lookup(dtable_t dtable, uint32_t uid)
|
2010-08-15 22:30:32 +00:00
|
|
|
{
|
|
|
|
if (NULL == dtable) { return NULL; }
|
|
|
|
|
|
|
|
struct objc_slot *slot = check_cache(dtable, uid);
|
|
|
|
|
|
|
|
if (NULL != slot)
|
|
|
|
{
|
|
|
|
return slot;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOCK_UNTIL_RETURN(&dtable->lock);
|
|
|
|
if (NULL == dtable->slots)
|
|
|
|
{
|
|
|
|
update_dtable(dtable);
|
|
|
|
}
|
|
|
|
//fprintf(stderr, "Using slow path: %d entries in table for %s\n", dtable->slot_count, dtable->cls->name);
|
|
|
|
struct slots_list *s = find_slot(uid, dtable->slots, dtable->slot_count);
|
|
|
|
if (NULL != s)
|
|
|
|
{
|
|
|
|
slot = s->slot;
|
|
|
|
int i = HASH_UID(uid);
|
|
|
|
volatile struct cache_line *cache = &dtable->cache[i];
|
|
|
|
cache->idx = 0;
|
|
|
|
cache->version = slot->version;
|
|
|
|
cache->slot = slot;
|
|
|
|
__sync_synchronize();
|
|
|
|
cache->idx = uid;
|
|
|
|
return slot;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NULL != dtable->cls->super_class)
|
|
|
|
{
|
|
|
|
return objc_dtable_lookup(dtable->cls->super_class->dtable, uid);
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
2011-04-14 12:25:17 +00:00
|
|
|
PRIVATE dtable_t objc_copy_dtable_for_class(dtable_t old, Class cls)
|
2010-10-04 17:44:17 +00:00
|
|
|
{
|
|
|
|
dtable_t dtable = calloc(1, sizeof(struct objc_dtable));
|
|
|
|
dtable->cls = cls;
|
|
|
|
INIT_LOCK(dtable->lock);
|
|
|
|
return dtable;
|
|
|
|
}
|
2010-08-15 22:30:32 +00:00
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
|
2011-04-14 12:25:17 +00:00
|
|
|
PRIVATE void __objc_init_dispatch_tables ()
|
2010-08-15 22:30:32 +00:00
|
|
|
{
|
|
|
|
INIT_LOCK(initialize_lock);
|
|
|
|
__objc_uninstalled_dtable = SparseArrayNewWithDepth(dtable_depth);
|
|
|
|
}
|
|
|
|
|
2010-05-16 21:32:43 +00:00
|
|
|
static BOOL installMethodInDtable(Class class,
|
2010-05-21 13:49:19 +00:00
|
|
|
Class owner,
|
2010-05-16 21:32:43 +00:00
|
|
|
SparseArray *dtable,
|
|
|
|
struct objc_method *method,
|
|
|
|
BOOL replaceExisting)
|
|
|
|
{
|
|
|
|
assert(__objc_uninstalled_dtable != dtable);
|
2011-03-15 17:27:26 +00:00
|
|
|
uint32_t sel_id = method->selector->index;
|
2010-05-16 21:32:43 +00:00
|
|
|
struct objc_slot *slot = SparseArrayLookup(dtable, sel_id);
|
2010-05-21 13:49:19 +00:00
|
|
|
if (NULL != slot)
|
2010-05-16 21:32:43 +00:00
|
|
|
{
|
2010-06-06 21:11:25 +00:00
|
|
|
//fprintf(stderr, "Slot already exists...\n");
|
2010-05-21 13:49:19 +00:00
|
|
|
// If this method is the one already installed, pretend to install it again.
|
|
|
|
if (slot->method == method->imp) { return NO; }
|
|
|
|
|
2010-05-21 19:00:02 +00:00
|
|
|
// If the existing slot is for this class, we can just replace the
|
|
|
|
// implementation. We don't need to bump the version; this operation
|
|
|
|
// updates cached slots, it doesn't invalidate them.
|
|
|
|
if (slot->owner == owner)
|
2010-05-16 21:32:43 +00:00
|
|
|
{
|
2010-05-21 19:00:02 +00:00
|
|
|
// Don't replace methods if we're not meant to (if they're from
|
|
|
|
// later in a method list, for example)
|
2010-05-21 13:49:19 +00:00
|
|
|
if (!replaceExisting) { return NO; }
|
2010-06-06 21:11:25 +00:00
|
|
|
//fprintf(stderr, "Replacing method %p %s in %s with %p\n", slot->method, sel_getName(method->selector), class->name, method->imp);
|
2010-05-21 13:49:19 +00:00
|
|
|
slot->method = method->imp;
|
|
|
|
return YES;
|
2010-05-16 21:32:43 +00:00
|
|
|
}
|
2010-05-21 19:00:02 +00:00
|
|
|
|
|
|
|
// Check whether the owner of this method is a subclass of the one that
|
|
|
|
// owns this method. If it is, then we don't want to install this
|
|
|
|
// method irrespective of other cases, because it has been overridden.
|
|
|
|
for (Class installedFor = slot->owner ;
|
|
|
|
Nil != installedFor ;
|
|
|
|
installedFor = installedFor->super_class)
|
|
|
|
{
|
2010-06-06 21:11:25 +00:00
|
|
|
if (installedFor == owner)
|
|
|
|
{
|
|
|
|
//fprintf(stderr, "Not installing %s from %s in %s - already overridden from %s\n", sel_getName(method->selector), owner->name, class->name, slot->owner->name);
|
|
|
|
return NO;
|
|
|
|
}
|
2010-05-21 19:00:02 +00:00
|
|
|
}
|
2010-05-16 21:32:43 +00:00
|
|
|
}
|
2010-05-21 13:49:19 +00:00
|
|
|
struct objc_slot *oldSlot = slot;
|
2010-06-06 21:11:25 +00:00
|
|
|
//fprintf(stderr, "Installing method %p (%d) %s in %s (previous slot owned by %s)\n", method->imp, sel_id, sel_getName(method->selector), class->name, slot? oldSlot->owner->name: "(no one)");
|
2010-05-21 13:49:19 +00:00
|
|
|
slot = new_slot_for_method_in_class((void*)method, owner);
|
|
|
|
SparseArrayInsert(dtable, sel_id, slot);
|
2010-06-29 12:42:20 +00:00
|
|
|
// In TDD mode, we also register the first typed method that we
|
|
|
|
// encounter as the untyped version.
|
|
|
|
#ifdef TYPE_DEPENDENT_DISPATCH
|
|
|
|
SparseArrayInsert(dtable, get_untyped_idx(method->selector), slot);
|
|
|
|
#endif
|
2010-05-21 13:49:19 +00:00
|
|
|
// Invalidate the old slot, if there is one.
|
|
|
|
if (NULL != oldSlot)
|
|
|
|
{
|
2010-06-06 21:11:25 +00:00
|
|
|
//fprintf(stderr, "Overriding method %p %s from %s in %s with %x\n", slot->method, sel_getName(method->selector), oldSlot->owner->name, class->name, method->imp);
|
2010-05-21 13:49:19 +00:00
|
|
|
oldSlot->version++;
|
|
|
|
}
|
2010-05-16 21:32:43 +00:00
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void installMethodsInClass(Class cls,
|
2010-05-21 13:49:19 +00:00
|
|
|
Class owner,
|
2010-05-16 21:32:43 +00:00
|
|
|
SparseArray *methods,
|
|
|
|
BOOL replaceExisting)
|
|
|
|
{
|
|
|
|
SparseArray *dtable = dtable_for_class(cls);
|
|
|
|
assert(__objc_uninstalled_dtable != dtable);
|
|
|
|
|
|
|
|
uint32_t idx = 0;
|
|
|
|
struct objc_method *m;
|
|
|
|
while ((m = SparseArrayNext(methods, &idx)))
|
|
|
|
{
|
2010-05-21 13:49:19 +00:00
|
|
|
if (!installMethodInDtable(cls, owner, dtable, m, replaceExisting))
|
2010-05-16 21:32:43 +00:00
|
|
|
{
|
|
|
|
// Remove this method from the list, if it wasn't actually installed
|
|
|
|
SparseArrayInsert(methods, idx, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-21 13:49:19 +00:00
|
|
|
static void mergeMethodsFromSuperclass(Class super, Class cls, SparseArray *methods)
|
2010-05-16 21:32:43 +00:00
|
|
|
{
|
|
|
|
for (struct objc_class *subclass=cls->subclass_list ;
|
|
|
|
Nil != subclass ; subclass = subclass->sibling_class)
|
|
|
|
{
|
|
|
|
// Don't bother updating dtables for subclasses that haven't been
|
|
|
|
// initialized yet
|
2010-05-21 13:49:19 +00:00
|
|
|
if (!classHasDtable(subclass)) { continue; }
|
2010-05-16 21:32:43 +00:00
|
|
|
|
|
|
|
// Create a new (copy-on-write) array to pass down to children
|
|
|
|
SparseArray *newMethods = SparseArrayCopy(methods);
|
|
|
|
// Install all of these methods except ones that are overridden in the
|
|
|
|
// subclass. All of the methods that we are updating were added in a
|
|
|
|
// superclass, so we don't replace versions registered to the subclass.
|
2010-05-21 13:49:19 +00:00
|
|
|
installMethodsInClass(subclass, super, newMethods, YES);
|
2010-05-16 21:32:43 +00:00
|
|
|
// Recursively add the methods to the subclass's subclasses.
|
2010-05-21 13:49:19 +00:00
|
|
|
mergeMethodsFromSuperclass(super, subclass, newMethods);
|
2010-05-16 21:32:43 +00:00
|
|
|
SparseArrayDestroy(newMethods);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Class class_getSuperclass(Class);
|
|
|
|
|
2011-04-14 12:25:17 +00:00
|
|
|
PRIVATE void objc_update_dtable_for_class(Class cls)
|
2010-05-16 21:32:43 +00:00
|
|
|
{
|
|
|
|
// Only update real dtables
|
|
|
|
if (!classHasDtable(cls)) { return; }
|
2010-05-21 19:00:02 +00:00
|
|
|
//fprintf(stderr, "Updating dtable for %s\n", cls->name);
|
2010-05-16 21:32:43 +00:00
|
|
|
|
|
|
|
LOCK_UNTIL_RETURN(__objc_runtime_mutex);
|
|
|
|
|
2010-06-06 21:11:25 +00:00
|
|
|
//fprintf(stderr, "Adding methods to %s\n", cls->name);
|
2010-05-16 21:32:43 +00:00
|
|
|
SparseArray *methods = SparseArrayNewWithDepth(dtable_depth);
|
|
|
|
collectMethodsForMethodListToSparseArray((void*)cls->methods, methods);
|
2010-05-21 13:49:19 +00:00
|
|
|
installMethodsInClass(cls, cls, methods, YES);
|
2010-05-16 21:32:43 +00:00
|
|
|
// Methods now contains only the new methods for this class.
|
2010-05-21 13:49:19 +00:00
|
|
|
mergeMethodsFromSuperclass(cls, cls, methods);
|
2010-05-16 21:32:43 +00:00
|
|
|
SparseArrayDestroy(methods);
|
|
|
|
}
|
2011-04-14 12:25:17 +00:00
|
|
|
PRIVATE void __objc_update_dispatch_table_for_class(Class cls)
|
2010-06-02 22:25:12 +00:00
|
|
|
{
|
|
|
|
static BOOL warned = NO;
|
|
|
|
if (!warned)
|
|
|
|
{
|
|
|
|
fprintf(stderr,
|
|
|
|
"Warning: Calling deprecated private ObjC runtime function %s\n", __func__);
|
|
|
|
warned = YES;
|
|
|
|
}
|
|
|
|
objc_update_dtable_for_class(cls);
|
|
|
|
}
|
2010-05-16 21:32:43 +00:00
|
|
|
|
|
|
|
static SparseArray *create_dtable_for_class(Class class)
|
|
|
|
{
|
|
|
|
// Don't create a dtable for a class that already has one
|
|
|
|
if (classHasDtable(class)) { return dtable_for_class(class); }
|
|
|
|
|
|
|
|
LOCK_UNTIL_RETURN(__objc_runtime_mutex);
|
|
|
|
|
|
|
|
// Make sure that another thread didn't create the dtable while we were
|
|
|
|
// waiting on the lock.
|
|
|
|
if (classHasDtable(class)) { return dtable_for_class(class); }
|
|
|
|
|
|
|
|
Class super = class_getSuperclass(class);
|
2011-03-09 14:43:24 +00:00
|
|
|
dtable_t dtable;
|
2010-05-16 21:32:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
if (Nil == super)
|
|
|
|
{
|
|
|
|
dtable = SparseArrayNewWithDepth(dtable_depth);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-03-09 14:43:24 +00:00
|
|
|
dtable_t super_dtable = dtable_for_class(super);
|
|
|
|
if (super_dtable == __objc_uninstalled_dtable)
|
|
|
|
{
|
|
|
|
super_dtable = create_dtable_for_class(super);
|
|
|
|
}
|
2010-05-16 21:32:43 +00:00
|
|
|
dtable = SparseArrayCopy(dtable_for_class(super));
|
|
|
|
}
|
|
|
|
|
|
|
|
// When constructing the initial dtable for a class, we iterate along the
|
|
|
|
// method list in forward-traversal order. The first method that we
|
|
|
|
// encounter is always the one that we want to keep, so we instruct
|
|
|
|
// installMethodInDtable() not to replace methods that are already
|
|
|
|
// associated with this class.
|
|
|
|
struct objc_method_list *list = (void*)class->methods;
|
|
|
|
|
|
|
|
while (NULL != list)
|
|
|
|
{
|
|
|
|
for (unsigned i=0 ; i<list->count ; i++)
|
|
|
|
{
|
2010-05-21 13:49:19 +00:00
|
|
|
installMethodInDtable(class, class, dtable, &list->methods[i], NO);
|
2010-05-16 21:32:43 +00:00
|
|
|
}
|
|
|
|
list = list->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
return dtable;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Class class_table_next(void **e);
|
|
|
|
|
2011-04-14 12:25:17 +00:00
|
|
|
PRIVATE void objc_resize_dtables(uint32_t newSize)
|
2010-05-16 21:32:43 +00:00
|
|
|
{
|
|
|
|
// If dtables already have enough space to store all registered selectors, do nothing
|
|
|
|
if (1<<dtable_depth > newSize) { return; }
|
|
|
|
|
|
|
|
LOCK_UNTIL_RETURN(__objc_runtime_mutex);
|
|
|
|
|
|
|
|
dtable_depth <<= 1;
|
|
|
|
|
|
|
|
uint32_t oldMask = __objc_uninstalled_dtable->mask;
|
|
|
|
|
|
|
|
SparseArrayExpandingArray(__objc_uninstalled_dtable);
|
|
|
|
// Resize all existing dtables
|
|
|
|
void *e = NULL;
|
|
|
|
struct objc_class *next;
|
|
|
|
while ((next = class_table_next(&e)))
|
|
|
|
{
|
|
|
|
if (next->dtable != (void*)__objc_uninstalled_dtable &&
|
|
|
|
NULL != next->dtable &&
|
|
|
|
((SparseArray*)next->dtable)->mask == oldMask)
|
|
|
|
{
|
|
|
|
SparseArrayExpandingArray((void*)next->dtable);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-14 12:25:17 +00:00
|
|
|
PRIVATE dtable_t objc_copy_dtable_for_class(dtable_t old, Class cls)
|
2010-10-04 17:44:17 +00:00
|
|
|
{
|
|
|
|
return SparseArrayCopy(old);
|
|
|
|
}
|
|
|
|
|
2010-08-15 22:30:32 +00:00
|
|
|
#endif // __OBJC_LOW_MEMORY__
|
|
|
|
|
2010-06-03 15:17:12 +00:00
|
|
|
void objc_resolve_class(Class);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send a +initialize message to the receiver, if required.
|
|
|
|
*/
|
2011-04-14 12:25:17 +00:00
|
|
|
PRIVATE void objc_send_initialize(id object)
|
2010-06-03 15:17:12 +00:00
|
|
|
{
|
|
|
|
Class class = object->isa;
|
|
|
|
// If the first message is sent to an instance (weird, but possible and
|
|
|
|
// likely for things like NSConstantString, make sure +initialize goes to
|
|
|
|
// the class not the metaclass.
|
|
|
|
if (objc_test_class_flag(class, objc_class_flag_meta))
|
|
|
|
{
|
|
|
|
class = (Class)object;
|
|
|
|
}
|
|
|
|
Class meta = class->isa;
|
|
|
|
|
|
|
|
// If this class is already initialized (e.g. in another thread), give up.
|
|
|
|
if (objc_test_class_flag(class, objc_class_flag_initialized)) { return; }
|
|
|
|
|
|
|
|
// Grab a lock to make sure we are only sending one +initialize message at
|
|
|
|
// once.
|
|
|
|
//
|
|
|
|
// NOTE: Ideally, we would actually lock on the class object using
|
|
|
|
// objc_sync_enter(). This should be fixed once sync.m contains a (fast)
|
|
|
|
// special case for classes.
|
|
|
|
LOCK_UNTIL_RETURN(&initialize_lock);
|
2010-05-16 21:32:43 +00:00
|
|
|
|
2010-06-03 15:17:12 +00:00
|
|
|
// Make sure that the class is resolved.
|
|
|
|
objc_resolve_class(class);
|
|
|
|
|
|
|
|
// Make sure that the superclass is initialized first.
|
|
|
|
if (Nil != class->super_class)
|
|
|
|
{
|
|
|
|
objc_send_initialize((id)class->super_class);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Superclass +initialize might possibly send a message to this class, in
|
|
|
|
// which case this method would be called again. See NSObject and
|
|
|
|
// NSAutoreleasePool +initialize interaction in GNUstep.
|
|
|
|
if (objc_test_class_flag(class, objc_class_flag_initialized)) { return; }
|
|
|
|
|
|
|
|
// Set the initialized flag on both this class and its metaclass, to make
|
|
|
|
// sure that +initialize is only ever sent once.
|
|
|
|
objc_set_class_flag(class, objc_class_flag_initialized);
|
|
|
|
objc_set_class_flag(meta, objc_class_flag_initialized);
|
|
|
|
|
|
|
|
|
|
|
|
// Create a temporary dtable, to be installed later.
|
2010-08-15 22:30:32 +00:00
|
|
|
dtable_t class_dtable = create_dtable_for_class(class);
|
2011-03-09 14:43:24 +00:00
|
|
|
InitializingDtable buffer = { class, class_dtable, temporary_dtables };
|
|
|
|
temporary_dtables = &buffer;
|
2010-06-03 15:17:12 +00:00
|
|
|
|
|
|
|
// Create an entry in the dtable look-aside buffer for this. When sending
|
|
|
|
// a message to this class in future, the lookup function will check this
|
|
|
|
// buffer if the receiver's dtable is not installed, and block if
|
|
|
|
// attempting to send a message to this class.
|
2011-03-09 14:43:24 +00:00
|
|
|
dtable_t dtable = create_dtable_for_class(meta);
|
2010-06-03 15:17:12 +00:00
|
|
|
InitializingDtable meta_buffer = { meta, dtable, temporary_dtables };
|
2011-03-09 14:43:24 +00:00
|
|
|
temporary_dtables = &meta_buffer;
|
2010-06-03 15:17:12 +00:00
|
|
|
|
|
|
|
// Store the buffer in the temporary dtables list. Note that it is safe to
|
|
|
|
// insert it into a global list, even though it's a temporary variable,
|
|
|
|
// because we will clean it up after this function.
|
|
|
|
//
|
|
|
|
// FIXME: This will actually break if +initialize throws an exception...
|
|
|
|
|
|
|
|
static SEL initializeSel = 0;
|
|
|
|
if (0 == initializeSel)
|
|
|
|
{
|
|
|
|
initializeSel = sel_registerName("initialize");
|
|
|
|
}
|
|
|
|
struct objc_slot *initializeSlot =
|
2011-03-15 17:27:26 +00:00
|
|
|
objc_dtable_lookup(dtable, initializeSel->index);
|
2010-06-03 15:17:12 +00:00
|
|
|
|
|
|
|
if (0 != initializeSlot)
|
|
|
|
{
|
|
|
|
if (Nil != class->super_class)
|
|
|
|
{
|
2011-03-09 14:43:24 +00:00
|
|
|
// The dtable to use for sending messages to the superclass. This
|
|
|
|
// is the superclass's metaclass' dtable.
|
|
|
|
dtable_t super_dtable = dtable_for_class(class->super_class->isa);
|
2010-08-15 22:30:32 +00:00
|
|
|
struct objc_slot *superSlot = objc_dtable_lookup(super_dtable,
|
2011-03-15 17:27:26 +00:00
|
|
|
initializeSel->index);
|
2011-03-09 14:43:24 +00:00
|
|
|
// Check that this IMP comes from the class, not from its
|
|
|
|
// superclass. We still have to use dtable_for_class() here
|
|
|
|
// because our +initialize call might be in response to a message
|
|
|
|
// sent from a subclass (e.g. NSObject +initialize sending a
|
|
|
|
// message to NSAutoreleasePool: NSObject's dtable won't have been
|
|
|
|
// installed at this point.
|
2010-06-03 15:17:12 +00:00
|
|
|
if (0 == superSlot || superSlot->method != initializeSlot->method)
|
|
|
|
{
|
|
|
|
initializeSlot->method((id)class, initializeSel);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
initializeSlot->method((id)class, initializeSel);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Install the real dtable for both the class and the metaclass.
|
|
|
|
meta->dtable = dtable;
|
|
|
|
class->dtable = class_dtable;
|
|
|
|
|
|
|
|
// Remove the look-aside buffer entry.
|
2011-03-09 14:43:24 +00:00
|
|
|
if (temporary_dtables == &meta_buffer)
|
2010-06-03 15:17:12 +00:00
|
|
|
{
|
2011-03-09 14:43:24 +00:00
|
|
|
temporary_dtables = buffer.next;
|
2010-06-03 15:17:12 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
InitializingDtable *prev = temporary_dtables;
|
2011-03-09 14:43:24 +00:00
|
|
|
while (prev->next->class != meta)
|
2010-06-03 15:17:12 +00:00
|
|
|
{
|
|
|
|
prev = prev->next;
|
|
|
|
}
|
2011-03-09 14:43:24 +00:00
|
|
|
prev->next = buffer.next;
|
2010-06-03 15:17:12 +00:00
|
|
|
}
|
|
|
|
}
|