]> git.saurik.com Git - apple/security.git/blob - OSX/libsecurity_utilities/lib/refcount.h
Security-58286.200.222.tar.gz
[apple/security.git] / OSX / libsecurity_utilities / lib / refcount.h
1 /*
2 * Copyright (c) 2000-2004,2011-2014 Apple Inc. All Rights Reserved.
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 */
27 #ifndef _SECURITY_REFCOUNT_H_
28 #define _SECURITY_REFCOUNT_H_
29
30 #include <security_utilities/threading.h>
31 #include <libkern/OSAtomic.h>
32
33 namespace Security {
34
35
36 //
37 // RefCount/RefPointer - a simple reference counting facility.
38 //
39 // To make an object reference-counted, inherit from RefCount. To track refcounted
40 // objects, use RefPointer<TheType>, where TheType must inherit from RefCount.
41 //
42 // RefCount is thread safe - any number of threads can hold and manipulate references
43 // in parallel. It does however NOT protect the contents of your object - just the
44 // reference count itself. If you need to share your object contents, you must provide
45 // appropriate locking yourself.
46 //
47 // There is no (thread safe) way to determine whether you are the only thread holding
48 // a pointer to a particular RefCount object. Thus there is no (thread safe)
49 // way to "demand copy" a RefCount subclass. Trust me; it's been tried. Don't.
50 //
51
52 // Uncomment to debug refcounts
53 //# define DEBUG_REFCOUNTS 1
54
55 #if DEBUG_REFCOUNTS
56 # define RCDEBUG_CREATE() secinfo("refcount", "%p: CREATE", this)
57 # define RCDEBUG(_kind, n) secinfo("refcount", "%p: %s: %d", this, #_kind, n)
58 #else
59 # define RCDEBUG_CREATE() /* nothing */
60 # define RCDEBUG(kind, _args...) /* nothing */
61 #endif
62
63
64 //
65 // Base class for reference counted objects
66 //
67 class RefCount {
68 public:
69 RefCount() : mRefCount(0) { RCDEBUG_CREATE(); }
70
71 protected:
72 template <class T> friend class RefPointer;
73
74 void ref() const
75 {
76 OSAtomicIncrement32(&mRefCount);
77 RCDEBUG(UP, mRefCount);
78 }
79
80 unsigned int unref() const
81 {
82 RCDEBUG(DOWN, mRefCount - 1);
83 return OSAtomicDecrement32(&mRefCount);
84 }
85
86 private:
87 volatile mutable int32_t mRefCount;
88 };
89
90
91 //
92 // A pointer type supported by reference counts.
93 // T must be derived from RefCount.
94 //
95 template <class T>
96 class RefPointer {
97 template <class Sub> friend class RefPointer; // share with other instances
98 public:
99 RefPointer() : ptr(0) {} // default to NULL pointer
100 RefPointer(const RefPointer& p) { if (p) p->ref(); ptr = p.ptr; }
101 RefPointer(T *p) { if (p) p->ref(); ptr = p; }
102
103 template <class Sub>
104 RefPointer(const RefPointer<Sub>& p) { if (p) p->ref(); ptr = p.ptr; }
105
106 ~RefPointer() { release(); }
107
108 RefPointer& operator = (const RefPointer& p) { setPointer(p.ptr); return *this; }
109 RefPointer& operator = (T * p) { setPointer(p); return *this; }
110
111 template <class Sub>
112 RefPointer& operator = (const RefPointer<Sub>& p) { setPointer(p.ptr); return *this; }
113
114 // dereference operations
115 T* get () const { _check(); return ptr; } // mimic auto_ptr
116 operator T * () const { _check(); return ptr; }
117 T * operator -> () const { _check(); return ptr; }
118 T & operator * () const { _check(); return *ptr; }
119
120 protected:
121 void release_internal()
122 {
123 if (ptr && ptr->unref() == 0)
124 {
125 delete ptr;
126 ptr = NULL;
127 }
128 }
129
130 void release()
131 {
132 StLock<Mutex> mutexLock(mMutex);
133 release_internal();
134 }
135
136 void setPointer(T *p)
137 {
138 StLock<Mutex> mutexLock(mMutex);
139 if (p)
140 {
141 p->ref();
142 }
143
144 release_internal();
145 ptr = p;
146 }
147
148 void _check() const { }
149
150 T *ptr;
151 Mutex mMutex;
152 };
153
154 template <class T>
155 bool operator <(const RefPointer<T> &r1, const RefPointer<T> &r2)
156 {
157 T *p1 = r1.get(), *p2 = r2.get();
158 return p1 && p2 ? *p1 < *p2 : p1 < p2;
159 }
160
161 template <class T>
162 bool operator ==(const RefPointer<T> &r1, const RefPointer<T> &r2)
163 {
164 T *p1 = r1.get(), *p2 = r2.get();
165 return p1 && p2 ? *p1 == *p2 : p1 == p2;
166 }
167
168 template <class T>
169 bool operator !=(const RefPointer<T> &r1, const RefPointer<T> &r2)
170 {
171 T *p1 = r1.get(), *p2 = r2.get();
172 return p1 && p2 ? *p1 != *p2 : p1 != p2;
173 }
174
175 } // end namespace Security
176
177 #endif // !_SECURITY_REFCOUNT_H_