]> git.saurik.com Git - apple/security.git/blame - Keychain/SecRuntime.cpp
Security-163.tar.gz
[apple/security.git] / Keychain / SecRuntime.cpp
CommitLineData
29654253
A
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>
df0e469f
A
23
24#ifndef NDEBUG
25#include <Security/debugging.h>
26#endif
29654253
A
27
28using namespace KeychainCore;
29
30//
31// SecCFObject
32//
df0e469f
A
33SecCFObject *
34SecCFObject::optional(CFTypeRef cfTypeRef) throw()
29654253 35{
df0e469f
A
36 if (!cfTypeRef)
37 return NULL;
38
39 return const_cast<SecCFObject *>(reinterpret_cast<const SecCFObject *>(reinterpret_cast<const uint8_t *>(cfTypeRef) + kAlignedRuntimeSize));
29654253
A
40}
41
df0e469f
A
42SecCFObject *
43SecCFObject::required(CFTypeRef cfTypeRef, OSStatus error)
29654253 44{
df0e469f
A
45 SecCFObject *object = optional(cfTypeRef);
46 if (!object)
47 MacOSError::throwMe(error);
48
49 return object;
29654253
A
50}
51
df0e469f
A
52void *
53SecCFObject::allocate(size_t size, CFTypeID typeID) throw(std::bad_alloc)
29654253 54{
df0e469f
A
55 void *p = const_cast<void *>(_CFRuntimeCreateInstance(NULL, typeID,
56 size + kAlignedRuntimeSize - sizeof(CFRuntimeBase), NULL));
57 if (p == NULL)
58 throw std::bad_alloc();
29654253 59
df0e469f 60 reinterpret_cast<SecRuntimeBase *>(p)->isNew = true;
29654253 61
df0e469f 62 void *q = reinterpret_cast<void *>(reinterpret_cast<uint8_t *>(p) + kAlignedRuntimeSize);
29654253 63
df0e469f
A
64 secdebug("sec", "SecCFObject allocated %p of type %lu", q, typeID);
65
66 return q;
29654253
A
67}
68
df0e469f
A
69void
70SecCFObject::operator delete(void *object) throw()
29654253 71{
df0e469f
A
72 secdebug("sec", "SecCFObject operator delete %p", object);
73 CFTypeRef cfType = reinterpret_cast<CFTypeRef>(reinterpret_cast<const uint8_t *>(object) - kAlignedRuntimeSize);
74 CFRelease(cfType);
29654253 75}
df0e469f
A
76
77SecCFObject::~SecCFObject() throw()
29654253 78{
df0e469f 79 secdebug("sec", "SecCFObject::~SecCFObject %p", this);
29654253 80}
df0e469f
A
81
82bool
83SecCFObject::equal(SecCFObject &other)
29654253 84{
df0e469f 85 return this == &other;
29654253
A
86}
87
88CFHashCode
df0e469f 89SecCFObject::hash()
29654253 90{
df0e469f 91 return CFHashCode(this);
29654253
A
92}
93
df0e469f
A
94CFStringRef
95SecCFObject::copyFormattingDesc(CFDictionaryRef dict)
29654253 96{
df0e469f 97 return NULL;
29654253
A
98}
99
df0e469f
A
100CFStringRef
101SecCFObject::copyDebugDesc()
29654253 102{
df0e469f 103 return NULL;
29654253
A
104}
105
df0e469f
A
106CFTypeRef
107SecCFObject::handle(bool retain) throw()
29654253 108{
df0e469f
A
109 CFTypeRef cfType = *this;
110 if (retain && !isNew()) CFRetain(cfType);
111 return cfType;
29654253 112}