1/*
2 * Copyright (c) 2009 Apple Inc. All rights reserved.
3 *
4 * @APPLE_APACHE_LICENSE_HEADER_START@
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 *     http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 * @APPLE_APACHE_LICENSE_HEADER_END@
19 */
20#import <Foundation/Foundation.h>
21#import <objc/objc-auto.h>
22
23// CONFIG open GC -C99 runtime: objc_assign_global_error
24
25// store thread local and global objects into global variable
26
27@interface TestObject : NSObject {
28@public
29    id string;
30    bool isGlobal;
31}
32- (void)makeGlobal;
33@end
34
35TestObject *target;
36
37TestObject *global;
38int localDidFinalize = 0;
39int globalDidFinalize = 0;
40int Errors;
41
42@implementation TestObject
43
44- (void)finalize {
45    if (isGlobal) {
46        static int counter = 0;
47        if (counter++ == 0) {
48            target = self;
49            globalDidFinalize = 1;
50        }
51    }
52    else  {
53        static int counter = 0;
54        if (counter++ == 0) {
55            target = self;
56            localDidFinalize = 1;
57        }
58    }
59    [super finalize];
60}
61- (void)makeGlobal {
62    [[NSGarbageCollector defaultCollector] disableCollectorForPointer:self];
63    [[NSGarbageCollector defaultCollector] enableCollectorForPointer:self];
64    isGlobal = true;
65}
66@end
67
68#define LOCALOBJECTS 0
69
70int main(int argc, char *argv[]) {
71    global = [[TestObject alloc] init];
72
73#if LOCALOBJECTS
74    for (int i = 0; i < 101; ++i) [[TestObject alloc] init];
75    [[NSGarbageCollector defaultCollector] collectIfNeeded];
76    if (localDidFinalize == 0) {
77        printf("%s: Whoops, no locals were finalized!!\n", argv[0]);
78        ++Errors;
79    }
80
81#else
82    for (int i = 0; i < 101; ++i) [[[TestObject alloc] init] makeGlobal];
83    [[NSGarbageCollector defaultCollector] collectExhaustively];
84    if (globalDidFinalize == 0) {
85        printf("%s: Whoops, no globals were finalized!!\n", argv[0]);
86        ++Errors;
87    }
88#endif
89
90    if (Errors == 0) {
91        printf("%s: Success!\n", argv[0]);
92    }
93    return Errors;
94
95}
96
97