blob: 0af7e6a0f496e89b770a0e90955e5b03c1d3a381 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2005 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ANDROID_REF_BASE_H
18#define ANDROID_REF_BASE_H
19
20#include <cutils/atomic.h>
21#include <utils/TextOutput.h>
22
23#include <stdint.h>
24#include <sys/types.h>
25#include <stdlib.h>
26
27// ---------------------------------------------------------------------------
28namespace android {
29
30template<typename T> class wp;
31
32// ---------------------------------------------------------------------------
33
Mathias Agopian951d3fe2011-02-09 18:38:55 -080034#define COMPARE_WEAK(_op_) \
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035inline bool operator _op_ (const sp<T>& o) const { \
36 return m_ptr _op_ o.m_ptr; \
37} \
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038inline bool operator _op_ (const T* o) const { \
39 return m_ptr _op_ o; \
40} \
41template<typename U> \
42inline bool operator _op_ (const sp<U>& o) const { \
43 return m_ptr _op_ o.m_ptr; \
44} \
45template<typename U> \
Mathias Agopian951d3fe2011-02-09 18:38:55 -080046inline bool operator _op_ (const U* o) const { \
47 return m_ptr _op_ o; \
48}
49
50#define COMPARE(_op_) \
51COMPARE_WEAK(_op_) \
52inline bool operator _op_ (const wp<T>& o) const { \
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053 return m_ptr _op_ o.m_ptr; \
54} \
55template<typename U> \
Mathias Agopian951d3fe2011-02-09 18:38:55 -080056inline bool operator _op_ (const wp<U>& o) const { \
57 return m_ptr _op_ o.m_ptr; \
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058}
59
60// ---------------------------------------------------------------------------
61
62class RefBase
63{
64public:
65 void incStrong(const void* id) const;
66 void decStrong(const void* id) const;
67
68 void forceIncStrong(const void* id) const;
69
70 //! DEBUGGING ONLY: Get current strong ref count.
71 int32_t getStrongCount() const;
72
73 class weakref_type
74 {
75 public:
76 RefBase* refBase() const;
77
78 void incWeak(const void* id);
79 void decWeak(const void* id);
80
81 bool attemptIncStrong(const void* id);
82
83 //! This is only safe if you have set OBJECT_LIFETIME_FOREVER.
84 bool attemptIncWeak(const void* id);
85
86 //! DEBUGGING ONLY: Get current weak ref count.
87 int32_t getWeakCount() const;
88
89 //! DEBUGGING ONLY: Print references held on object.
90 void printRefs() const;
91
92 //! DEBUGGING ONLY: Enable tracking for this object.
93 // enable -- enable/disable tracking
94 // retain -- when tracking is enable, if true, then we save a stack trace
95 // for each reference and dereference; when retain == false, we
96 // match up references and dereferences and keep only the
97 // outstanding ones.
98
99 void trackMe(bool enable, bool retain);
100 };
101
102 weakref_type* createWeak(const void* id) const;
103
104 weakref_type* getWeakRefs() const;
105
106 //! DEBUGGING ONLY: Print references held on object.
107 inline void printRefs() const { getWeakRefs()->printRefs(); }
108
109 //! DEBUGGING ONLY: Enable tracking of object.
110 inline void trackMe(bool enable, bool retain)
111 {
112 getWeakRefs()->trackMe(enable, retain);
113 }
114
Mathias Agopianafffa8f2011-06-12 18:05:53 -0700115 // used to override the RefBase destruction.
116 class Destroyer {
117 friend class RefBase;
Mathias Agopian913a5a32011-06-14 11:50:22 -0700118 friend class weakref_type;
Mathias Agopianafffa8f2011-06-12 18:05:53 -0700119 public:
120 virtual ~Destroyer();
121 private:
122 virtual void destroy(RefBase const* base) = 0;
123 };
124
125 // Make sure to never acquire a strong reference from this function. The
126 // same restrictions than for destructors apply.
127 void setDestroyer(Destroyer* destroyer);
128
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800129protected:
130 RefBase();
131 virtual ~RefBase();
Mathias Agopian9e763662011-05-19 18:03:31 -0700132
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133 //! Flags for extendObjectLifetime()
134 enum {
135 OBJECT_LIFETIME_WEAK = 0x0001,
136 OBJECT_LIFETIME_FOREVER = 0x0003
137 };
138
139 void extendObjectLifetime(int32_t mode);
140
141 //! Flags for onIncStrongAttempted()
142 enum {
143 FIRST_INC_STRONG = 0x0001
144 };
145
146 virtual void onFirstRef();
147 virtual void onLastStrongRef(const void* id);
148 virtual bool onIncStrongAttempted(uint32_t flags, const void* id);
149 virtual void onLastWeakRef(const void* id);
150
151private:
152 friend class weakref_type;
153 class weakref_impl;
154
155 RefBase(const RefBase& o);
156 RefBase& operator=(const RefBase& o);
157
158 weakref_impl* const mRefs;
159};
160
161// ---------------------------------------------------------------------------
162
163template <class T>
164class LightRefBase
165{
166public:
167 inline LightRefBase() : mCount(0) { }
168 inline void incStrong(const void* id) const {
169 android_atomic_inc(&mCount);
170 }
171 inline void decStrong(const void* id) const {
172 if (android_atomic_dec(&mCount) == 1) {
173 delete static_cast<const T*>(this);
174 }
175 }
Mathias Agopiandff8e582009-05-04 14:17:04 -0700176 //! DEBUGGING ONLY: Get current strong ref count.
177 inline int32_t getStrongCount() const {
178 return mCount;
179 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800180
181protected:
182 inline ~LightRefBase() { }
183
184private:
185 mutable volatile int32_t mCount;
186};
187
188// ---------------------------------------------------------------------------
189
190template <typename T>
191class sp
192{
193public:
194 typedef typename RefBase::weakref_type weakref_type;
195
196 inline sp() : m_ptr(0) { }
197
198 sp(T* other);
199 sp(const sp<T>& other);
200 template<typename U> sp(U* other);
201 template<typename U> sp(const sp<U>& other);
202
203 ~sp();
204
205 // Assignment
206
207 sp& operator = (T* other);
208 sp& operator = (const sp<T>& other);
209
210 template<typename U> sp& operator = (const sp<U>& other);
211 template<typename U> sp& operator = (U* other);
212
213 //! Special optimization for use by ProcessState (and nobody else).
214 void force_set(T* other);
215
216 // Reset
217
218 void clear();
219
220 // Accessors
221
222 inline T& operator* () const { return *m_ptr; }
223 inline T* operator-> () const { return m_ptr; }
224 inline T* get() const { return m_ptr; }
225
226 // Operators
227
228 COMPARE(==)
229 COMPARE(!=)
230 COMPARE(>)
231 COMPARE(<)
232 COMPARE(<=)
233 COMPARE(>=)
234
235private:
236 template<typename Y> friend class sp;
237 template<typename Y> friend class wp;
238
239 // Optimization for wp::promote().
240 sp(T* p, weakref_type* refs);
241
242 T* m_ptr;
243};
244
245template <typename T>
246TextOutput& operator<<(TextOutput& to, const sp<T>& val);
247
248// ---------------------------------------------------------------------------
249
250template <typename T>
251class wp
252{
253public:
254 typedef typename RefBase::weakref_type weakref_type;
255
256 inline wp() : m_ptr(0) { }
257
258 wp(T* other);
259 wp(const wp<T>& other);
260 wp(const sp<T>& other);
261 template<typename U> wp(U* other);
262 template<typename U> wp(const sp<U>& other);
263 template<typename U> wp(const wp<U>& other);
264
265 ~wp();
266
267 // Assignment
268
269 wp& operator = (T* other);
270 wp& operator = (const wp<T>& other);
271 wp& operator = (const sp<T>& other);
272
273 template<typename U> wp& operator = (U* other);
274 template<typename U> wp& operator = (const wp<U>& other);
275 template<typename U> wp& operator = (const sp<U>& other);
276
277 void set_object_and_refs(T* other, weakref_type* refs);
278
279 // promotion to sp
280
281 sp<T> promote() const;
282
283 // Reset
284
285 void clear();
286
287 // Accessors
288
289 inline weakref_type* get_refs() const { return m_refs; }
290
291 inline T* unsafe_get() const { return m_ptr; }
292
293 // Operators
Mathias Agopian951d3fe2011-02-09 18:38:55 -0800294
295 COMPARE_WEAK(==)
296 COMPARE_WEAK(!=)
297 COMPARE_WEAK(>)
298 COMPARE_WEAK(<)
299 COMPARE_WEAK(<=)
300 COMPARE_WEAK(>=)
301
302 inline bool operator == (const wp<T>& o) const {
303 return (m_ptr == o.m_ptr) && (m_refs == o.m_refs);
304 }
305 template<typename U>
306 inline bool operator == (const wp<U>& o) const {
307 return m_ptr == o.m_ptr;
308 }
309
310 inline bool operator > (const wp<T>& o) const {
311 return (m_ptr == o.m_ptr) ? (m_refs > o.m_refs) : (m_ptr > o.m_ptr);
312 }
313 template<typename U>
314 inline bool operator > (const wp<U>& o) const {
315 return (m_ptr == o.m_ptr) ? (m_refs > o.m_refs) : (m_ptr > o.m_ptr);
316 }
317
318 inline bool operator < (const wp<T>& o) const {
319 return (m_ptr == o.m_ptr) ? (m_refs < o.m_refs) : (m_ptr < o.m_ptr);
320 }
321 template<typename U>
322 inline bool operator < (const wp<U>& o) const {
323 return (m_ptr == o.m_ptr) ? (m_refs < o.m_refs) : (m_ptr < o.m_ptr);
324 }
325 inline bool operator != (const wp<T>& o) const { return m_refs != o.m_refs; }
326 template<typename U> inline bool operator != (const wp<U>& o) const { return !operator == (o); }
327 inline bool operator <= (const wp<T>& o) const { return !operator > (o); }
328 template<typename U> inline bool operator <= (const wp<U>& o) const { return !operator > (o); }
329 inline bool operator >= (const wp<T>& o) const { return !operator < (o); }
330 template<typename U> inline bool operator >= (const wp<U>& o) const { return !operator < (o); }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800331
332private:
333 template<typename Y> friend class sp;
334 template<typename Y> friend class wp;
335
336 T* m_ptr;
337 weakref_type* m_refs;
338};
339
340template <typename T>
341TextOutput& operator<<(TextOutput& to, const wp<T>& val);
342
343#undef COMPARE
Mathias Agopian951d3fe2011-02-09 18:38:55 -0800344#undef COMPARE_WEAK
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800345
346// ---------------------------------------------------------------------------
347// No user serviceable parts below here.
348
349template<typename T>
350sp<T>::sp(T* other)
351 : m_ptr(other)
352{
353 if (other) other->incStrong(this);
354}
355
356template<typename T>
357sp<T>::sp(const sp<T>& other)
358 : m_ptr(other.m_ptr)
359{
360 if (m_ptr) m_ptr->incStrong(this);
361}
362
363template<typename T> template<typename U>
364sp<T>::sp(U* other) : m_ptr(other)
365{
366 if (other) other->incStrong(this);
367}
368
369template<typename T> template<typename U>
370sp<T>::sp(const sp<U>& other)
371 : m_ptr(other.m_ptr)
372{
373 if (m_ptr) m_ptr->incStrong(this);
374}
375
376template<typename T>
377sp<T>::~sp()
378{
379 if (m_ptr) m_ptr->decStrong(this);
380}
381
382template<typename T>
383sp<T>& sp<T>::operator = (const sp<T>& other) {
Mathias Agopian51a6aef2010-06-24 21:49:02 -0700384 T* otherPtr(other.m_ptr);
385 if (otherPtr) otherPtr->incStrong(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800386 if (m_ptr) m_ptr->decStrong(this);
Mathias Agopian51a6aef2010-06-24 21:49:02 -0700387 m_ptr = otherPtr;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800388 return *this;
389}
390
391template<typename T>
392sp<T>& sp<T>::operator = (T* other)
393{
394 if (other) other->incStrong(this);
395 if (m_ptr) m_ptr->decStrong(this);
396 m_ptr = other;
397 return *this;
398}
399
400template<typename T> template<typename U>
401sp<T>& sp<T>::operator = (const sp<U>& other)
402{
Mathias Agopian51a6aef2010-06-24 21:49:02 -0700403 U* otherPtr(other.m_ptr);
404 if (otherPtr) otherPtr->incStrong(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800405 if (m_ptr) m_ptr->decStrong(this);
Mathias Agopian51a6aef2010-06-24 21:49:02 -0700406 m_ptr = otherPtr;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800407 return *this;
408}
409
410template<typename T> template<typename U>
411sp<T>& sp<T>::operator = (U* other)
412{
413 if (other) other->incStrong(this);
414 if (m_ptr) m_ptr->decStrong(this);
415 m_ptr = other;
416 return *this;
417}
418
419template<typename T>
420void sp<T>::force_set(T* other)
421{
422 other->forceIncStrong(this);
423 m_ptr = other;
424}
425
426template<typename T>
427void sp<T>::clear()
428{
429 if (m_ptr) {
430 m_ptr->decStrong(this);
431 m_ptr = 0;
432 }
433}
434
435template<typename T>
436sp<T>::sp(T* p, weakref_type* refs)
437 : m_ptr((p && refs->attemptIncStrong(this)) ? p : 0)
438{
439}
440
441template <typename T>
442inline TextOutput& operator<<(TextOutput& to, const sp<T>& val)
443{
444 to << "sp<>(" << val.get() << ")";
445 return to;
446}
447
448// ---------------------------------------------------------------------------
449
450template<typename T>
451wp<T>::wp(T* other)
452 : m_ptr(other)
453{
454 if (other) m_refs = other->createWeak(this);
455}
456
457template<typename T>
458wp<T>::wp(const wp<T>& other)
459 : m_ptr(other.m_ptr), m_refs(other.m_refs)
460{
461 if (m_ptr) m_refs->incWeak(this);
462}
463
464template<typename T>
465wp<T>::wp(const sp<T>& other)
466 : m_ptr(other.m_ptr)
467{
468 if (m_ptr) {
469 m_refs = m_ptr->createWeak(this);
470 }
471}
472
473template<typename T> template<typename U>
474wp<T>::wp(U* other)
475 : m_ptr(other)
476{
477 if (other) m_refs = other->createWeak(this);
478}
479
480template<typename T> template<typename U>
481wp<T>::wp(const wp<U>& other)
482 : m_ptr(other.m_ptr)
483{
484 if (m_ptr) {
485 m_refs = other.m_refs;
486 m_refs->incWeak(this);
487 }
488}
489
490template<typename T> template<typename U>
491wp<T>::wp(const sp<U>& other)
492 : m_ptr(other.m_ptr)
493{
494 if (m_ptr) {
495 m_refs = m_ptr->createWeak(this);
496 }
497}
498
499template<typename T>
500wp<T>::~wp()
501{
502 if (m_ptr) m_refs->decWeak(this);
503}
504
505template<typename T>
506wp<T>& wp<T>::operator = (T* other)
507{
508 weakref_type* newRefs =
509 other ? other->createWeak(this) : 0;
510 if (m_ptr) m_refs->decWeak(this);
511 m_ptr = other;
512 m_refs = newRefs;
513 return *this;
514}
515
516template<typename T>
517wp<T>& wp<T>::operator = (const wp<T>& other)
518{
Mathias Agopian51a6aef2010-06-24 21:49:02 -0700519 weakref_type* otherRefs(other.m_refs);
520 T* otherPtr(other.m_ptr);
521 if (otherPtr) otherRefs->incWeak(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800522 if (m_ptr) m_refs->decWeak(this);
Mathias Agopian51a6aef2010-06-24 21:49:02 -0700523 m_ptr = otherPtr;
524 m_refs = otherRefs;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800525 return *this;
526}
527
528template<typename T>
529wp<T>& wp<T>::operator = (const sp<T>& other)
530{
531 weakref_type* newRefs =
532 other != NULL ? other->createWeak(this) : 0;
Mathias Agopian51a6aef2010-06-24 21:49:02 -0700533 T* otherPtr(other.m_ptr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800534 if (m_ptr) m_refs->decWeak(this);
Mathias Agopian51a6aef2010-06-24 21:49:02 -0700535 m_ptr = otherPtr;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800536 m_refs = newRefs;
537 return *this;
538}
539
540template<typename T> template<typename U>
541wp<T>& wp<T>::operator = (U* other)
542{
543 weakref_type* newRefs =
544 other ? other->createWeak(this) : 0;
545 if (m_ptr) m_refs->decWeak(this);
546 m_ptr = other;
547 m_refs = newRefs;
548 return *this;
549}
550
551template<typename T> template<typename U>
552wp<T>& wp<T>::operator = (const wp<U>& other)
553{
Mathias Agopian51a6aef2010-06-24 21:49:02 -0700554 weakref_type* otherRefs(other.m_refs);
555 U* otherPtr(other.m_ptr);
556 if (otherPtr) otherRefs->incWeak(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800557 if (m_ptr) m_refs->decWeak(this);
Mathias Agopian51a6aef2010-06-24 21:49:02 -0700558 m_ptr = otherPtr;
559 m_refs = otherRefs;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800560 return *this;
561}
562
563template<typename T> template<typename U>
564wp<T>& wp<T>::operator = (const sp<U>& other)
565{
566 weakref_type* newRefs =
567 other != NULL ? other->createWeak(this) : 0;
Mathias Agopian51a6aef2010-06-24 21:49:02 -0700568 U* otherPtr(other.m_ptr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800569 if (m_ptr) m_refs->decWeak(this);
Mathias Agopian51a6aef2010-06-24 21:49:02 -0700570 m_ptr = otherPtr;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800571 m_refs = newRefs;
572 return *this;
573}
574
575template<typename T>
576void wp<T>::set_object_and_refs(T* other, weakref_type* refs)
577{
578 if (other) refs->incWeak(this);
579 if (m_ptr) m_refs->decWeak(this);
580 m_ptr = other;
581 m_refs = refs;
582}
583
584template<typename T>
585sp<T> wp<T>::promote() const
586{
587 return sp<T>(m_ptr, m_refs);
588}
589
590template<typename T>
591void wp<T>::clear()
592{
593 if (m_ptr) {
594 m_refs->decWeak(this);
595 m_ptr = 0;
596 }
597}
598
599template <typename T>
600inline TextOutput& operator<<(TextOutput& to, const wp<T>& val)
601{
602 to << "wp<>(" << val.unsafe_get() << ")";
603 return to;
604}
605
606}; // namespace android
607
608// ---------------------------------------------------------------------------
609
610#endif // ANDROID_REF_BASE_H