/MapView/GTM/GTMGarbageCollection.h

http://github.com/route-me/route-me · C Header · 72 lines · 19 code · 13 blank · 40 comment · 2 complexity · 1248f54402de0d61d2432440d35620be MD5 · raw file

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