1998-09-21 03:22:07 +02:00
|
|
|
/* GNU Objective C Runtime selector related functions
|
2017-01-01 13:07:43 +01:00
|
|
|
Copyright (C) 1993-2017 Free Software Foundation, Inc.
|
1998-09-21 03:22:07 +02:00
|
|
|
Contributed by Kresten Krab Thorup
|
|
|
|
|
hash.c, [...]: Replace "GNU CC" with "GCC".
* hash.c, init.c, libobjc.def, libobjc_entry.c, linking.m,
makefile.dos, misc.c, nil_method.c, objects.c, sarray.c,
selector.c, sendmsg.c, thr-dce.c, thr-decosf1.c, thr-irix.c,
thr-mach.c, thr-objc.c, thr-os2.c, thr-posix.c, thr-pthreads.c,
thr-rtems.c, thr-single.c, thr-solaris.c, thr-vxworks.c,
thr-win32.c, thr.c: Replace "GNU CC" with "GCC".
From-SVN: r67134
2003-05-23 22:25:39 +02:00
|
|
|
This file is part of GCC.
|
1998-09-21 03:22:07 +02:00
|
|
|
|
hash.c, [...]: Replace "GNU CC" with "GCC".
* hash.c, init.c, libobjc.def, libobjc_entry.c, linking.m,
makefile.dos, misc.c, nil_method.c, objects.c, sarray.c,
selector.c, sendmsg.c, thr-dce.c, thr-decosf1.c, thr-irix.c,
thr-mach.c, thr-objc.c, thr-os2.c, thr-posix.c, thr-pthreads.c,
thr-rtems.c, thr-single.c, thr-solaris.c, thr-vxworks.c,
thr-win32.c, thr.c: Replace "GNU CC" with "GCC".
From-SVN: r67134
2003-05-23 22:25:39 +02:00
|
|
|
GCC is free software; you can redistribute it and/or modify it under the
|
1998-09-21 03:22:07 +02:00
|
|
|
terms of the GNU General Public License as published by the Free Software
|
2009-04-09 17:00:19 +02:00
|
|
|
Foundation; either version 3, or (at your option) any later version.
|
1998-09-21 03:22:07 +02:00
|
|
|
|
hash.c, [...]: Replace "GNU CC" with "GCC".
* hash.c, init.c, libobjc.def, libobjc_entry.c, linking.m,
makefile.dos, misc.c, nil_method.c, objects.c, sarray.c,
selector.c, sendmsg.c, thr-dce.c, thr-decosf1.c, thr-irix.c,
thr-mach.c, thr-objc.c, thr-os2.c, thr-posix.c, thr-pthreads.c,
thr-rtems.c, thr-single.c, thr-solaris.c, thr-vxworks.c,
thr-win32.c, thr.c: Replace "GNU CC" with "GCC".
From-SVN: r67134
2003-05-23 22:25:39 +02:00
|
|
|
GCC is distributed in the hope that it will be useful, but WITHOUT ANY
|
1998-09-21 03:22:07 +02:00
|
|
|
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
|
|
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
|
|
|
details.
|
|
|
|
|
2009-04-09 17:00:19 +02:00
|
|
|
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.
|
|
|
|
|
|
|
|
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/>. */
|
1998-09-21 03:22:07 +02:00
|
|
|
|
2010-09-12 00:47:14 +02:00
|
|
|
#include "objc-private/common.h"
|
2010-12-14 22:57:31 +01:00
|
|
|
#include "objc/runtime.h"
|
2010-09-11 14:58:27 +02:00
|
|
|
#include "objc/thr.h"
|
2010-09-17 18:35:15 +02:00
|
|
|
#include "objc-private/hash.h"
|
2010-12-14 22:57:31 +01:00
|
|
|
#include "objc-private/objc-list.h"
|
|
|
|
#include "objc-private/module-abi-8.h"
|
2010-09-11 14:58:27 +02:00
|
|
|
#include "objc-private/runtime.h"
|
2010-09-18 11:34:58 +02:00
|
|
|
#include "objc-private/sarray.h"
|
2010-12-19 01:28:52 +01:00
|
|
|
#include "objc-private/selector.h"
|
2010-12-24 18:00:19 +01:00
|
|
|
#include <stdlib.h> /* For malloc. */
|
1998-09-21 03:22:07 +02:00
|
|
|
|
2010-12-14 21:52:10 +01:00
|
|
|
/* Initial selector hash table size. Value doesn't matter much. */
|
1998-09-21 03:22:07 +02:00
|
|
|
#define SELECTOR_HASH_SIZE 128
|
|
|
|
|
2010-12-14 21:52:10 +01:00
|
|
|
/* Tables mapping selector names to uid and opposite. */
|
2002-07-02 21:43:03 +02:00
|
|
|
static struct sarray *__objc_selector_array = 0; /* uid -> sel !T:MUTEX */
|
|
|
|
static struct sarray *__objc_selector_names = 0; /* uid -> name !T:MUTEX */
|
1998-09-21 03:22:07 +02:00
|
|
|
static cache_ptr __objc_selector_hash = 0; /* name -> uid !T:MUTEX */
|
|
|
|
|
2010-12-14 21:52:10 +01:00
|
|
|
/* Number of selectors stored in each of the above tables. */
|
2002-06-21 16:36:37 +02:00
|
|
|
unsigned int __objc_selector_max_index = 0; /* !T:MUTEX */
|
1998-09-21 03:22:07 +02:00
|
|
|
|
2010-12-21 13:49:26 +01:00
|
|
|
/* Forward-declare an internal function. */
|
|
|
|
static SEL
|
|
|
|
__sel_register_typed_name (const char *name, const char *types,
|
|
|
|
struct objc_selector *orig, BOOL is_const);
|
|
|
|
|
2004-12-12 00:26:00 +01:00
|
|
|
void __objc_init_selector_tables (void)
|
1998-09-21 03:22:07 +02:00
|
|
|
{
|
|
|
|
__objc_selector_array = sarray_new (SELECTOR_HASH_SIZE, 0);
|
|
|
|
__objc_selector_names = sarray_new (SELECTOR_HASH_SIZE, 0);
|
|
|
|
__objc_selector_hash
|
re PR libobjc/19024 (name collisions libobjc/libmysqlclient)
2005-03-02 David Ayers <d.ayers@inode.at>
PR libobjc/19024
* Makefile.in (OBJS): Add hash_compat.lo.
(OBJS_GC): Add hash_compat_gc.lo.
(hash_compat_gc.lo): New target and rule.
* objc/hash.h (hash_new, hash_delete, hash_add, hash_remove)
(hash_next, hash_value_for_key, hash_is_key_in_hash)
(hash_ptr, hash_string, compare_ptrs, compare_strings): Prefix
with objc_. Add deprecated non prefixed inlined versions.
(OBJC_IGNORE_DEPRECATED_API): New macro to hide deprecated
declarations.
* hash.c (hash_new, hash_delete, hash_add, hash_remove, hash_next)
(hash_value_for_key, hash_is_key_in_hash): Prefix with objc_ and
update callers.
* hash_compat.c: New file.
* archive.c: Update callers.
* init.c: Likewise.
* selector.c: Likewise.
* libobjc.def: Add objc_ versions of hash functions.
From-SVN: r95793
2005-03-02 20:37:03 +01:00
|
|
|
= objc_hash_new (SELECTOR_HASH_SIZE,
|
|
|
|
(hash_func_type) objc_hash_string,
|
|
|
|
(compare_func_type) objc_compare_strings);
|
1998-09-21 03:22:07 +02:00
|
|
|
}
|
|
|
|
|
2010-12-21 13:49:26 +01:00
|
|
|
/* Register a bunch of selectors from the table of selectors in a
|
|
|
|
module. 'selectors' should not be NULL. The list is terminated by
|
|
|
|
a selectors with a NULL sel_id. The selectors are assumed to
|
|
|
|
contain the 'name' in the sel_id field; this is replaced with the
|
|
|
|
final selector id after they are registered. */
|
|
|
|
void
|
|
|
|
__objc_register_selectors_from_module (struct objc_selector *selectors)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; selectors[i].sel_id; ++i)
|
|
|
|
{
|
|
|
|
const char *name, *type;
|
|
|
|
name = (char *) selectors[i].sel_id;
|
|
|
|
type = (char *) selectors[i].sel_types;
|
|
|
|
/* Constructors are constant static data and we can safely store
|
|
|
|
pointers to them in the runtime structures, so we set
|
|
|
|
is_const == YES. */
|
|
|
|
__sel_register_typed_name (name, type, (struct objc_selector *) &(selectors[i]),
|
|
|
|
/* is_const */ YES);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-14 21:52:10 +01:00
|
|
|
/* This routine is given a class and records all of the methods in its
|
|
|
|
class structure in the record table. */
|
1998-09-21 03:22:07 +02:00
|
|
|
void
|
|
|
|
__objc_register_selectors_from_class (Class class)
|
|
|
|
{
|
2010-12-14 22:57:31 +01:00
|
|
|
struct objc_method_list * method_list;
|
1998-09-21 03:22:07 +02:00
|
|
|
|
|
|
|
method_list = class->methods;
|
|
|
|
while (method_list)
|
|
|
|
{
|
2004-05-25 21:10:54 +02:00
|
|
|
__objc_register_selectors_from_list (method_list);
|
1998-09-21 03:22:07 +02:00
|
|
|
method_list = method_list->method_next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-12-14 21:52:10 +01:00
|
|
|
/* This routine is given a list of methods and records each of the
|
|
|
|
methods in the record table. This is the routine that does the
|
|
|
|
actual recording work.
|
1998-09-21 03:22:07 +02:00
|
|
|
|
2004-05-25 21:10:54 +02:00
|
|
|
The name and type pointers in the method list must be permanent and
|
2010-12-14 21:52:10 +01:00
|
|
|
immutable. */
|
2004-05-25 21:10:54 +02:00
|
|
|
void
|
2010-12-14 22:57:31 +01:00
|
|
|
__objc_register_selectors_from_list (struct objc_method_list *method_list)
|
1998-09-21 03:22:07 +02:00
|
|
|
{
|
|
|
|
int i = 0;
|
2004-05-25 21:10:54 +02:00
|
|
|
|
|
|
|
objc_mutex_lock (__objc_runtime_mutex);
|
1998-09-21 03:22:07 +02:00
|
|
|
while (i < method_list->method_count)
|
|
|
|
{
|
2010-12-14 22:57:31 +01:00
|
|
|
Method method = &method_list->method_list[i];
|
2004-05-25 21:10:54 +02:00
|
|
|
if (method->method_name)
|
|
|
|
{
|
|
|
|
method->method_name
|
|
|
|
= __sel_register_typed_name ((const char *) method->method_name,
|
|
|
|
method->method_types, 0, YES);
|
|
|
|
}
|
1998-09-21 03:22:07 +02:00
|
|
|
i += 1;
|
|
|
|
}
|
2004-05-25 21:10:54 +02:00
|
|
|
objc_mutex_unlock (__objc_runtime_mutex);
|
1998-09-21 03:22:07 +02:00
|
|
|
}
|
|
|
|
|
2010-10-15 12:35:00 +02:00
|
|
|
/* The same as __objc_register_selectors_from_list, but works on a
|
|
|
|
struct objc_method_description_list* instead of a struct
|
|
|
|
objc_method_list*. This is only used for protocols, which have
|
2010-12-14 21:52:10 +01:00
|
|
|
lists of method descriptions, not methods. */
|
2010-10-15 12:35:00 +02:00
|
|
|
void
|
|
|
|
__objc_register_selectors_from_description_list
|
|
|
|
(struct objc_method_description_list *method_list)
|
|
|
|
{
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
objc_mutex_lock (__objc_runtime_mutex);
|
|
|
|
while (i < method_list->count)
|
|
|
|
{
|
|
|
|
struct objc_method_description *method = &method_list->list[i];
|
|
|
|
if (method->name)
|
|
|
|
{
|
|
|
|
method->name
|
|
|
|
= __sel_register_typed_name ((const char *) method->name,
|
|
|
|
method->types, 0, YES);
|
|
|
|
}
|
|
|
|
i += 1;
|
|
|
|
}
|
|
|
|
objc_mutex_unlock (__objc_runtime_mutex);
|
|
|
|
}
|
1998-09-21 03:22:07 +02:00
|
|
|
|
2010-12-14 21:52:10 +01:00
|
|
|
/* Register instance methods as class methods for root classes. */
|
2002-07-02 21:43:03 +02:00
|
|
|
void __objc_register_instance_methods_to_class (Class class)
|
1998-09-21 03:22:07 +02:00
|
|
|
{
|
2010-12-14 22:57:31 +01:00
|
|
|
struct objc_method_list *method_list;
|
|
|
|
struct objc_method_list *class_method_list;
|
1998-09-21 03:22:07 +02:00
|
|
|
int max_methods_no = 16;
|
2010-12-14 22:57:31 +01:00
|
|
|
struct objc_method_list *new_list;
|
|
|
|
Method curr_method;
|
1998-09-21 03:22:07 +02:00
|
|
|
|
|
|
|
/* Only if a root class. */
|
2002-07-02 21:43:03 +02:00
|
|
|
if (class->super_class)
|
1998-09-21 03:22:07 +02:00
|
|
|
return;
|
|
|
|
|
2010-12-14 21:52:10 +01:00
|
|
|
/* Allocate a method list to hold the new class methods. */
|
2002-07-02 21:43:03 +02:00
|
|
|
new_list = objc_calloc (sizeof (struct objc_method_list)
|
2010-12-14 21:52:10 +01:00
|
|
|
+ sizeof (struct objc_method[max_methods_no]), 1);
|
1998-09-21 03:22:07 +02:00
|
|
|
method_list = class->methods;
|
|
|
|
class_method_list = class->class_pointer->methods;
|
|
|
|
curr_method = &new_list->method_list[0];
|
2010-12-14 21:52:10 +01:00
|
|
|
|
|
|
|
/* Iterate through the method lists for the class. */
|
1998-09-21 03:22:07 +02:00
|
|
|
while (method_list)
|
|
|
|
{
|
|
|
|
int i;
|
2010-12-14 21:52:10 +01:00
|
|
|
|
|
|
|
/* Iterate through the methods from this method list. */
|
1998-09-21 03:22:07 +02:00
|
|
|
for (i = 0; i < method_list->method_count; i++)
|
|
|
|
{
|
2010-12-14 22:57:31 +01:00
|
|
|
Method mth = &method_list->method_list[i];
|
1998-09-21 03:22:07 +02:00
|
|
|
if (mth->method_name
|
2002-07-02 21:43:03 +02:00
|
|
|
&& ! search_for_method_in_list (class_method_list,
|
1998-09-21 03:22:07 +02:00
|
|
|
mth->method_name))
|
|
|
|
{
|
2010-12-14 21:52:10 +01:00
|
|
|
/* This instance method isn't a class method. Add it
|
|
|
|
into the new_list. */
|
1998-09-21 03:22:07 +02:00
|
|
|
*curr_method = *mth;
|
2010-12-14 21:52:10 +01:00
|
|
|
|
|
|
|
/* Reallocate the method list if necessary. */
|
2002-07-02 21:43:03 +02:00
|
|
|
if (++new_list->method_count == max_methods_no)
|
1998-09-21 03:22:07 +02:00
|
|
|
new_list =
|
2002-07-02 21:43:03 +02:00
|
|
|
objc_realloc (new_list, sizeof (struct objc_method_list)
|
|
|
|
+ sizeof (struct
|
2010-12-14 21:52:10 +01:00
|
|
|
objc_method[max_methods_no += 16]));
|
1998-09-21 03:22:07 +02:00
|
|
|
curr_method = &new_list->method_list[new_list->method_count];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
method_list = method_list->method_next;
|
|
|
|
}
|
|
|
|
|
2010-12-14 21:52:10 +01:00
|
|
|
/* If we created any new class methods then attach the method list
|
|
|
|
to the class. */
|
1998-09-21 03:22:07 +02:00
|
|
|
if (new_list->method_count)
|
|
|
|
{
|
|
|
|
new_list =
|
2002-07-02 21:43:03 +02:00
|
|
|
objc_realloc (new_list, sizeof (struct objc_method_list)
|
2010-12-14 21:52:10 +01:00
|
|
|
+ sizeof (struct objc_method[new_list->method_count]));
|
1998-09-21 03:22:07 +02:00
|
|
|
new_list->method_next = class->class_pointer->methods;
|
|
|
|
class->class_pointer->methods = new_list;
|
|
|
|
}
|
2004-01-15 02:49:41 +01:00
|
|
|
else
|
|
|
|
objc_free(new_list);
|
2010-12-14 21:52:10 +01:00
|
|
|
|
|
|
|
__objc_update_dispatch_table_for_class (class->class_pointer);
|
1998-09-21 03:22:07 +02:00
|
|
|
}
|
|
|
|
|
2010-10-12 04:43:25 +02:00
|
|
|
BOOL
|
|
|
|
sel_isEqual (SEL s1, SEL s2)
|
|
|
|
{
|
|
|
|
if (s1 == 0 || s2 == 0)
|
|
|
|
return s1 == s2;
|
|
|
|
else
|
|
|
|
return s1->sel_id == s2->sel_id;
|
|
|
|
}
|
1998-09-21 03:22:07 +02:00
|
|
|
|
2010-12-14 21:52:10 +01:00
|
|
|
/* Return YES iff t1 and t2 have same method types. Ignore the
|
|
|
|
argframe layout. */
|
2011-06-08 00:07:48 +02:00
|
|
|
static BOOL
|
2002-07-02 21:43:03 +02:00
|
|
|
sel_types_match (const char *t1, const char *t2)
|
1998-09-21 03:22:07 +02:00
|
|
|
{
|
2002-07-02 21:43:03 +02:00
|
|
|
if (! t1 || ! t2)
|
1998-09-21 03:22:07 +02:00
|
|
|
return NO;
|
|
|
|
while (*t1 && *t2)
|
|
|
|
{
|
|
|
|
if (*t1 == '+') t1++;
|
|
|
|
if (*t2 == '+') t2++;
|
2002-07-02 21:43:03 +02:00
|
|
|
while (isdigit ((unsigned char) *t1)) t1++;
|
|
|
|
while (isdigit ((unsigned char) *t2)) t2++;
|
1998-09-21 03:22:07 +02:00
|
|
|
/* xxx Remove these next two lines when qualifiers are put in
|
2010-12-14 21:52:10 +01:00
|
|
|
all selectors, not just Protocol selectors. */
|
2002-07-02 21:43:03 +02:00
|
|
|
t1 = objc_skip_type_qualifiers (t1);
|
|
|
|
t2 = objc_skip_type_qualifiers (t2);
|
|
|
|
if (! *t1 && ! *t2)
|
1998-09-21 03:22:07 +02:00
|
|
|
return YES;
|
|
|
|
if (*t1 != *t2)
|
|
|
|
return NO;
|
|
|
|
t1++;
|
|
|
|
t2++;
|
|
|
|
}
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
2010-12-14 21:52:10 +01:00
|
|
|
/* Return selector representing name. */
|
1998-09-21 03:22:07 +02:00
|
|
|
SEL
|
|
|
|
sel_get_any_uid (const char *name)
|
|
|
|
{
|
|
|
|
struct objc_list *l;
|
|
|
|
sidx i;
|
|
|
|
|
2002-07-02 21:43:03 +02:00
|
|
|
objc_mutex_lock (__objc_runtime_mutex);
|
1998-09-21 03:22:07 +02:00
|
|
|
|
re PR libobjc/19024 (name collisions libobjc/libmysqlclient)
2005-03-02 David Ayers <d.ayers@inode.at>
PR libobjc/19024
* Makefile.in (OBJS): Add hash_compat.lo.
(OBJS_GC): Add hash_compat_gc.lo.
(hash_compat_gc.lo): New target and rule.
* objc/hash.h (hash_new, hash_delete, hash_add, hash_remove)
(hash_next, hash_value_for_key, hash_is_key_in_hash)
(hash_ptr, hash_string, compare_ptrs, compare_strings): Prefix
with objc_. Add deprecated non prefixed inlined versions.
(OBJC_IGNORE_DEPRECATED_API): New macro to hide deprecated
declarations.
* hash.c (hash_new, hash_delete, hash_add, hash_remove, hash_next)
(hash_value_for_key, hash_is_key_in_hash): Prefix with objc_ and
update callers.
* hash_compat.c: New file.
* archive.c: Update callers.
* init.c: Likewise.
* selector.c: Likewise.
* libobjc.def: Add objc_ versions of hash functions.
From-SVN: r95793
2005-03-02 20:37:03 +01:00
|
|
|
i = (sidx) objc_hash_value_for_key (__objc_selector_hash, name);
|
1998-09-21 03:22:07 +02:00
|
|
|
if (soffset_decode (i) == 0)
|
|
|
|
{
|
2002-07-02 21:43:03 +02:00
|
|
|
objc_mutex_unlock (__objc_runtime_mutex);
|
1998-09-21 03:22:07 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-07-02 21:43:03 +02:00
|
|
|
l = (struct objc_list *) sarray_get_safe (__objc_selector_array, i);
|
|
|
|
objc_mutex_unlock (__objc_runtime_mutex);
|
1998-09-21 03:22:07 +02:00
|
|
|
|
|
|
|
if (l == 0)
|
|
|
|
return 0;
|
|
|
|
|
2002-07-02 21:43:03 +02:00
|
|
|
return (SEL) l->head;
|
1998-09-21 03:22:07 +02:00
|
|
|
}
|
|
|
|
|
2010-12-24 18:00:19 +01:00
|
|
|
SEL
|
|
|
|
sel_getTypedSelector (const char *name)
|
|
|
|
{
|
|
|
|
sidx i;
|
|
|
|
|
2010-12-24 21:10:46 +01:00
|
|
|
if (name == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
objc_mutex_lock (__objc_runtime_mutex);
|
|
|
|
|
2010-12-24 18:00:19 +01:00
|
|
|
/* Look for a typed selector. */
|
|
|
|
i = (sidx) objc_hash_value_for_key (__objc_selector_hash, name);
|
|
|
|
if (i != 0)
|
|
|
|
{
|
|
|
|
struct objc_list *l;
|
2011-02-28 15:48:23 +01:00
|
|
|
SEL returnValue = NULL;
|
2010-12-24 18:00:19 +01:00
|
|
|
|
|
|
|
for (l = (struct objc_list *) sarray_get_safe (__objc_selector_array, i);
|
|
|
|
l; l = l->tail)
|
|
|
|
{
|
|
|
|
SEL s = (SEL) l->head;
|
|
|
|
if (s->sel_types)
|
|
|
|
{
|
2011-02-28 15:48:23 +01:00
|
|
|
if (returnValue == NULL)
|
|
|
|
{
|
|
|
|
/* First typed selector that we find. Keep it in
|
|
|
|
returnValue, but keep checking as we want to
|
|
|
|
detect conflicts. */
|
|
|
|
returnValue = s;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* We had already found a typed selectors, so we
|
|
|
|
have multiple ones. Double-check that they have
|
|
|
|
different types, just in case for some reason we
|
|
|
|
got duplicates with the same types. If so, it's
|
|
|
|
OK, we'll ignore the duplicate. */
|
|
|
|
if (returnValue->sel_types == s->sel_types)
|
|
|
|
continue;
|
|
|
|
else if (sel_types_match (returnValue->sel_types, s->sel_types))
|
|
|
|
continue;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* The types of the two selectors are different;
|
|
|
|
it's a conflict. Too bad. Return NULL. */
|
|
|
|
objc_mutex_unlock (__objc_runtime_mutex);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
2010-12-24 18:00:19 +01:00
|
|
|
}
|
|
|
|
}
|
2011-02-28 15:48:23 +01:00
|
|
|
|
|
|
|
if (returnValue != NULL)
|
|
|
|
{
|
|
|
|
objc_mutex_unlock (__objc_runtime_mutex);
|
|
|
|
return returnValue;
|
|
|
|
}
|
2010-12-24 18:00:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* No typed selector found. Return NULL. */
|
|
|
|
objc_mutex_unlock (__objc_runtime_mutex);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
SEL *
|
|
|
|
sel_copyTypedSelectorList (const char *name, unsigned int *numberOfReturnedSelectors)
|
|
|
|
{
|
|
|
|
unsigned int count = 0;
|
|
|
|
SEL *returnValue = NULL;
|
|
|
|
sidx i;
|
|
|
|
|
|
|
|
if (name == NULL)
|
|
|
|
{
|
|
|
|
if (numberOfReturnedSelectors)
|
|
|
|
*numberOfReturnedSelectors = 0;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
objc_mutex_lock (__objc_runtime_mutex);
|
|
|
|
|
|
|
|
/* Count how many selectors we have. */
|
|
|
|
i = (sidx) objc_hash_value_for_key (__objc_selector_hash, name);
|
|
|
|
if (i != 0)
|
|
|
|
{
|
|
|
|
struct objc_list *selector_list = NULL;
|
|
|
|
selector_list = (struct objc_list *) sarray_get_safe (__objc_selector_array, i);
|
|
|
|
|
|
|
|
/* Count how many selectors we have. */
|
|
|
|
{
|
|
|
|
struct objc_list *l;
|
|
|
|
for (l = selector_list; l; l = l->tail)
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count != 0)
|
|
|
|
{
|
|
|
|
/* Allocate enough memory to hold them. */
|
|
|
|
returnValue = (SEL *)(malloc (sizeof (SEL) * (count + 1)));
|
|
|
|
|
|
|
|
/* Copy the selectors. */
|
|
|
|
{
|
|
|
|
unsigned int j;
|
|
|
|
for (j = 0; j < count; j++)
|
|
|
|
{
|
|
|
|
returnValue[j] = (SEL)(selector_list->head);
|
|
|
|
selector_list = selector_list->tail;
|
|
|
|
}
|
|
|
|
returnValue[j] = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
objc_mutex_unlock (__objc_runtime_mutex);
|
|
|
|
|
|
|
|
if (numberOfReturnedSelectors)
|
|
|
|
*numberOfReturnedSelectors = count;
|
|
|
|
|
|
|
|
return returnValue;
|
|
|
|
}
|
|
|
|
|
2010-12-14 21:52:10 +01:00
|
|
|
/* Get the name of a selector. If the selector is unknown, the empty
|
|
|
|
string "" is returned. */
|
2010-10-11 01:28:12 +02:00
|
|
|
const char *sel_getName (SEL selector)
|
1998-09-21 03:22:07 +02:00
|
|
|
{
|
|
|
|
const char *ret;
|
|
|
|
|
2010-10-12 02:27:02 +02:00
|
|
|
if (selector == NULL)
|
|
|
|
return "<null selector>";
|
2010-12-14 21:52:10 +01:00
|
|
|
|
2002-07-02 21:43:03 +02:00
|
|
|
objc_mutex_lock (__objc_runtime_mutex);
|
|
|
|
if ((soffset_decode ((sidx)selector->sel_id) > 0)
|
|
|
|
&& (soffset_decode ((sidx)selector->sel_id) <= __objc_selector_max_index))
|
1998-09-21 03:22:07 +02:00
|
|
|
ret = sarray_get_safe (__objc_selector_names, (sidx) selector->sel_id);
|
|
|
|
else
|
|
|
|
ret = 0;
|
2002-07-02 21:43:03 +02:00
|
|
|
objc_mutex_unlock (__objc_runtime_mutex);
|
1998-09-21 03:22:07 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL
|
|
|
|
sel_is_mapped (SEL selector)
|
|
|
|
{
|
|
|
|
unsigned int idx = soffset_decode ((sidx)selector->sel_id);
|
|
|
|
return ((idx > 0) && (idx <= __objc_selector_max_index));
|
|
|
|
}
|
|
|
|
|
2010-12-24 18:00:19 +01:00
|
|
|
const char *sel_getTypeEncoding (SEL selector)
|
1998-09-21 03:22:07 +02:00
|
|
|
{
|
|
|
|
if (selector)
|
|
|
|
return selector->sel_types;
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-12-14 21:52:10 +01:00
|
|
|
/* The uninstalled dispatch table. */
|
2002-07-02 21:43:03 +02:00
|
|
|
extern struct sarray *__objc_uninstalled_dtable;
|
1998-09-21 03:22:07 +02:00
|
|
|
|
2004-05-25 21:10:54 +02:00
|
|
|
/* __sel_register_typed_name allocates lots of struct objc_selector:s
|
2010-12-14 21:52:10 +01:00
|
|
|
of 8 (16, if pointers are 64 bits) bytes at startup. To reduce the
|
|
|
|
number of malloc calls and memory lost to malloc overhead, we
|
|
|
|
allocate objc_selector:s in blocks here. This is only called from
|
|
|
|
__sel_register_typed_name, and __sel_register_typed_name may only
|
|
|
|
be called when __objc_runtime_mutex is locked.
|
|
|
|
|
|
|
|
Note that the objc_selector:s allocated from
|
|
|
|
__sel_register_typed_name are never freed.
|
|
|
|
|
|
|
|
62 because 62 * sizeof (struct objc_selector) = 496 (992). This
|
|
|
|
should let malloc add some overhead and use a nice, round 512
|
|
|
|
(1024) byte chunk. */
|
2004-05-25 21:10:54 +02:00
|
|
|
#define SELECTOR_POOL_SIZE 62
|
|
|
|
static struct objc_selector *selector_pool;
|
|
|
|
static int selector_pool_left;
|
|
|
|
|
|
|
|
static struct objc_selector *
|
|
|
|
pool_alloc_selector(void)
|
|
|
|
{
|
|
|
|
if (!selector_pool_left)
|
|
|
|
{
|
|
|
|
selector_pool = objc_malloc (sizeof (struct objc_selector)
|
|
|
|
* SELECTOR_POOL_SIZE);
|
|
|
|
selector_pool_left = SELECTOR_POOL_SIZE;
|
|
|
|
}
|
|
|
|
return &selector_pool[--selector_pool_left];
|
|
|
|
}
|
|
|
|
|
2010-12-14 21:52:10 +01:00
|
|
|
/* Store the passed selector name in the selector record and return
|
|
|
|
its selector value (value returned by sel_get_uid). Assume that
|
|
|
|
the calling function has locked down __objc_runtime_mutex. The
|
2010-12-21 13:49:26 +01:00
|
|
|
'is_const' parameter tells us if the name and types parameters are
|
2010-12-14 21:52:10 +01:00
|
|
|
really constant or not. If YES then they are constant and we can
|
|
|
|
just store the pointers. If NO then we need to copy name and types
|
2010-12-21 13:49:26 +01:00
|
|
|
because the pointers may disappear later on. If the 'orig'
|
|
|
|
parameter is not NULL, then we are registering a selector from a
|
|
|
|
module, and 'orig' is that selector. In this case, we can put the
|
|
|
|
selector in the tables if needed, and orig->sel_id is updated with
|
|
|
|
the selector ID of the registered selector, and 'orig' is
|
|
|
|
returned. */
|
|
|
|
static SEL
|
1998-09-21 03:22:07 +02:00
|
|
|
__sel_register_typed_name (const char *name, const char *types,
|
|
|
|
struct objc_selector *orig, BOOL is_const)
|
|
|
|
{
|
2002-07-02 21:43:03 +02:00
|
|
|
struct objc_selector *j;
|
1998-09-21 03:22:07 +02:00
|
|
|
sidx i;
|
|
|
|
struct objc_list *l;
|
|
|
|
|
re PR libobjc/19024 (name collisions libobjc/libmysqlclient)
2005-03-02 David Ayers <d.ayers@inode.at>
PR libobjc/19024
* Makefile.in (OBJS): Add hash_compat.lo.
(OBJS_GC): Add hash_compat_gc.lo.
(hash_compat_gc.lo): New target and rule.
* objc/hash.h (hash_new, hash_delete, hash_add, hash_remove)
(hash_next, hash_value_for_key, hash_is_key_in_hash)
(hash_ptr, hash_string, compare_ptrs, compare_strings): Prefix
with objc_. Add deprecated non prefixed inlined versions.
(OBJC_IGNORE_DEPRECATED_API): New macro to hide deprecated
declarations.
* hash.c (hash_new, hash_delete, hash_add, hash_remove, hash_next)
(hash_value_for_key, hash_is_key_in_hash): Prefix with objc_ and
update callers.
* hash_compat.c: New file.
* archive.c: Update callers.
* init.c: Likewise.
* selector.c: Likewise.
* libobjc.def: Add objc_ versions of hash functions.
From-SVN: r95793
2005-03-02 20:37:03 +01:00
|
|
|
i = (sidx) objc_hash_value_for_key (__objc_selector_hash, name);
|
1998-09-21 03:22:07 +02:00
|
|
|
if (soffset_decode (i) != 0)
|
|
|
|
{
|
2010-12-21 14:44:04 +01:00
|
|
|
/* There are already selectors with that name. Examine them to
|
|
|
|
see if the one we're registering already exists. */
|
|
|
|
for (l = (struct objc_list *)sarray_get_safe (__objc_selector_array, i);
|
1998-09-21 03:22:07 +02:00
|
|
|
l; l = l->tail)
|
|
|
|
{
|
2010-12-21 14:44:04 +01:00
|
|
|
SEL s = (SEL)l->head;
|
1998-09-21 03:22:07 +02:00
|
|
|
if (types == 0 || s->sel_types == 0)
|
|
|
|
{
|
|
|
|
if (s->sel_types == types)
|
|
|
|
{
|
|
|
|
if (orig)
|
|
|
|
{
|
2010-12-21 14:44:04 +01:00
|
|
|
orig->sel_id = (void *)i;
|
1998-09-21 03:22:07 +02:00
|
|
|
return orig;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
}
|
2011-05-24 23:39:24 +02:00
|
|
|
else if (sel_types_match (s->sel_types, types))
|
1998-09-21 03:22:07 +02:00
|
|
|
{
|
|
|
|
if (orig)
|
|
|
|
{
|
2010-12-21 14:44:04 +01:00
|
|
|
orig->sel_id = (void *)i;
|
1998-09-21 03:22:07 +02:00
|
|
|
return orig;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
}
|
2010-12-21 14:44:04 +01:00
|
|
|
/* A selector with this specific name/type combination does not
|
|
|
|
exist yet. We need to register it. */
|
1998-09-21 03:22:07 +02:00
|
|
|
if (orig)
|
|
|
|
j = orig;
|
|
|
|
else
|
2004-05-25 21:10:54 +02:00
|
|
|
j = pool_alloc_selector ();
|
2010-12-14 21:52:10 +01:00
|
|
|
|
2010-12-21 14:44:04 +01:00
|
|
|
j->sel_id = (void *)i;
|
|
|
|
/* Can we use the pointer or must we copy types ? Don't copy if
|
2010-12-14 21:52:10 +01:00
|
|
|
NULL. */
|
1998-09-21 03:22:07 +02:00
|
|
|
if ((is_const) || (types == 0))
|
2010-12-21 14:44:04 +01:00
|
|
|
j->sel_types = types;
|
2010-12-14 21:52:10 +01:00
|
|
|
else
|
|
|
|
{
|
2010-12-21 14:44:04 +01:00
|
|
|
j->sel_types = (char *)objc_malloc (strlen (types) + 1);
|
|
|
|
strcpy ((char *)j->sel_types, types);
|
2010-12-14 21:52:10 +01:00
|
|
|
}
|
2010-12-21 14:44:04 +01:00
|
|
|
l = (struct objc_list *)sarray_get_safe (__objc_selector_array, i);
|
1998-09-21 03:22:07 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-12-21 14:44:04 +01:00
|
|
|
/* There are no other selectors with this name registered in the
|
|
|
|
runtime tables. */
|
|
|
|
const char *new_name;
|
|
|
|
|
|
|
|
/* Determine i. */
|
1998-09-21 03:22:07 +02:00
|
|
|
__objc_selector_max_index += 1;
|
2002-07-02 21:43:03 +02:00
|
|
|
i = soffset_encode (__objc_selector_max_index);
|
2010-12-21 14:44:04 +01:00
|
|
|
|
|
|
|
/* Prepare the selector. */
|
1998-09-21 03:22:07 +02:00
|
|
|
if (orig)
|
|
|
|
j = orig;
|
|
|
|
else
|
2004-05-25 21:10:54 +02:00
|
|
|
j = pool_alloc_selector ();
|
2010-12-14 21:52:10 +01:00
|
|
|
|
2010-12-21 14:44:04 +01:00
|
|
|
j->sel_id = (void *)i;
|
|
|
|
/* Can we use the pointer or must we copy types ? Don't copy if
|
2010-12-14 21:52:10 +01:00
|
|
|
NULL. */
|
2010-12-21 14:44:04 +01:00
|
|
|
if (is_const || (types == 0))
|
|
|
|
j->sel_types = types;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
j->sel_types = (char *)objc_malloc (strlen (types) + 1);
|
|
|
|
strcpy ((char *)j->sel_types, types);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Since this is the first selector with this name, we need to
|
|
|
|
register the correspondence between 'i' (the sel_id) and
|
|
|
|
'name' (the actual string) in __objc_selector_names and
|
|
|
|
__objc_selector_hash. */
|
|
|
|
|
|
|
|
/* Can we use the pointer or must we copy name ? Don't copy if
|
|
|
|
NULL. (FIXME: Can the name really be NULL here ?) */
|
|
|
|
if (is_const || (name == 0))
|
|
|
|
new_name = name;
|
2010-12-14 21:52:10 +01:00
|
|
|
else
|
|
|
|
{
|
2010-12-21 14:44:04 +01:00
|
|
|
new_name = (char *)objc_malloc (strlen (name) + 1);
|
|
|
|
strcpy ((char *)new_name, name);
|
2010-12-14 21:52:10 +01:00
|
|
|
}
|
2010-12-21 14:44:04 +01:00
|
|
|
|
|
|
|
/* This maps the sel_id to the name. */
|
|
|
|
sarray_at_put_safe (__objc_selector_names, i, (void *)new_name);
|
|
|
|
|
|
|
|
/* This maps the name to the sel_id. */
|
|
|
|
objc_hash_add (&__objc_selector_hash, (void *)new_name, (void *)i);
|
|
|
|
|
1998-09-21 03:22:07 +02:00
|
|
|
l = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
DEBUG_PRINTF ("Record selector %s[%s] as: %ld\n", name, types,
|
2010-12-21 14:44:04 +01:00
|
|
|
(long)soffset_decode (i));
|
|
|
|
|
|
|
|
/* Now add the selector to the list of selectors with that id. */
|
|
|
|
l = list_cons ((void *)j, l);
|
|
|
|
sarray_at_put_safe (__objc_selector_array, i, (void *)l);
|
|
|
|
|
2002-07-02 21:43:03 +02:00
|
|
|
sarray_realloc (__objc_uninstalled_dtable, __objc_selector_max_index + 1);
|
2010-12-14 21:52:10 +01:00
|
|
|
|
2010-12-21 14:44:04 +01:00
|
|
|
return (SEL)j;
|
1998-09-21 03:22:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
SEL
|
2010-10-11 01:28:12 +02:00
|
|
|
sel_registerName (const char *name)
|
1998-09-21 03:22:07 +02:00
|
|
|
{
|
|
|
|
SEL ret;
|
2010-12-24 21:10:46 +01:00
|
|
|
|
|
|
|
if (name == NULL)
|
|
|
|
return NULL;
|
1998-09-21 03:22:07 +02:00
|
|
|
|
2002-07-02 21:43:03 +02:00
|
|
|
objc_mutex_lock (__objc_runtime_mutex);
|
1998-09-21 03:22:07 +02:00
|
|
|
/* Assume that name is not constant static memory and needs to be
|
2010-12-14 21:52:10 +01:00
|
|
|
copied before put into a runtime structure. is_const == NO. */
|
1998-09-21 03:22:07 +02:00
|
|
|
ret = __sel_register_typed_name (name, 0, 0, NO);
|
2002-07-02 21:43:03 +02:00
|
|
|
objc_mutex_unlock (__objc_runtime_mutex);
|
1998-09-21 03:22:07 +02:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2010-10-11 01:28:12 +02:00
|
|
|
SEL
|
|
|
|
sel_registerTypedName (const char *name, const char *type)
|
1998-09-21 03:22:07 +02:00
|
|
|
{
|
|
|
|
SEL ret;
|
2004-05-25 21:10:54 +02:00
|
|
|
|
2010-12-24 21:10:46 +01:00
|
|
|
if (name == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
2002-07-02 21:43:03 +02:00
|
|
|
objc_mutex_lock (__objc_runtime_mutex);
|
2010-12-14 21:52:10 +01:00
|
|
|
/* Assume that name and type are not constant static memory and need
|
|
|
|
to be copied before put into a runtime structure. is_const ==
|
|
|
|
NO. */
|
1998-09-21 03:22:07 +02:00
|
|
|
ret = __sel_register_typed_name (name, type, 0, NO);
|
2002-07-02 21:43:03 +02:00
|
|
|
objc_mutex_unlock (__objc_runtime_mutex);
|
1998-09-21 03:22:07 +02:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2010-10-11 01:28:12 +02:00
|
|
|
|
2010-12-14 21:52:10 +01:00
|
|
|
/* Return the selector representing name. */
|
2010-10-11 01:28:12 +02:00
|
|
|
SEL
|
|
|
|
sel_getUid (const char *name)
|
|
|
|
{
|
|
|
|
return sel_registerTypedName (name, 0);
|
|
|
|
}
|