]>
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>
31 #include <security_utilities/globalizer.h>
32 #if( __cplusplus <= 201103L)
33 #include <stdatomic.h>
36 SecPointerBase::SecPointerBase(const SecPointerBase
& p
)
40 CFRetain(p
.ptr
->operator CFTypeRef());
46 SecPointerBase::SecPointerBase(SecCFObject
*p
)
50 CFRetain(p
->operator CFTypeRef());
57 SecPointerBase::~SecPointerBase()
61 CFRelease(ptr
->operator CFTypeRef());
67 SecPointerBase
& SecPointerBase::operator = (const SecPointerBase
& p
)
71 CFTypeRef tr
= p
.ptr
->operator CFTypeRef();
76 CFRelease(ptr
->operator CFTypeRef());
84 void SecPointerBase::assign(SecCFObject
* p
)
88 CFRetain(p
->operator CFTypeRef());
92 CFRelease(ptr
->operator CFTypeRef());
99 void SecPointerBase::copy(SecCFObject
* p
)
103 CFRelease(ptr
->operator CFTypeRef());
115 SecCFObject::optional(CFTypeRef cfTypeRef
) _NOEXCEPT
120 return const_cast<SecCFObject
*>(reinterpret_cast<const SecCFObject
*>(reinterpret_cast<const uint8_t *>(cfTypeRef
) + kAlignedRuntimeSize
));
124 SecCFObject::required(CFTypeRef cfTypeRef
, OSStatus error
)
126 SecCFObject
*object
= optional(cfTypeRef
);
128 MacOSError::throwMe(error
);
134 SecCFObject::allocate(size_t size
, const CFClass
&cfclass
)
136 CFTypeRef p
= _CFRuntimeCreateInstance(NULL
, cfclass
.typeID
,
137 size
+ kAlignedRuntimeSize
- sizeof(CFRuntimeBase
), NULL
);
139 throw std::bad_alloc();
141 atomic_flag_clear(&((SecRuntimeBase
*) p
)->isOld
);
143 void *q
= ((u_int8_t
*) p
) + kAlignedRuntimeSize
;
149 SecCFObject::operator delete(void *object
) _NOEXCEPT
151 CFTypeRef cfType
= reinterpret_cast<CFTypeRef
>(reinterpret_cast<const uint8_t *>(object
) - kAlignedRuntimeSize
);
153 CFAllocatorRef allocator
= CFGetAllocator(cfType
);
154 CFAllocatorDeallocate(allocator
, (void*) cfType
);
157 SecCFObject::SecCFObject()
160 mRetainLock
= OS_UNFAIR_LOCK_INIT
;
163 uint32_t SecCFObject::updateRetainCount(intptr_t direction
, uint32_t *oldCount
)
165 os_unfair_lock_lock(&mRetainLock
);
167 if (oldCount
!= NULL
)
169 *oldCount
= mRetainCount
;
172 if (direction
!= -1 || mRetainCount
!= 0)
174 // if we are decrementing
175 if (direction
== -1 || UINT32_MAX
!= mRetainCount
)
177 mRetainCount
+= direction
;
181 uint32_t result
= mRetainCount
;
183 os_unfair_lock_unlock(&mRetainLock
);
190 SecCFObject::~SecCFObject()
192 //SECURITY_DEBUG_SEC_DESTROY(this);
196 SecCFObject::equal(SecCFObject
&other
)
198 return this == &other
;
204 return CFHashCode(this);
208 SecCFObject::copyFormattingDesc(CFDictionaryRef dict
)
214 SecCFObject::copyDebugDesc()
220 SecCFObject::handle(bool retain
) _NOEXCEPT
222 CFTypeRef cfType
= *this;
223 if (retain
&& !isNew()) CFRetain(cfType
);
230 SecCFObject::aboutToDestruct()
237 SecCFObject::getMutexForObject() const
239 return NULL
; // we only worry about descendants of KeychainImpl and ItemImpl
244 bool SecCFObject::mayDelete()