]>
git.saurik.com Git - apple/security.git/blob - OSX/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 <stdatomic.h>
33 SecPointerBase::SecPointerBase(const SecPointerBase
& p
)
37 CFRetain(p
.ptr
->operator CFTypeRef());
43 SecPointerBase::SecPointerBase(SecCFObject
*p
)
47 CFRetain(p
->operator CFTypeRef());
54 SecPointerBase::~SecPointerBase()
58 CFRelease(ptr
->operator CFTypeRef());
64 SecPointerBase
& SecPointerBase::operator = (const SecPointerBase
& p
)
68 CFTypeRef tr
= p
.ptr
->operator CFTypeRef();
73 CFRelease(ptr
->operator CFTypeRef());
81 void SecPointerBase::assign(SecCFObject
* p
)
85 CFRetain(p
->operator CFTypeRef());
89 CFRelease(ptr
->operator CFTypeRef());
96 void SecPointerBase::copy(SecCFObject
* p
)
100 CFRelease(ptr
->operator CFTypeRef());
112 SecCFObject::optional(CFTypeRef cfTypeRef
) throw()
117 return const_cast<SecCFObject
*>(reinterpret_cast<const SecCFObject
*>(reinterpret_cast<const uint8_t *>(cfTypeRef
) + kAlignedRuntimeSize
));
121 SecCFObject::required(CFTypeRef cfTypeRef
, OSStatus error
)
123 SecCFObject
*object
= optional(cfTypeRef
);
125 MacOSError::throwMe(error
);
131 SecCFObject::allocate(size_t size
, const CFClass
&cfclass
) throw(std::bad_alloc
)
133 CFTypeRef p
= _CFRuntimeCreateInstance(NULL
, cfclass
.typeID
,
134 size
+ kAlignedRuntimeSize
- sizeof(CFRuntimeBase
), NULL
);
136 throw std::bad_alloc();
138 atomic_flag_clear(&((SecRuntimeBase
*) p
)->isOld
);
140 void *q
= ((u_int8_t
*) p
) + kAlignedRuntimeSize
;
146 SecCFObject::operator delete(void *object
) throw()
148 CFTypeRef cfType
= reinterpret_cast<CFTypeRef
>(reinterpret_cast<const uint8_t *>(object
) - kAlignedRuntimeSize
);
149 if (CF_IS_COLLECTABLE(cfType
))
154 CFAllocatorRef allocator
= CFGetAllocator(cfType
);
155 CFAllocatorDeallocate(allocator
, (void*) cfType
);
158 SecCFObject::SecCFObject()
161 mRetainSpinLock
= OS_SPINLOCK_INIT
;
164 uint32_t SecCFObject::updateRetainCount(intptr_t direction
, uint32_t *oldCount
)
166 OSSpinLockLock(&mRetainSpinLock
);
168 if (oldCount
!= NULL
)
170 *oldCount
= mRetainCount
;
173 if (direction
!= -1 || mRetainCount
!= 0)
175 // if we are decrementing
176 if (direction
== -1 || UINT32_MAX
!= mRetainCount
)
178 mRetainCount
+= direction
;
182 uint32_t result
= mRetainCount
;
184 OSSpinLockUnlock(&mRetainSpinLock
);
191 SecCFObject::~SecCFObject()
193 //SECURITY_DEBUG_SEC_DESTROY(this);
197 SecCFObject::equal(SecCFObject
&other
)
199 return this == &other
;
205 return CFHashCode(this);
209 SecCFObject::copyFormattingDesc(CFDictionaryRef dict
)
215 SecCFObject::copyDebugDesc()
221 SecCFObject::handle(bool retain
) throw()
223 CFTypeRef cfType
= *this;
224 if (retain
&& !isNew()) CFRetain(cfType
);
231 SecCFObject::aboutToDestruct()
238 SecCFObject::getMutexForObject() const
240 return NULL
; // we only worry about descendants of KeychainImpl and ItemImpl
245 bool SecCFObject::mayDelete()