]> git.saurik.com Git - apple/security.git/blob - libsecurity_utilities/lib/refcount.h
Security-55163.44.tar.gz
[apple/security.git] / libsecurity_utilities / lib / refcount.h
1 /*
2 * Copyright (c) 2000-2004 Apple Computer, 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 Based on code donated by Perry Kiehtreiber
27 */
28 #ifndef _SECURITY_REFCOUNT_H_
29 #define _SECURITY_REFCOUNT_H_
30
31 #include <security_utilities/threading.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 #if !defined(DEBUG_REFCOUNTS)
53 # define DEBUG_REFCOUNTS 1
54 #endif
55
56 #if DEBUG_REFCOUNTS
57 # define RCDEBUG(_kind, _args...) SECURITY_DEBUG_REFCOUNT_##_kind((void *)this, ##_args)
58 #else
59 # define RCDEBUG(kind) /* nothing */
60 #endif
61
62
63 //
64 // Base class for reference counted objects
65 //
66 class RefCount {
67 public:
68 RefCount() : mRefCount(0) { RCDEBUG(CREATE); }
69
70 protected:
71 template <class T> friend class RefPointer;
72
73 void ref() const { ++mRefCount; RCDEBUG(UP, mRefCount); }
74 unsigned int unref() const { RCDEBUG(DOWN, mRefCount - 1); return --mRefCount; }
75
76 // if you call this for anything but debug output, you will go to hell (free handbasket included)
77 unsigned int refCountForDebuggingOnly() const { return mRefCount; }
78
79 private:
80 mutable AtomicCounter<unsigned int> mRefCount;
81 };
82
83
84 //
85 // A pointer type supported by reference counts.
86 // T must be derived from RefCount.
87 //
88 template <class T>
89 class RefPointer {
90 template <class Sub> friend class RefPointer; // share with other instances
91 public:
92 RefPointer() : ptr(0) {} // default to NULL pointer
93 RefPointer(const RefPointer& p) { if (p) p->ref(); ptr = p.ptr; }
94 RefPointer(T *p) { if (p) p->ref(); ptr = p; }
95
96 template <class Sub>
97 RefPointer(const RefPointer<Sub>& p) { if (p) p->ref(); ptr = p.ptr; }
98
99 ~RefPointer() { release(); }
100
101 RefPointer& operator = (const RefPointer& p) { setPointer(p.ptr); return *this; }
102 RefPointer& operator = (T * p) { setPointer(p); return *this; }
103
104 template <class Sub>
105 RefPointer& operator = (const RefPointer<Sub>& p) { setPointer(p.ptr); return *this; }
106
107 // dereference operations
108 T* get () const { _check(); return ptr; } // mimic auto_ptr
109 operator T * () const { _check(); return ptr; }
110 T * operator -> () const { _check(); return ptr; }
111 T & operator * () const { _check(); return *ptr; }
112
113 protected:
114 void release()
115 {
116 if (ptr && ptr->unref() == 0)
117 {
118 delete ptr;
119 ptr = NULL;
120 }
121 }
122
123 void setPointer(T *p) { if (p) p->ref(); release(); ptr = p; }
124
125 void _check() const { }
126
127 T *ptr;
128 };
129
130 template <class T>
131 bool operator <(const RefPointer<T> &r1, const RefPointer<T> &r2)
132 {
133 T *p1 = r1.get(), *p2 = r2.get();
134 return p1 && p2 ? *p1 < *p2 : p1 < p2;
135 }
136
137 template <class T>
138 bool operator ==(const RefPointer<T> &r1, const RefPointer<T> &r2)
139 {
140 T *p1 = r1.get(), *p2 = r2.get();
141 return p1 && p2 ? *p1 == *p2 : p1 == p2;
142 }
143
144 template <class T>
145 bool operator !=(const RefPointer<T> &r1, const RefPointer<T> &r2)
146 {
147 T *p1 = r1.get(), *p2 = r2.get();
148 return p1 && p2 ? *p1 != *p2 : p1 != p2;
149 }
150
151 } // end namespace Security
152
153 #endif // !_SECURITY_REFCOUNT_H_