]>
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 #if( __cplusplus <= 201103L)
32 #include <stdatomic.h>
35 SecPointerBase::SecPointerBase(const SecPointerBase
& p
)
39 CFRetain(p
.ptr
->operator CFTypeRef());
45 SecPointerBase::SecPointerBase(SecCFObject
*p
)
49 CFRetain(p
->operator CFTypeRef());
56 SecPointerBase::~SecPointerBase()
60 CFRelease(ptr
->operator CFTypeRef());
66 SecPointerBase
& SecPointerBase::operator = (const SecPointerBase
& p
)
70 CFTypeRef tr
= p
.ptr
->operator CFTypeRef();
75 CFRelease(ptr
->operator CFTypeRef());
83 void SecPointerBase::assign(SecCFObject
* p
)
87 CFRetain(p
->operator CFTypeRef());
91 CFRelease(ptr
->operator CFTypeRef());
98 void SecPointerBase::copy(SecCFObject
* p
)
102 CFRelease(ptr
->operator CFTypeRef());
114 SecCFObject::optional(CFTypeRef cfTypeRef
) throw()
119 return const_cast<SecCFObject
*>(reinterpret_cast<const SecCFObject
*>(reinterpret_cast<const uint8_t *>(cfTypeRef
) + kAlignedRuntimeSize
));
123 SecCFObject::required(CFTypeRef cfTypeRef
, OSStatus error
)
125 SecCFObject
*object
= optional(cfTypeRef
);
127 MacOSError::throwMe(error
);
133 SecCFObject::allocate(size_t size
, const CFClass
&cfclass
) throw(std::bad_alloc
)
135 CFTypeRef p
= _CFRuntimeCreateInstance(NULL
, cfclass
.typeID
,
136 size
+ kAlignedRuntimeSize
- sizeof(CFRuntimeBase
), NULL
);
138 throw std::bad_alloc();
140 atomic_flag_clear(&((SecRuntimeBase
*) p
)->isOld
);
142 void *q
= ((u_int8_t
*) p
) + kAlignedRuntimeSize
;
148 SecCFObject::operator delete(void *object
) throw()
150 CFTypeRef cfType
= reinterpret_cast<CFTypeRef
>(reinterpret_cast<const uint8_t *>(object
) - kAlignedRuntimeSize
);
152 CFAllocatorRef allocator
= CFGetAllocator(cfType
);
153 CFAllocatorDeallocate(allocator
, (void*) cfType
);
156 SecCFObject::SecCFObject()
159 mRetainSpinLock
= OS_SPINLOCK_INIT
;
162 uint32_t SecCFObject::updateRetainCount(intptr_t direction
, uint32_t *oldCount
)
164 OSSpinLockLock(&mRetainSpinLock
);
166 if (oldCount
!= NULL
)
168 *oldCount
= mRetainCount
;
171 if (direction
!= -1 || mRetainCount
!= 0)
173 // if we are decrementing
174 if (direction
== -1 || UINT32_MAX
!= mRetainCount
)
176 mRetainCount
+= direction
;
180 uint32_t result
= mRetainCount
;
182 OSSpinLockUnlock(&mRetainSpinLock
);
189 SecCFObject::~SecCFObject()
191 //SECURITY_DEBUG_SEC_DESTROY(this);
195 SecCFObject::equal(SecCFObject
&other
)
197 return this == &other
;
203 return CFHashCode(this);
207 SecCFObject::copyFormattingDesc(CFDictionaryRef dict
)
213 SecCFObject::copyDebugDesc()
219 SecCFObject::handle(bool retain
) throw()
221 CFTypeRef cfType
= *this;
222 if (retain
&& !isNew()) CFRetain(cfType
);
229 SecCFObject::aboutToDestruct()
236 SecCFObject::getMutexForObject() const
238 return NULL
; // we only worry about descendants of KeychainImpl and ItemImpl
243 bool SecCFObject::mayDelete()