]> git.saurik.com Git - apple/security.git/blob - Keychain/SecRuntime.cpp
a8c01a92fd56462d3c15840df8b92ecc229a53d5
[apple/security.git] / Keychain / SecRuntime.cpp
1 /*
2 * Copyright (c) 2002 Apple Computer, Inc. All Rights Reserved.
3 *
4 * The contents of this file constitute Original Code as defined in and are
5 * subject to the Apple Public Source License Version 1.2 (the 'License').
6 * You may not use this file except in compliance with the License. Please obtain
7 * a copy of the License at http://www.apple.com/publicsource and read it before
8 * using this file.
9 *
10 * This Original Code and all software distributed under the License are
11 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS
12 * OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT
13 * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14 * PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see the License for the
15 * specific language governing rights and limitations under the License.
16 */
17
18 //
19 // SecRuntime.cpp - CF runtime interface
20 //
21
22 #include <Security/SecRuntime.h>
23
24 #ifndef NDEBUG
25 #include <Security/debugging.h>
26 #endif
27
28 using namespace KeychainCore;
29
30 //
31 // SecCFObject
32 //
33 SecCFObject *
34 SecCFObject::optional(CFTypeRef cfTypeRef) throw()
35 {
36 if (!cfTypeRef)
37 return NULL;
38
39 return const_cast<SecCFObject *>(reinterpret_cast<const SecCFObject *>(reinterpret_cast<const uint8_t *>(cfTypeRef) + kAlignedRuntimeSize));
40 }
41
42 SecCFObject *
43 SecCFObject::required(CFTypeRef cfTypeRef, OSStatus error)
44 {
45 SecCFObject *object = optional(cfTypeRef);
46 if (!object)
47 MacOSError::throwMe(error);
48
49 return object;
50 }
51
52 void *
53 SecCFObject::allocate(size_t size, CFTypeID typeID) throw(std::bad_alloc)
54 {
55 void *p = const_cast<void *>(_CFRuntimeCreateInstance(NULL, typeID,
56 size + kAlignedRuntimeSize - sizeof(CFRuntimeBase), NULL));
57 if (p == NULL)
58 throw std::bad_alloc();
59
60 reinterpret_cast<SecRuntimeBase *>(p)->isNew = true;
61
62 void *q = reinterpret_cast<void *>(reinterpret_cast<uint8_t *>(p) + kAlignedRuntimeSize);
63
64 secdebug("sec", "SecCFObject allocated %p of type %lu", q, typeID);
65
66 return q;
67 }
68
69 void
70 SecCFObject::operator delete(void *object) throw()
71 {
72 secdebug("sec", "SecCFObject operator delete %p", object);
73 CFTypeRef cfType = reinterpret_cast<CFTypeRef>(reinterpret_cast<const uint8_t *>(object) - kAlignedRuntimeSize);
74 CFRelease(cfType);
75 }
76
77 SecCFObject::~SecCFObject() throw()
78 {
79 secdebug("sec", "SecCFObject::~SecCFObject %p", this);
80 }
81
82 bool
83 SecCFObject::equal(SecCFObject &other)
84 {
85 return this == &other;
86 }
87
88 CFHashCode
89 SecCFObject::hash()
90 {
91 return CFHashCode(this);
92 }
93
94 CFStringRef
95 SecCFObject::copyFormattingDesc(CFDictionaryRef dict)
96 {
97 return NULL;
98 }
99
100 CFStringRef
101 SecCFObject::copyDebugDesc()
102 {
103 return NULL;
104 }
105
106 CFTypeRef
107 SecCFObject::handle(bool retain) throw()
108 {
109 CFTypeRef cfType = *this;
110 if (retain && !isNew()) CFRetain(cfType);
111 return cfType;
112 }