]>
git.saurik.com Git - apple/objc4.git/blob - runtime/NSObject-internal.h
2 * Copyright (c) 2019 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@
24 #ifndef _NSOBJECT_INTERNAL_H
25 #define _NSOBJECT_INTERNAL_H
28 * WARNING DANGER HAZARD BEWARE EEK
30 * Everything in this file is for Apple Internal use only.
31 * These will change in arbitrary OS updates and in unpredictable ways.
32 * When your program breaks, you get to keep both pieces.
36 * NSObject-internal.h: Private SPI for use by other system frameworks.
39 /***********************************************************************
40 Autorelease pool implementation
42 A thread's autorelease pool is a stack of pointers.
43 Each pointer is either an object to release, or POOL_BOUNDARY which is
44 an autorelease pool boundary.
45 A pool token is a pointer to the POOL_BOUNDARY for that pool. When
46 the pool is popped, every object hotter than the sentinel is released.
47 The stack is divided into a doubly-linked list of pages. Pages are added
48 and deleted as necessary.
49 Thread-local storage points to the hot page, where newly autoreleased
51 **********************************************************************/
53 // structure version number. Only bump if ABI compatability is broken
54 #define AUTORELEASEPOOL_VERSION 1
56 // Set this to 1 to mprotect() autorelease pool contents
57 #define PROTECT_AUTORELEASEPOOL 0
59 // Set this to 1 to validate the entire autorelease pool header all the time
60 // (i.e. use check() instead of fastcheck() everywhere)
61 #define CHECK_AUTORELEASEPOOL (DEBUG)
66 #include <objc/objc.h>
71 #if __has_feature(cxx_static_assert)
72 #define C_ASSERT(expr) static_assert(expr, "(" #expr ")!")
73 #elif __has_feature(c_static_assert)
74 #define C_ASSERT(expr) _Static_assert(expr, "(" #expr ")!")
76 #define C_ASSERT(expr)
80 // Make ASSERT work when objc-private.h hasn't been included.
82 #define ASSERT(x) assert(x)
86 static const uint32_t M0
= 0xA1A1A1A1;
87 # define M1 "AUTORELEASE!"
88 static const size_t M1_len
= 12;
92 ASSERT(M1_len
== strlen(M1
));
93 ASSERT(M1_len
== 3 * sizeof(m
[1]));
96 strncpy((char *)&m
[1], M1
, M1_len
);
100 // Clear magic before deallocation.
101 // This prevents some false positives in memory debugging tools.
102 // fixme semantically this should be memset_s(), but the
103 // compiler doesn't optimize that at all (rdar://44856676).
104 volatile uint64_t *p
= (volatile uint64_t *)m
;
109 return (m
[0] == M0
&& 0 == strncmp((char *)&m
[1], M1
, M1_len
));
112 bool fastcheck() const {
113 #if CHECK_AUTORELEASEPOOL
123 class AutoreleasePoolPage
;
124 struct AutoreleasePoolPageData
126 #if SUPPORT_AUTORELEASEPOOL_DEDUP_PTRS
127 struct AutoreleasePoolEntry
{
131 static const uintptr_t maxCount
= 65535; // 2^16 - 1
133 static_assert((AutoreleasePoolEntry
){ .ptr
= MACH_VM_MAX_ADDRESS
}.ptr
== MACH_VM_MAX_ADDRESS
, "MACH_VM_MAX_ADDRESS doesn't fit into AutoreleasePoolEntry::ptr!");
137 __unsafe_unretained id
*next
;
138 pthread_t
const thread
;
139 AutoreleasePoolPage
* const parent
;
140 AutoreleasePoolPage
*child
;
141 uint32_t const depth
;
144 AutoreleasePoolPageData(__unsafe_unretained id
* _next
, pthread_t _thread
, AutoreleasePoolPage
* _parent
, uint32_t _depth
, uint32_t _hiwat
)
145 : magic(), next(_next
), thread(_thread
),
146 parent(_parent
), child(nil
),
147 depth(_depth
), hiwat(_hiwat
)
156 pthread_t
const thread
;
157 uint32_t const hiwat
;
158 uint32_t const depth
;
160 pthread_t
const thread
;
161 uint32_t const hiwat
;
162 uint32_t const depth
;
166 C_ASSERT(sizeof(thread_data_t
) == 16);