/ OSX / utilities / SecCFRelease.h
SecCFRelease.h
 1  /*
 2   * Copyright (c) 2012,2014 Apple Inc. All Rights Reserved.
 3   *
 4   * @APPLE_LICENSE_HEADER_START@
 5   * 
 6   * This file contains Original Code and/or Modifications of Original Code
 7   * as defined in and that are subject to the Apple Public Source License
 8   * Version 2.0 (the 'License'). You may not use this file except in
 9   * compliance with the License. Please obtain a copy of the License at
10   * http://www.opensource.apple.com/apsl/ and read it before using this
11   * file.
12   * 
13   * The Original Code and all software distributed under the License are
14   * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15   * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16   * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17   * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18   * Please see the License for the specific language governing rights and
19   * limitations under the License.
20   * 
21   * @APPLE_LICENSE_HEADER_END@
22   */
23  
24  
25  #ifndef _SECCFRELEASE_H_
26  #define _SECCFRELEASE_H_
27  
28  #include <CoreFoundation/CFBase.h>
29  
30  // Retains its argument unless it's NULL.  Always returns its argument.
31  #define CFRetainSafe(CF) ({ __typeof__(CF) _cf = (CF); _cf ? (CFRetain(_cf), _cf) : _cf; })
32  
33  // Releases CF unless it's NULL.  Always returns NULL, for your convenience and constructs like:
34  // return CFReleaseSafe(foo);
35  #define CFReleaseSafe(CF) ({ __typeof__(CF) _cf = (CF); (_cf ? (CFRelease(_cf), ((__typeof__(CF))0)) : _cf); })
36  
37  
38  #if 0
39  // Objective-C defines &<bridged-object> to be a constant reference, which we can't assign to, so we
40  // suffer multiple evaluations of arguments (avoid using it with side-effects) to make things compile well
41  
42  #define CFTransferRetained(VAR1, VAR2) ({ CFReleaseSafe(VAR1); VAR1 = VAR2; VAR2 = ((__typeof__(VAR2))0)); })
43  
44  #define CFAssignRetained(VAR, CF) ({ CFReleaseSafe(VAR); VAR1 = CF; CF = ((__typeof__(CF))0)); })
45  
46  #define CFRetainAssign(VAR,CF) ({ CFReleaseSafe(VAR); VAR = CFRetainSafe(CF); })
47  
48  #define CFReleaseNull(CF) ({ CFReleaseSafe(CF); CF = ((__typeof__(CF))0)); })
49  
50  #else
51  
52  // Assume VAR1 is NULL or an already retained object and CFReleaseSafe VAR1 and assigns VAR2 to VAR1. Returns VAR2.
53  #define CFTransferRetained(VAR1, VAR2) ({ \
54      __typeof__(VAR1) *const _pvar1 = &(VAR1); \
55      __typeof__(VAR2) *const _pvar2 = &(VAR2); \
56      __typeof__(VAR2) _var2 = *_pvar2; \
57      (*_pvar2) = NULL; \
58      (*_pvar1) = *_pvar1 ? (CFRelease(*_pvar1), _var2) : _var2; \
59  })
60  
61  
62  // Assume CF is NULL or an already retained object and CFReleaseSafe VAR and assigns CF to VAR. Returns CF.
63  #define CFAssignRetained(VAR,CF) ({ \
64      __typeof__(VAR) *const _pvar = &(VAR); \
65      __typeof__(CF) _cf = (CF); \
66      (*_pvar) = *_pvar ? (CFRelease(*_pvar), _cf) : _cf; \
67  })
68  
69  // CFRetainSafe CF and CFReleaseSafe VAR and assigns CF to VAR. Returns CF.
70  #define CFRetainAssign(VAR,CF) ({ \
71      __typeof__(VAR) *const _pvar = &(VAR); \
72      __typeof__(CF) _cf = (CF); \
73      (((*_pvar) == _cf) ? _cf \
74      : ((*_pvar) = (_cf ? (CFRetain(_cf), (*_pvar ? (CFRelease(*_pvar), _cf) : _cf)) \
75                     : (CFRelease(*_pvar), ((__typeof__(_cf))0))))); \
76  })
77  
78  
79  // Assigns NULL to CF. Releases the value stored at CF unless it was NULL.  Always returns NULL, for your convenience
80  #define CFReleaseNull(CF) ({ __typeof__(CF) *const _pcf = &(CF), _cf = *_pcf; (_cf ? (*_pcf) = ((__typeof__(CF))0), (CFRelease(_cf), ((__typeof__(CF))0)) : _cf); })
81  
82  #endif
83  
84  #endif /* _SECCFRELEASE_H_ */