]>
git.saurik.com Git - apple/security.git/blob - Security/libsecurity_utilities/lib/seccfobject.cpp
2 * Copyright (c) 2000-2004,2011-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/seccfobject.h>
25 #include <security_utilities/cfclass.h>
26 #include <security_utilities/errors.h>
27 #include <security_utilities/debugging.h>
30 #include <security_utilities/globalizer.h>
31 #include <auto_zone.h>
33 SecPointerBase::SecPointerBase(const SecPointerBase
& p
)
37 CFRetain(p
.ptr
->operator CFTypeRef());
45 static void CheckForRelease(SecCFObject
* ptr
)
47 CFTypeRef tr
= ptr
->operator CFTypeRef();
48 CFIndex retainCount
= CFGetRetainCount(tr
);
49 if (retainCount
== 1 || retainCount
== -1)
51 ptr
->aboutToDestruct();
57 SecPointerBase::SecPointerBase(SecCFObject
*p
)
61 CFRetain(p
->operator CFTypeRef());
68 SecPointerBase::~SecPointerBase()
73 CFRelease(ptr
->operator CFTypeRef());
79 SecPointerBase
& SecPointerBase::operator = (const SecPointerBase
& p
)
83 CFTypeRef tr
= p
.ptr
->operator CFTypeRef();
89 CFRelease(ptr
->operator CFTypeRef());
97 void SecPointerBase::assign(SecCFObject
* p
)
101 CFRetain(p
->operator CFTypeRef());
105 CheckForRelease(ptr
);
106 CFRelease(ptr
->operator CFTypeRef());
113 void SecPointerBase::copy(SecCFObject
* p
)
117 CheckForRelease(ptr
);
118 CFRelease(ptr
->operator CFTypeRef());
130 SecCFObject::optional(CFTypeRef cfTypeRef
) throw()
135 return const_cast<SecCFObject
*>(reinterpret_cast<const SecCFObject
*>(reinterpret_cast<const uint8_t *>(cfTypeRef
) + kAlignedRuntimeSize
));
139 SecCFObject::required(CFTypeRef cfTypeRef
, OSStatus error
)
141 SecCFObject
*object
= optional(cfTypeRef
);
143 MacOSError::throwMe(error
);
149 SecCFObject::allocate(size_t size
, const CFClass
&cfclass
) throw(std::bad_alloc
)
151 CFTypeRef p
= _CFRuntimeCreateInstance(NULL
, cfclass
.typeID
,
152 size
+ kAlignedRuntimeSize
- sizeof(CFRuntimeBase
), NULL
);
154 throw std::bad_alloc();
156 ((SecRuntimeBase
*) p
)->isNew
= true;
158 void *q
= ((u_int8_t
*) p
) + kAlignedRuntimeSize
;
160 if (SECURITY_DEBUG_SEC_CREATE_ENABLED()) {
161 const CFRuntimeClass
*rtc
= _CFRuntimeGetClassWithTypeID(cfclass
.typeID
);
162 SECURITY_DEBUG_SEC_CREATE(q
, rtc
? (char *)rtc
->className
: NULL
, (unsigned int)cfclass
.typeID
);
168 SecCFObject::operator delete(void *object
) throw()
170 CFTypeRef cfType
= reinterpret_cast<CFTypeRef
>(reinterpret_cast<const uint8_t *>(object
) - kAlignedRuntimeSize
);
171 if (CF_IS_COLLECTABLE(cfType
))
176 CFAllocatorRef allocator
= CFGetAllocator(cfType
);
177 CFAllocatorDeallocate(allocator
, (void*) cfType
);
180 SecCFObject::SecCFObject()
183 mRetainSpinLock
= OS_SPINLOCK_INIT
;
186 uint32_t SecCFObject::updateRetainCount(intptr_t direction
, uint32_t *oldCount
)
188 OSSpinLockLock(&mRetainSpinLock
);
190 if (oldCount
!= NULL
)
192 *oldCount
= mRetainCount
;
195 if (direction
!= -1 || mRetainCount
!= 0)
197 // if we are decrementing
198 if (direction
== -1 || UINT32_MAX
!= mRetainCount
)
200 mRetainCount
+= direction
;
204 uint32_t result
= mRetainCount
;
206 OSSpinLockUnlock(&mRetainSpinLock
);
213 SecCFObject::~SecCFObject()
215 SECURITY_DEBUG_SEC_DESTROY(this);
219 SecCFObject::equal(SecCFObject
&other
)
221 return this == &other
;
227 return CFHashCode(this);
231 SecCFObject::copyFormattingDesc(CFDictionaryRef dict
)
237 SecCFObject::copyDebugDesc()
243 SecCFObject::handle(bool retain
) throw()
245 CFTypeRef cfType
= *this;
246 if (retain
&& !isNew()) CFRetain(cfType
);
253 SecCFObject::aboutToDestruct()
260 SecCFObject::getMutexForObject()
262 return NULL
; // we only worry about descendants of KeychainImpl and ItemImpl
267 bool SecCFObject::mayDelete()