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 #undef OBJC_OLD_DISPATCH_PROTOTYPES
43 #define OBJC_OLD_DISPATCH_PROTOTYPES 0
45 #include <cstddef> // for nullptr_t
49 // An assert that's disabled for release builds but still ensures the expression compiles.
51 #define ASSERT(x) (void)sizeof(!(x))
53 #define ASSERT(x) assert(x)
59 typedef struct objc_class
*Class
;
60 typedef struct objc_object
*id
;
70 isa_t(uintptr_t value
) : bits(value
) { }
74 #if defined(ISA_BITFIELD)
76 ISA_BITFIELD
; // defined in isa.h
88 // ISA() assumes this is NOT a tagged pointer object
91 // rawISA() assumes this is NOT a tagged pointer object or a non pointer ISA
94 // getIsa() allows this to be a tagged pointer object
97 uintptr_t isaBits() const;
99 // initIsa() should be used to init the isa of new objects only.
100 // If this object already has an isa, use changeIsa() for correctness.
101 // initInstanceIsa(): objects with no custom RR/AWZ
102 // initClassIsa(): class objects
103 // initProtocolIsa(): protocol objects
104 // initIsa(): other objects
105 void initIsa(Class cls
/*nonpointer=false*/);
106 void initClassIsa(Class cls
/*nonpointer=maybe*/);
107 void initProtocolIsa(Class cls
/*nonpointer=maybe*/);
108 void initInstanceIsa(Class cls
, bool hasCxxDtor
);
110 // changeIsa() should be used to change the isa of existing objects.
111 // If this is a new object, use initIsa() for performance.
112 Class
changeIsa(Class newCls
);
114 bool hasNonpointerIsa();
115 bool isTaggedPointer();
116 bool isBasicTaggedPointer();
117 bool isExtTaggedPointer();
120 // object may have associated objects?
121 bool hasAssociatedObjects();
122 void setHasAssociatedObjects();
124 // object may be weakly referenced?
125 bool isWeaklyReferenced();
126 void setWeaklyReferenced_nolock();
128 // object may have -.cxx_destruct implementation?
131 // Optimized calls to retain/release methods
136 // Implementations of retain/release methods
139 id
rootAutorelease();
140 bool rootTryRetain();
141 bool rootReleaseShouldDealloc();
142 uintptr_t rootRetainCount();
144 // Implementation of dealloc methods
145 bool rootIsDeallocating();
146 void clearDeallocating();
150 void initIsa(Class newCls
, bool nonpointer
, bool hasCxxDtor
);
152 // Slow paths for inline control
153 id
rootAutorelease2();
154 uintptr_t overrelease_error();
156 #if SUPPORT_NONPOINTER_ISA
157 // Unified retain count manipulation for nonpointer isa
158 id
rootRetain(bool tryRetain
, bool handleOverflow
);
159 bool rootRelease(bool performDealloc
, bool handleUnderflow
);
160 id
rootRetain_overflow(bool tryRetain
);
161 uintptr_t rootRelease_underflow(bool performDealloc
);
163 void clearDeallocating_slow();
165 // Side table retain count overflow for nonpointer isa
166 void sidetable_lock();
167 void sidetable_unlock();
169 void sidetable_moveExtraRC_nolock(size_t extra_rc
, bool isDeallocating
, bool weaklyReferenced
);
170 bool sidetable_addExtraRC_nolock(size_t delta_rc
);
171 size_t sidetable_subExtraRC_nolock(size_t delta_rc
);
172 size_t sidetable_getExtraRC_nolock();
175 // Side-table-only retain count
176 bool sidetable_isDeallocating();
177 void sidetable_clearDeallocating();
179 bool sidetable_isWeaklyReferenced();
180 void sidetable_setWeaklyReferenced_nolock();
182 id
sidetable_retain();
183 id
sidetable_retain_slow(SideTable
& table
);
185 uintptr_t sidetable_release(bool performDealloc
= true);
186 uintptr_t sidetable_release_slow(SideTable
& table
, bool performDealloc
= true);
188 bool sidetable_tryRetain();
190 uintptr_t sidetable_retainCount();
192 bool sidetable_present();
198 typedef struct method_t
*Method
;
199 typedef struct ivar_t
*Ivar
;
200 typedef struct category_t
*Category
;
201 typedef struct property_t
*objc_property_t
;
203 typedef struct old_method
*Method
;
204 typedef struct old_ivar
*Ivar
;
205 typedef struct old_category
*Category
;
206 typedef struct old_property
*objc_property_t
;
214 #include "objc-abi.h"
215 #include "objc-api.h"
216 #include "objc-config.h"
217 #include "objc-internal.h"
218 #include "maptable.h"
219 #include "hashtable2.h"
221 /* Do not include message.h here. */
222 /* #include "message.h" */
224 #define __APPLE_API_PRIVATE
225 #include "objc-gdb.h"
226 #undef __APPLE_API_PRIVATE
231 #include "objc-ptrauth.h"
234 #include "objc-runtime-new.h"
236 #include "objc-runtime-old.h"
239 #include "objc-references.h"
240 #include "objc-initialize.h"
241 #include "objc-loadmethod.h"
244 #if SUPPORT_PREOPT && __cplusplus
245 #include <objc-shared-cache.h>
246 using objc_selopt_t
= const objc_opt::objc_selopt_t
;
248 struct objc_selopt_t
;
252 #define STRINGIFY(x) #x
253 #define STRINGIFY2(x) STRINGIFY(x)
265 inline bool contains(uintptr_t ptr
) const {
266 uintptr_t m_start
, m_end
;
268 // <rdar://problem/48304934> Force the compiler to use ldp
269 // we really don't want 2 loads and 2 jumps.
272 "ldp %x[one], %x[two], [%x[src]]"
274 "ldp %w[one], %w[two], [%x[src]]"
276 : [one
] "=r" (m_start
), [two
] "=r" (m_end
)
283 return m_start
<= ptr
&& ptr
< m_end
;
287 struct Range
*ranges
;
293 inline bool contains(uint16_t witness
, uintptr_t ptr
) const {
294 return witness
< count
&& ranges
[witness
].contains(ptr
);
297 bool find(uintptr_t ptr
, uint32_t &pos
);
298 void add(uintptr_t start
, uintptr_t end
);
299 void remove(uintptr_t start
, uintptr_t end
);
302 extern struct SafeRanges dataSegmentsRanges
;
308 // Split out the rw data from header info. For now put it in a huge array
309 // that more than exceeds the space needed. In future we'll just allocate
310 // this in the shared cache builder.
311 typedef struct header_info_rw
{
313 bool getLoaded() const {
317 void setLoaded(bool v
) {
321 bool getAllClassesRealized() const {
322 return allClassesRealized
;
325 void setAllClassesRealized(bool v
) {
326 allClassesRealized
= v
? 1: 0;
329 header_info
*getNext() const {
330 return (header_info
*)(next
<< 2);
333 void setNext(header_info
*v
) {
334 next
= ((uintptr_t)v
) >> 2;
339 uintptr_t isLoaded
: 1;
340 uintptr_t allClassesRealized
: 1;
343 uintptr_t isLoaded
: 1;
344 uintptr_t allClassesRealized
: 1;
349 struct header_info_rw
* getPreoptimizedHeaderRW(const struct header_info
*const hdr
);
351 typedef struct header_info
{
353 // Note, this is no longer a pointer, but instead an offset to a pointer
354 // from this location.
355 intptr_t mhdr_offset
;
357 // Note, this is no longer a pointer, but instead an offset to a pointer
358 // from this location.
359 intptr_t info_offset
;
361 // Do not add fields without editing ObjCModernAbstraction.hpp
364 header_info_rw
*getHeaderInfoRW() {
365 header_info_rw
*preopt
=
366 isPreoptimized() ? getPreoptimizedHeaderRW(this) : nil
;
367 if (preopt
) return preopt
;
368 else return &rw_data
[0];
371 const headerType
*mhdr() const {
372 return (const headerType
*)(((intptr_t)&mhdr_offset
) + mhdr_offset
);
375 void setmhdr(const headerType
*mhdr
) {
376 mhdr_offset
= (intptr_t)mhdr
- (intptr_t)&mhdr_offset
;
379 const objc_image_info
*info() const {
380 return (const objc_image_info
*)(((intptr_t)&info_offset
) + info_offset
);
383 void setinfo(const objc_image_info
*info
) {
384 info_offset
= (intptr_t)info
- (intptr_t)&info_offset
;
388 return getHeaderInfoRW()->getLoaded();
391 void setLoaded(bool v
) {
392 getHeaderInfoRW()->setLoaded(v
);
395 bool areAllClassesRealized() {
396 return getHeaderInfoRW()->getAllClassesRealized();
399 void setAllClassesRealized(bool v
) {
400 getHeaderInfoRW()->setAllClassesRealized(v
);
403 header_info
*getNext() {
404 return getHeaderInfoRW()->getNext();
407 void setNext(header_info
*v
) {
408 getHeaderInfoRW()->setNext(v
);
412 return mhdr()->filetype
== MH_BUNDLE
;
415 const char *fname() const {
416 return dyld_image_path_containing_address(mhdr());
419 bool isPreoptimized() const;
421 bool hasPreoptimizedSelectors() const;
423 bool hasPreoptimizedClasses() const;
425 bool hasPreoptimizedProtocols() const;
428 struct old_protocol
**proto_refs
;
429 struct objc_module
*mod_ptr
;
432 struct objc_module
**modules
;
434 struct old_protocol
**protocols
;
435 size_t protocolCount
;
437 size_t imageinfoBytes
;
440 struct objc_class
**clsrefs
;
447 // Images in the shared cache will have an empty array here while those
448 // allocated at run time will allocate a single entry.
449 header_info_rw rw_data
[];
452 extern header_info
*FirstHeader
;
453 extern header_info
*LastHeader
;
455 extern void appendHeader(header_info
*hi
);
456 extern void removeHeader(header_info
*hi
);
458 extern objc_image_info
*_getObjcImageInfo(const headerType
*head
, size_t *size
);
459 extern bool _hasObjcContents(const header_info
*hi
);
462 // Mach-O segment and section names are 16 bytes and may be un-terminated.
464 static inline bool segnameEquals(const char *lhs
, const char *rhs
) {
465 return 0 == strncmp(lhs
, rhs
, 16);
468 static inline bool segnameStartsWith(const char *segname
, const char *prefix
) {
469 return 0 == strncmp(segname
, prefix
, strlen(prefix
));
472 static inline bool sectnameEquals(const char *lhs
, const char *rhs
) {
473 return segnameEquals(lhs
, rhs
);
476 static inline bool sectnameStartsWith(const char *sectname
, const char *prefix
){
477 return segnameStartsWith(sectname
, prefix
);
482 extern bool didCallDyldNotifyRegister
;
487 extern void sel_init(size_t selrefCount
);
488 extern SEL
sel_registerNameNoLock(const char *str
, bool copy
);
490 extern SEL SEL_cxx_construct
;
491 extern SEL SEL_cxx_destruct
;
493 /* preoptimization */
494 extern void preopt_init(void);
495 extern void disableSharedCacheOptimizations(void);
496 extern bool isPreoptimized(void);
497 extern bool noMissingWeakSuperclasses(void);
498 extern header_info
*preoptimizedHinfoForHeader(const headerType
*mhdr
);
500 extern objc_selopt_t
*preoptimizedSelectors(void);
502 extern bool sharedCacheSupportsProtocolRoots(void);
503 extern Protocol
*getPreoptimizedProtocol(const char *name
);
504 extern Protocol
*getSharedCachePreoptimizedProtocol(const char *name
);
506 extern unsigned getPreoptimizedClassUnreasonableCount();
507 extern Class
getPreoptimizedClass(const char *name
);
508 extern Class
* copyPreoptimizedClasses(const char *name
, int *outCount
);
510 extern Class
_calloc_class(size_t size
);
514 LOOKUP_INITIALIZE
= 1,
519 extern IMP
lookUpImpOrForward(id obj
, SEL
, Class cls
, int behavior
);
522 lookUpImpOrNil(id obj
, SEL sel
, Class cls
, int behavior
= 0)
524 return lookUpImpOrForward(obj
, sel
, cls
, behavior
| LOOKUP_CACHE
| LOOKUP_NIL
);
527 extern IMP
lookupMethodInClassAndLoadCache(Class cls
, SEL sel
);
528 extern BOOL
class_respondsToSelector_inst(id inst
, SEL sel
, Class cls
);
529 extern Class
class_initialize(Class cls
, id inst
);
531 extern bool objcMsgLogEnabled
;
532 extern bool logMessageSend(bool isClassMethod
,
533 const char *objectsClass
,
534 const char *implementingClass
,
537 /* message dispatcher */
539 #if !OBJC_OLD_DISPATCH_PROTOTYPES
540 extern void _objc_msgForward_impcache(void);
542 extern id
_objc_msgForward_impcache(id
, SEL
, ...);
546 extern id(*badAllocHandler
)(Class
);
547 extern id
_objc_callBadAllocHandler(Class cls
) __attribute__((cold
, noinline
));
548 extern void __objc_error(id
, const char *, ...) __attribute__((cold
, format (printf
, 2, 3), noreturn
));
549 extern void _objc_inform(const char *fmt
, ...) __attribute__((cold
, format(printf
, 1, 2)));
550 extern void _objc_inform_on_crash(const char *fmt
, ...) __attribute__((cold
, format (printf
, 1, 2)));
551 extern void _objc_inform_now_and_on_crash(const char *fmt
, ...) __attribute__((cold
, format (printf
, 1, 2)));
552 extern void _objc_inform_deprecated(const char *oldname
, const char *newname
) __attribute__((cold
, noinline
));
553 extern void inform_duplicate(const char *name
, Class oldCls
, Class cls
);
556 extern Class
_objc_getFreedObjectClass (void);
558 /* map table additions */
559 extern void *NXMapKeyCopyingInsert(NXMapTable
*table
, const void *key
, const void *value
);
560 extern void *NXMapKeyFreeingRemove(NXMapTable
*table
, const void *key
);
562 /* hash table additions */
563 extern unsigned _NXHashCapacity(NXHashTable
*table
);
564 extern void _NXHashRehashToCapacity(NXHashTable
*table
, unsigned newCapacity
);
566 /* property attribute parsing */
567 extern const char *copyPropertyAttributeString(const objc_property_attribute_t
*attrs
, unsigned int count
);
568 extern objc_property_attribute_t
*copyPropertyAttributeList(const char *attrs
, unsigned int *outCount
);
569 extern char *copyPropertyAttributeValue(const char *attrs
, const char *name
);
573 class monitor_locker_t
: nocopy_t
{
576 monitor_locker_t(monitor_t
& newLock
) : lock(newLock
) { lock
.enter(); }
577 ~monitor_locker_t() { lock
.leave(); }
580 class recursive_mutex_locker_t
: nocopy_t
{
581 recursive_mutex_t
& lock
;
583 recursive_mutex_locker_t(recursive_mutex_t
& newLock
)
584 : lock(newLock
) { lock
.lock(); }
585 ~recursive_mutex_locker_t() { lock
.unlock(); }
590 struct alt_handler_list
;
591 extern void exception_init(void);
592 extern void _destroyAltHandlerList(struct alt_handler_list
*list
);
594 /* Class change notifications (gdb only for now) */
595 #define OBJC_CLASS_ADDED (1<<0)
596 #define OBJC_CLASS_REMOVED (1<<1)
597 #define OBJC_CLASS_IVARS_CHANGED (1<<2)
598 #define OBJC_CLASS_METHODS_CHANGED (1<<3)
599 extern void gdb_objc_class_changed(Class cls
, unsigned long changes
, const char *classname
)
600 __attribute__((noinline
));
603 // Settings from environment variables
604 #define OPTION(var, env, help) extern bool var;
605 #include "objc-env.h"
608 extern void environ_init(void);
609 extern void runtime_init(void);
611 extern void logReplacedMethod(const char *className
, SEL s
, bool isMeta
, const char *catName
, IMP oldImp
, IMP newImp
);
614 // objc per-thread storage
616 struct _objc_initializing_classes
*initializingClasses
; // for +initialize
617 struct SyncCache
*syncCache
; // for @synchronize
618 struct alt_handler_list
*handlerList
; // for exception alt handlers
619 char *printableNames
[4]; // temporary demangled names for logging
620 const char **classNameLookups
; // for objc_getClass() hooks
621 unsigned classNameLookupsAllocated
;
622 unsigned classNameLookupsUsed
;
624 // If you add new fields here, don't forget to update
625 // _objc_pthread_destroyspecific()
627 } _objc_pthread_data
;
629 extern _objc_pthread_data
*_objc_fetch_pthread_data(bool create
);
630 extern void tls_init(void);
633 extern unsigned int encoding_getNumberOfArguments(const char *typedesc
);
634 extern unsigned int encoding_getSizeOfArguments(const char *typedesc
);
635 extern unsigned int encoding_getArgumentInfo(const char *typedesc
, unsigned int arg
, const char **type
, int *offset
);
636 extern void encoding_getReturnType(const char *t
, char *dst
, size_t dst_len
);
637 extern char * encoding_copyReturnType(const char *t
);
638 extern void encoding_getArgumentType(const char *t
, unsigned int index
, char *dst
, size_t dst_len
);
639 extern char *encoding_copyArgumentType(const char *t
, unsigned int index
);
642 extern void _destroySyncCache(struct SyncCache
*cache
);
645 extern void arr_init(void);
646 extern id
objc_autoreleaseReturnValue(id obj
);
649 extern void _imp_implementationWithBlock_init(void);
650 extern IMP
_imp_implementationWithBlockNoCopy(id block
);
656 size_t bitsAllocated
;
659 extern layout_bitmap
layout_bitmap_create(const unsigned char *layout_string
, size_t layoutStringInstanceSize
, size_t instanceSize
, bool weak
);
660 extern layout_bitmap
layout_bitmap_create_empty(size_t instanceSize
, bool weak
);
661 extern void layout_bitmap_free(layout_bitmap bits
);
662 extern const unsigned char *layout_string_create(layout_bitmap bits
);
663 extern void layout_bitmap_set_ivar(layout_bitmap bits
, const char *type
, size_t offset
);
664 extern void layout_bitmap_grow(layout_bitmap
*bits
, size_t newCount
);
665 extern void layout_bitmap_slide(layout_bitmap
*bits
, size_t oldPos
, size_t newPos
);
666 extern void layout_bitmap_slide_anywhere(layout_bitmap
*bits
, size_t oldPos
, size_t newPos
);
667 extern bool layout_bitmap_splat(layout_bitmap dst
, layout_bitmap src
,
668 size_t oldSrcInstanceSize
);
669 extern bool layout_bitmap_or(layout_bitmap dst
, layout_bitmap src
, const char *msg
);
670 extern bool layout_bitmap_clear(layout_bitmap dst
, layout_bitmap src
, const char *msg
);
671 extern void layout_bitmap_print(layout_bitmap bits
);
675 extern bool MultithreadedForkChild
;
676 extern id
objc_noop_imp(id self
, SEL _cmd
);
677 extern Class
look_up_class(const char *aClassName
, bool includeUnconnected
, bool includeClassHandler
);
678 extern "C" void map_images(unsigned count
, const char * const paths
[],
679 const struct mach_header
* const mhdrs
[]);
680 extern void map_images_nolock(unsigned count
, const char * const paths
[],
681 const struct mach_header
* const mhdrs
[]);
682 extern void load_images(const char *path
, const struct mach_header
*mh
);
683 extern void unmap_image(const char *path
, const struct mach_header
*mh
);
684 extern void unmap_image_nolock(const struct mach_header
*mh
);
685 extern void _read_images(header_info
**hList
, uint32_t hCount
, int totalClasses
, int unoptimizedTotalClass
);
686 extern void _unload_image(header_info
*hi
);
688 extern const header_info
*_headerForClass(Class cls
);
690 extern Class
_class_remap(Class cls
);
691 extern Ivar
_class_getVariable(Class cls
, const char *name
);
693 extern unsigned _class_createInstancesFromZone(Class cls
, size_t extraBytes
, void *zone
, id
*results
, unsigned num_requested
);
695 extern const char *_category_getName(Category cat
);
696 extern const char *_category_getClassName(Category cat
);
697 extern Class
_category_getClass(Category cat
);
698 extern IMP
_category_getLoadMethod(Category cat
);
701 OBJECT_CONSTRUCT_NONE
= 0,
702 OBJECT_CONSTRUCT_FREE_ONFAILURE
= 1,
703 OBJECT_CONSTRUCT_CALL_BADALLOC
= 2,
705 extern id
object_cxxConstructFromClass(id obj
, Class cls
, int flags
);
706 extern void object_cxxDestruct(id obj
);
708 extern void fixupCopiedIvars(id newObject
, id oldObject
);
709 extern Class
_class_getClassForIvar(Class cls
, Ivar ivar
);
712 #define OBJC_WARN_DEPRECATED \
714 static int warned = 0; \
717 _objc_inform_deprecated(__FUNCTION__, NULL); \
724 #ifndef STATIC_ASSERT
725 # define STATIC_ASSERT(x) _STATIC_ASSERT2(x, __LINE__)
726 # define _STATIC_ASSERT2(x, line) _STATIC_ASSERT3(x, line)
727 # define _STATIC_ASSERT3(x, line) \
729 int _static_assert[(x) ? 0 : -1]; \
730 } _static_assert_ ## line __attribute__((unavailable))
733 #define countof(arr) (sizeof(arr) / sizeof((arr)[0]))
736 static __inline
uint32_t _objc_strhash(const char *s
) {
741 hash
+= (hash
<< 8) + a
;
748 template <typename T
>
749 static inline T
log2u(T x
) {
750 return (x
<2) ? 0 : log2u(x
>>1)+1;
753 template <typename T
>
754 static inline T
exp2u(T x
) {
758 template <typename T
>
759 static T
exp2m1u(T x
) {
765 // Misalignment-safe integer types
766 __attribute__((aligned(1))) typedef uintptr_t unaligned_uintptr_t
;
767 __attribute__((aligned(1))) typedef intptr_t unaligned_intptr_t
;
768 __attribute__((aligned(1))) typedef uint64_t unaligned_uint64_t
;
769 __attribute__((aligned(1))) typedef int64_t unaligned_int64_t
;
770 __attribute__((aligned(1))) typedef uint32_t unaligned_uint32_t
;
771 __attribute__((aligned(1))) typedef int32_t unaligned_int32_t
;
772 __attribute__((aligned(1))) typedef uint16_t unaligned_uint16_t
;
773 __attribute__((aligned(1))) typedef int16_t unaligned_int16_t
;
776 // Global operator new and delete. We must not use any app overrides.
777 // This ALSO REQUIRES each of these be in libobjc's unexported symbol list.
779 #pragma clang diagnostic push
780 #pragma clang diagnostic ignored "-Winline-new-delete"
782 inline void* operator new(std::size_t size
) throw (std::bad_alloc
) { return malloc(size
); }
783 inline void* operator new[](std::size_t size
) throw (std::bad_alloc
) { return malloc(size
); }
784 inline void* operator new(std::size_t size
, const std::nothrow_t
&) throw() { return malloc(size
); }
785 inline void* operator new[](std::size_t size
, const std::nothrow_t
&) throw() { return malloc(size
); }
786 inline void operator delete(void* p
) throw() { free(p
); }
787 inline void operator delete[](void* p
) throw() { free(p
); }
788 inline void operator delete(void* p
, const std::nothrow_t
&) throw() { free(p
); }
789 inline void operator delete[](void* p
, const std::nothrow_t
&) throw() { free(p
); }
790 #pragma clang diagnostic pop
798 TimeLogger(bool record
= true)
799 : mStart(nanoseconds())
803 void log(const char *msg
) {
805 uint64_t end
= nanoseconds();
806 _objc_inform("%.2f ms: %s", (end
- mStart
) / 1000000.0, msg
);
807 mStart
= nanoseconds();
812 enum { CacheLineSize
= 64 };
814 // StripedMap<T> is a map of void* -> T, sized appropriately
815 // for cache-friendly lock striping.
816 // For example, this may be used as StripedMap<spinlock_t>
817 // or as StripedMap<SomeStruct> where SomeStruct stores a spin lock.
820 #if TARGET_OS_IPHONE && !TARGET_OS_SIMULATOR
821 enum { StripeCount
= 8 };
823 enum { StripeCount
= 64 };
827 T value
alignas(CacheLineSize
);
830 PaddedT array
[StripeCount
];
832 static unsigned int indexForPointer(const void *p
) {
833 uintptr_t addr
= reinterpret_cast<uintptr_t>(p
);
834 return ((addr
>> 4) ^ (addr
>> 9)) % StripeCount
;
838 T
& operator[] (const void *p
) {
839 return array
[indexForPointer(p
)].value
;
841 const T
& operator[] (const void *p
) const {
842 return const_cast<StripedMap
<T
>>(this)[p
];
845 // Shortcuts for StripedMaps of locks.
847 for (unsigned int i
= 0; i
< StripeCount
; i
++) {
848 array
[i
].value
.lock();
853 for (unsigned int i
= 0; i
< StripeCount
; i
++) {
854 array
[i
].value
.unlock();
858 void forceResetAll() {
859 for (unsigned int i
= 0; i
< StripeCount
; i
++) {
860 array
[i
].value
.forceReset();
864 void defineLockOrder() {
865 for (unsigned int i
= 1; i
< StripeCount
; i
++) {
866 lockdebug_lock_precedes_lock(&array
[i
-1].value
, &array
[i
].value
);
870 void precedeLock(const void *newlock
) {
871 // assumes defineLockOrder is also called
872 lockdebug_lock_precedes_lock(&array
[StripeCount
-1].value
, newlock
);
875 void succeedLock(const void *oldlock
) {
876 // assumes defineLockOrder is also called
877 lockdebug_lock_precedes_lock(oldlock
, &array
[0].value
);
880 const void *getLock(int i
) {
881 if (i
< StripeCount
) return &array
[i
].value
;
887 // Verify alignment expectations.
888 uintptr_t base
= (uintptr_t)&array
[0].value
;
889 uintptr_t delta
= (uintptr_t)&array
[1].value
- base
;
890 ASSERT(delta
% CacheLineSize
== 0);
891 ASSERT(base
% CacheLineSize
== 0);
894 constexpr StripedMap() {}
899 // DisguisedPtr<T> acts like pointer type T*, except the
900 // stored value is disguised to hide it from tools like `leaks`.
901 // nil is disguised as itself so zero-filled memory works as expected,
902 // which means 0x80..00 is also disguised as itself but we don't care.
903 // Note that weak_entry_t knows about this encoding.
904 template <typename T
>
908 static uintptr_t disguise(T
* ptr
) {
909 return -(uintptr_t)ptr
;
912 static T
* undisguise(uintptr_t val
) {
919 : value(disguise(ptr
)) { }
920 DisguisedPtr(const DisguisedPtr
<T
>& ptr
)
921 : value(ptr
.value
) { }
923 DisguisedPtr
<T
>& operator = (T
* rhs
) {
924 value
= disguise(rhs
);
927 DisguisedPtr
<T
>& operator = (const DisguisedPtr
<T
>& rhs
) {
932 operator T
* () const {
933 return undisguise(value
);
935 T
* operator -> () const {
936 return undisguise(value
);
938 T
& operator * () const {
939 return *undisguise(value
);
941 T
& operator [] (size_t i
) const {
942 return undisguise(value
)[i
];
945 // pointer arithmetic operators omitted
946 // because we don't currently use them anywhere
949 // fixme type id is weird and not identical to objc_object*
950 static inline bool operator == (DisguisedPtr
<objc_object
> lhs
, id rhs
) {
951 return lhs
== (objc_object
*)rhs
;
953 static inline bool operator != (DisguisedPtr
<objc_object
> lhs
, id rhs
) {
954 return lhs
!= (objc_object
*)rhs
;
958 // Storage for a thread-safe chained hook function.
959 // get() returns the value for calling.
960 // set() installs a new function and returns the old one for chaining.
961 // More precisely, set() writes the old value to a variable supplied by
962 // the caller. get() and set() use appropriate barriers so that the
963 // old value is safely written to the variable before the new value is
966 // T1: store to old variable; store-release to hook variable
967 // T2: load-acquire from hook variable; call it; called hook loads old variable
969 template <typename Fn
>
970 class ChainedHookFunction
{
971 std::atomic
<Fn
> hook
{nil
};
974 ChainedHookFunction(Fn f
) : hook
{f
} { };
977 return hook
.load(std::memory_order_acquire
);
980 void set(Fn newValue
, Fn
*oldVariable
)
982 Fn oldValue
= hook
.load(std::memory_order_relaxed
);
984 *oldVariable
= oldValue
;
985 } while (!hook
.compare_exchange_weak(oldValue
, newValue
,
986 std::memory_order_release
,
987 std::memory_order_relaxed
));
992 // A small vector for use as a global variable. Only supports appending and
993 // iteration. Stores a single element inline, and multiple elements in a heap
994 // allocation. There is no attempt to amortize reallocation cost; this is
995 // intended to be used in situation where zero or one element is common, two
996 // might happen, and three or more is very rare.
998 // This does not clean up its allocation, and thus cannot be used as a local
999 // variable or member of something with limited lifetime.
1001 template <typename T
, unsigned InlineCount
>
1002 class GlobalSmallVector
{
1003 static_assert(std::is_pod
<T
>::value
, "SmallVector requires POD types");
1008 T inlineElements
[InlineCount
];
1013 void append(const T
&val
) {
1014 if (count
< InlineCount
) {
1015 // We have space. Store the new value inline.
1016 inlineElements
[count
] = val
;
1017 } else if (count
== InlineCount
) {
1018 // Inline storage is full. Switch to a heap allocation.
1019 T
*newElements
= (T
*)malloc((count
+ 1) * sizeof(T
));
1020 memcpy(newElements
, inlineElements
, count
* sizeof(T
));
1021 newElements
[count
] = val
;
1022 elements
= newElements
;
1024 // Resize the heap allocation and append.
1025 elements
= (T
*)realloc(elements
, (count
+ 1) * sizeof(T
));
1026 elements
[count
] = val
;
1031 const T
*begin() const {
1032 return count
<= InlineCount
? inlineElements
: elements
;
1035 const T
*end() const {
1036 return begin() + count
;
1040 // A small vector that cleans up its internal memory allocation when destroyed.
1041 template <typename T
, unsigned InlineCount
>
1042 class SmallVector
: public GlobalSmallVector
<T
, InlineCount
> {
1045 if (this->count
> InlineCount
)
1046 free(this->elements
);
1049 template <unsigned OtherCount
>
1050 void initFrom(const GlobalSmallVector
<T
, OtherCount
> &other
) {
1051 ASSERT(this->count
== 0);
1052 this->count
= (unsigned)(other
.end() - other
.begin());
1053 if (this->count
> InlineCount
) {
1054 this->elements
= (T
*)memdup(other
.begin(), this->count
* sizeof(T
));
1056 memcpy(this->inlineElements
, other
.begin(), this->count
* sizeof(T
));
1061 // Pointer hash function.
1062 // This is not a terrific hash, but it is fast
1063 // and not outrageously flawed for our purposes.
1065 // Based on principles from http://locklessinc.com/articles/fast_hash/
1066 // and evaluation ideas from http://floodyberry.com/noncryptohashzoo/
1068 static inline uint32_t ptr_hash(uint64_t key
)
1071 key
*= 0x8a970be7488fda55;
1072 key
^= __builtin_bswap64(key
);
1073 return (uint32_t)key
;
1076 static inline uint32_t ptr_hash(uint32_t key
)
1080 key
^= __builtin_bswap32(key
);
1086 Higher-quality hash function. This is measurably slower in some workloads.
1088 uint32_t ptr_hash(uint64_t key)
1090 key -= __builtin_bswap64(key);
1091 key *= 0x8a970be7488fda55;
1092 key ^= __builtin_bswap64(key);
1093 key *= 0x8a970be7488fda55;
1094 key ^= __builtin_bswap64(key);
1095 return (uint32_t)key;
1098 static uint32_t ptr_hash(uint32_t key)
1100 key -= __builtin_bswap32(key);
1102 key ^= __builtin_bswap32(key);
1104 key ^= __builtin_bswap32(key);
1112 // Lock declarations
1113 #include "objc-locks.h"
1115 // Inlined parts of objc_object's implementation
1116 #include "objc-object.h"
1118 #endif /* _OBJC_PRIVATE_H_ */