]> git.saurik.com Git - apple/objc4.git/blob - test/rr-autorelease2.m
objc4-680.tar.gz
[apple/objc4.git] / test / rr-autorelease2.m
1 // Define FOUNDATION=1 for NSObject and NSAutoreleasePool
2 // Define FOUNDATION=0 for _objc_root* and _objc_autoreleasePool*
3
4 #include "test.h"
5
6 #if FOUNDATION
7 # define RR_PUSH() [[NSAutoreleasePool alloc] init]
8 # define RR_POP(p) [(id)p release]
9 # define RR_RETAIN(o) [o retain]
10 # define RR_RELEASE(o) [o release]
11 # define RR_AUTORELEASE(o) [o autorelease]
12 # define RR_RETAINCOUNT(o) [o retainCount]
13 #else
14 # define RR_PUSH() _objc_autoreleasePoolPush()
15 # define RR_POP(p) _objc_autoreleasePoolPop(p)
16 # define RR_RETAIN(o) _objc_rootRetain((id)o)
17 # define RR_RELEASE(o) _objc_rootRelease((id)o)
18 # define RR_AUTORELEASE(o) _objc_rootAutorelease((id)o)
19 # define RR_RETAINCOUNT(o) _objc_rootRetainCount((id)o)
20 #endif
21
22 #include <objc/objc-internal.h>
23 #include <Foundation/Foundation.h>
24
25 static int state;
26 static pthread_attr_t smallstack;
27
28 #define NESTED_COUNT 8
29
30 @interface Deallocator : NSObject @end
31 @implementation Deallocator
32 -(void) dealloc
33 {
34 // testprintf("-[Deallocator %p dealloc]\n", self);
35 state++;
36 [super dealloc];
37 }
38 @end
39
40 @interface AutoreleaseDuringDealloc : NSObject @end
41 @implementation AutoreleaseDuringDealloc
42 -(void) dealloc
43 {
44 state++;
45 RR_AUTORELEASE([[Deallocator alloc] init]);
46 [super dealloc];
47 }
48 @end
49
50 @interface AutoreleasePoolDuringDealloc : NSObject @end
51 @implementation AutoreleasePoolDuringDealloc
52 -(void) dealloc
53 {
54 // caller's pool
55 for (int i = 0; i < NESTED_COUNT; i++) {
56 RR_AUTORELEASE([[Deallocator alloc] init]);
57 }
58
59 // local pool, popped
60 void *pool = RR_PUSH();
61 for (int i = 0; i < NESTED_COUNT; i++) {
62 RR_AUTORELEASE([[Deallocator alloc] init]);
63 }
64 RR_POP(pool);
65
66 // caller's pool again
67 for (int i = 0; i < NESTED_COUNT; i++) {
68 RR_AUTORELEASE([[Deallocator alloc] init]);
69 }
70
71 #if FOUNDATION
72 {
73 static bool warned;
74 if (!warned) testwarn("rdar://7138159 NSAutoreleasePool leaks");
75 warned = true;
76 }
77 state += NESTED_COUNT;
78 #else
79 // local pool, not popped
80 RR_PUSH();
81 for (int i = 0; i < NESTED_COUNT; i++) {
82 RR_AUTORELEASE([[Deallocator alloc] init]);
83 }
84 #endif
85
86 [super dealloc];
87 }
88 @end
89
90 void *autorelease_lots_fn(void *singlePool)
91 {
92 // Enough to blow out the stack if AutoreleasePoolPage is recursive.
93 const int COUNT = 1024*1024;
94 state = 0;
95
96 int p = 0;
97 void **pools = (void**)malloc((COUNT+1) * sizeof(void*));
98 pools[p++] = RR_PUSH();
99
100 id obj = RR_AUTORELEASE([[Deallocator alloc] init]);
101
102 // last pool has only 1 autorelease in it
103 pools[p++] = RR_PUSH();
104
105 for (int i = 0; i < COUNT; i++) {
106 if (rand() % 1000 == 0 && !singlePool) {
107 pools[p++] = RR_PUSH();
108 } else {
109 RR_AUTORELEASE(RR_RETAIN(obj));
110 }
111 }
112
113 testassert(state == 0);
114 while (--p) {
115 RR_POP(pools[p]);
116 }
117 testassert(state == 0);
118 testassert(RR_RETAINCOUNT(obj) == 1);
119 RR_POP(pools[0]);
120 testassert(state == 1);
121 free(pools);
122
123 return NULL;
124 }
125
126 void *nsthread_fn(void *arg __unused)
127 {
128 [NSThread currentThread];
129 void *pool = RR_PUSH();
130 RR_AUTORELEASE([[Deallocator alloc] init]);
131 RR_POP(pool);
132 return NULL;
133 }
134
135 void cycle(void)
136 {
137 // Normal autorelease.
138 testprintf("-- Normal autorelease.\n");
139 {
140 void *pool = RR_PUSH();
141 state = 0;
142 RR_AUTORELEASE([[Deallocator alloc] init]);
143 testassert(state == 0);
144 RR_POP(pool);
145 testassert(state == 1);
146 }
147
148 // Autorelease during dealloc during autoreleasepool-pop.
149 // That autorelease is handled by the popping pool, not the one above it.
150 testprintf("-- Autorelease during dealloc during autoreleasepool-pop.\n");
151 {
152 void *pool = RR_PUSH();
153 state = 0;
154 RR_AUTORELEASE([[AutoreleaseDuringDealloc alloc] init]);
155 testassert(state == 0);
156 RR_POP(pool);
157 testassert(state == 2);
158 }
159
160 // Autorelease pool during dealloc during autoreleasepool-pop.
161 testprintf("-- Autorelease pool during dealloc during autoreleasepool-pop.\n");
162 {
163 void *pool = RR_PUSH();
164 state = 0;
165 RR_AUTORELEASE([[AutoreleasePoolDuringDealloc alloc] init]);
166 testassert(state == 0);
167 RR_POP(pool);
168 testassert(state == 4 * NESTED_COUNT);
169 }
170
171 // Top-level thread pool popped normally.
172 testprintf("-- Thread-level pool popped normally.\n");
173 {
174 state = 0;
175 testonthread(^{
176 void *pool = RR_PUSH();
177 RR_AUTORELEASE([[Deallocator alloc] init]);
178 RR_POP(pool);
179 });
180 testassert(state == 1);
181 }
182
183
184 // Autorelease with no pool.
185 testprintf("-- Autorelease with no pool.\n");
186 {
187 state = 0;
188 testonthread(^{
189 RR_AUTORELEASE([[Deallocator alloc] init]);
190 });
191 testassert(state == 1);
192 }
193
194 // Autorelease with no pool after popping the top-level pool.
195 testprintf("-- Autorelease with no pool after popping the last pool.\n");
196 {
197 state = 0;
198 testonthread(^{
199 void *pool = RR_PUSH();
200 RR_AUTORELEASE([[Deallocator alloc] init]);
201 RR_POP(pool);
202 RR_AUTORELEASE([[Deallocator alloc] init]);
203 });
204 testassert(state == 2);
205 }
206
207 // Top-level thread pool not popped.
208 // The runtime should clean it up.
209 #if FOUNDATION
210 {
211 static bool warned;
212 if (!warned) testwarn("rdar://7138159 NSAutoreleasePool leaks");
213 warned = true;
214 }
215 #else
216 testprintf("-- Thread-level pool not popped.\n");
217 {
218 state = 0;
219 testonthread(^{
220 RR_PUSH();
221 RR_AUTORELEASE([[Deallocator alloc] init]);
222 // pool not popped
223 });
224 testassert(state == 1);
225 }
226 #endif
227
228 // Intermediate pool not popped.
229 // Popping the containing pool should clean up the skipped pool first.
230 #if FOUNDATION
231 {
232 static bool warned;
233 if (!warned) testwarn("rdar://7138159 NSAutoreleasePool leaks");
234 warned = true;
235 }
236 #else
237 testprintf("-- Intermediate pool not popped.\n");
238 {
239 void *pool = RR_PUSH();
240 void *pool2 = RR_PUSH();
241 RR_AUTORELEASE([[Deallocator alloc] init]);
242 state = 0;
243 (void)pool2; // pool2 not popped
244 RR_POP(pool);
245 testassert(state == 1);
246 }
247 #endif
248 }
249
250
251 static void
252 slow_cycle(void)
253 {
254 // Large autorelease stack.
255 // Do this only once because it's slow.
256 testprintf("-- Large autorelease stack.\n");
257 {
258 // limit stack size: autorelease pop should not be recursive
259 pthread_t th;
260 pthread_create(&th, &smallstack, &autorelease_lots_fn, NULL);
261 pthread_join(th, NULL);
262 }
263
264 // Single large autorelease pool.
265 // Do this only once because it's slow.
266 testprintf("-- Large autorelease pool.\n");
267 {
268 // limit stack size: autorelease pop should not be recursive
269 pthread_t th;
270 pthread_create(&th, &smallstack, &autorelease_lots_fn, (void*)1);
271 pthread_join(th, NULL);
272 }
273 }
274
275
276 int main()
277 {
278 pthread_attr_init(&smallstack);
279 pthread_attr_setstacksize(&smallstack, 16384);
280
281 // inflate the refcount side table so it doesn't show up in leak checks
282 {
283 int count = 10000;
284 id *objs = (id *)malloc(count*sizeof(id));
285 for (int i = 0; i < count; i++) {
286 objs[i] = RR_RETAIN([NSObject new]);
287 }
288 for (int i = 0; i < count; i++) {
289 RR_RELEASE(objs[i]);
290 RR_RELEASE(objs[i]);
291 }
292 free(objs);
293 }
294
295 #if FOUNDATION
296 // inflate NSAutoreleasePool's instance cache
297 {
298 int count = 32;
299 id *objs = (id *)malloc(count * sizeof(id));
300 for (int i = 0; i < count; i++) {
301 objs[i] = [[NSAutoreleasePool alloc] init];
302 }
303 for (int i = 0; i < count; i++) {
304 [objs[count-i-1] release];
305 }
306
307 free(objs);
308 }
309 #endif
310
311 // preheat
312 {
313 for (int i = 0; i < 100; i++) {
314 cycle();
315 }
316
317 slow_cycle();
318 }
319
320 // check for leaks using top-level pools
321 {
322 leak_mark();
323
324 for (int i = 0; i < 1000; i++) {
325 cycle();
326 }
327
328 leak_check(0);
329
330 slow_cycle();
331
332 leak_check(0);
333 }
334
335 // check for leaks using pools not at top level
336 void *pool = RR_PUSH();
337 {
338 leak_mark();
339
340 for (int i = 0; i < 1000; i++) {
341 cycle();
342 }
343
344 leak_check(0);
345
346 slow_cycle();
347
348 leak_check(0);
349 }
350 RR_POP(pool);
351
352 // NSThread.
353 // Can't leak check this because it's too noisy.
354 testprintf("-- NSThread.\n");
355 {
356 pthread_t th;
357 pthread_create(&th, &smallstack, &nsthread_fn, 0);
358 pthread_join(th, NULL);
359 }
360
361 // NO LEAK CHECK HERE
362
363 succeed(NAME);
364 }