]>
Commit | Line | Data |
---|---|---|
b1ab9ed8 | 1 | /* |
d8f41ccd | 2 | * Copyright (c) 2000-2004,2011,2013-2014 Apple Inc. All Rights Reserved. |
b1ab9ed8 A |
3 | * |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
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 | |
11 | * file. | |
12 | * | |
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. | |
20 | * | |
21 | * @APPLE_LICENSE_HEADER_END@ | |
22 | */ | |
23 | ||
24 | ||
25 | ||
26 | #ifndef _SECCFOBJECT_H | |
27 | #define _SECCFOBJECT_H | |
28 | ||
29 | #include <CoreFoundation/CFRuntime.h> | |
30 | #include <new> | |
31 | #include "threading.h" | |
866f8763 | 32 | #include <os/lock.h> |
6b200bc3 A |
33 | |
34 | #if( __cplusplus <= 201103L) | |
fa7225c8 | 35 | #include <stdatomic.h> |
6b200bc3 | 36 | #endif |
b1ab9ed8 A |
37 | |
38 | namespace Security { | |
39 | ||
40 | class CFClass; | |
41 | ||
fa7225c8 | 42 | #define SECCFFUNCTIONS_BASE(OBJTYPE, APIPTR) \ |
b1ab9ed8 A |
43 | \ |
44 | operator APIPTR() const \ | |
45 | { return (APIPTR)(this->operator CFTypeRef()); } \ | |
46 | \ | |
47 | OBJTYPE *retain() \ | |
48 | { SecCFObject::handle(true); return this; } \ | |
49 | APIPTR handle(bool retain = true) \ | |
fa7225c8 A |
50 | { return (APIPTR)SecCFObject::handle(retain); } |
51 | ||
52 | #define SECCFFUNCTIONS_CREATABLE(OBJTYPE, APIPTR, CFCLASS) \ | |
53 | SECCFFUNCTIONS_BASE(OBJTYPE, APIPTR)\ | |
54 | \ | |
55 | void *operator new(size_t size) throw(std::bad_alloc) \ | |
56 | { return SecCFObject::allocate(size, CFCLASS); } | |
57 | ||
58 | #define SECCFFUNCTIONS(OBJTYPE, APIPTR, ERRCODE, CFCLASS) \ | |
59 | SECCFFUNCTIONS_CREATABLE(OBJTYPE, APIPTR, CFCLASS) \ | |
b1ab9ed8 A |
60 | \ |
61 | static OBJTYPE *required(APIPTR ptr) \ | |
62 | { if (OBJTYPE *p = dynamic_cast<OBJTYPE *>(SecCFObject::required(ptr, ERRCODE))) \ | |
63 | return p; else MacOSError::throwMe(ERRCODE); } \ | |
64 | \ | |
65 | static OBJTYPE *optional(APIPTR ptr) \ | |
66 | { if (SecCFObject *p = SecCFObject::optional(ptr)) \ | |
67 | if (OBJTYPE *pp = dynamic_cast<OBJTYPE *>(p)) return pp; else MacOSError::throwMe(ERRCODE); \ | |
68 | else return NULL; } | |
69 | ||
70 | #define SECALIGNUP(SIZE, ALIGNMENT) (((SIZE - 1) & ~(ALIGNMENT - 1)) + ALIGNMENT) | |
71 | ||
72 | struct SecRuntimeBase: CFRuntimeBase | |
73 | { | |
fa7225c8 | 74 | atomic_flag isOld; |
b1ab9ed8 A |
75 | }; |
76 | ||
77 | class SecCFObject | |
78 | { | |
79 | private: | |
80 | void *operator new(size_t) throw(std::bad_alloc); | |
81 | ||
82 | // Align up to a multiple of 16 bytes | |
83 | static const size_t kAlignedRuntimeSize = SECALIGNUP(sizeof(SecRuntimeBase), 4); | |
84 | ||
85 | uint32_t mRetainCount; | |
866f8763 | 86 | os_unfair_lock mRetainLock; |
b1ab9ed8 A |
87 | |
88 | public: | |
89 | // For use by SecPointer only. Returns true once the first time it's called after the object has been created. | |
90 | bool isNew() | |
91 | { | |
92 | SecRuntimeBase *base = reinterpret_cast<SecRuntimeBase *>(reinterpret_cast<uint8_t *>(this) - kAlignedRuntimeSize); | |
fa7225c8 A |
93 | |
94 | // atomic flags start clear, and like to go high. | |
95 | return !atomic_flag_test_and_set(&(base->isOld)); | |
b1ab9ed8 A |
96 | } |
97 | ||
98 | static SecCFObject *optional(CFTypeRef) throw(); | |
99 | static SecCFObject *required(CFTypeRef, OSStatus error); | |
100 | static void *allocate(size_t size, const CFClass &cfclass) throw(std::bad_alloc); | |
101 | ||
102 | SecCFObject(); | |
103 | virtual ~SecCFObject(); | |
104 | uint32_t updateRetainCount(intptr_t direction, uint32_t *oldCount); | |
105 | uint32_t getRetainCount() {return updateRetainCount(0, NULL);} | |
106 | ||
107 | static void operator delete(void *object) throw(); | |
fa7225c8 | 108 | virtual operator CFTypeRef() const throw() |
b1ab9ed8 A |
109 | { |
110 | return reinterpret_cast<CFTypeRef>(reinterpret_cast<const uint8_t *>(this) - kAlignedRuntimeSize); | |
111 | } | |
112 | ||
113 | // This bumps up the retainCount by 1, by calling CFRetain(), iff retain is true | |
114 | CFTypeRef handle(bool retain = true) throw(); | |
115 | ||
116 | virtual bool equal(SecCFObject &other); | |
117 | virtual CFHashCode hash(); | |
118 | virtual CFStringRef copyFormattingDesc(CFDictionaryRef dict); | |
119 | virtual CFStringRef copyDebugDesc(); | |
120 | virtual void aboutToDestruct(); | |
fa7225c8 | 121 | virtual Mutex* getMutexForObject() const; |
427c49bc | 122 | virtual bool mayDelete(); |
b1ab9ed8 A |
123 | }; |
124 | ||
125 | // | |
126 | // A pointer type for SecCFObjects. | |
127 | // T must be derived from SecCFObject. | |
128 | // | |
129 | class SecPointerBase | |
130 | { | |
131 | public: | |
132 | SecPointerBase() : ptr(NULL) | |
133 | {} | |
134 | SecPointerBase(const SecPointerBase& p); | |
135 | SecPointerBase(SecCFObject *p); | |
136 | ~SecPointerBase(); | |
137 | SecPointerBase& operator = (const SecPointerBase& p); | |
138 | ||
139 | protected: | |
140 | void assign(SecCFObject * p); | |
141 | void copy(SecCFObject * p); | |
142 | SecCFObject *ptr; | |
143 | }; | |
144 | ||
145 | template <class T> | |
146 | class SecPointer : public SecPointerBase | |
147 | { | |
148 | public: | |
149 | SecPointer() : SecPointerBase() {} | |
150 | SecPointer(const SecPointer& p) : SecPointerBase(p) {} | |
151 | SecPointer(T *p): SecPointerBase(p) {} | |
152 | SecPointer &operator =(T *p) { this->assign(p); return *this; } | |
153 | SecPointer &take(T *p) { this->copy(p); return *this; } | |
154 | T *yield() { T *result = static_cast<T *>(ptr); ptr = NULL; return result; } | |
155 | ||
156 | // dereference operations | |
157 | T* get () const { return static_cast<T*>(ptr); } // mimic auto_ptr | |
158 | operator T * () const { return static_cast<T*>(ptr); } | |
159 | T * operator -> () const { return static_cast<T*>(ptr); } | |
160 | T & operator * () const { return *static_cast<T*>(ptr); } | |
161 | }; | |
162 | ||
163 | template <class T> | |
164 | bool operator <(const SecPointer<T> &r1, const SecPointer<T> &r2) | |
165 | { | |
166 | T *p1 = r1.get(), *p2 = r2.get(); | |
167 | return p1 && p2 ? *p1 < *p2 : p1 < p2; | |
168 | } | |
169 | ||
170 | template <class T> | |
171 | bool operator ==(const SecPointer<T> &r1, const SecPointer<T> &r2) | |
172 | { | |
173 | T *p1 = r1.get(), *p2 = r2.get(); | |
174 | return p1 && p2 ? *p1 == *p2 : p1 == p2; | |
175 | } | |
176 | ||
177 | template <class T> | |
178 | bool operator !=(const SecPointer<T> &r1, const SecPointer<T> &r2) | |
179 | { | |
180 | T *p1 = r1.get(), *p2 = r2.get(); | |
181 | return p1 && p2 ? *p1 != *p2 : p1 != p2; | |
182 | } | |
183 | ||
184 | } // end namespace Security | |
185 | ||
186 | ||
187 | #endif |