2 * Copyright (c) 1999-2007 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
25 * Copyright 1988-1996, NeXT Software, Inc.
28 #ifndef _OBJC_PRIVATE_H_
29 #define _OBJC_PRIVATE_H_
31 #include "objc-config.h"
33 /* Isolate ourselves from the definitions of id and Class in the compiler
38 #error include objc-private.h before other headers
41 #define OBJC_TYPES_DEFINED 1
42 #define OBJC_OLD_DISPATCH_PROTOTYPES 0
44 #include <cstddef> // for nullptr_t
51 typedef struct objc_class
*Class
;
52 typedef struct objc_object
*id
;
62 isa_t(uintptr_t value
) : bits(value
) { }
67 #if SUPPORT_NONPOINTER_ISA
69 // extra_rc must be the MSB-most field (so it matches carry/overflow flags)
70 // indexed must be the LSB (fixme or get rid of it)
71 // shiftcls must occupy the same bits that a real class pointer would
72 // bits + RC_ONE is equivalent to extra_rc + 1
73 // RC_HALF is the high bit of extra_rc (i.e. half of its range)
76 // uintptr_t fast_rr : 1; // no r/r overrides
77 // uintptr_t lock : 2; // lock for atomic property, @synch
78 // uintptr_t extraBytes : 1; // allocated with extra bytes
81 # define ISA_MASK 0x0000000ffffffff8ULL
82 # define ISA_MAGIC_MASK 0x000003f000000001ULL
83 # define ISA_MAGIC_VALUE 0x000001a000000001ULL
85 uintptr_t indexed
: 1;
86 uintptr_t has_assoc
: 1;
87 uintptr_t has_cxx_dtor
: 1;
88 uintptr_t shiftcls
: 33; // MACH_VM_MAX_ADDRESS 0x1000000000
90 uintptr_t weakly_referenced
: 1;
91 uintptr_t deallocating
: 1;
92 uintptr_t has_sidetable_rc
: 1;
93 uintptr_t extra_rc
: 19;
94 # define RC_ONE (1ULL<<45)
95 # define RC_HALF (1ULL<<18)
99 # define ISA_MASK 0x00007ffffffffff8ULL
100 # define ISA_MAGIC_MASK 0x001f800000000001ULL
101 # define ISA_MAGIC_VALUE 0x001d800000000001ULL
103 uintptr_t indexed
: 1;
104 uintptr_t has_assoc
: 1;
105 uintptr_t has_cxx_dtor
: 1;
106 uintptr_t shiftcls
: 44; // MACH_VM_MAX_ADDRESS 0x7fffffe00000
108 uintptr_t weakly_referenced
: 1;
109 uintptr_t deallocating
: 1;
110 uintptr_t has_sidetable_rc
: 1;
111 uintptr_t extra_rc
: 8;
112 # define RC_ONE (1ULL<<56)
113 # define RC_HALF (1ULL<<7)
117 // Available bits in isa field are architecture-specific.
118 # error unknown architecture
121 // SUPPORT_NONPOINTER_ISA
133 // ISA() assumes this is NOT a tagged pointer object
136 // getIsa() allows this to be a tagged pointer object
139 // initIsa() should be used to init the isa of new objects only.
140 // If this object already has an isa, use changeIsa() for correctness.
141 // initInstanceIsa(): objects with no custom RR/AWZ
142 // initClassIsa(): class objects
143 // initProtocolIsa(): protocol objects
144 // initIsa(): other objects
145 void initIsa(Class cls
/*indexed=false*/);
146 void initClassIsa(Class cls
/*indexed=maybe*/);
147 void initProtocolIsa(Class cls
/*indexed=maybe*/);
148 void initInstanceIsa(Class cls
, bool hasCxxDtor
);
150 // changeIsa() should be used to change the isa of existing objects.
151 // If this is a new object, use initIsa() for performance.
152 Class
changeIsa(Class newCls
);
154 bool hasIndexedIsa();
155 bool isTaggedPointer();
158 // object may have associated objects?
159 bool hasAssociatedObjects();
160 void setHasAssociatedObjects();
162 // object may be weakly referenced?
163 bool isWeaklyReferenced();
164 void setWeaklyReferenced_nolock();
166 // object may have -.cxx_destruct implementation?
169 // Optimized calls to retain/release methods
174 // Implementations of retain/release methods
177 id
rootAutorelease();
178 bool rootTryRetain();
179 bool rootReleaseShouldDealloc();
180 uintptr_t rootRetainCount();
182 // Implementation of dealloc methods
183 bool rootIsDeallocating();
184 void clearDeallocating();
188 void initIsa(Class newCls
, bool indexed
, bool hasCxxDtor
);
190 // Slow paths for inline control
191 id
rootAutorelease2();
192 bool overrelease_error();
194 #if SUPPORT_NONPOINTER_ISA
195 // Unified retain count manipulation for nonpointer isa
196 id
rootRetain(bool tryRetain
, bool handleOverflow
);
197 bool rootRelease(bool performDealloc
, bool handleUnderflow
);
198 id
rootRetain_overflow(bool tryRetain
);
199 bool rootRelease_underflow(bool performDealloc
);
201 void clearDeallocating_slow();
203 // Side table retain count overflow for nonpointer isa
204 void sidetable_lock();
205 void sidetable_unlock();
207 void sidetable_moveExtraRC_nolock(size_t extra_rc
, bool isDeallocating
, bool weaklyReferenced
);
208 bool sidetable_addExtraRC_nolock(size_t delta_rc
);
209 size_t sidetable_subExtraRC_nolock(size_t delta_rc
);
210 size_t sidetable_getExtraRC_nolock();
213 // Side-table-only retain count
214 bool sidetable_isDeallocating();
215 void sidetable_clearDeallocating();
217 bool sidetable_isWeaklyReferenced();
218 void sidetable_setWeaklyReferenced_nolock();
220 id
sidetable_retain();
221 id
sidetable_retain_slow(SideTable
& table
);
223 uintptr_t sidetable_release(bool performDealloc
= true);
224 uintptr_t sidetable_release_slow(SideTable
& table
, bool performDealloc
= true);
226 bool sidetable_tryRetain();
228 uintptr_t sidetable_retainCount();
230 bool sidetable_present();
236 typedef struct method_t
*Method
;
237 typedef struct ivar_t
*Ivar
;
238 typedef struct category_t
*Category
;
239 typedef struct property_t
*objc_property_t
;
241 typedef struct old_method
*Method
;
242 typedef struct old_ivar
*Ivar
;
243 typedef struct old_category
*Category
;
244 typedef struct old_property
*objc_property_t
;
252 #include "objc-abi.h"
253 #include "objc-api.h"
254 #include "objc-config.h"
255 #include "objc-internal.h"
256 #include "maptable.h"
257 #include "hashtable2.h"
260 #include "objc-auto.h"
263 /* Do not include message.h here. */
264 /* #include "message.h" */
266 #define __APPLE_API_PRIVATE
267 #include "objc-gdb.h"
268 #undef __APPLE_API_PRIVATE
274 #include "objc-runtime-new.h"
276 #include "objc-runtime-old.h"
279 #include "objc-references.h"
280 #include "objc-initialize.h"
281 #include "objc-loadmethod.h"
284 #if SUPPORT_PREOPT && __cplusplus
285 #include <objc-shared-cache.h>
286 using objc_selopt_t
= const objc_opt::objc_selopt_t
;
288 struct objc_selopt_t
;
295 #if (defined(OBJC_NO_GC) && SUPPORT_GC) || \
296 (!defined(OBJC_NO_GC) && !SUPPORT_GC)
297 # error OBJC_NO_GC and SUPPORT_GC inconsistent
301 # include <auto_zone.h>
302 // PRIVATE_EXTERN is needed to help the compiler know "how" extern these are
303 PRIVATE_EXTERN
extern int8_t UseGC
; // equivalent to calling objc_collecting_enabled()
304 PRIVATE_EXTERN
extern auto_zone_t
*gc_zone
; // the GC zone, or NULL if no GC
305 extern void objc_addRegisteredClass(Class c
);
306 extern void objc_removeRegisteredClass(Class c
);
309 # define gc_zone NULL
310 # define objc_addRegisteredClass(c) do {} while(0)
311 # define objc_removeRegisteredClass(c) do {} while(0)
312 /* Uses of the following must be protected with UseGC. */
313 extern id
gc_unsupported_dont_call();
314 # define auto_zone_allocate_object gc_unsupported_dont_call
315 # define auto_zone_retain gc_unsupported_dont_call
316 # define auto_zone_release gc_unsupported_dont_call
317 # define auto_zone_is_valid_pointer gc_unsupported_dont_call
318 # define auto_zone_write_barrier_memmove gc_unsupported_dont_call
319 # define AUTO_OBJECT_SCANNED 0
323 #define _objcHeaderIsReplacement(h) ((h)->info && ((h)->info->flags & OBJC_IMAGE_IS_REPLACEMENT))
325 /* OBJC_IMAGE_IS_REPLACEMENT:
326 Don't load any classes
327 Don't load any categories
328 Do fix up selector refs (@selector points to them)
329 Do fix up class refs (@class and objc_msgSend points to them)
330 Do fix up protocols (@protocol points to them)
331 Do fix up superclass pointers in classes ([super ...] points to them)
332 Future: do load new classes?
333 Future: do load new categories?
334 Future: do insert new methods on existing classes?
335 Future: do insert new methods on existing categories?
338 #define _objcInfoSupportsGC(info) (((info)->flags & OBJC_IMAGE_SUPPORTS_GC) ? 1 : 0)
339 #define _objcInfoRequiresGC(info) (((info)->flags & OBJC_IMAGE_REQUIRES_GC) ? 1 : 0)
340 #define _objcHeaderSupportsGC(h) ((h)->info && _objcInfoSupportsGC((h)->info))
341 #define _objcHeaderRequiresGC(h) ((h)->info && _objcInfoRequiresGC((h)->info))
343 /* OBJC_IMAGE_SUPPORTS_GC:
344 was compiled with -fobjc-gc flag, regardless of whether write-barriers were issued
345 if executable image compiled this way, then all subsequent libraries etc. must also be this way
348 #define _objcHeaderOptimizedByDyld(h) ((h)->info && ((h)->info->flags & OBJC_IMAGE_OPTIMIZED_BY_DYLD))
350 /* OBJC_IMAGE_OPTIMIZED_BY_DYLD:
351 Assorted metadata precooked in the dyld shared cache.
352 Never set for images outside the shared cache file itself.
356 typedef struct header_info
{
357 struct header_info
*next
;
358 const headerType
*mhdr
;
359 const objc_image_info
*info
;
360 const char *fname
; // same as Dl_info.dli_fname
363 bool allClassesRealized
;
365 // Do not add fields without editing ObjCModernAbstraction.hpp
372 return mhdr
->filetype
== MH_BUNDLE
;
375 bool isPreoptimized() const;
378 struct old_protocol
**proto_refs
;
379 struct objc_module
*mod_ptr
;
382 struct objc_module
**modules
;
384 struct old_protocol
**protocols
;
385 size_t protocolCount
;
387 size_t imageinfoBytes
;
390 struct objc_class
**clsrefs
;
397 extern header_info
*FirstHeader
;
398 extern header_info
*LastHeader
;
399 extern int HeaderCount
;
401 extern void appendHeader(header_info
*hi
);
402 extern void removeHeader(header_info
*hi
);
404 extern objc_image_info
*_getObjcImageInfo(const headerType
*head
, size_t *size
);
405 extern bool _hasObjcContents(const header_info
*hi
);
409 extern void sel_init(bool gc
, size_t selrefCount
);
410 extern SEL
sel_registerNameNoLock(const char *str
, bool copy
);
411 extern void sel_lock(void);
412 extern void sel_unlock(void);
415 extern SEL SEL_initialize
;
416 extern SEL SEL_resolveClassMethod
;
417 extern SEL SEL_resolveInstanceMethod
;
418 extern SEL SEL_cxx_construct
;
419 extern SEL SEL_cxx_destruct
;
420 extern SEL SEL_retain
;
421 extern SEL SEL_release
;
422 extern SEL SEL_autorelease
;
423 extern SEL SEL_retainCount
;
424 extern SEL SEL_alloc
;
425 extern SEL SEL_allocWithZone
;
426 extern SEL SEL_dealloc
;
429 extern SEL SEL_finalize
;
430 extern SEL SEL_forwardInvocation
;
431 extern SEL SEL_tryRetain
;
432 extern SEL SEL_isDeallocating
;
433 extern SEL SEL_retainWeakReference
;
434 extern SEL SEL_allowsWeakReference
;
436 /* preoptimization */
437 extern void preopt_init(void);
438 extern void disableSharedCacheOptimizations(void);
439 extern bool isPreoptimized(void);
440 extern header_info
*preoptimizedHinfoForHeader(const headerType
*mhdr
);
442 extern objc_selopt_t
*preoptimizedSelectors(void);
444 extern Protocol
*getPreoptimizedProtocol(const char *name
);
446 extern Class
getPreoptimizedClass(const char *name
);
447 extern Class
* copyPreoptimizedClasses(const char *name
, int *outCount
);
449 extern Class
_calloc_class(size_t size
);
452 extern IMP
lookUpImpOrNil(Class
, SEL
, id obj
, bool initialize
, bool cache
, bool resolver
);
453 extern IMP
lookUpImpOrForward(Class
, SEL
, id obj
, bool initialize
, bool cache
, bool resolver
);
455 extern IMP
lookupMethodInClassAndLoadCache(Class cls
, SEL sel
);
456 extern bool class_respondsToSelector_inst(Class cls
, SEL sel
, id inst
);
458 extern bool objcMsgLogEnabled
;
459 extern bool logMessageSend(bool isClassMethod
,
460 const char *objectsClass
,
461 const char *implementingClass
,
464 /* message dispatcher */
465 extern IMP
_class_lookupMethodAndLoadCache3(id
, SEL
, Class
);
467 #if !OBJC_OLD_DISPATCH_PROTOTYPES
468 extern void _objc_msgForward_impcache(void);
469 extern void _objc_ignored_method(void);
470 extern void _objc_msgSend_uncached_impcache(void);
472 extern id
_objc_msgForward_impcache(id
, SEL
, ...);
473 extern id
_objc_ignored_method(id
, SEL
, ...);
474 extern id
_objc_msgSend_uncached_impcache(id
, SEL
, ...);
478 extern void __objc_error(id
, const char *, ...) __attribute__((format (printf
, 2, 3), noreturn
));
479 extern void _objc_inform(const char *fmt
, ...) __attribute__((format (printf
, 1, 2)));
480 extern void _objc_inform_on_crash(const char *fmt
, ...) __attribute__((format (printf
, 1, 2)));
481 extern void _objc_inform_now_and_on_crash(const char *fmt
, ...) __attribute__((format (printf
, 1, 2)));
482 extern void _objc_inform_deprecated(const char *oldname
, const char *newname
) __attribute__((noinline
));
483 extern void inform_duplicate(const char *name
, Class oldCls
, Class cls
);
484 extern bool crashlog_header_name(header_info
*hi
);
485 extern bool crashlog_header_name_string(const char *name
);
488 extern Class
_objc_getFreedObjectClass (void);
490 /* map table additions */
491 extern void *NXMapKeyCopyingInsert(NXMapTable
*table
, const void *key
, const void *value
);
492 extern void *NXMapKeyFreeingRemove(NXMapTable
*table
, const void *key
);
494 /* hash table additions */
495 extern unsigned _NXHashCapacity(NXHashTable
*table
);
496 extern void _NXHashRehashToCapacity(NXHashTable
*table
, unsigned newCapacity
);
498 /* property attribute parsing */
499 extern const char *copyPropertyAttributeString(const objc_property_attribute_t
*attrs
, unsigned int count
);
500 extern objc_property_attribute_t
*copyPropertyAttributeList(const char *attrs
, unsigned int *outCount
);
501 extern char *copyPropertyAttributeValue(const char *attrs
, const char *name
);
504 extern void lock_init(void);
505 extern rwlock_t selLock
;
506 extern mutex_t cacheUpdateLock
;
507 extern recursive_mutex_t loadMethodLock
;
509 extern rwlock_t runtimeLock
;
511 extern mutex_t classLock
;
512 extern mutex_t methodListLock
;
515 class monitor_locker_t
: nocopy_t
{
518 monitor_locker_t(monitor_t
& newLock
) : lock(newLock
) { lock
.enter(); }
519 ~monitor_locker_t() { lock
.leave(); }
522 class mutex_locker_t
: nocopy_t
{
525 mutex_locker_t(mutex_t
& newLock
)
526 : lock(newLock
) { lock
.lock(); }
527 ~mutex_locker_t() { lock
.unlock(); }
530 class recursive_mutex_locker_t
: nocopy_t
{
531 recursive_mutex_t
& lock
;
533 recursive_mutex_locker_t(recursive_mutex_t
& newLock
)
534 : lock(newLock
) { lock
.lock(); }
535 ~recursive_mutex_locker_t() { lock
.unlock(); }
538 class rwlock_reader_t
: nocopy_t
{
541 rwlock_reader_t(rwlock_t
& newLock
) : lock(newLock
) { lock
.read(); }
542 ~rwlock_reader_t() { lock
.unlockRead(); }
545 class rwlock_writer_t
: nocopy_t
{
548 rwlock_writer_t(rwlock_t
& newLock
) : lock(newLock
) { lock
.write(); }
549 ~rwlock_writer_t() { lock
.unlockWrite(); }
552 /* ignored selector support */
554 /* Non-GC: no ignored selectors
555 GC (i386 Mac): some selectors ignored, remapped to kIgnore
556 GC (others): some selectors ignored, but not remapped
559 static inline int ignoreSelector(SEL sel
)
563 #elif SUPPORT_IGNORED_SELECTOR_CONSTANT
564 return UseGC
&& sel
== (SEL
)kIgnore
;
567 (sel
== @
selector(retain
) ||
568 sel
== @
selector(release
) ||
569 sel
== @
selector(autorelease
) ||
570 sel
== @
selector(retainCount
) ||
571 sel
== @
selector(dealloc
));
575 static inline int ignoreSelectorNamed(const char *sel
)
580 // release retain retainCount dealloc autorelease
582 ( (sel
[0] == 'r' && sel
[1] == 'e' &&
583 (strcmp(&sel
[2], "lease") == 0 ||
584 strcmp(&sel
[2], "tain") == 0 ||
585 strcmp(&sel
[2], "tainCount") == 0 ))
587 (strcmp(sel
, "dealloc") == 0)
589 (sel
[0] == 'a' && sel
[1] == 'u' &&
590 strcmp(&sel
[2], "torelease") == 0)));
595 extern void gc_init(bool wantsGC
);
596 extern void gc_init2(void);
599 struct alt_handler_list
;
600 extern void exception_init(void);
601 extern void _destroyAltHandlerList(struct alt_handler_list
*list
);
603 /* Class change notifications (gdb only for now) */
604 #define OBJC_CLASS_ADDED (1<<0)
605 #define OBJC_CLASS_REMOVED (1<<1)
606 #define OBJC_CLASS_IVARS_CHANGED (1<<2)
607 #define OBJC_CLASS_METHODS_CHANGED (1<<3)
608 extern void gdb_objc_class_changed(Class cls
, unsigned long changes
, const char *classname
)
609 __attribute__((noinline
));
613 /* Write barrier implementations */
614 extern id
objc_getAssociatedObject_non_gc(id object
, const void *key
);
615 extern void objc_setAssociatedObject_non_gc(id object
, const void *key
, id value
, objc_AssociationPolicy policy
);
617 extern id
objc_getAssociatedObject_gc(id object
, const void *key
);
618 extern void objc_setAssociatedObject_gc(id object
, const void *key
, id value
, objc_AssociationPolicy policy
);
621 extern objc_xref_t
_object_addExternalReference_non_gc(id obj
, objc_xref_t type
);
622 extern id
_object_readExternalReference_non_gc(objc_xref_t ref
);
623 extern void _object_removeExternalReference_non_gc(objc_xref_t ref
);
625 extern objc_xref_t
_object_addExternalReference_gc(id obj
, objc_xref_t type
);
626 extern id
_object_readExternalReference_gc(objc_xref_t ref
);
627 extern void _object_removeExternalReference_gc(objc_xref_t ref
);
629 /* GC weak reference fixup. */
630 extern void gc_fixup_weakreferences(id newObject
, id oldObject
);
632 /* GC datasegment registration. */
633 extern void gc_register_datasegment(uintptr_t base
, size_t size
);
634 extern void gc_unregister_datasegment(uintptr_t base
, size_t size
);
636 /* objc_dumpHeap implementation */
637 extern bool _objc_dumpHeap(auto_zone_t
*zone
, const char *filename
);
642 // Settings from environment variables
643 #define OPTION(var, env, help) extern bool var;
644 #include "objc-env.h"
647 extern void environ_init(void);
649 extern void logReplacedMethod(const char *className
, SEL s
, bool isMeta
, const char *catName
, IMP oldImp
, IMP newImp
);
652 // objc per-thread storage
654 struct _objc_initializing_classes
*initializingClasses
; // for +initialize
655 struct SyncCache
*syncCache
; // for @synchronize
656 struct alt_handler_list
*handlerList
; // for exception alt handlers
657 char *printableNames
[4]; // temporary demangled names for logging
659 // If you add new fields here, don't forget to update
660 // _objc_pthread_destroyspecific()
662 } _objc_pthread_data
;
664 extern _objc_pthread_data
*_objc_fetch_pthread_data(bool create
);
665 extern void tls_init(void);
668 extern unsigned int encoding_getNumberOfArguments(const char *typedesc
);
669 extern unsigned int encoding_getSizeOfArguments(const char *typedesc
);
670 extern unsigned int encoding_getArgumentInfo(const char *typedesc
, unsigned int arg
, const char **type
, int *offset
);
671 extern void encoding_getReturnType(const char *t
, char *dst
, size_t dst_len
);
672 extern char * encoding_copyReturnType(const char *t
);
673 extern void encoding_getArgumentType(const char *t
, unsigned int index
, char *dst
, size_t dst_len
);
674 extern char *encoding_copyArgumentType(const char *t
, unsigned int index
);
677 extern void _destroySyncCache(struct SyncCache
*cache
);
680 extern void arr_init(void);
681 extern id
objc_autoreleaseReturnValue(id obj
);
684 extern IMP
_imp_implementationWithBlockNoCopy(id block
);
690 size_t bitsAllocated
;
693 extern layout_bitmap
layout_bitmap_create(const unsigned char *layout_string
, size_t layoutStringInstanceSize
, size_t instanceSize
, bool weak
);
694 extern layout_bitmap
layout_bitmap_create_empty(size_t instanceSize
, bool weak
);
695 extern void layout_bitmap_free(layout_bitmap bits
);
696 extern const unsigned char *layout_string_create(layout_bitmap bits
);
697 extern void layout_bitmap_set_ivar(layout_bitmap bits
, const char *type
, size_t offset
);
698 extern void layout_bitmap_grow(layout_bitmap
*bits
, size_t newCount
);
699 extern void layout_bitmap_slide(layout_bitmap
*bits
, size_t oldPos
, size_t newPos
);
700 extern void layout_bitmap_slide_anywhere(layout_bitmap
*bits
, size_t oldPos
, size_t newPos
);
701 extern bool layout_bitmap_splat(layout_bitmap dst
, layout_bitmap src
,
702 size_t oldSrcInstanceSize
);
703 extern bool layout_bitmap_or(layout_bitmap dst
, layout_bitmap src
, const char *msg
);
704 extern bool layout_bitmap_clear(layout_bitmap dst
, layout_bitmap src
, const char *msg
);
705 extern void layout_bitmap_print(layout_bitmap bits
);
709 extern Class
look_up_class(const char *aClassName
, bool includeUnconnected
, bool includeClassHandler
);
710 extern "C" const char *map_2_images(enum dyld_image_states state
, uint32_t infoCount
, const struct dyld_image_info infoList
[]);
711 extern const char *map_images_nolock(enum dyld_image_states state
, uint32_t infoCount
, const struct dyld_image_info infoList
[]);
712 extern const char * load_images(enum dyld_image_states state
, uint32_t infoCount
, const struct dyld_image_info infoList
[]);
713 extern bool load_images_nolock(enum dyld_image_states state
, uint32_t infoCount
, const struct dyld_image_info infoList
[]);
714 extern void unmap_image(const struct mach_header
*mh
, intptr_t vmaddr_slide
);
715 extern void unmap_image_nolock(const struct mach_header
*mh
);
716 extern void _read_images(header_info
**hList
, uint32_t hCount
);
717 extern void prepare_load_methods(const headerType
*mhdr
);
718 extern bool hasLoadMethods(const headerType
*mhdr
);
719 extern void _unload_image(header_info
*hi
);
720 extern const char ** _objc_copyClassNamesForImage(header_info
*hi
, unsigned int *outCount
);
723 extern const header_info
*_headerForClass(Class cls
);
725 extern Class
_class_remap(Class cls
);
726 extern Class
_class_getNonMetaClass(Class cls
, id obj
);
727 extern Ivar
_class_getVariable(Class cls
, const char *name
, Class
*memberOf
);
728 extern uint32_t _class_getInstanceStart(Class cls
);
730 extern unsigned _class_createInstancesFromZone(Class cls
, size_t extraBytes
, void *zone
, id
*results
, unsigned num_requested
);
731 extern id
_objc_constructOrFree(id bytes
, Class cls
);
733 extern const char *_category_getName(Category cat
);
734 extern const char *_category_getClassName(Category cat
);
735 extern Class
_category_getClass(Category cat
);
736 extern IMP
_category_getLoadMethod(Category cat
);
738 extern id
object_cxxConstructFromClass(id obj
, Class cls
);
739 extern void object_cxxDestruct(id obj
);
741 extern void _class_resolveMethod(Class cls
, SEL sel
, id inst
);
743 #define OBJC_WARN_DEPRECATED \
745 static int warned = 0; \
748 _objc_inform_deprecated(__FUNCTION__, NULL); \
755 #ifndef STATIC_ASSERT
756 # define STATIC_ASSERT(x) _STATIC_ASSERT2(x, __LINE__)
757 # define _STATIC_ASSERT2(x, line) _STATIC_ASSERT3(x, line)
758 # define _STATIC_ASSERT3(x, line) \
760 int _static_assert[(x) ? 0 : -1]; \
761 } _static_assert_ ## line __attribute__((unavailable))
764 #define countof(arr) (sizeof(arr) / sizeof((arr)[0]))
767 static __inline
uint32_t _objc_strhash(const char *s
) {
772 hash
+= (hash
<< 8) + a
;
779 template <typename T
>
780 static inline T
log2u(T x
) {
781 return (x
<2) ? 0 : log2u(x
>>1)+1;
784 template <typename T
>
785 static inline T
exp2u(T x
) {
789 template <typename T
>
790 static T
exp2m1u(T x
) {
797 // Global operator new and delete. We must not use any app overrides.
798 // This ALSO REQUIRES each of these be in libobjc's unexported symbol list.
800 #pragma clang diagnostic push
801 #pragma clang diagnostic ignored "-Winline-new-delete"
803 inline void* operator new(std::size_t size
) throw (std::bad_alloc
) { return malloc(size
); }
804 inline void* operator new[](std::size_t size
) throw (std::bad_alloc
) { return malloc(size
); }
805 inline void* operator new(std::size_t size
, const std::nothrow_t
&) throw() { return malloc(size
); }
806 inline void* operator new[](std::size_t size
, const std::nothrow_t
&) throw() { return malloc(size
); }
807 inline void operator delete(void* p
) throw() { free(p
); }
808 inline void operator delete[](void* p
) throw() { free(p
); }
809 inline void operator delete(void* p
, const std::nothrow_t
&) throw() { free(p
); }
810 inline void operator delete[](void* p
, const std::nothrow_t
&) throw() { free(p
); }
811 #pragma clang diagnostic pop
819 TimeLogger(bool record
= true)
820 : mStart(nanoseconds())
824 void log(const char *msg
) {
826 uint64_t end
= nanoseconds();
827 _objc_inform("%.2f ms: %s", (end
- mStart
) / 1000000.0, msg
);
828 mStart
= nanoseconds();
834 // StripedMap<T> is a map of void* -> T, sized appropriately
835 // for cache-friendly lock striping.
836 // For example, this may be used as StripedMap<spinlock_t>
837 // or as StripedMap<SomeStruct> where SomeStruct stores a spin lock.
841 enum { CacheLineSize
= 64 };
843 #if TARGET_OS_EMBEDDED
844 enum { StripeCount
= 8 };
846 enum { StripeCount
= 64 };
850 T value
alignas(CacheLineSize
);
853 PaddedT array
[StripeCount
];
855 static unsigned int indexForPointer(const void *p
) {
856 uintptr_t addr
= reinterpret_cast<uintptr_t>(p
);
857 return ((addr
>> 4) ^ (addr
>> 9)) % StripeCount
;
861 T
& operator[] (const void *p
) {
862 return array
[indexForPointer(p
)].value
;
864 const T
& operator[] (const void *p
) const {
865 return const_cast<StripedMap
<T
>>(this)[p
];
870 // Verify alignment expectations.
871 uintptr_t base
= (uintptr_t)&array
[0].value
;
872 uintptr_t delta
= (uintptr_t)&array
[1].value
- base
;
873 assert(delta
% CacheLineSize
== 0);
874 assert(base
% CacheLineSize
== 0);
880 // DisguisedPtr<T> acts like pointer type T*, except the
881 // stored value is disguised to hide it from tools like `leaks`.
882 // nil is disguised as itself so zero-filled memory works as expected,
883 // which means 0x80..00 is also diguised as itself but we don't care
884 template <typename T
>
888 static uintptr_t disguise(T
* ptr
) {
889 return -(uintptr_t)ptr
;
892 static T
* undisguise(uintptr_t val
) {
899 : value(disguise(ptr
)) { }
900 DisguisedPtr(const DisguisedPtr
<T
>& ptr
)
901 : value(ptr
.value
) { }
903 DisguisedPtr
<T
>& operator = (T
* rhs
) {
904 value
= disguise(rhs
);
907 DisguisedPtr
<T
>& operator = (const DisguisedPtr
<T
>& rhs
) {
912 operator T
* () const {
913 return undisguise(value
);
915 T
* operator -> () const {
916 return undisguise(value
);
918 T
& operator * () const {
919 return *undisguise(value
);
921 T
& operator [] (size_t i
) const {
922 return undisguise(value
)[i
];
925 // pointer arithmetic operators omitted
926 // because we don't currently use them anywhere
929 // fixme type id is weird and not identical to objc_object*
930 static inline bool operator == (DisguisedPtr
<objc_object
> lhs
, id rhs
) {
931 return lhs
== (objc_object
*)rhs
;
933 static inline bool operator != (DisguisedPtr
<objc_object
> lhs
, id rhs
) {
934 return lhs
!= (objc_object
*)rhs
;
938 // Pointer hash function.
939 // This is not a terrific hash, but it is fast
940 // and not outrageously flawed for our purposes.
942 // Based on principles from http://locklessinc.com/articles/fast_hash/
943 // and evaluation ideas from http://floodyberry.com/noncryptohashzoo/
945 static inline uint32_t ptr_hash(uint64_t key
)
948 key
*= 0x8a970be7488fda55;
949 key
^= __builtin_bswap64(key
);
950 return (uint32_t)key
;
953 static inline uint32_t ptr_hash(uint32_t key
)
957 key
^= __builtin_bswap32(key
);
963 Higher-quality hash function. This is measurably slower in some workloads.
965 uint32_t ptr_hash(uint64_t key)
967 key -= __builtin_bswap64(key);
968 key *= 0x8a970be7488fda55;
969 key ^= __builtin_bswap64(key);
970 key *= 0x8a970be7488fda55;
971 key ^= __builtin_bswap64(key);
972 return (uint32_t)key;
975 static uint32_t ptr_hash(uint32_t key)
977 key -= __builtin_bswap32(key);
979 key ^= __builtin_bswap32(key);
981 key ^= __builtin_bswap32(key);
988 // Inlined parts of objc_object's implementation
989 #include "objc-object.h"
991 #endif /* _OBJC_PRIVATE_H_ */