|
1 // |
|
2 // GTMGarbageCollection.h |
|
3 // |
|
4 // Copyright 2007-2008 Google Inc. |
|
5 // |
|
6 // Licensed under the Apache License, Version 2.0 (the "License"); you may not |
|
7 // use this file except in compliance with the License. You may obtain a copy |
|
8 // 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, WITHOUT |
|
14 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
|
15 // License for the specific language governing permissions and limitations under |
|
16 // the License. |
|
17 // |
|
18 |
|
19 #import <Foundation/Foundation.h> |
|
20 |
|
21 #import "GTMDefines.h" |
|
22 |
|
23 // This allows us to easily move our code from GC to non GC. |
|
24 // They are no-ops unless we are require Leopard or above. |
|
25 // See |
|
26 // http://developer.apple.com/documentation/Cocoa/Conceptual/GarbageCollection/index.html |
|
27 // and |
|
28 // http://developer.apple.com/documentation/Cocoa/Conceptual/GarbageCollection/Articles/gcCoreFoundation.html#//apple_ref/doc/uid/TP40006687-SW1 |
|
29 // for details. |
|
30 |
|
31 #if (MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5) && !GTM_IPHONE_SDK |
|
32 // General use would be to call this through GTMCFAutorelease |
|
33 // but there may be a reason the you want to make something collectable |
|
34 // but not autoreleased, especially in pure GC code where you don't |
|
35 // want to bother with the nop autorelease. Done as a define instead of an |
|
36 // inline so that tools like Clang's scan-build don't report code as leaking. |
|
37 #define GTMNSMakeCollectable(cf) ((id)NSMakeCollectable(cf)) |
|
38 |
|
39 // GTMNSMakeUncollectable is for global maps, etc. that we don't |
|
40 // want released ever. You should still retain these in non-gc code. |
|
41 GTM_INLINE void GTMNSMakeUncollectable(id object) { |
|
42 [[NSGarbageCollector defaultCollector] disableCollectorForPointer:object]; |
|
43 } |
|
44 |
|
45 // Hopefully no code really needs this, but GTMIsGarbageCollectionEnabled is |
|
46 // a common way to check at runtime if GC is on. |
|
47 // There are some places where GC doesn't work w/ things w/in Apple's |
|
48 // frameworks, so this is here so GTM unittests and detect it, and not run |
|
49 // individual tests to work around bugs in Apple's frameworks. |
|
50 GTM_INLINE BOOL GTMIsGarbageCollectionEnabled(void) { |
|
51 return ([NSGarbageCollector defaultCollector] != nil); |
|
52 } |
|
53 |
|
54 #else |
|
55 |
|
56 #define GTMNSMakeCollectable(cf) ((id)(cf)) |
|
57 |
|
58 GTM_INLINE void GTMNSMakeUncollectable(id object) { |
|
59 } |
|
60 |
|
61 GTM_INLINE BOOL GTMIsGarbageCollectionEnabled(void) { |
|
62 return NO; |
|
63 } |
|
64 |
|
65 #endif |
|
66 |
|
67 // GTMCFAutorelease makes a CF object collectable in GC mode, or adds it |
|
68 // to the autorelease pool in non-GC mode. Either way it is taken care |
|
69 // of. Done as a define instead of an inline so that tools like Clang's |
|
70 // scan-build don't report code as leaking. |
|
71 #define GTMCFAutorelease(cf) ([GTMNSMakeCollectable(cf) autorelease]) |
|
72 |