]> git.saurik.com Git - apple/objc4.git/blob - test/bigrc.m
objc4-680.tar.gz
[apple/objc4.git] / test / bigrc.m
1 // TEST_CONFIG MEM=mrc
2 /*
3 TEST_RUN_OUTPUT
4 objc\[\d+\]: Deallocator object 0x[0-9a-fA-F]+ overreleased while already deallocating; break on objc_overrelease_during_dealloc_error to debug
5 OK: bigrc.m
6 OR
7 no overrelease enforcement
8 OK: bigrc.m
9 END
10 */
11
12 #include "test.h"
13 #include "testroot.i"
14
15 static size_t LOTS;
16
17 @interface Deallocator : TestRoot @end
18 @implementation Deallocator
19
20 -(void)dealloc
21 {
22 id o = self;
23 size_t rc = 1;
24
25
26 testprintf("Retain a lot during dealloc\n");
27
28 testassert(rc == 1);
29 testassert([o retainCount] == rc);
30 do {
31 [o retain];
32 if (rc % 0x100000 == 0) testprintf("%zx/%zx ++\n", rc, LOTS);
33 } while (++rc < LOTS);
34
35 testassert([o retainCount] == rc);
36
37 do {
38 [o release];
39 if (rc % 0x100000 == 0) testprintf("%zx/%zx --\n", rc, LOTS);
40 } while (--rc > 1);
41
42 testassert(rc == 1);
43 testassert([o retainCount] == rc);
44
45
46 testprintf("Overrelease during dealloc\n");
47
48 // Not all architectures enforce this.
49 #if !SUPPORT_NONPOINTER_ISA
50 testwarn("no overrelease enforcement");
51 fprintf(stderr, "no overrelease enforcement\n");
52 #endif
53 [o release];
54
55 [super dealloc];
56 }
57
58 @end
59
60
61 int main()
62 {
63 Deallocator *o = [Deallocator new];
64 size_t rc = 1;
65
66 [o retain];
67
68 uintptr_t isa = *(uintptr_t *)o;
69 if (isa & 1) {
70 // Assume refcount in high bits.
71 LOTS = 1 << (4 + __builtin_clzll(isa));
72 testprintf("LOTS %zu via cntlzw\n", LOTS);
73 } else {
74 LOTS = 0x1000000;
75 testprintf("LOTS %zu via guess\n", LOTS);
76 }
77
78 [o release];
79
80
81 testprintf("Retain a lot\n");
82
83 testassert(rc == 1);
84 testassert([o retainCount] == rc);
85 do {
86 [o retain];
87 if (rc % 0x100000 == 0) testprintf("%zx/%zx ++\n", rc, LOTS);
88 } while (++rc < LOTS);
89
90 testassert([o retainCount] == rc);
91
92 do {
93 [o release];
94 if (rc % 0x100000 == 0) testprintf("%zx/%zx --\n", rc, LOTS);
95 } while (--rc > 1);
96
97 testassert(rc == 1);
98 testassert([o retainCount] == rc);
99
100
101 testprintf("tryRetain a lot\n");
102
103 id w;
104 objc_storeWeak(&w, o);
105 testassert(w == o);
106
107 testassert(rc == 1);
108 testassert([o retainCount] == rc);
109 do {
110 objc_loadWeakRetained(&w);
111 if (rc % 0x100000 == 0) testprintf("%zx/%zx ++\n", rc, LOTS);
112 } while (++rc < LOTS);
113
114 testassert([o retainCount] == rc);
115
116 do {
117 [o release];
118 if (rc % 0x100000 == 0) testprintf("%zx/%zx --\n", rc, LOTS);
119 } while (--rc > 1);
120
121 testassert(rc == 1);
122 testassert([o retainCount] == rc);
123
124 testprintf("dealloc\n");
125
126 testassert(TestRootDealloc == 0);
127 testassert(w != nil);
128 [o release];
129 testassert(TestRootDealloc == 1);
130 testassert(w == nil);
131
132 succeed(__FILE__);
133 }