source: trunk/src/gcc/libobjc/class.c@ 1816

Last change on this file since 1816 was 1392, checked in by bird, 22 years ago

This commit was generated by cvs2svn to compensate for changes in r1391,
which included commits to RCS files with non-trunk default branches.

  • Property cvs2svn:cvs-rev set to 1.1.1.2
  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 20.4 KB
Line 
1/* GNU Objective C Runtime class related functions
2 Copyright (C) 1993, 1995, 1996, 1997, 2001, 2002
3 Free Software Foundation, Inc.
4 Contributed by Kresten Krab Thorup and Dennis Glatting.
5
6 Lock-free class table code designed and written from scratch by
7 Nicola Pero, 2001.
8
9This file is part of GNU CC.
10
11GNU CC is free software; you can redistribute it and/or modify it under the
12terms of the GNU General Public License as published by the Free Software
13Foundation; either version 2, or (at your option) any later version.
14
15GNU CC is distributed in the hope that it will be useful, but WITHOUT ANY
16WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
18details.
19
20You should have received a copy of the GNU General Public License along with
21GNU CC; see the file COPYING. If not, write to the Free Software
22Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
23
24/* As a special exception, if you link this library with files compiled with
25 GCC to produce an executable, this does not cause the resulting executable
26 to be covered by the GNU General Public License. This exception does not
27 however invalidate any other reasons why the executable file might be
28 covered by the GNU General Public License. */
29
30/*
31 The code in this file critically affects class method invocation
32 speed. This long preamble comment explains why, and the issues
33 involved.
34
35
36 One of the traditional weaknesses of the GNU Objective-C runtime is
37 that class method invocations are slow. The reason is that when you
38 write
39
40 array = [NSArray new];
41
42 this gets basically compiled into the equivalent of
43
44 array = [(objc_get_class ("NSArray")) new];
45
46 objc_get_class returns the class pointer corresponding to the string
47 `NSArray'; and because of the lookup, the operation is more
48 complicated and slow than a simple instance method invocation.
49
50 Most high performance Objective-C code (using the GNU Objc runtime)
51 I had the opportunity to read (or write) work around this problem by
52 caching the class pointer:
53
54 Class arrayClass = [NSArray class];
55
56 ... later on ...
57
58 array = [arrayClass new];
59 array = [arrayClass new];
60 array = [arrayClass new];
61
62 In this case, you always perform a class lookup (the first one), but
63 then all the [arrayClass new] methods run exactly as fast as an
64 instance method invocation. It helps if you have many class method
65 invocations to the same class.
66
67 The long-term solution to this problem would be to modify the
68 compiler to output tables of class pointers corresponding to all the
69 class method invocations, and to add code to the runtime to update
70 these tables - that should in the end allow class method invocations
71 to perform precisely as fast as instance method invocations, because
72 no class lookup would be involved. I think the Apple Objective-C
73 runtime uses this technique. Doing this involves synchronized
74 modifications in the runtime and in the compiler.
75
76 As a first medicine to the problem, I [NP] have redesigned and
77 rewritten the way the runtime is performing class lookup. This
78 doesn't give as much speed as the other (definitive) approach, but
79 at least a class method invocation now takes approximately 4.5 times
80 an instance method invocation on my machine (it would take approx 12
81 times before the rewriting), which is a lot better.
82
83 One of the main reason the new class lookup is so faster is because
84 I implemented it in a way that can safely run multithreaded without
85 using locks - a so-called `lock-free' data structure. The atomic
86 operation is pointer assignment. The reason why in this problem
87 lock-free data structures work so well is that you never remove
88 classes from the table - and the difficult thing with lock-free data
89 structures is freeing data when is removed from the structures. */
90
91#include "runtime.h" /* the kitchen sink */
92#include "sarray.h"
93
94#include <objc/objc.h>
95#include <objc/objc-api.h>
96#include <objc/thr.h>
97
98/* We use a table which maps a class name to the corresponding class
99 * pointer. The first part of this file defines this table, and
100 * functions to do basic operations on the table. The second part of
101 * the file implements some higher level Objective-C functionality for
102 * classes by using the functions provided in the first part to manage
103 * the table. */
104
105/**
106 ** Class Table Internals
107 **/
108
109/* A node holding a class */
110typedef struct class_node
111{
112 struct class_node *next; /* Pointer to next entry on the list.
113 NULL indicates end of list. */
114
115 const char *name; /* The class name string */
116 int length; /* The class name string length */
117 Class pointer; /* The Class pointer */
118
119} *class_node_ptr;
120
121/* A table containing classes is a class_node_ptr (pointing to the
122 first entry in the table - if it is NULL, then the table is
123 empty). */
124
125/* We have 1024 tables. Each table contains all class names which
126 have the same hash (which is a number between 0 and 1023). To look
127 up a class_name, we compute its hash, and get the corresponding
128 table. Once we have the table, we simply compare strings directly
129 till we find the one which we want (using the length first). The
130 number of tables is quite big on purpose (a normal big application
131 has less than 1000 classes), so that you shouldn't normally get any
132 collisions, and get away with a single comparison (which we can't
133 avoid since we need to know that you have got the right thing). */
134#define CLASS_TABLE_SIZE 1024
135#define CLASS_TABLE_MASK 1023
136
137static class_node_ptr class_table_array[CLASS_TABLE_SIZE];
138
139/* The table writing mutex - we lock on writing to avoid conflicts
140 between different writers, but we read without locks. That is
141 possible because we assume pointer assignment to be an atomic
142 operation. */
143static objc_mutex_t __class_table_lock = NULL;
144
145/* CLASS_TABLE_HASH is how we compute the hash of a class name. It is
146 a macro - *not* a function - arguments *are* modified directly.
147
148 INDEX should be a variable holding an int;
149 HASH should be a variable holding an int;
150 CLASS_NAME should be a variable holding a (char *) to the class_name.
151
152 After the macro is executed, INDEX contains the length of the
153 string, and HASH the computed hash of the string; CLASS_NAME is
154 untouched. */
155
156#define CLASS_TABLE_HASH(INDEX, HASH, CLASS_NAME) \
157 HASH = 0; \
158 for (INDEX = 0; CLASS_NAME[INDEX] != '\0'; INDEX++) \
159 { \
160 HASH = (HASH << 4) ^ (HASH >> 28) ^ CLASS_NAME[INDEX]; \
161 } \
162 \
163 HASH = (HASH ^ (HASH >> 10) ^ (HASH >> 20)) & CLASS_TABLE_MASK;
164
165/* Setup the table. */
166static void
167class_table_setup (void)
168{
169 /* Start - nothing in the table. */
170 memset (class_table_array, 0, sizeof (class_node_ptr) * CLASS_TABLE_SIZE);
171
172 /* The table writing mutex. */
173 __class_table_lock = objc_mutex_allocate ();
174}
175
176
177/* Insert a class in the table (used when a new class is registered). */
178static void
179class_table_insert (const char *class_name, Class class_pointer)
180{
181 int hash, length;
182 class_node_ptr new_node;
183
184 /* Find out the class name's hash and length. */
185 CLASS_TABLE_HASH (length, hash, class_name);
186
187 /* Prepare the new node holding the class. */
188 new_node = objc_malloc (sizeof (struct class_node));
189 new_node->name = class_name;
190 new_node->length = length;
191 new_node->pointer = class_pointer;
192
193 /* Lock the table for modifications. */
194 objc_mutex_lock (__class_table_lock);
195
196 /* Insert the new node in the table at the beginning of the table at
197 class_table_array[hash]. */
198 new_node->next = class_table_array[hash];
199 class_table_array[hash] = new_node;
200
201 objc_mutex_unlock (__class_table_lock);
202}
203
204/* Replace a class in the table (used only by poseAs:). */
205static void
206class_table_replace (Class old_class_pointer, Class new_class_pointer)
207{
208 int hash;
209 class_node_ptr node;
210
211 objc_mutex_lock (__class_table_lock);
212
213 hash = 0;
214 node = class_table_array[hash];
215
216 while (hash < CLASS_TABLE_SIZE)
217 {
218 if (node == NULL)
219 {
220 hash++;
221 if (hash < CLASS_TABLE_SIZE)
222 {
223 node = class_table_array[hash];
224 }
225 }
226 else
227 {
228 Class class1 = node->pointer;
229
230 if (class1 == old_class_pointer)
231 {
232 node->pointer = new_class_pointer;
233 }
234 node = node->next;
235 }
236 }
237
238 objc_mutex_unlock (__class_table_lock);
239}
240
241
242/* Get a class from the table. This does not need mutex protection.
243 Currently, this function is called each time you call a static
244 method, this is why it must be very fast. */
245static inline Class
246class_table_get_safe (const char *class_name)
247{
248 class_node_ptr node;
249 int length, hash;
250
251 /* Compute length and hash. */
252 CLASS_TABLE_HASH (length, hash, class_name);
253
254 node = class_table_array[hash];
255
256 if (node != NULL)
257 {
258 do
259 {
260 if (node->length == length)
261 {
262 /* Compare the class names. */
263 int i;
264
265 for (i = 0; i < length; i++)
266 {
267 if ((node->name)[i] != class_name[i])
268 {
269 break;
270 }
271 }
272
273 if (i == length)
274 {
275 /* They are equal! */
276 return node->pointer;
277 }
278 }
279 }
280 while ((node = node->next) != NULL);
281 }
282
283 return Nil;
284}