1*e4b17023SJohn Marino /* GNU Objective C Runtime selector related functions
2*e4b17023SJohn Marino Copyright (C) 1993, 1995, 1996, 1997, 2002, 2004, 2009, 2010
3*e4b17023SJohn Marino Free Software Foundation, Inc.
4*e4b17023SJohn Marino Contributed by Kresten Krab Thorup
5*e4b17023SJohn Marino
6*e4b17023SJohn Marino This file is part of GCC.
7*e4b17023SJohn Marino
8*e4b17023SJohn Marino GCC is free software; you can redistribute it and/or modify it under the
9*e4b17023SJohn Marino terms of the GNU General Public License as published by the Free Software
10*e4b17023SJohn Marino Foundation; either version 3, or (at your option) any later version.
11*e4b17023SJohn Marino
12*e4b17023SJohn Marino GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13*e4b17023SJohn Marino WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14*e4b17023SJohn Marino FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
15*e4b17023SJohn Marino details.
16*e4b17023SJohn Marino
17*e4b17023SJohn Marino Under Section 7 of GPL version 3, you are granted additional
18*e4b17023SJohn Marino permissions described in the GCC Runtime Library Exception, version
19*e4b17023SJohn Marino 3.1, as published by the Free Software Foundation.
20*e4b17023SJohn Marino
21*e4b17023SJohn Marino You should have received a copy of the GNU General Public License and
22*e4b17023SJohn Marino a copy of the GCC Runtime Library Exception along with this program;
23*e4b17023SJohn Marino see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24*e4b17023SJohn Marino <http://www.gnu.org/licenses/>. */
25*e4b17023SJohn Marino
26*e4b17023SJohn Marino #include "objc-private/common.h"
27*e4b17023SJohn Marino #include "objc/runtime.h"
28*e4b17023SJohn Marino #include "objc/thr.h"
29*e4b17023SJohn Marino #include "objc-private/hash.h"
30*e4b17023SJohn Marino #include "objc-private/objc-list.h"
31*e4b17023SJohn Marino #include "objc-private/module-abi-8.h"
32*e4b17023SJohn Marino #include "objc-private/runtime.h"
33*e4b17023SJohn Marino #include "objc-private/sarray.h"
34*e4b17023SJohn Marino #include "objc-private/selector.h"
35*e4b17023SJohn Marino #include <stdlib.h> /* For malloc. */
36*e4b17023SJohn Marino
37*e4b17023SJohn Marino /* Initial selector hash table size. Value doesn't matter much. */
38*e4b17023SJohn Marino #define SELECTOR_HASH_SIZE 128
39*e4b17023SJohn Marino
40*e4b17023SJohn Marino /* Tables mapping selector names to uid and opposite. */
41*e4b17023SJohn Marino static struct sarray *__objc_selector_array = 0; /* uid -> sel !T:MUTEX */
42*e4b17023SJohn Marino static struct sarray *__objc_selector_names = 0; /* uid -> name !T:MUTEX */
43*e4b17023SJohn Marino static cache_ptr __objc_selector_hash = 0; /* name -> uid !T:MUTEX */
44*e4b17023SJohn Marino
45*e4b17023SJohn Marino /* Number of selectors stored in each of the above tables. */
46*e4b17023SJohn Marino unsigned int __objc_selector_max_index = 0; /* !T:MUTEX */
47*e4b17023SJohn Marino
48*e4b17023SJohn Marino /* Forward-declare an internal function. */
49*e4b17023SJohn Marino static SEL
50*e4b17023SJohn Marino __sel_register_typed_name (const char *name, const char *types,
51*e4b17023SJohn Marino struct objc_selector *orig, BOOL is_const);
52*e4b17023SJohn Marino
__objc_init_selector_tables(void)53*e4b17023SJohn Marino void __objc_init_selector_tables (void)
54*e4b17023SJohn Marino {
55*e4b17023SJohn Marino __objc_selector_array = sarray_new (SELECTOR_HASH_SIZE, 0);
56*e4b17023SJohn Marino __objc_selector_names = sarray_new (SELECTOR_HASH_SIZE, 0);
57*e4b17023SJohn Marino __objc_selector_hash
58*e4b17023SJohn Marino = objc_hash_new (SELECTOR_HASH_SIZE,
59*e4b17023SJohn Marino (hash_func_type) objc_hash_string,
60*e4b17023SJohn Marino (compare_func_type) objc_compare_strings);
61*e4b17023SJohn Marino }
62*e4b17023SJohn Marino
63*e4b17023SJohn Marino /* Register a bunch of selectors from the table of selectors in a
64*e4b17023SJohn Marino module. 'selectors' should not be NULL. The list is terminated by
65*e4b17023SJohn Marino a selectors with a NULL sel_id. The selectors are assumed to
66*e4b17023SJohn Marino contain the 'name' in the sel_id field; this is replaced with the
67*e4b17023SJohn Marino final selector id after they are registered. */
68*e4b17023SJohn Marino void
__objc_register_selectors_from_module(struct objc_selector * selectors)69*e4b17023SJohn Marino __objc_register_selectors_from_module (struct objc_selector *selectors)
70*e4b17023SJohn Marino {
71*e4b17023SJohn Marino int i;
72*e4b17023SJohn Marino
73*e4b17023SJohn Marino for (i = 0; selectors[i].sel_id; ++i)
74*e4b17023SJohn Marino {
75*e4b17023SJohn Marino const char *name, *type;
76*e4b17023SJohn Marino name = (char *) selectors[i].sel_id;
77*e4b17023SJohn Marino type = (char *) selectors[i].sel_types;
78*e4b17023SJohn Marino /* Constructors are constant static data and we can safely store
79*e4b17023SJohn Marino pointers to them in the runtime structures, so we set
80*e4b17023SJohn Marino is_const == YES. */
81*e4b17023SJohn Marino __sel_register_typed_name (name, type, (struct objc_selector *) &(selectors[i]),
82*e4b17023SJohn Marino /* is_const */ YES);
83*e4b17023SJohn Marino }
84*e4b17023SJohn Marino }
85*e4b17023SJohn Marino
86*e4b17023SJohn Marino /* This routine is given a class and records all of the methods in its
87*e4b17023SJohn Marino class structure in the record table. */
88*e4b17023SJohn Marino void
__objc_register_selectors_from_class(Class class)89*e4b17023SJohn Marino __objc_register_selectors_from_class (Class class)
90*e4b17023SJohn Marino {
91*e4b17023SJohn Marino struct objc_method_list * method_list;
92*e4b17023SJohn Marino
93*e4b17023SJohn Marino method_list = class->methods;
94*e4b17023SJohn Marino while (method_list)
95*e4b17023SJohn Marino {
96*e4b17023SJohn Marino __objc_register_selectors_from_list (method_list);
97*e4b17023SJohn Marino method_list = method_list->method_next;
98*e4b17023SJohn Marino }
99*e4b17023SJohn Marino }
100*e4b17023SJohn Marino
101*e4b17023SJohn Marino
102*e4b17023SJohn Marino /* This routine is given a list of methods and records each of the
103*e4b17023SJohn Marino methods in the record table. This is the routine that does the
104*e4b17023SJohn Marino actual recording work.
105*e4b17023SJohn Marino
106*e4b17023SJohn Marino The name and type pointers in the method list must be permanent and
107*e4b17023SJohn Marino immutable. */
108*e4b17023SJohn Marino void
__objc_register_selectors_from_list(struct objc_method_list * method_list)109*e4b17023SJohn Marino __objc_register_selectors_from_list (struct objc_method_list *method_list)
110*e4b17023SJohn Marino {
111*e4b17023SJohn Marino int i = 0;
112*e4b17023SJohn Marino
113*e4b17023SJohn Marino objc_mutex_lock (__objc_runtime_mutex);
114*e4b17023SJohn Marino while (i < method_list->method_count)
115*e4b17023SJohn Marino {
116*e4b17023SJohn Marino Method method = &method_list->method_list[i];
117*e4b17023SJohn Marino if (method->method_name)
118*e4b17023SJohn Marino {
119*e4b17023SJohn Marino method->method_name
120*e4b17023SJohn Marino = __sel_register_typed_name ((const char *) method->method_name,
121*e4b17023SJohn Marino method->method_types, 0, YES);
122*e4b17023SJohn Marino }
123*e4b17023SJohn Marino i += 1;
124*e4b17023SJohn Marino }
125*e4b17023SJohn Marino objc_mutex_unlock (__objc_runtime_mutex);
126*e4b17023SJohn Marino }
127*e4b17023SJohn Marino
128*e4b17023SJohn Marino /* The same as __objc_register_selectors_from_list, but works on a
129*e4b17023SJohn Marino struct objc_method_description_list* instead of a struct
130*e4b17023SJohn Marino objc_method_list*. This is only used for protocols, which have
131*e4b17023SJohn Marino lists of method descriptions, not methods. */
132*e4b17023SJohn Marino void
__objc_register_selectors_from_description_list(struct objc_method_description_list * method_list)133*e4b17023SJohn Marino __objc_register_selectors_from_description_list
134*e4b17023SJohn Marino (struct objc_method_description_list *method_list)
135*e4b17023SJohn Marino {
136*e4b17023SJohn Marino int i = 0;
137*e4b17023SJohn Marino
138*e4b17023SJohn Marino objc_mutex_lock (__objc_runtime_mutex);
139*e4b17023SJohn Marino while (i < method_list->count)
140*e4b17023SJohn Marino {
141*e4b17023SJohn Marino struct objc_method_description *method = &method_list->list[i];
142*e4b17023SJohn Marino if (method->name)
143*e4b17023SJohn Marino {
144*e4b17023SJohn Marino method->name
145*e4b17023SJohn Marino = __sel_register_typed_name ((const char *) method->name,
146*e4b17023SJohn Marino method->types, 0, YES);
147*e4b17023SJohn Marino }
148*e4b17023SJohn Marino i += 1;
149*e4b17023SJohn Marino }
150*e4b17023SJohn Marino objc_mutex_unlock (__objc_runtime_mutex);
151*e4b17023SJohn Marino }
152*e4b17023SJohn Marino
153*e4b17023SJohn Marino /* Register instance methods as class methods for root classes. */
__objc_register_instance_methods_to_class(Class class)154*e4b17023SJohn Marino void __objc_register_instance_methods_to_class (Class class)
155*e4b17023SJohn Marino {
156*e4b17023SJohn Marino struct objc_method_list *method_list;
157*e4b17023SJohn Marino struct objc_method_list *class_method_list;
158*e4b17023SJohn Marino int max_methods_no = 16;
159*e4b17023SJohn Marino struct objc_method_list *new_list;
160*e4b17023SJohn Marino Method curr_method;
161*e4b17023SJohn Marino
162*e4b17023SJohn Marino /* Only if a root class. */
163*e4b17023SJohn Marino if (class->super_class)
164*e4b17023SJohn Marino return;
165*e4b17023SJohn Marino
166*e4b17023SJohn Marino /* Allocate a method list to hold the new class methods. */
167*e4b17023SJohn Marino new_list = objc_calloc (sizeof (struct objc_method_list)
168*e4b17023SJohn Marino + sizeof (struct objc_method[max_methods_no]), 1);
169*e4b17023SJohn Marino method_list = class->methods;
170*e4b17023SJohn Marino class_method_list = class->class_pointer->methods;
171*e4b17023SJohn Marino curr_method = &new_list->method_list[0];
172*e4b17023SJohn Marino
173*e4b17023SJohn Marino /* Iterate through the method lists for the class. */
174*e4b17023SJohn Marino while (method_list)
175*e4b17023SJohn Marino {
176*e4b17023SJohn Marino int i;
177*e4b17023SJohn Marino
178*e4b17023SJohn Marino /* Iterate through the methods from this method list. */
179*e4b17023SJohn Marino for (i = 0; i < method_list->method_count; i++)
180*e4b17023SJohn Marino {
181*e4b17023SJohn Marino Method mth = &method_list->method_list[i];
182*e4b17023SJohn Marino if (mth->method_name
183*e4b17023SJohn Marino && ! search_for_method_in_list (class_method_list,
184*e4b17023SJohn Marino mth->method_name))
185*e4b17023SJohn Marino {
186*e4b17023SJohn Marino /* This instance method isn't a class method. Add it
187*e4b17023SJohn Marino into the new_list. */
188*e4b17023SJohn Marino *curr_method = *mth;
189*e4b17023SJohn Marino
190*e4b17023SJohn Marino /* Reallocate the method list if necessary. */
191*e4b17023SJohn Marino if (++new_list->method_count == max_methods_no)
192*e4b17023SJohn Marino new_list =
193*e4b17023SJohn Marino objc_realloc (new_list, sizeof (struct objc_method_list)
194*e4b17023SJohn Marino + sizeof (struct
195*e4b17023SJohn Marino objc_method[max_methods_no += 16]));
196*e4b17023SJohn Marino curr_method = &new_list->method_list[new_list->method_count];
197*e4b17023SJohn Marino }
198*e4b17023SJohn Marino }
199*e4b17023SJohn Marino
200*e4b17023SJohn Marino method_list = method_list->method_next;
201*e4b17023SJohn Marino }
202*e4b17023SJohn Marino
203*e4b17023SJohn Marino /* If we created any new class methods then attach the method list
204*e4b17023SJohn Marino to the class. */
205*e4b17023SJohn Marino if (new_list->method_count)
206*e4b17023SJohn Marino {
207*e4b17023SJohn Marino new_list =
208*e4b17023SJohn Marino objc_realloc (new_list, sizeof (struct objc_method_list)
209*e4b17023SJohn Marino + sizeof (struct objc_method[new_list->method_count]));
210*e4b17023SJohn Marino new_list->method_next = class->class_pointer->methods;
211*e4b17023SJohn Marino class->class_pointer->methods = new_list;
212*e4b17023SJohn Marino }
213*e4b17023SJohn Marino else
214*e4b17023SJohn Marino objc_free(new_list);
215*e4b17023SJohn Marino
216*e4b17023SJohn Marino __objc_update_dispatch_table_for_class (class->class_pointer);
217*e4b17023SJohn Marino }
218*e4b17023SJohn Marino
219*e4b17023SJohn Marino BOOL
sel_isEqual(SEL s1,SEL s2)220*e4b17023SJohn Marino sel_isEqual (SEL s1, SEL s2)
221*e4b17023SJohn Marino {
222*e4b17023SJohn Marino if (s1 == 0 || s2 == 0)
223*e4b17023SJohn Marino return s1 == s2;
224*e4b17023SJohn Marino else
225*e4b17023SJohn Marino return s1->sel_id == s2->sel_id;
226*e4b17023SJohn Marino }
227*e4b17023SJohn Marino
228*e4b17023SJohn Marino /* Return YES iff t1 and t2 have same method types. Ignore the
229*e4b17023SJohn Marino argframe layout. */
230*e4b17023SJohn Marino static BOOL
sel_types_match(const char * t1,const char * t2)231*e4b17023SJohn Marino sel_types_match (const char *t1, const char *t2)
232*e4b17023SJohn Marino {
233*e4b17023SJohn Marino if (! t1 || ! t2)
234*e4b17023SJohn Marino return NO;
235*e4b17023SJohn Marino while (*t1 && *t2)
236*e4b17023SJohn Marino {
237*e4b17023SJohn Marino if (*t1 == '+') t1++;
238*e4b17023SJohn Marino if (*t2 == '+') t2++;
239*e4b17023SJohn Marino while (isdigit ((unsigned char) *t1)) t1++;
240*e4b17023SJohn Marino while (isdigit ((unsigned char) *t2)) t2++;
241*e4b17023SJohn Marino /* xxx Remove these next two lines when qualifiers are put in
242*e4b17023SJohn Marino all selectors, not just Protocol selectors. */
243*e4b17023SJohn Marino t1 = objc_skip_type_qualifiers (t1);
244*e4b17023SJohn Marino t2 = objc_skip_type_qualifiers (t2);
245*e4b17023SJohn Marino if (! *t1 && ! *t2)
246*e4b17023SJohn Marino return YES;
247*e4b17023SJohn Marino if (*t1 != *t2)
248*e4b17023SJohn Marino return NO;
249*e4b17023SJohn Marino t1++;
250*e4b17023SJohn Marino t2++;
251*e4b17023SJohn Marino }
252*e4b17023SJohn Marino return NO;
253*e4b17023SJohn Marino }
254*e4b17023SJohn Marino
255*e4b17023SJohn Marino /* Return selector representing name. */
256*e4b17023SJohn Marino SEL
sel_get_any_uid(const char * name)257*e4b17023SJohn Marino sel_get_any_uid (const char *name)
258*e4b17023SJohn Marino {
259*e4b17023SJohn Marino struct objc_list *l;
260*e4b17023SJohn Marino sidx i;
261*e4b17023SJohn Marino
262*e4b17023SJohn Marino objc_mutex_lock (__objc_runtime_mutex);
263*e4b17023SJohn Marino
264*e4b17023SJohn Marino i = (sidx) objc_hash_value_for_key (__objc_selector_hash, name);
265*e4b17023SJohn Marino if (soffset_decode (i) == 0)
266*e4b17023SJohn Marino {
267*e4b17023SJohn Marino objc_mutex_unlock (__objc_runtime_mutex);
268*e4b17023SJohn Marino return 0;
269*e4b17023SJohn Marino }
270*e4b17023SJohn Marino
271*e4b17023SJohn Marino l = (struct objc_list *) sarray_get_safe (__objc_selector_array, i);
272*e4b17023SJohn Marino objc_mutex_unlock (__objc_runtime_mutex);
273*e4b17023SJohn Marino
274*e4b17023SJohn Marino if (l == 0)
275*e4b17023SJohn Marino return 0;
276*e4b17023SJohn Marino
277*e4b17023SJohn Marino return (SEL) l->head;
278*e4b17023SJohn Marino }
279*e4b17023SJohn Marino
280*e4b17023SJohn Marino SEL
sel_getTypedSelector(const char * name)281*e4b17023SJohn Marino sel_getTypedSelector (const char *name)
282*e4b17023SJohn Marino {
283*e4b17023SJohn Marino sidx i;
284*e4b17023SJohn Marino
285*e4b17023SJohn Marino if (name == NULL)
286*e4b17023SJohn Marino return NULL;
287*e4b17023SJohn Marino
288*e4b17023SJohn Marino objc_mutex_lock (__objc_runtime_mutex);
289*e4b17023SJohn Marino
290*e4b17023SJohn Marino /* Look for a typed selector. */
291*e4b17023SJohn Marino i = (sidx) objc_hash_value_for_key (__objc_selector_hash, name);
292*e4b17023SJohn Marino if (i != 0)
293*e4b17023SJohn Marino {
294*e4b17023SJohn Marino struct objc_list *l;
295*e4b17023SJohn Marino SEL returnValue = NULL;
296*e4b17023SJohn Marino
297*e4b17023SJohn Marino for (l = (struct objc_list *) sarray_get_safe (__objc_selector_array, i);
298*e4b17023SJohn Marino l; l = l->tail)
299*e4b17023SJohn Marino {
300*e4b17023SJohn Marino SEL s = (SEL) l->head;
301*e4b17023SJohn Marino if (s->sel_types)
302*e4b17023SJohn Marino {
303*e4b17023SJohn Marino if (returnValue == NULL)
304*e4b17023SJohn Marino {
305*e4b17023SJohn Marino /* First typed selector that we find. Keep it in
306*e4b17023SJohn Marino returnValue, but keep checking as we want to
307*e4b17023SJohn Marino detect conflicts. */
308*e4b17023SJohn Marino returnValue = s;
309*e4b17023SJohn Marino }
310*e4b17023SJohn Marino else
311*e4b17023SJohn Marino {
312*e4b17023SJohn Marino /* We had already found a typed selectors, so we
313*e4b17023SJohn Marino have multiple ones. Double-check that they have
314*e4b17023SJohn Marino different types, just in case for some reason we
315*e4b17023SJohn Marino got duplicates with the same types. If so, it's
316*e4b17023SJohn Marino OK, we'll ignore the duplicate. */
317*e4b17023SJohn Marino if (returnValue->sel_types == s->sel_types)
318*e4b17023SJohn Marino continue;
319*e4b17023SJohn Marino else if (sel_types_match (returnValue->sel_types, s->sel_types))
320*e4b17023SJohn Marino continue;
321*e4b17023SJohn Marino else
322*e4b17023SJohn Marino {
323*e4b17023SJohn Marino /* The types of the two selectors are different;
324*e4b17023SJohn Marino it's a conflict. Too bad. Return NULL. */
325*e4b17023SJohn Marino objc_mutex_unlock (__objc_runtime_mutex);
326*e4b17023SJohn Marino return NULL;
327*e4b17023SJohn Marino }
328*e4b17023SJohn Marino }
329*e4b17023SJohn Marino }
330*e4b17023SJohn Marino }
331*e4b17023SJohn Marino
332*e4b17023SJohn Marino if (returnValue != NULL)
333*e4b17023SJohn Marino {
334*e4b17023SJohn Marino objc_mutex_unlock (__objc_runtime_mutex);
335*e4b17023SJohn Marino return returnValue;
336*e4b17023SJohn Marino }
337*e4b17023SJohn Marino }
338*e4b17023SJohn Marino
339*e4b17023SJohn Marino /* No typed selector found. Return NULL. */
340*e4b17023SJohn Marino objc_mutex_unlock (__objc_runtime_mutex);
341*e4b17023SJohn Marino return 0;
342*e4b17023SJohn Marino }
343*e4b17023SJohn Marino
344*e4b17023SJohn Marino SEL *
sel_copyTypedSelectorList(const char * name,unsigned int * numberOfReturnedSelectors)345*e4b17023SJohn Marino sel_copyTypedSelectorList (const char *name, unsigned int *numberOfReturnedSelectors)
346*e4b17023SJohn Marino {
347*e4b17023SJohn Marino unsigned int count = 0;
348*e4b17023SJohn Marino SEL *returnValue = NULL;
349*e4b17023SJohn Marino sidx i;
350*e4b17023SJohn Marino
351*e4b17023SJohn Marino if (name == NULL)
352*e4b17023SJohn Marino {
353*e4b17023SJohn Marino if (numberOfReturnedSelectors)
354*e4b17023SJohn Marino *numberOfReturnedSelectors = 0;
355*e4b17023SJohn Marino return NULL;
356*e4b17023SJohn Marino }
357*e4b17023SJohn Marino
358*e4b17023SJohn Marino objc_mutex_lock (__objc_runtime_mutex);
359*e4b17023SJohn Marino
360*e4b17023SJohn Marino /* Count how many selectors we have. */
361*e4b17023SJohn Marino i = (sidx) objc_hash_value_for_key (__objc_selector_hash, name);
362*e4b17023SJohn Marino if (i != 0)
363*e4b17023SJohn Marino {
364*e4b17023SJohn Marino struct objc_list *selector_list = NULL;
365*e4b17023SJohn Marino selector_list = (struct objc_list *) sarray_get_safe (__objc_selector_array, i);
366*e4b17023SJohn Marino
367*e4b17023SJohn Marino /* Count how many selectors we have. */
368*e4b17023SJohn Marino {
369*e4b17023SJohn Marino struct objc_list *l;
370*e4b17023SJohn Marino for (l = selector_list; l; l = l->tail)
371*e4b17023SJohn Marino count++;
372*e4b17023SJohn Marino }
373*e4b17023SJohn Marino
374*e4b17023SJohn Marino if (count != 0)
375*e4b17023SJohn Marino {
376*e4b17023SJohn Marino /* Allocate enough memory to hold them. */
377*e4b17023SJohn Marino returnValue = (SEL *)(malloc (sizeof (SEL) * (count + 1)));
378*e4b17023SJohn Marino
379*e4b17023SJohn Marino /* Copy the selectors. */
380*e4b17023SJohn Marino {
381*e4b17023SJohn Marino unsigned int j;
382*e4b17023SJohn Marino for (j = 0; j < count; j++)
383*e4b17023SJohn Marino {
384*e4b17023SJohn Marino returnValue[j] = (SEL)(selector_list->head);
385*e4b17023SJohn Marino selector_list = selector_list->tail;
386*e4b17023SJohn Marino }
387*e4b17023SJohn Marino returnValue[j] = NULL;
388*e4b17023SJohn Marino }
389*e4b17023SJohn Marino }
390*e4b17023SJohn Marino }
391*e4b17023SJohn Marino
392*e4b17023SJohn Marino objc_mutex_unlock (__objc_runtime_mutex);
393*e4b17023SJohn Marino
394*e4b17023SJohn Marino if (numberOfReturnedSelectors)
395*e4b17023SJohn Marino *numberOfReturnedSelectors = count;
396*e4b17023SJohn Marino
397*e4b17023SJohn Marino return returnValue;
398*e4b17023SJohn Marino }
399*e4b17023SJohn Marino
400*e4b17023SJohn Marino /* Get the name of a selector. If the selector is unknown, the empty
401*e4b17023SJohn Marino string "" is returned. */
sel_getName(SEL selector)402*e4b17023SJohn Marino const char *sel_getName (SEL selector)
403*e4b17023SJohn Marino {
404*e4b17023SJohn Marino const char *ret;
405*e4b17023SJohn Marino
406*e4b17023SJohn Marino if (selector == NULL)
407*e4b17023SJohn Marino return "<null selector>";
408*e4b17023SJohn Marino
409*e4b17023SJohn Marino objc_mutex_lock (__objc_runtime_mutex);
410*e4b17023SJohn Marino if ((soffset_decode ((sidx)selector->sel_id) > 0)
411*e4b17023SJohn Marino && (soffset_decode ((sidx)selector->sel_id) <= __objc_selector_max_index))
412*e4b17023SJohn Marino ret = sarray_get_safe (__objc_selector_names, (sidx) selector->sel_id);
413*e4b17023SJohn Marino else
414*e4b17023SJohn Marino ret = 0;
415*e4b17023SJohn Marino objc_mutex_unlock (__objc_runtime_mutex);
416*e4b17023SJohn Marino return ret;
417*e4b17023SJohn Marino }
418*e4b17023SJohn Marino
419*e4b17023SJohn Marino BOOL
sel_is_mapped(SEL selector)420*e4b17023SJohn Marino sel_is_mapped (SEL selector)
421*e4b17023SJohn Marino {
422*e4b17023SJohn Marino unsigned int idx = soffset_decode ((sidx)selector->sel_id);
423*e4b17023SJohn Marino return ((idx > 0) && (idx <= __objc_selector_max_index));
424*e4b17023SJohn Marino }
425*e4b17023SJohn Marino
sel_getTypeEncoding(SEL selector)426*e4b17023SJohn Marino const char *sel_getTypeEncoding (SEL selector)
427*e4b17023SJohn Marino {
428*e4b17023SJohn Marino if (selector)
429*e4b17023SJohn Marino return selector->sel_types;
430*e4b17023SJohn Marino else
431*e4b17023SJohn Marino return 0;
432*e4b17023SJohn Marino }
433*e4b17023SJohn Marino
434*e4b17023SJohn Marino /* The uninstalled dispatch table. */
435*e4b17023SJohn Marino extern struct sarray *__objc_uninstalled_dtable;
436*e4b17023SJohn Marino
437*e4b17023SJohn Marino /* __sel_register_typed_name allocates lots of struct objc_selector:s
438*e4b17023SJohn Marino of 8 (16, if pointers are 64 bits) bytes at startup. To reduce the
439*e4b17023SJohn Marino number of malloc calls and memory lost to malloc overhead, we
440*e4b17023SJohn Marino allocate objc_selector:s in blocks here. This is only called from
441*e4b17023SJohn Marino __sel_register_typed_name, and __sel_register_typed_name may only
442*e4b17023SJohn Marino be called when __objc_runtime_mutex is locked.
443*e4b17023SJohn Marino
444*e4b17023SJohn Marino Note that the objc_selector:s allocated from
445*e4b17023SJohn Marino __sel_register_typed_name are never freed.
446*e4b17023SJohn Marino
447*e4b17023SJohn Marino 62 because 62 * sizeof (struct objc_selector) = 496 (992). This
448*e4b17023SJohn Marino should let malloc add some overhead and use a nice, round 512
449*e4b17023SJohn Marino (1024) byte chunk. */
450*e4b17023SJohn Marino #define SELECTOR_POOL_SIZE 62
451*e4b17023SJohn Marino static struct objc_selector *selector_pool;
452*e4b17023SJohn Marino static int selector_pool_left;
453*e4b17023SJohn Marino
454*e4b17023SJohn Marino static struct objc_selector *
pool_alloc_selector(void)455*e4b17023SJohn Marino pool_alloc_selector(void)
456*e4b17023SJohn Marino {
457*e4b17023SJohn Marino if (!selector_pool_left)
458*e4b17023SJohn Marino {
459*e4b17023SJohn Marino selector_pool = objc_malloc (sizeof (struct objc_selector)
460*e4b17023SJohn Marino * SELECTOR_POOL_SIZE);
461*e4b17023SJohn Marino selector_pool_left = SELECTOR_POOL_SIZE;
462*e4b17023SJohn Marino }
463*e4b17023SJohn Marino return &selector_pool[--selector_pool_left];
464*e4b17023SJohn Marino }
465*e4b17023SJohn Marino
466*e4b17023SJohn Marino /* Store the passed selector name in the selector record and return
467*e4b17023SJohn Marino its selector value (value returned by sel_get_uid). Assume that
468*e4b17023SJohn Marino the calling function has locked down __objc_runtime_mutex. The
469*e4b17023SJohn Marino 'is_const' parameter tells us if the name and types parameters are
470*e4b17023SJohn Marino really constant or not. If YES then they are constant and we can
471*e4b17023SJohn Marino just store the pointers. If NO then we need to copy name and types
472*e4b17023SJohn Marino because the pointers may disappear later on. If the 'orig'
473*e4b17023SJohn Marino parameter is not NULL, then we are registering a selector from a
474*e4b17023SJohn Marino module, and 'orig' is that selector. In this case, we can put the
475*e4b17023SJohn Marino selector in the tables if needed, and orig->sel_id is updated with
476*e4b17023SJohn Marino the selector ID of the registered selector, and 'orig' is
477*e4b17023SJohn Marino returned. */
478*e4b17023SJohn Marino static SEL
__sel_register_typed_name(const char * name,const char * types,struct objc_selector * orig,BOOL is_const)479*e4b17023SJohn Marino __sel_register_typed_name (const char *name, const char *types,
480*e4b17023SJohn Marino struct objc_selector *orig, BOOL is_const)
481*e4b17023SJohn Marino {
482*e4b17023SJohn Marino struct objc_selector *j;
483*e4b17023SJohn Marino sidx i;
484*e4b17023SJohn Marino struct objc_list *l;
485*e4b17023SJohn Marino
486*e4b17023SJohn Marino i = (sidx) objc_hash_value_for_key (__objc_selector_hash, name);
487*e4b17023SJohn Marino if (soffset_decode (i) != 0)
488*e4b17023SJohn Marino {
489*e4b17023SJohn Marino /* There are already selectors with that name. Examine them to
490*e4b17023SJohn Marino see if the one we're registering already exists. */
491*e4b17023SJohn Marino for (l = (struct objc_list *)sarray_get_safe (__objc_selector_array, i);
492*e4b17023SJohn Marino l; l = l->tail)
493*e4b17023SJohn Marino {
494*e4b17023SJohn Marino SEL s = (SEL)l->head;
495*e4b17023SJohn Marino if (types == 0 || s->sel_types == 0)
496*e4b17023SJohn Marino {
497*e4b17023SJohn Marino if (s->sel_types == types)
498*e4b17023SJohn Marino {
499*e4b17023SJohn Marino if (orig)
500*e4b17023SJohn Marino {
501*e4b17023SJohn Marino orig->sel_id = (void *)i;
502*e4b17023SJohn Marino return orig;
503*e4b17023SJohn Marino }
504*e4b17023SJohn Marino else
505*e4b17023SJohn Marino return s;
506*e4b17023SJohn Marino }
507*e4b17023SJohn Marino }
508*e4b17023SJohn Marino else if (sel_types_match (s->sel_types, types))
509*e4b17023SJohn Marino {
510*e4b17023SJohn Marino if (orig)
511*e4b17023SJohn Marino {
512*e4b17023SJohn Marino orig->sel_id = (void *)i;
513*e4b17023SJohn Marino return orig;
514*e4b17023SJohn Marino }
515*e4b17023SJohn Marino else
516*e4b17023SJohn Marino return s;
517*e4b17023SJohn Marino }
518*e4b17023SJohn Marino }
519*e4b17023SJohn Marino /* A selector with this specific name/type combination does not
520*e4b17023SJohn Marino exist yet. We need to register it. */
521*e4b17023SJohn Marino if (orig)
522*e4b17023SJohn Marino j = orig;
523*e4b17023SJohn Marino else
524*e4b17023SJohn Marino j = pool_alloc_selector ();
525*e4b17023SJohn Marino
526*e4b17023SJohn Marino j->sel_id = (void *)i;
527*e4b17023SJohn Marino /* Can we use the pointer or must we copy types ? Don't copy if
528*e4b17023SJohn Marino NULL. */
529*e4b17023SJohn Marino if ((is_const) || (types == 0))
530*e4b17023SJohn Marino j->sel_types = types;
531*e4b17023SJohn Marino else
532*e4b17023SJohn Marino {
533*e4b17023SJohn Marino j->sel_types = (char *)objc_malloc (strlen (types) + 1);
534*e4b17023SJohn Marino strcpy ((char *)j->sel_types, types);
535*e4b17023SJohn Marino }
536*e4b17023SJohn Marino l = (struct objc_list *)sarray_get_safe (__objc_selector_array, i);
537*e4b17023SJohn Marino }
538*e4b17023SJohn Marino else
539*e4b17023SJohn Marino {
540*e4b17023SJohn Marino /* There are no other selectors with this name registered in the
541*e4b17023SJohn Marino runtime tables. */
542*e4b17023SJohn Marino const char *new_name;
543*e4b17023SJohn Marino
544*e4b17023SJohn Marino /* Determine i. */
545*e4b17023SJohn Marino __objc_selector_max_index += 1;
546*e4b17023SJohn Marino i = soffset_encode (__objc_selector_max_index);
547*e4b17023SJohn Marino
548*e4b17023SJohn Marino /* Prepare the selector. */
549*e4b17023SJohn Marino if (orig)
550*e4b17023SJohn Marino j = orig;
551*e4b17023SJohn Marino else
552*e4b17023SJohn Marino j = pool_alloc_selector ();
553*e4b17023SJohn Marino
554*e4b17023SJohn Marino j->sel_id = (void *)i;
555*e4b17023SJohn Marino /* Can we use the pointer or must we copy types ? Don't copy if
556*e4b17023SJohn Marino NULL. */
557*e4b17023SJohn Marino if (is_const || (types == 0))
558*e4b17023SJohn Marino j->sel_types = types;
559*e4b17023SJohn Marino else
560*e4b17023SJohn Marino {
561*e4b17023SJohn Marino j->sel_types = (char *)objc_malloc (strlen (types) + 1);
562*e4b17023SJohn Marino strcpy ((char *)j->sel_types, types);
563*e4b17023SJohn Marino }
564*e4b17023SJohn Marino
565*e4b17023SJohn Marino /* Since this is the first selector with this name, we need to
566*e4b17023SJohn Marino register the correspondence between 'i' (the sel_id) and
567*e4b17023SJohn Marino 'name' (the actual string) in __objc_selector_names and
568*e4b17023SJohn Marino __objc_selector_hash. */
569*e4b17023SJohn Marino
570*e4b17023SJohn Marino /* Can we use the pointer or must we copy name ? Don't copy if
571*e4b17023SJohn Marino NULL. (FIXME: Can the name really be NULL here ?) */
572*e4b17023SJohn Marino if (is_const || (name == 0))
573*e4b17023SJohn Marino new_name = name;
574*e4b17023SJohn Marino else
575*e4b17023SJohn Marino {
576*e4b17023SJohn Marino new_name = (char *)objc_malloc (strlen (name) + 1);
577*e4b17023SJohn Marino strcpy ((char *)new_name, name);
578*e4b17023SJohn Marino }
579*e4b17023SJohn Marino
580*e4b17023SJohn Marino /* This maps the sel_id to the name. */
581*e4b17023SJohn Marino sarray_at_put_safe (__objc_selector_names, i, (void *)new_name);
582*e4b17023SJohn Marino
583*e4b17023SJohn Marino /* This maps the name to the sel_id. */
584*e4b17023SJohn Marino objc_hash_add (&__objc_selector_hash, (void *)new_name, (void *)i);
585*e4b17023SJohn Marino
586*e4b17023SJohn Marino l = 0;
587*e4b17023SJohn Marino }
588*e4b17023SJohn Marino
589*e4b17023SJohn Marino DEBUG_PRINTF ("Record selector %s[%s] as: %ld\n", name, types,
590*e4b17023SJohn Marino (long)soffset_decode (i));
591*e4b17023SJohn Marino
592*e4b17023SJohn Marino /* Now add the selector to the list of selectors with that id. */
593*e4b17023SJohn Marino l = list_cons ((void *)j, l);
594*e4b17023SJohn Marino sarray_at_put_safe (__objc_selector_array, i, (void *)l);
595*e4b17023SJohn Marino
596*e4b17023SJohn Marino sarray_realloc (__objc_uninstalled_dtable, __objc_selector_max_index + 1);
597*e4b17023SJohn Marino
598*e4b17023SJohn Marino return (SEL)j;
599*e4b17023SJohn Marino }
600*e4b17023SJohn Marino
601*e4b17023SJohn Marino SEL
sel_registerName(const char * name)602*e4b17023SJohn Marino sel_registerName (const char *name)
603*e4b17023SJohn Marino {
604*e4b17023SJohn Marino SEL ret;
605*e4b17023SJohn Marino
606*e4b17023SJohn Marino if (name == NULL)
607*e4b17023SJohn Marino return NULL;
608*e4b17023SJohn Marino
609*e4b17023SJohn Marino objc_mutex_lock (__objc_runtime_mutex);
610*e4b17023SJohn Marino /* Assume that name is not constant static memory and needs to be
611*e4b17023SJohn Marino copied before put into a runtime structure. is_const == NO. */
612*e4b17023SJohn Marino ret = __sel_register_typed_name (name, 0, 0, NO);
613*e4b17023SJohn Marino objc_mutex_unlock (__objc_runtime_mutex);
614*e4b17023SJohn Marino
615*e4b17023SJohn Marino return ret;
616*e4b17023SJohn Marino }
617*e4b17023SJohn Marino
618*e4b17023SJohn Marino SEL
sel_registerTypedName(const char * name,const char * type)619*e4b17023SJohn Marino sel_registerTypedName (const char *name, const char *type)
620*e4b17023SJohn Marino {
621*e4b17023SJohn Marino SEL ret;
622*e4b17023SJohn Marino
623*e4b17023SJohn Marino if (name == NULL)
624*e4b17023SJohn Marino return NULL;
625*e4b17023SJohn Marino
626*e4b17023SJohn Marino objc_mutex_lock (__objc_runtime_mutex);
627*e4b17023SJohn Marino /* Assume that name and type are not constant static memory and need
628*e4b17023SJohn Marino to be copied before put into a runtime structure. is_const ==
629*e4b17023SJohn Marino NO. */
630*e4b17023SJohn Marino ret = __sel_register_typed_name (name, type, 0, NO);
631*e4b17023SJohn Marino objc_mutex_unlock (__objc_runtime_mutex);
632*e4b17023SJohn Marino
633*e4b17023SJohn Marino return ret;
634*e4b17023SJohn Marino }
635*e4b17023SJohn Marino
636*e4b17023SJohn Marino /* Return the selector representing name. */
637*e4b17023SJohn Marino SEL
sel_getUid(const char * name)638*e4b17023SJohn Marino sel_getUid (const char *name)
639*e4b17023SJohn Marino {
640*e4b17023SJohn Marino return sel_registerTypedName (name, 0);
641*e4b17023SJohn Marino }
642