annotate libobjc/selector.c @ 158:494b0b89df80 default tip

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