/* GNU Objective C Runtime class related functions
- Copyright (C) 1993, 1995, 1996, 1997, 2001, 2002
+ Copyright (C) 1993, 1995, 1996, 1997, 2001, 2002, 2009
Free Software Foundation, Inc.
Contributed by Kresten Krab Thorup and Dennis Glatting.
GCC is free software; you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
-Foundation; either version 2, or (at your option) any later version.
+Foundation; either version 3, or (at your option) any later version.
GCC is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
details.
-You should have received a copy of the GNU General Public License along with
-GCC; see the file COPYING. If not, write to the Free Software
-Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
+Under Section 7 of GPL version 3, you are granted additional
+permissions described in the GCC Runtime Library Exception, version
+3.1, as published by the Free Software Foundation.
-/* As a special exception, if you link this library with files compiled with
- GCC to produce an executable, this does not cause the resulting executable
- to be covered by the GNU General Public License. This exception does not
- however invalidate any other reasons why the executable file might be
- covered by the GNU General Public License. */
+You should have received a copy of the GNU General Public License and
+a copy of the GCC Runtime Library Exception along with this program;
+see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
+<http://www.gnu.org/licenses/>. */
/*
The code in this file critically affects class method invocation
classes from the table - and the difficult thing with lock-free data
structures is freeing data when is removed from the structures. */
-#include "runtime.h" /* the kitchen sink */
-#include "sarray.h"
-
-#include <objc/objc.h>
-#include <objc/objc-api.h>
-#include <objc/thr.h>
+#include "objc-private/common.h"
+#include "objc-private/error.h"
+#include "objc/runtime.h"
+#include "objc/thr.h"
+#include "objc-private/module-abi-8.h" /* For CLS_ISCLASS and similar. */
+#include "objc-private/runtime.h" /* the kitchen sink */
+#include "objc-private/sarray.h" /* For sarray_put_at_safe. */
+#include <string.h> /* For memset */
/* We use a table which maps a class name to the corresponding class
* pointer. The first part of this file defines this table, and
/* The table writing mutex - we lock on writing to avoid conflicts
between different writers, but we read without locks. That is
possible because we assume pointer assignment to be an atomic
- operation. */
+ operation. TODO: This is only true under certain circumstances,
+ which should be clarified. */
static objc_mutex_t __class_table_lock = NULL;
/* CLASS_TABLE_HASH is how we compute the hash of a class name. It is
/* This is a hook which is called by objc_get_class and
objc_lookup_class if the runtime is not able to find the class.
- This may e.g. try to load in the class using dynamic loading. */
+ This may e.g. try to load in the class using dynamic loading.
+
+ This hook was a public, global variable in the Traditional GNU
+ Objective-C Runtime API (objc/objc-api.h). The modern GNU
+ Objective-C Runtime API (objc/runtime.h) provides the
+ objc_setGetUnknownClassHandler() function instead.
+*/
Class (*_objc_lookup_class) (const char *name) = 0; /* !T:SAFE */
+/* The handler currently in use. PS: if both
+ __obj_get_unknown_class_handler and _objc_lookup_class are defined,
+ __objc_get_unknown_class_handler is called first. */
+static objc_get_unknown_class_handler
+__objc_get_unknown_class_handler = NULL;
+
+objc_get_unknown_class_handler
+objc_setGetUnknownClassHandler (objc_get_unknown_class_handler
+ new_handler)
+{
+ objc_get_unknown_class_handler old_handler
+ = __objc_get_unknown_class_handler;
+ __objc_get_unknown_class_handler = new_handler;
+ return old_handler;
+}
+
/* True when class links has been resolved. */
BOOL __objc_class_links_resolved = NO; /* !T:UNUSED */
objc_mutex_unlock (__objc_runtime_mutex);
}
-/* Get the class object for the class named NAME. If NAME does not
- identify a known class, the hook _objc_lookup_class is called. If
- this fails, nil is returned. */
Class
-objc_lookup_class (const char *name)
+objc_getClass (const char *name)
{
Class class;
- class = class_table_get_safe (name);
+ if (name == NULL)
+ return Nil;
+ class = class_table_get_safe (name);
+
if (class)
return class;
+ if (__objc_get_unknown_class_handler)
+ return (*__objc_get_unknown_class_handler) (name);
+
if (_objc_lookup_class)
return (*_objc_lookup_class) (name);
+
+ return Nil;
+}
+
+Class
+objc_lookupClass (const char *name)
+{
+ if (name == NULL)
+ return Nil;
else
- return 0;
+ return class_table_get_safe (name);
+}
+
+Class
+objc_getMetaClass (const char *name)
+{
+ Class class = objc_getClass (name);
+
+ if (class)
+ return class->class_pointer;
+ else
+ return Nil;
+}
+
+Class
+objc_getRequiredClass (const char *name)
+{
+ Class class = objc_getClass (name);
+
+ if (class)
+ return class;
+ else
+ _objc_abort ("objc_getRequiredClass ('%s') failed: class not found\n", name);
+}
+
+int
+objc_getClassList (Class *returnValue, int maxNumberOfClassesToReturn)
+{
+ /* Iterate over all entries in the table. */
+ int hash, count = 0;
+
+ for (hash = 0; hash < CLASS_TABLE_SIZE; hash++)
+ {
+ class_node_ptr node = class_table_array[hash];
+
+ while (node != NULL)
+ {
+ if (returnValue)
+ {
+ if (count < maxNumberOfClassesToReturn)
+ returnValue[count] = node->pointer;
+ else
+ {
+ return count;
+ }
+ }
+ count++;
+ node = node->next;
+ }
+ }
+
+ return count;
+}
+
+Class
+objc_allocateClassPair (Class super_class, const char *class_name, size_t extraBytes)
+{
+ Class new_class;
+ Class new_meta_class;
+
+ if (class_name == NULL)
+ return Nil;
+
+ if (objc_getClass (class_name))
+ return Nil;
+
+ if (super_class)
+ {
+ /* If you want to build a hierarchy of classes, you need to
+ build and register them one at a time. The risk is that you
+ are able to cause confusion by registering a subclass before
+ the superclass or similar. */
+ if (CLS_IS_IN_CONSTRUCTION (super_class))
+ return Nil;
+ }
+
+ /* Technically, we should create the metaclass first, then use
+ class_createInstance() to create the class. That complication
+ would be relevant if we had class variables, but we don't, so we
+ just ignore it and create everything directly and assume all
+ classes have the same size. */
+ new_class = objc_calloc (1, sizeof (struct objc_class) + extraBytes);
+ new_meta_class = objc_calloc (1, sizeof (struct objc_class) + extraBytes);
+
+ /* We create an unresolved class, similar to one generated by the
+ compiler. It will be resolved later when we register it.
+
+ Note how the metaclass details are not that important; when the
+ class is resolved, the ones that matter will be fixed up. */
+ new_class->class_pointer = new_meta_class;
+ new_meta_class->class_pointer = 0;
+
+ if (super_class)
+ {
+ /* Force the name of the superclass in place of the link to the
+ actual superclass, which will be put there when the class is
+ resolved. */
+ const char *super_class_name = class_getName (super_class);
+ new_class->super_class = (void *)super_class_name;
+ new_meta_class->super_class = (void *)super_class_name;
+ }
+ else
+ {
+ new_class->super_class = (void *)0;
+ new_meta_class->super_class = (void *)0;
+ }
+
+ new_class->name = objc_malloc (strlen (class_name) + 1);
+ strcpy ((char*)new_class->name, class_name);
+ new_meta_class->name = new_class->name;
+
+ new_class->version = 0;
+ new_meta_class->version = 0;
+
+ new_class->info = _CLS_CLASS | _CLS_IN_CONSTRUCTION;
+ new_meta_class->info = _CLS_META | _CLS_IN_CONSTRUCTION;
+
+ if (super_class)
+ new_class->instance_size = super_class->instance_size;
+ else
+ new_class->instance_size = 0;
+ new_meta_class->instance_size = sizeof (struct objc_class);
+
+ return new_class;
+}
+
+void
+objc_registerClassPair (Class class_)
+{
+ if (class_ == Nil)
+ return;
+
+ if ((! CLS_ISCLASS (class_)) || (! CLS_IS_IN_CONSTRUCTION (class_)))
+ return;
+
+ if ((! CLS_ISMETA (class_->class_pointer)) || (! CLS_IS_IN_CONSTRUCTION (class_->class_pointer)))
+ return;
+
+ objc_mutex_lock (__objc_runtime_mutex);
+
+ if (objc_getClass (class_->name))
+ {
+ objc_mutex_unlock (__objc_runtime_mutex);
+ return;
+ }
+
+ CLS_SET_NOT_IN_CONSTRUCTION (class_);
+ CLS_SET_NOT_IN_CONSTRUCTION (class_->class_pointer);
+
+ __objc_init_class (class_);
+
+ /* Resolve class links immediately. No point in waiting. */
+ __objc_resolve_class_links ();
+
+ objc_mutex_unlock (__objc_runtime_mutex);
+}
+
+void
+objc_disposeClassPair (Class class_)
+{
+ if (class_ == Nil)
+ return;
+
+ if ((! CLS_ISCLASS (class_)) || (! CLS_IS_IN_CONSTRUCTION (class_)))
+ return;
+
+ if ((! CLS_ISMETA (class_->class_pointer)) || (! CLS_IS_IN_CONSTRUCTION (class_->class_pointer)))
+ return;
+
+ /* Undo any class_addIvar(). */
+ if (class_->ivars)
+ {
+ int i;
+ for (i = 0; i < class_->ivars->ivar_count; i++)
+ {
+ struct objc_ivar *ivar = &(class_->ivars->ivar_list[i]);
+
+ objc_free ((char *)ivar->ivar_name);
+ objc_free ((char *)ivar->ivar_type);
+ }
+
+ objc_free (class_->ivars);
+ }
+
+ /* Undo any class_addMethod(). */
+ if (class_->methods)
+ {
+ struct objc_method_list *list = class_->methods;
+ while (list)
+ {
+ int i;
+ struct objc_method_list *next = list->method_next;
+
+ for (i = 0; i < list->method_count; i++)
+ {
+ struct objc_method *method = &(list->method_list[i]);
+
+ objc_free ((char *)method->method_name);
+ objc_free ((char *)method->method_types);
+ }
+
+ objc_free (list);
+ list = next;
+ }
+ }
+
+ /* Undo any class_addProtocol(). */
+ if (class_->protocols)
+ {
+ struct objc_protocol_list *list = class_->protocols;
+ while (list)
+ {
+ struct objc_protocol_list *next = list->next;
+
+ objc_free (list);
+ list = next;
+ }
+ }
+
+ /* Undo any class_addMethod() on the meta-class. */
+ if (class_->class_pointer->methods)
+ {
+ struct objc_method_list *list = class_->class_pointer->methods;
+ while (list)
+ {
+ int i;
+ struct objc_method_list *next = list->method_next;
+
+ for (i = 0; i < list->method_count; i++)
+ {
+ struct objc_method *method = &(list->method_list[i]);
+
+ objc_free ((char *)method->method_name);
+ objc_free ((char *)method->method_types);
+ }
+
+ objc_free (list);
+ list = next;
+ }
+ }
+
+ /* Undo objc_allocateClassPair(). */
+ objc_free ((char *)(class_->name));
+ objc_free (class_->class_pointer);
+ objc_free (class_);
+}
+
+/* Traditional GNU Objective-C Runtime API. */
+/* Get the class object for the class named NAME. If NAME does not
+ identify a known class, the hook _objc_lookup_class is called. If
+ this fails, nil is returned. */
+Class
+objc_lookup_class (const char *name)
+{
+ return objc_getClass (name);
}
+/* Traditional GNU Objective-C Runtime API. Important: this method is
+ called automatically by the compiler while messaging (if using the
+ traditional ABI), so it is worth keeping it fast; don't make it
+ just a wrapper around objc_getClass(). */
+/* Note that this is roughly equivalent to objc_getRequiredClass(). */
/* Get the class object for the class named NAME. If NAME does not
identify a known class, the hook _objc_lookup_class is called. If
this fails, an error message is issued and the system aborts. */
if (class)
return class;
- if (_objc_lookup_class)
+ if (__objc_get_unknown_class_handler)
+ class = (*__objc_get_unknown_class_handler) (name);
+
+ if ((!class) && _objc_lookup_class)
class = (*_objc_lookup_class) (name);
if (class)
return class;
- objc_error (nil, OBJC_ERR_BAD_CLASS,
- "objc runtime: cannot find class %s\n", name);
+ _objc_abort ("objc runtime: cannot find class %s\n", name);
+
return 0;
}
return class;
}
+/* This is used when the implementation of a method changes. It goes
+ through all classes, looking for the ones that have these methods
+ (either method_a or method_b; method_b can be NULL), and reloads
+ the implementation for these. You should call this with the
+ runtime mutex already locked. */
+void
+__objc_update_classes_with_methods (struct objc_method *method_a, struct objc_method *method_b)
+{
+ int hash;
+
+ /* Iterate over all classes. */
+ for (hash = 0; hash < CLASS_TABLE_SIZE; hash++)
+ {
+ class_node_ptr node = class_table_array[hash];
+
+ while (node != NULL)
+ {
+ /* Iterate over all methods in the class. */
+ Class class = node->pointer;
+ struct objc_method_list * method_list = class->methods;
+
+ while (method_list)
+ {
+ int i;
+
+ for (i = 0; i < method_list->method_count; ++i)
+ {
+ struct objc_method *method = &method_list->method_list[i];
+
+ /* If the method is one of the ones we are looking
+ for, update the implementation. */
+ if (method == method_a)
+ {
+ sarray_at_put_safe (class->dtable,
+ (sidx) method_a->method_name->sel_id,
+ method_a->method_imp);
+ }
+
+ if (method == method_b)
+ {
+ if (method_b != NULL)
+ {
+ sarray_at_put_safe (class->dtable,
+ (sidx) method_b->method_name->sel_id,
+ method_b->method_imp);
+ }
+ }
+ }
+
+ method_list = method_list->method_next;
+ }
+ node = node->next;
+ }
+ }
+}
+
/* Resolve super/subclass links for all classes. The only thing we
can be sure of is that the class_pointer for class objects point to
the right meta class objects. */
objc_mutex_unlock (__objc_runtime_mutex);
}
+const char *
+class_getName (Class class_)
+{
+ if (class_ == Nil)
+ return "nil";
+
+ return class_->name;
+}
+
+BOOL
+class_isMetaClass (Class class_)
+{
+ /* CLS_ISMETA includes the check for Nil class_. */
+ return CLS_ISMETA (class_);
+}
+
+/* Even inside libobjc it may be worth using class_getSuperclass
+ instead of accessing class_->super_class directly because it
+ resolves the class links if needed. If you access
+ class_->super_class directly, make sure to deal with the situation
+ where the class is not resolved yet! */
+Class
+class_getSuperclass (Class class_)
+{
+ if (class_ == Nil)
+ return Nil;
+
+ /* Classes that are in construction are not resolved and can not be
+ resolved! */
+ if (CLS_IS_IN_CONSTRUCTION (class_))
+ return Nil;
+
+ /* If the class is not resolved yet, super_class would point to a
+ string (the name of the super class) as opposed to the actual
+ super class. In that case, we need to resolve the class links
+ before we can return super_class. */
+ if (! CLS_ISRESOLV (class_))
+ __objc_resolve_class_links ();
+
+ return class_->super_class;
+}
+
+int
+class_getVersion (Class class_)
+{
+ if (class_ == Nil)
+ return 0;
+
+ return (int)(class_->version);
+}
+void
+class_setVersion (Class class_, int version)
+{
+ if (class_ == Nil)
+ return;
+
+ class_->version = version;
+}
+
+size_t
+class_getInstanceSize (Class class_)
+{
+ if (class_ == Nil)
+ return 0;
+
+ return class_->instance_size;
+}
#define CLASSOF(c) ((c)->class_pointer)