2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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
22 * @APPLE_LICENSE_HEADER_END@
26 * Copyright 1988-1996, NeXT Software, Inc.
29 #if !defined(_OBJC_PRIVATE_H_)
30 #define _OBJC_PRIVATE_H_
32 #import <objc/objc-api.h> // for OBJC_EXPORT
34 OBJC_EXPORT
void checkUniqueness();
36 #import "objc-config.h"
39 #define LITERAL_STRING_OBJECTS
40 #import <mach/cthreads_private.h>
45 #import <pdo.h> // for pdo_malloc and pdo_free defines
50 #define mutex_alloc() (pthread_mutex_t*)calloc(1, sizeof(pthread_mutex_t))
51 #define mutex_init(m) pthread_mutex_init(m, NULL)
52 #define mutex_lock(m) pthread_mutex_lock(m)
53 #define mutex_try_lock(m) (! pthread_mutex_trylock(m))
54 #define mutex_unlock(m) pthread_mutex_unlock(m)
55 #define mutex_clear(m)
56 #define mutex_t pthread_mutex_t*
57 #define mutex MUTEX_DEFINE_ERROR
67 #import <objc/objc-runtime.h>
69 // This needs <...> -- malloc.h is not ours, really...
70 #import <objc/malloc.h>
73 /* Opaque cookie used in _getObjc... routines. File format independant.
74 * This is used in place of the mach_header. In fact, when compiling
75 * for NEXTSTEP, this is really a (struct mach_header *).
77 * had been: typedef void *objc_header;
80 typedef void headerType
;
82 #import <mach-o/loader.h>
83 typedef struct mach_header headerType
;
86 #import <objc/Protocol.h>
88 typedef struct _ProtocolTemplate
{ @
defs(Protocol
) } ProtocolTemplate
;
89 typedef struct _NXConstantStringTemplate
{
93 } NXConstantStringTemplate
;
96 #define OBJC_CONSTANT_STRING_PTR NXConstantStringTemplate**
97 #define OBJC_CONSTANT_STRING_DEREF
98 #define OBJC_PROTOCOL_PTR ProtocolTemplate**
99 #define OBJC_PROTOCOL_DEREF ->
100 #elif defined(__MACH__)
101 #define OBJC_CONSTANT_STRING_PTR NXConstantStringTemplate*
102 #define OBJC_CONSTANT_STRING_DEREF &
103 #define OBJC_PROTOCOL_PTR ProtocolTemplate*
104 #define OBJC_PROTOCOL_DEREF .
108 OBJC_EXPORT headerType
** _getObjcHeaders();
109 OBJC_EXPORT Module
_getObjcModules(headerType
*head
, int *nmodules
);
110 OBJC_EXPORT Class
* _getObjcClassRefs(headerType
*head
, int *nclasses
);
111 OBJC_EXPORT
void * _getObjcHeaderData(headerType
*head
, unsigned *size
);
112 OBJC_EXPORT
const char * _getObjcHeaderName(headerType
*head
);
114 #if defined(NeXT_PDO) // GENERIC_OBJ_FILE
115 OBJC_EXPORT ProtocolTemplate
** _getObjcProtocols(headerType
*head
, int *nprotos
);
116 OBJC_EXPORT NXConstantStringTemplate
**_getObjcStringObjects(headerType
*head
, int *nstrs
);
117 #elif defined(__MACH__)
118 OBJC_EXPORT ProtocolTemplate
* _getObjcProtocols(headerType
*head
, int *nprotos
);
119 OBJC_EXPORT NXConstantStringTemplate
*_getObjcStringObjects(headerType
*head
, int *nstrs
);
120 OBJC_EXPORT SEL
* _getObjcMessageRefs(headerType
*head
, int *nmess
);
123 #define END_OF_METHODS_LIST ((struct objc_method_list*)-1)
127 const headerType
* mhdr
;
129 unsigned int mod_count
;
130 unsigned long image_slide
;
131 unsigned int objcSize
;
133 typedef struct header_info header_info
;
134 OBJC_EXPORT header_info
*_objc_headerVector (const headerType
* const *machhdrs
);
135 OBJC_EXPORT
unsigned int _objc_headerCount (void);
136 OBJC_EXPORT
void _objc_addHeader (const headerType
*header
, unsigned long vmaddr_slide
);
138 OBJC_EXPORT
int _objcModuleCount();
139 OBJC_EXPORT
const char *_objcModuleNameAtIndex(int i
);
140 OBJC_EXPORT Class
objc_getOrigClass (const char *name
);
142 extern struct objc_method_list
**get_base_method_list(Class cls
);
145 OBJC_EXPORT
const char *__S(_nameForHeader
) (const headerType
*);
148 OBJC_EXPORT
void _sel_resolve_conflicts(headerType
* header
, unsigned long slide
);
149 OBJC_EXPORT
void _class_install_relationships(Class
, long);
150 OBJC_EXPORT
void _objc_add_category(Category
, int);
151 OBJC_EXPORT
void *_objc_create_zone(void);
153 OBJC_EXPORT SEL
sel_registerNameNoCopy(const char *str
);
155 /* selector fixup in method lists */
157 #define _OBJC_FIXED_UP ((void *)1771)
159 static inline struct objc_method_list
*_objc_inlined_fixup_selectors_in_method_list(struct objc_method_list
*mlist
)
163 struct objc_method_list
*old_mlist
;
165 if ( ! mlist
) return (struct objc_method_list
*)0;
166 if ( mlist
->obsolete
!= _OBJC_FIXED_UP
) {
168 size
= sizeof(struct objc_method_list
) - sizeof(struct objc_method
) + old_mlist
->method_count
* sizeof(struct objc_method
);
169 mlist
= malloc_zone_malloc(_objc_create_zone(), size
);
170 memmove(mlist
, old_mlist
, size
);
171 for ( i
= 0; i
< mlist
->method_count
; i
+= 1 ) {
172 method
= &mlist
->method_list
[i
];
173 method
->method_name
=
174 sel_registerNameNoCopy((const char *)method
->method_name
);
176 mlist
->obsolete
= _OBJC_FIXED_UP
;
182 /* -- inline version of class_nextMethodList(Class, void **) -- */
184 static inline struct objc_method_list
*_class_inlinedNextMethodList(Class cls
, void **it
)
186 struct objc_method_list
***iterator
;
188 iterator
= (struct objc_method_list
***)it
;
189 if (*iterator
== NULL
) {
190 *iterator
= &((((struct objc_class
*) cls
)->methodLists
)[0]);
192 else (*iterator
) += 1;
193 // Check for list end
194 if ((**iterator
== NULL
) || (**iterator
== END_OF_METHODS_LIST
)) {
199 **iterator
= _objc_inlined_fixup_selectors_in_method_list(**iterator
);
201 // Return method list pointer
205 OBJC_EXPORT BOOL
class_respondsToMethod(Class
, SEL
);
206 OBJC_EXPORT IMP
class_lookupMethod(Class
, SEL
);
207 OBJC_EXPORT IMP
class_lookupMethodInMethodList(struct objc_method_list
*mlist
, SEL sel
);
208 OBJC_EXPORT IMP
class_lookupNamedMethodInMethodList(struct objc_method_list
*mlist
, const char *meth_name
);
209 OBJC_EXPORT
void _objc_insertMethods( struct objc_method_list
*mlist
, struct objc_method_list
***list
);
210 OBJC_EXPORT
void _objc_removeMethods( struct objc_method_list
*mlist
, struct objc_method_list
***list
);
212 /* message dispatcher */
213 OBJC_EXPORT Cache
_cache_create(Class
);
214 OBJC_EXPORT IMP
_class_lookupMethodAndLoadCache(Class
, SEL
);
215 OBJC_EXPORT id
_objc_msgForward (id self
, SEL sel
, ...);
218 OBJC_EXPORT
volatile void __S(_objc_fatal
)(const char *message
);
219 OBJC_EXPORT
volatile void _objc_error(id
, const char *, va_list);
220 OBJC_EXPORT
volatile void __objc_error(id
, const char *, ...);
221 OBJC_EXPORT
void _objc_inform(const char *fmt
, ...);
222 OBJC_EXPORT
void _NXLogError(const char *format
, ...);
225 OBJC_EXPORT Class
_objc_getFreedObjectClass (void);
226 OBJC_EXPORT
const struct objc_cache emptyCache
;
227 OBJC_EXPORT
void _objc_flush_caches (Class cls
);
230 #if defined(NeXT_PDO)
232 #define MUTEX_TYPE long
233 #define OBJC_DECLARE_LOCK(MUTEX) MUTEX_TYPE MUTEX = 0L;
235 #define MUTEX_TYPE long
236 #define OBJC_DECLARE_LOCK(MUTEX) MUTEX_TYPE MUTEX = 0L;
237 #elif defined(__alpha__)
238 #define MUTEX_TYPE long
239 #define OBJC_DECLARE_LOCK(MUTEX) MUTEX_TYPE MUTEX = 0L;
240 #elif defined(__hpux__) || defined(hpux)
241 typedef struct { int a
; int b
; int c
; int d
; } __mutex_struct
;
242 #define MUTEX_TYPE __mutex_struct
243 #define OBJC_DECLARE_LOCK(MUTEX) MUTEX_TYPE MUTEX = { 1, 1, 1, 1 };
244 #else // unknown pdo platform
245 #define MUTEX_TYPE long
246 #define OBJC_DECLARE_LOCK(MUTEX) struct mutex MUTEX = { 0 };
248 OBJC_EXPORT MUTEX_TYPE classLock
;
249 OBJC_EXPORT MUTEX_TYPE messageLock
;
251 #define MUTEX_TYPE pthread_mutex_t*
252 #define OBJC_DECLARE_LOCK(MTX) pthread_mutex_t MTX = PTHREAD_MUTEX_INITIALIZER
253 OBJC_EXPORT pthread_mutex_t classLock
;
254 OBJC_EXPORT pthread_mutex_t messageLock
;
257 OBJC_EXPORT
int _objc_multithread_mask
;
259 // _objc_msgNil is actually (unsigned dummy, id, SEL) for i386;
260 // currently not implemented for any sparc or hppa platforms
261 OBJC_EXPORT
void (*_objc_msgNil
)(id
, SEL
);
268 static inline int selEqual( SEL s1
, SEL s2
) {
269 OBJC_EXPORT
int rocketLaunchingDebug
;
270 if ( rocketLaunchingDebug
)
271 checkUniqueness(s1
, s2
);
275 #if defined(OBJC_COLLECTING_CACHE)
276 #define OBJC_LOCK(MUTEX) mutex_lock (MUTEX)
277 #define OBJC_UNLOCK(MUTEX) mutex_unlock (MUTEX)
278 #define OBJC_TRYLOCK(MUTEX) mutex_try_lock (MUTEX)
279 #elif defined(NeXT_PDO)
281 /* Where are these defined? NT should probably be using them! */
282 OBJC_EXPORT
void _objc_private_lock(MUTEX_TYPE
*);
283 OBJC_EXPORT
void _objc_private_unlock(MUTEX_TYPE
*);
285 /* I don't think this should be commented out for NT, should it? */
286 #define OBJC_LOCK(MUTEX) \
287 do {if (!_objc_multithread_mask) \
288 _objc_private_lock(MUTEX);} while(0)
289 #define OBJC_UNLOCK(MUTEX) \
290 do {if (!_objc_multithread_mask) \
291 _objc_private_unlock(MUTEX);} while(0)
293 #define OBJC_LOCK(MUTEX) \
294 do {if (!_objc_multithread_mask) \
295 if( *MUTEX == 0 ) *MUTEX = 1;} while(0)
296 #define OBJC_UNLOCK(MUTEX) \
297 do {if (!_objc_multithread_mask) \
298 *MUTEX = 0;} while(0)
301 #else // not NeXT_PDO
302 #define OBJC_LOCK(MUTEX) \
305 if (!_objc_multithread_mask) \
306 mutex_lock (MUTEX); \
310 #define OBJC_UNLOCK(MUTEX) \
313 if (!_objc_multithread_mask) \
314 mutex_unlock (MUTEX); \
317 #endif /* OBJC_COLLECTING_CACHE */
319 #if !defined(SEG_OBJC)
320 #define SEG_OBJC "__OBJC" /* objective-C runtime segment */
323 #if defined(NeXT_PDO)
325 void send_load_message_to_category(Category cat
, void *header_addr
);
326 void send_load_message_to_class(Class cls
, void *header_addr
);
329 #if !defined(__MACH__)
330 typedef struct _objcSectionStruct
{
331 void **data
; /* Pointer to array */
332 int count
; /* # of elements */
333 int size
; /* sizeof an element */
336 typedef struct _objcModHeader
{
338 objcSectionStruct Modules
;
339 objcSectionStruct Classes
;
340 objcSectionStruct Methods
;
341 objcSectionStruct Protocols
;
342 objcSectionStruct StringObjects
;
347 static __inline__
int _objc_strcmp(const unsigned char *s1
, const unsigned char *s2
) {
352 if (a
!= b
|| 0 == a
) break;
358 static __inline__
unsigned int _objc_strhash(const unsigned char *s
) {
359 unsigned int hash
= 0;
363 hash
+= (hash
<< 8) + a
;
368 #endif /* _OBJC_PRIVATE_H_ */