]> git.saurik.com Git - apple/security.git/blob - Keychain/SecRuntime.cpp
ec87f19fd744b78b6bec77a37f33afc09526a40a
[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 #include <Security/SecCFTypes.h>
24
25 using namespace KeychainCore;
26
27 //
28 // SecCFObject
29 //
30 SecCFObject::~SecCFObject()
31 {
32 }
33
34 bool
35 SecCFObject::equal(SecCFObject &other)
36 {
37 return this == &other;
38 }
39
40 CFHashCode
41 SecCFObject::hash()
42 {
43 return CFHashCode(this);
44 }
45
46
47 //
48 // SecCFType
49 //
50 SecCFType::SecCFType(SecCFObject *obj) :
51 mObject(obj)
52 {
53 }
54
55 SecCFType::~SecCFType()
56 {
57 mObject = NULL;
58 }
59
60 //
61 // CFClassBase
62 //
63 CFClassBase::CFClassBase(const char *name)
64 {
65 // initialize the CFRuntimeClass structure
66 version = 0;
67 className = name;
68 init = NULL;
69 copy = NULL;
70 finalize = finalizeType;
71 equal = equalType;
72 hash = hashType;
73 copyFormattingDesc = NULL;
74 copyDebugDesc = NULL;
75
76 // register
77 typeId = _CFRuntimeRegisterClass(this);
78 assert(typeId != _kCFRuntimeNotATypeID);
79 }
80
81 void
82 CFClassBase::finalizeType(CFTypeRef cf)
83 {
84 const SecCFType *type = reinterpret_cast<const SecCFType *>(cf);
85 StLock<Mutex> _(gTypes().mapLock);
86 gTypes().map.erase(type->mObject.get());
87 type->~SecCFType();
88 }
89
90 Boolean
91 CFClassBase::equalType(CFTypeRef cf1, CFTypeRef cf2)
92 {
93 const SecCFType *t1 = reinterpret_cast<const SecCFType *>(cf1);
94 const SecCFType *t2 = reinterpret_cast<const SecCFType *>(cf2);
95 // CF checks for pointer equality and ensures type equality already
96 return t1->mObject->equal(*t2->mObject);
97 }
98
99 CFHashCode
100 CFClassBase::hashType(CFTypeRef cf)
101 {
102 return reinterpret_cast<const SecCFType *>(cf)->mObject->hash();
103 }
104
105 const SecCFType *
106 CFClassBase::makeNew(SecCFObject *obj)
107 {
108 void *p = const_cast<void *>(_CFRuntimeCreateInstance(NULL, typeId,
109 sizeof(SecCFType) - sizeof(CFRuntimeBase), NULL));
110 new (p) SecCFType(obj);
111 return reinterpret_cast<const SecCFType *>(p);
112 }
113
114 const SecCFType *
115 CFClassBase::handle(SecCFObject *obj)
116 {
117 SecCFTypes::Map &map = gTypes().map;
118 StLock<Mutex> _(gTypes().mapLock);
119 SecCFTypes::Map::const_iterator it = map.find(obj);
120 if (it == map.end())
121 {
122 const SecCFType *p = makeNew(obj);
123 map[obj] = p;
124 return p;
125 }
126 else
127 {
128 CFRetain(it->second);
129 return it->second;
130 }
131 }
132
133 SecCFObject *
134 CFClassBase::required(const SecCFType *type, OSStatus errorCode)
135 {
136 if (!type)
137 MacOSError::throwMe(errorCode);
138
139 return type->mObject.get();
140 }