michael@0: // michael@0: // GTMGarbageCollection.h michael@0: // michael@0: // Copyright 2007-2008 Google Inc. michael@0: // michael@0: // Licensed under the Apache License, Version 2.0 (the "License"); you may not michael@0: // use this file except in compliance with the License. You may obtain a copy michael@0: // of the License at michael@0: // michael@0: // http://www.apache.org/licenses/LICENSE-2.0 michael@0: // michael@0: // Unless required by applicable law or agreed to in writing, software michael@0: // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT michael@0: // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the michael@0: // License for the specific language governing permissions and limitations under michael@0: // the License. michael@0: // michael@0: michael@0: #import michael@0: michael@0: #import "GTMDefines.h" michael@0: michael@0: // This allows us to easily move our code from GC to non GC. michael@0: // They are no-ops unless we are require Leopard or above. michael@0: // See michael@0: // http://developer.apple.com/documentation/Cocoa/Conceptual/GarbageCollection/index.html michael@0: // and michael@0: // http://developer.apple.com/documentation/Cocoa/Conceptual/GarbageCollection/Articles/gcCoreFoundation.html#//apple_ref/doc/uid/TP40006687-SW1 michael@0: // for details. michael@0: michael@0: #if (MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5) && !GTM_IPHONE_SDK michael@0: // General use would be to call this through GTMCFAutorelease michael@0: // but there may be a reason the you want to make something collectable michael@0: // but not autoreleased, especially in pure GC code where you don't michael@0: // want to bother with the nop autorelease. Done as a define instead of an michael@0: // inline so that tools like Clang's scan-build don't report code as leaking. michael@0: #define GTMNSMakeCollectable(cf) ((id)NSMakeCollectable(cf)) michael@0: michael@0: // GTMNSMakeUncollectable is for global maps, etc. that we don't michael@0: // want released ever. You should still retain these in non-gc code. michael@0: GTM_INLINE void GTMNSMakeUncollectable(id object) { michael@0: [[NSGarbageCollector defaultCollector] disableCollectorForPointer:object]; michael@0: } michael@0: michael@0: // Hopefully no code really needs this, but GTMIsGarbageCollectionEnabled is michael@0: // a common way to check at runtime if GC is on. michael@0: // There are some places where GC doesn't work w/ things w/in Apple's michael@0: // frameworks, so this is here so GTM unittests and detect it, and not run michael@0: // individual tests to work around bugs in Apple's frameworks. michael@0: GTM_INLINE BOOL GTMIsGarbageCollectionEnabled(void) { michael@0: return ([NSGarbageCollector defaultCollector] != nil); michael@0: } michael@0: michael@0: #else michael@0: michael@0: #define GTMNSMakeCollectable(cf) ((id)(cf)) michael@0: michael@0: GTM_INLINE void GTMNSMakeUncollectable(id object) { michael@0: } michael@0: michael@0: GTM_INLINE BOOL GTMIsGarbageCollectionEnabled(void) { michael@0: return NO; michael@0: } michael@0: michael@0: #endif michael@0: michael@0: // GTMCFAutorelease makes a CF object collectable in GC mode, or adds it michael@0: // to the autorelease pool in non-GC mode. Either way it is taken care michael@0: // of. Done as a define instead of an inline so that tools like Clang's michael@0: // scan-build don't report code as leaking. michael@0: #define GTMCFAutorelease(cf) ([GTMNSMakeCollectable(cf) autorelease]) michael@0: