2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
27 * Copyright 1988-1996, NeXT Software, Inc.
30 #if !defined(_OBJC_PRIVATE_H_)
31 #define _OBJC_PRIVATE_H_
33 #import <objc/objc-api.h> // for OBJC_EXPORT
35 OBJC_EXPORT
void checkUniqueness();
37 #import "objc-config.h"
43 #define mutex_alloc() (pthread_mutex_t*)calloc(1, sizeof(pthread_mutex_t))
44 #define mutex_init(m) pthread_mutex_init(m, NULL)
45 #define mutex_lock(m) pthread_mutex_lock(m)
46 #define mutex_try_lock(m) (! pthread_mutex_trylock(m))
47 #define mutex_unlock(m) pthread_mutex_unlock(m)
48 #define mutex_clear(m)
49 #define mutex_t pthread_mutex_t*
50 #define mutex MUTEX_DEFINE_ERROR
59 #import <objc/objc-runtime.h>
61 #import <malloc/malloc.h>
64 /* Opaque cookie used in _getObjc... routines. File format independant.
65 * This is used in place of the mach_header. In fact, when compiling
66 * for NEXTSTEP, this is really a (struct mach_header *).
68 * had been: typedef void *objc_header;
70 #import <mach-o/loader.h>
71 typedef struct mach_header headerType
;
73 #import <objc/Protocol.h>
75 typedef struct _ProtocolTemplate
{ @
defs(Protocol
) } ProtocolTemplate
;
76 typedef struct _NXConstantStringTemplate
{
80 } NXConstantStringTemplate
;
82 #define OBJC_CONSTANT_STRING_PTR NXConstantStringTemplate*
83 #define OBJC_CONSTANT_STRING_DEREF &
84 #define OBJC_PROTOCOL_PTR ProtocolTemplate*
85 #define OBJC_PROTOCOL_DEREF .
88 uint32_t version
; // currently 0
92 // masks for objc_image_info.flags
93 #define OBJC_IMAGE_IS_REPLACEMENT (1<<0)
94 #define OBJC_IMAGE_SUPPORTS_GC (1<<1)
97 #define _objcHeaderIsReplacement(h) ((h)->info && ((h)->info->flags & OBJC_IMAGE_IS_REPLACEMENT))
99 /* OBJC_IMAGE_IS_REPLACEMENT:
100 Don't load any classes
101 Don't load any categories
102 Do fix up selector refs (@selector points to them)
103 Do fix up class refs (@class and objc_msgSend points to them)
104 Do fix up protocols (@protocol points to them)
105 Do fix up super_class pointers in classes ([super ...] points to them)
106 Future: do load new classes?
107 Future: do load new categories?
108 Future: do insert new methods on existing classes?
109 Future: do insert new methods on existing categories?
112 #define _objcHeaderSupportsGC(h) ((h)->info && ((h)->info->flags & OBJC_IMAGE_SUPPORTS_GC))
114 /* OBJC_IMAGE_SUPPORTS_GC:
115 was compiled with -fobjc-gc flag, regardless of whether write-barriers were issued
116 if executable image compiled this way, then all subsequent libraries etc. must also be this way
120 OBJC_EXPORT headerType
** _getObjcHeaders();
121 OBJC_EXPORT Module
_getObjcModules(const headerType
*head
, int *nmodules
);
122 OBJC_EXPORT Class
* _getObjcClassRefs(headerType
*head
, int *nclasses
);
123 OBJC_EXPORT
const struct segment_command
*getsegbynamefromheader(const headerType
*head
, const char *segname
);
124 OBJC_EXPORT
const char * _getObjcHeaderName(const headerType
*head
);
125 OBJC_EXPORT objc_image_info
* _getObjcImageInfo(const headerType
*head
, uint32_t *size
);
126 OBJC_EXPORT
ptrdiff_t _getImageSlide(const headerType
*header
);
129 // internal routines for delaying binding
130 void _objc_resolve_categories_for_class (struct objc_class
* cls
);
132 // someday a logging facility
133 // ObjC is assigned the range 0xb000 - 0xbfff for first parameter
134 #define trace(a, b, c, d) do {} while (0)
138 OBJC_EXPORT ProtocolTemplate
* _getObjcProtocols(headerType
*head
, int *nprotos
);
139 OBJC_EXPORT NXConstantStringTemplate
*_getObjcStringObjects(headerType
*head
, int *nstrs
);
140 OBJC_EXPORT SEL
* _getObjcMessageRefs(headerType
*head
, int *nmess
);
142 #define END_OF_METHODS_LIST ((struct objc_method_list*)-1)
144 typedef struct _header_info
146 const headerType
* mhdr
;
147 Module mod_ptr
; // already slid
148 unsigned int mod_count
;
149 unsigned long image_slide
;
150 const struct segment_command
* objcSegmentHeader
; // already slid
151 objc_image_info
* info
; // already slid
152 struct _header_info
* next
;
154 OBJC_EXPORT header_info
*_objc_headerStart ();
156 OBJC_EXPORT
int _objcModuleCount();
157 OBJC_EXPORT
const char *_objcModuleNameAtIndex(int i
);
158 OBJC_EXPORT Class
objc_getOrigClass (const char *name
);
160 OBJC_EXPORT
const char *__S(_nameForHeader
) (const headerType
*);
162 OBJC_EXPORT SEL
sel_registerNameNoLock(const char *str
, BOOL copy
);
163 OBJC_EXPORT
void sel_lock(void);
164 OBJC_EXPORT
void sel_unlock(void);
166 /* optional malloc zone for runtime data */
167 OBJC_EXPORT malloc_zone_t
*_objc_internal_zone(void);
168 OBJC_EXPORT
void *_malloc_internal(size_t size
);
169 OBJC_EXPORT
void *_calloc_internal(size_t count
, size_t size
);
170 OBJC_EXPORT
void *_realloc_internal(void *ptr
, size_t size
);
171 OBJC_EXPORT
char *_strdup_internal(const char *str
);
172 OBJC_EXPORT
void _free_internal(void *ptr
);
174 OBJC_EXPORT BOOL
class_respondsToMethod(Class
, SEL
);
175 OBJC_EXPORT IMP
class_lookupMethod(Class
, SEL
);
176 OBJC_EXPORT IMP
lookupNamedMethodInMethodList(struct objc_method_list
*mlist
, const char *meth_name
);
177 OBJC_EXPORT
void _objc_insertMethods(struct objc_class
*cls
, struct objc_method_list
*mlist
);
178 OBJC_EXPORT
void _objc_removeMethods(struct objc_class
*cls
, struct objc_method_list
*mlist
);
180 OBJC_EXPORT IMP
_cache_getImp(Class cls
, SEL sel
);
181 OBJC_EXPORT Method
_cache_getMethod(Class cls
, SEL sel
, IMP objc_msgForward_imp
);
183 /* message dispatcher */
184 OBJC_EXPORT IMP
_class_lookupMethodAndLoadCache(Class
, SEL
);
185 OBJC_EXPORT id
_objc_msgForward (id self
, SEL sel
, ...);
188 OBJC_EXPORT
volatile void _objc_fatal(const char *fmt
, ...);
189 OBJC_EXPORT
volatile void _objc_error(id
, const char *, va_list);
190 OBJC_EXPORT
volatile void __objc_error(id
, const char *, ...);
191 OBJC_EXPORT
void _objc_inform(const char *fmt
, ...);
192 OBJC_EXPORT
void _objc_syslog(const char *fmt
, ...);
195 OBJC_EXPORT Class
_objc_getFreedObjectClass (void);
196 #ifndef OBJC_INSTRUMENTED
197 OBJC_EXPORT
const struct objc_cache emptyCache
;
199 OBJC_EXPORT
struct objc_cache emptyCache
;
201 OBJC_EXPORT
void _objc_flush_caches (Class cls
);
204 #define MUTEX_TYPE pthread_mutex_t*
205 #define OBJC_DECLARE_LOCK(MTX) pthread_mutex_t MTX = PTHREAD_MUTEX_INITIALIZER
206 OBJC_EXPORT pthread_mutex_t classLock
;
207 OBJC_EXPORT pthread_mutex_t methodListLock
;
209 /* nil handler object */
210 OBJC_EXPORT id _objc_nilReceiver
;
211 OBJC_EXPORT id
_objc_setNilReceiver(id newNilReceiver
);
212 OBJC_EXPORT id
_objc_getNilReceiver(void);
214 /* C++ interoperability */
215 OBJC_EXPORT SEL cxx_construct_sel
;
216 OBJC_EXPORT SEL cxx_destruct_sel
;
217 OBJC_EXPORT
const char *cxx_construct_name
;
218 OBJC_EXPORT
const char *cxx_destruct_name
;
219 OBJC_EXPORT BOOL
object_cxxConstruct(id obj
);
220 OBJC_EXPORT
void object_cxxDestruct(id obj
);
222 /* GC and RTP startup */
223 OBJC_EXPORT
void gc_init(BOOL on
);
224 OBJC_EXPORT
void rtp_init(void);
226 /* Write barrier implementations */
227 OBJC_EXPORT id
objc_assign_strongCast_gc(id val
, id
*dest
);
228 OBJC_EXPORT id
objc_assign_global_gc(id val
, id
*dest
);
229 OBJC_EXPORT id
objc_assign_ivar_gc(id value
, id dest
, unsigned int offset
);
230 OBJC_EXPORT id
objc_assign_strongCast_non_gc(id value
, id
*dest
);
231 OBJC_EXPORT id
objc_assign_global_non_gc(id value
, id
*dest
);
232 OBJC_EXPORT id
objc_assign_ivar_non_gc(id value
, id dest
, unsigned int offset
);
234 /* Code modification */
236 OBJC_EXPORT
size_t objc_write_branch(void *entry
, void *target
);
239 /* Thread-safe info field */
240 OBJC_EXPORT
void _class_setInfo(struct objc_class
*cls
, long set
);
241 OBJC_EXPORT
void _class_clearInfo(struct objc_class
*cls
, long clear
);
242 OBJC_EXPORT
void _class_changeInfo(struct objc_class
*cls
, long set
, long clear
);
244 /* Secure /tmp usage */
245 OBJC_EXPORT
int secure_open(const char *filename
, int flags
, uid_t euid
);
252 static inline int selEqual( SEL s1
, SEL s2
) {
256 #define OBJC_LOCK(MUTEX) mutex_lock (MUTEX)
257 #define OBJC_UNLOCK(MUTEX) mutex_unlock (MUTEX)
258 #define OBJC_TRYLOCK(MUTEX) mutex_try_lock (MUTEX)
260 #if !defined(SEG_OBJC)
261 #define SEG_OBJC "__OBJC" /* objective-C runtime segment */
265 // Settings from environment variables
266 OBJC_EXPORT
int PrintImages
; // env OBJC_PRINT_IMAGES
267 OBJC_EXPORT
int PrintLoading
; // env OBJC_PRINT_LOAD_METHODS
268 OBJC_EXPORT
int PrintConnecting
; // env OBJC_PRINT_CLASS_CONNECTION
269 OBJC_EXPORT
int PrintRTP
; // env OBJC_PRINT_RTP
270 OBJC_EXPORT
int PrintGC
; // env OBJC_PRINT_GC
271 OBJC_EXPORT
int PrintSharing
; // env OBJC_PRINT_SHARING
272 OBJC_EXPORT
int PrintCxxCtors
; // env OBJC_PRINT_CXX_CTORS
274 OBJC_EXPORT
int UseInternalZone
; // env OBJC_USE_INTERNAL_ZONE
275 OBJC_EXPORT
int AllowInterposing
;// env OBJC_ALLOW_INTERPOSING
277 OBJC_EXPORT
int DebugUnload
; // env OBJC_DEBUG_UNLOAD
278 OBJC_EXPORT
int DebugFragileSuperclasses
; // env OBJC_DEBUG_FRAGILE_SUPERCLASSES
280 OBJC_EXPORT
int ForceGC
; // env OBJC_FORCE_GC
281 OBJC_EXPORT
int ForceNoGC
; // env OBJC_FORCE_NO_GC
282 OBJC_EXPORT
int CheckFinalizers
; // env OBJC_CHECK_FINALIZERS
284 OBJC_EXPORT BOOL UseGC
; // equivalent to calling objc_collecting_enabled()
286 static __inline__
int _objc_strcmp(const unsigned char *s1
, const unsigned char *s2
) {
287 unsigned char c1
, c2
;
288 for ( ; (c1
= *s1
) == (c2
= *s2
); s1
++, s2
++)
294 static __inline__
unsigned int _objc_strhash(const unsigned char *s
) {
295 unsigned int hash
= 0;
299 hash
+= (hash
<< 8) + a
;
305 // objc per-thread storage
306 OBJC_EXPORT pthread_key_t _objc_pthread_key
;
308 struct _objc_initializing_classes
*initializingClasses
; // for +initialize
310 // If you add new fields here, don't forget to update
311 // _objc_pthread_destroyspecific()
313 } _objc_pthread_data
;
317 #define ISCLASS(cls) ((((struct objc_class *) cls)->info & CLS_CLASS) != 0)
318 #define ISMETA(cls) ((((struct objc_class *) cls)->info & CLS_META) != 0)
319 #define GETMETA(cls) (ISMETA(cls) ? ((struct objc_class *) cls) : ((struct objc_class *) cls)->isa)
320 #define ISINITIALIZED(cls) ((((volatile long)GETMETA(cls)->info) & CLS_INITIALIZED) != 0)
321 #define ISINITIALIZING(cls) ((((volatile long)GETMETA(cls)->info) & CLS_INITIALIZING) != 0)
324 // Attribute for global variables to keep them out of bss storage
325 // To save one page per non-Objective-C process, variables used in
326 // the "Objective-C not used" case should not be in bss storage.
327 // On Tiger, this reduces the number of touched pages for each
328 // CoreFoundation-only process from three to two. See #3857126 and #3857136.
329 #define NOBSS __attribute__((section("__DATA,__data")))
331 #endif /* _OBJC_PRIVATE_H_ */