2 * Copyright (c) 2000-2004,2011,2013-2014 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
24 #include <security_utilities/cfclass.h>
25 #include <security_utilities/seccfobject.h>
26 #include <security_utilities/threading.h>
27 #include <CoreFoundation/CFString.h>
33 CFClass::CFClass(const char *name
)
35 // initialize the CFRuntimeClass structure
40 finalize
= finalizeType
;
43 copyFormattingDesc
= copyFormattingDescType
;
44 copyDebugDesc
= copyDebugDescType
;
46 // update because we are now doing our own reference counting
47 version
|= _kCFRuntimeCustomRefCount
; // see ma, no hands!
48 refcount
= refCountForType
;
51 typeID
= _CFRuntimeRegisterClass(this);
52 assert(typeID
!= _kCFRuntimeNotATypeID
);
56 CFClass::cleanupObject(intptr_t op
, CFTypeRef cf
, bool &zap
)
58 // the default is to not throw away the object
61 uint32_t currentCount
;
62 SecCFObject
*obj
= SecCFObject::optional(cf
);
65 currentCount
= obj
->updateRetainCount(op
, &oldCount
);
71 else if (currentCount
== 0)
73 // we may not be able to delete if the caller has active children
77 zap
= true; // ask the caller to release the mutex and zap the object
92 CFClass::refCountForType(intptr_t op
, CFTypeRef cf
) throw()
99 SecCFObject
*obj
= SecCFObject::optional(cf
);
100 Mutex
* mutex
= obj
->getMutexForObject();
103 // if the object didn't have a mutex, it wasn't cached.
104 // Just clean it up and get out.
105 result
= cleanupObject(op
, cf
, zap
);
109 // we have a mutex, so we need to do our cleanup operation under its control
110 StLock
<Mutex
> _(*mutex
);
111 result
= cleanupObject(op
, cf
, zap
);
114 if (zap
) // did we release the object?
116 delete obj
; // should call the overloaded delete for the object
123 // keep the compiler happy
130 CFClass::finalizeType(CFTypeRef cf
) throw()
133 We need to control the lifetime of the object. This means
134 that the cache lock has to be asserted while we are determining if the
135 object should live or die. The mutex is recursive, which means that
136 we won't end up with mutex inversion.
139 SecCFObject
*obj
= SecCFObject::optional(cf
);
143 Mutex
* mutex
= obj
->getMutexForObject();
146 // if the object didn't have a mutex, it wasn't cached.
147 // Just clean it up and get out.
148 obj
->aboutToDestruct(); // removes the object from its associated cache.
152 StLock
<Mutex
> _(*mutex
);
156 // New objects aren't in the cache.
157 // Just clean it up and get out.
158 obj
->aboutToDestruct(); // removes the object from its associated cache.
162 obj
->aboutToDestruct(); // removes the object from its associated cache.
171 CFClass::equalType(CFTypeRef cf1
, CFTypeRef cf2
) throw()
173 // CF checks for pointer equality and ensures type equality already
175 return SecCFObject::optional(cf1
)->equal(*SecCFObject::optional(cf2
));
182 CFClass::hashType(CFTypeRef cf
) throw()
185 return SecCFObject::optional(cf
)->hash();
187 return 666; /* Beasty return for error */
192 CFClass::copyFormattingDescType(CFTypeRef cf
, CFDictionaryRef dict
) throw()
195 return SecCFObject::optional(cf
)->copyFormattingDesc(dict
);
197 return CFSTR("Exception thrown trying to format object");
202 CFClass::copyDebugDescType(CFTypeRef cf
) throw()
205 return SecCFObject::optional(cf
)->copyDebugDesc();
207 return CFSTR("Exception thrown trying to format object");