]> git.saurik.com Git - apple/objc4.git/blob - runtime/objc-private.h
a178b19fdc987d05decd10fe2fb6d0e8464aa1fe
[apple/objc4.git] / runtime / objc-private.h
1 /*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.1 (the "License"). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
12 * this file.
13 *
14 * The Original Code and all software distributed under the License are
15 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
20 * under the License.
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
24 /*
25 * objc-private.h
26 * Copyright 1988-1996, NeXT Software, Inc.
27 */
28
29 #if !defined(_OBJC_PRIVATE_H_)
30 #define _OBJC_PRIVATE_H_
31
32 #import <objc/objc-api.h> // for OBJC_EXPORT
33
34 OBJC_EXPORT void checkUniqueness();
35
36 #import "objc-config.h"
37
38 #import <pthread.h>
39 #define mutex_alloc() (pthread_mutex_t*)calloc(1, sizeof(pthread_mutex_t))
40 #define mutex_init(m) pthread_mutex_init(m, NULL)
41 #define mutex_lock(m) pthread_mutex_lock(m)
42 #define mutex_try_lock(m) (! pthread_mutex_trylock(m))
43 #define mutex_unlock(m) pthread_mutex_unlock(m)
44 #define mutex_clear(m)
45 #define mutex_t pthread_mutex_t*
46 #define mutex MUTEX_DEFINE_ERROR
47 #import <sys/time.h>
48
49 #import <stdlib.h>
50 #import <stdarg.h>
51 #import <stdio.h>
52 #import <string.h>
53 #import <ctype.h>
54
55 #import <objc/objc-runtime.h>
56
57 // This needs <...> -- malloc.h is not ours, really...
58 #import <objc/malloc.h>
59
60
61 /* Opaque cookie used in _getObjc... routines. File format independant.
62 * This is used in place of the mach_header. In fact, when compiling
63 * for NEXTSTEP, this is really a (struct mach_header *).
64 *
65 * had been: typedef void *objc_header;
66 */
67 #if defined(NeXT_PDO)
68 typedef void headerType;
69 #else
70 #import <mach-o/loader.h>
71 typedef struct mach_header headerType;
72 #endif
73
74 #import <objc/Protocol.h>
75
76 typedef struct _ProtocolTemplate { @defs(Protocol) } ProtocolTemplate;
77 typedef struct _NXConstantStringTemplate {
78 Class isa;
79 void *characters;
80 unsigned int _length;
81 } NXConstantStringTemplate;
82
83 #if defined(NeXT_PDO)
84 #define OBJC_CONSTANT_STRING_PTR NXConstantStringTemplate**
85 #define OBJC_CONSTANT_STRING_DEREF
86 #define OBJC_PROTOCOL_PTR ProtocolTemplate**
87 #define OBJC_PROTOCOL_DEREF ->
88 #elif defined(__MACH__)
89 #define OBJC_CONSTANT_STRING_PTR NXConstantStringTemplate*
90 #define OBJC_CONSTANT_STRING_DEREF &
91 #define OBJC_PROTOCOL_PTR ProtocolTemplate*
92 #define OBJC_PROTOCOL_DEREF .
93 #endif
94
95 // both
96 OBJC_EXPORT headerType ** _getObjcHeaders();
97 OBJC_EXPORT Module _getObjcModules(headerType *head, int *nmodules);
98 OBJC_EXPORT Class * _getObjcClassRefs(headerType *head, int *nclasses);
99 OBJC_EXPORT void * _getObjcHeaderData(headerType *head, unsigned *size);
100 OBJC_EXPORT const char * _getObjcHeaderName(headerType *head);
101
102 // internal routines for delaying binding
103 void _objc_resolve_categories_for_class (struct objc_class * cls);
104 void _objc_bindModuleContainingClass(struct objc_class * cls);
105
106 // someday a logging facility
107 // ObjC is assigned the range 0xb000 - 0xbfff for first parameter
108 #define trace(a, b, c, d) do {} while (0)
109
110
111
112 OBJC_EXPORT ProtocolTemplate * _getObjcProtocols(headerType *head, int *nprotos);
113 OBJC_EXPORT NXConstantStringTemplate *_getObjcStringObjects(headerType *head, int *nstrs);
114 OBJC_EXPORT SEL * _getObjcMessageRefs(headerType *head, int *nmess);
115
116 #define END_OF_METHODS_LIST ((struct objc_method_list*)-1)
117
118 typedef struct _header_info
119 {
120 const headerType * mhdr;
121 Module mod_ptr;
122 unsigned int mod_count;
123 unsigned long image_slide;
124 struct _header_info * next;
125 } header_info;
126 OBJC_EXPORT header_info *_objc_headerStart ();
127
128 OBJC_EXPORT int _objcModuleCount();
129 OBJC_EXPORT const char *_objcModuleNameAtIndex(int i);
130 OBJC_EXPORT Class objc_getOrigClass (const char *name);
131
132 extern struct objc_method_list **get_base_method_list(Class cls);
133
134
135 OBJC_EXPORT const char *__S(_nameForHeader) (const headerType*);
136
137 /* initialize */
138 OBJC_EXPORT void _sel_resolve_conflicts(headerType * header, unsigned long slide);
139 OBJC_EXPORT void _class_install_relationships(Class, long);
140 OBJC_EXPORT void *_objc_create_zone(void);
141
142 OBJC_EXPORT SEL sel_registerNameNoCopy(const char *str);
143
144 /* selector fixup in method lists */
145
146 #define _OBJC_FIXED_UP ((void *)1771)
147
148 static inline struct objc_method_list *_objc_inlined_fixup_selectors_in_method_list(struct objc_method_list *mlist)
149 {
150 unsigned i, size;
151 Method method;
152 struct objc_method_list *old_mlist;
153
154 if ( ! mlist ) return (struct objc_method_list *)0;
155 if ( mlist->obsolete != _OBJC_FIXED_UP ) {
156 old_mlist = mlist;
157 size = sizeof(struct objc_method_list) - sizeof(struct objc_method) + old_mlist->method_count * sizeof(struct objc_method);
158 mlist = malloc_zone_malloc(_objc_create_zone(), size);
159 memmove(mlist, old_mlist, size);
160 for ( i = 0; i < mlist->method_count; i += 1 ) {
161 method = &mlist->method_list[i];
162 method->method_name =
163 sel_registerNameNoCopy((const char *)method->method_name);
164 }
165 mlist->obsolete = _OBJC_FIXED_UP;
166 }
167 return mlist;
168 }
169
170 /* method lookup */
171 /* -- inline version of class_nextMethodList(Class, void **) -- */
172
173 static inline struct objc_method_list *_class_inlinedNextMethodList(Class cls, void **it)
174 {
175 struct objc_method_list ***iterator;
176
177 iterator = (struct objc_method_list***)it;
178 if (*iterator == NULL) {
179 *iterator = &((((struct objc_class *) cls)->methodLists)[0]);
180 }
181 else (*iterator) += 1;
182 // Check for list end
183 if ((**iterator == NULL) || (**iterator == END_OF_METHODS_LIST)) {
184 *it = nil;
185 return NULL;
186 }
187
188 **iterator = _objc_inlined_fixup_selectors_in_method_list(**iterator);
189
190 // Return method list pointer
191 return **iterator;
192 }
193
194 OBJC_EXPORT BOOL class_respondsToMethod(Class, SEL);
195 OBJC_EXPORT IMP class_lookupMethod(Class, SEL);
196 OBJC_EXPORT IMP class_lookupMethodInMethodList(struct objc_method_list *mlist, SEL sel);
197 OBJC_EXPORT IMP class_lookupNamedMethodInMethodList(struct objc_method_list *mlist, const char *meth_name);
198 OBJC_EXPORT void _objc_insertMethods( struct objc_method_list *mlist, struct objc_method_list ***list );
199 OBJC_EXPORT void _objc_removeMethods( struct objc_method_list *mlist, struct objc_method_list ***list );
200
201 /* message dispatcher */
202 OBJC_EXPORT Cache _cache_create(Class);
203 OBJC_EXPORT IMP _class_lookupMethodAndLoadCache(Class, SEL);
204 OBJC_EXPORT id _objc_msgForward (id self, SEL sel, ...);
205
206 /* errors */
207 OBJC_EXPORT volatile void __S(_objc_fatal)(const char *message);
208 OBJC_EXPORT volatile void _objc_error(id, const char *, va_list);
209 OBJC_EXPORT volatile void __objc_error(id, const char *, ...);
210 OBJC_EXPORT void _objc_inform(const char *fmt, ...);
211 OBJC_EXPORT void _objc_syslog(const char *fmt, ...);
212
213 /* magic */
214 OBJC_EXPORT Class _objc_getFreedObjectClass (void);
215 OBJC_EXPORT const struct objc_cache emptyCache;
216 OBJC_EXPORT void _objc_flush_caches (Class cls);
217
218 /* locking */
219 #define MUTEX_TYPE pthread_mutex_t*
220 #define OBJC_DECLARE_LOCK(MTX) pthread_mutex_t MTX = PTHREAD_MUTEX_INITIALIZER
221 OBJC_EXPORT pthread_mutex_t classLock;
222 OBJC_EXPORT pthread_mutex_t messageLock;
223
224 OBJC_EXPORT int _objc_multithread_mask;
225
226 // _objc_msgNil is actually (unsigned dummy, id, SEL) for i386;
227 // currently not implemented for any sparc or hppa platforms
228 OBJC_EXPORT void (*_objc_msgNil)(id, SEL);
229
230 typedef struct {
231 long addressOffset;
232 long selectorOffset;
233 } FixupEntry;
234
235 static inline int selEqual( SEL s1, SEL s2 ) {
236 return (s1 == s2);
237 }
238
239 #if defined(OBJC_COLLECTING_CACHE)
240 #define OBJC_LOCK(MUTEX) mutex_lock (MUTEX)
241 #define OBJC_UNLOCK(MUTEX) mutex_unlock (MUTEX)
242 #define OBJC_TRYLOCK(MUTEX) mutex_try_lock (MUTEX)
243 #else // not OBJC_COLLECTING_CACHE
244 #define OBJC_LOCK(MUTEX) \
245 do \
246 { \
247 if (!_objc_multithread_mask) \
248 mutex_lock (MUTEX); \
249 } \
250 while (0)
251
252 #define OBJC_UNLOCK(MUTEX) \
253 do \
254 { \
255 if (!_objc_multithread_mask) \
256 mutex_unlock (MUTEX); \
257 } \
258 while (0)
259 #endif /* OBJC_COLLECTING_CACHE */
260
261 #if !defined(SEG_OBJC)
262 #define SEG_OBJC "__OBJC" /* objective-C runtime segment */
263 #endif
264
265
266
267
268 static __inline__ int _objc_strcmp(const unsigned char *s1, const unsigned char *s2) {
269 int a, b, idx = 0;
270 for (;;) {
271 a = s1[idx];
272 b = s2[idx];
273 if (a != b || 0 == a) break;
274 idx++;
275 }
276 return a - b;
277 }
278
279 static __inline__ unsigned int _objc_strhash(const unsigned char *s) {
280 unsigned int hash = 0;
281 for (;;) {
282 int a = *s++;
283 if (0 == a) break;
284 hash += (hash << 8) + a;
285 }
286 return hash;
287 }
288
289 #endif /* _OBJC_PRIVATE_H_ */
290