blob: 8e304e75d1e9a33371da40d827d494bb6357b8d0 [file] [log] [blame]
The Android Open Source Projectcbb10112009-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>
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080021
22#include <stdint.h>
23#include <sys/types.h>
24#include <stdlib.h>
Mathias Agopianb26ea8b2011-02-16 20:23:43 -080025#include <string.h>
26
27#include <utils/StrongPointer.h>
Jeff Brown9a0a76d2012-03-16 14:45:49 -070028#include <utils/TypeHelpers.h>
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080029
30// ---------------------------------------------------------------------------
31namespace android {
32
Mathias Agopian84a23fa2011-02-16 15:23:08 -080033class TextOutput;
Mathias Agopian84a23fa2011-02-16 15:23:08 -080034TextOutput& printWeakPointer(TextOutput& to, const void* val);
35
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080036// ---------------------------------------------------------------------------
37
Mathias Agopianff49de72011-02-09 18:38:55 -080038#define COMPARE_WEAK(_op_) \
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080039inline bool operator _op_ (const sp<T>& o) const { \
40 return m_ptr _op_ o.m_ptr; \
41} \
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080042inline bool operator _op_ (const T* o) const { \
43 return m_ptr _op_ o; \
44} \
45template<typename U> \
46inline bool operator _op_ (const sp<U>& o) const { \
47 return m_ptr _op_ o.m_ptr; \
48} \
49template<typename U> \
Mathias Agopianff49de72011-02-09 18:38:55 -080050inline bool operator _op_ (const U* o) const { \
51 return m_ptr _op_ o; \
52}
53
Mathias Agopianb26ea8b2011-02-16 20:23:43 -080054// ---------------------------------------------------------------------------
Mathias Agopianb26ea8b2011-02-16 20:23:43 -080055class ReferenceMover;
56class ReferenceConverterBase {
57public:
58 virtual size_t getReferenceTypeSize() const = 0;
59 virtual void* getReferenceBase(void const*) const = 0;
60 inline virtual ~ReferenceConverterBase() { }
61};
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080062
63// ---------------------------------------------------------------------------
64
65class RefBase
66{
67public:
68 void incStrong(const void* id) const;
69 void decStrong(const void* id) const;
70
71 void forceIncStrong(const void* id) const;
72
73 //! DEBUGGING ONLY: Get current strong ref count.
74 int32_t getStrongCount() const;
75
76 class weakref_type
77 {
78 public:
79 RefBase* refBase() const;
80
81 void incWeak(const void* id);
82 void decWeak(const void* id);
83
Mathias Agopianad099652011-08-10 21:07:02 -070084 // acquires a strong reference if there is already one.
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080085 bool attemptIncStrong(const void* id);
86
Mathias Agopianad099652011-08-10 21:07:02 -070087 // acquires a weak reference if there is already one.
88 // This is not always safe. see ProcessState.cpp and BpBinder.cpp
89 // for proper use.
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080090 bool attemptIncWeak(const void* id);
91
92 //! DEBUGGING ONLY: Get current weak ref count.
93 int32_t getWeakCount() const;
94
95 //! DEBUGGING ONLY: Print references held on object.
96 void printRefs() const;
97
98 //! DEBUGGING ONLY: Enable tracking for this object.
99 // enable -- enable/disable tracking
100 // retain -- when tracking is enable, if true, then we save a stack trace
101 // for each reference and dereference; when retain == false, we
102 // match up references and dereferences and keep only the
103 // outstanding ones.
104
105 void trackMe(bool enable, bool retain);
106 };
107
108 weakref_type* createWeak(const void* id) const;
109
110 weakref_type* getWeakRefs() const;
111
112 //! DEBUGGING ONLY: Print references held on object.
113 inline void printRefs() const { getWeakRefs()->printRefs(); }
114
115 //! DEBUGGING ONLY: Enable tracking of object.
116 inline void trackMe(bool enable, bool retain)
117 {
118 getWeakRefs()->trackMe(enable, retain);
119 }
120
Mathias Agopianb26ea8b2011-02-16 20:23:43 -0800121 typedef RefBase basetype;
122
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800123protected:
124 RefBase();
125 virtual ~RefBase();
Mathias Agopian7f57eac2011-06-16 17:15:51 -0700126
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800127 //! Flags for extendObjectLifetime()
128 enum {
Mathias Agopianad099652011-08-10 21:07:02 -0700129 OBJECT_LIFETIME_STRONG = 0x0000,
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800130 OBJECT_LIFETIME_WEAK = 0x0001,
Mathias Agopianad099652011-08-10 21:07:02 -0700131 OBJECT_LIFETIME_MASK = 0x0001
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800132 };
133
134 void extendObjectLifetime(int32_t mode);
135
136 //! Flags for onIncStrongAttempted()
137 enum {
138 FIRST_INC_STRONG = 0x0001
139 };
140
141 virtual void onFirstRef();
142 virtual void onLastStrongRef(const void* id);
143 virtual bool onIncStrongAttempted(uint32_t flags, const void* id);
144 virtual void onLastWeakRef(const void* id);
145
146private:
Mathias Agopianb26ea8b2011-02-16 20:23:43 -0800147 friend class ReferenceMover;
148 static void moveReferences(void* d, void const* s, size_t n,
149 const ReferenceConverterBase& caster);
150
151private:
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800152 friend class weakref_type;
153 class weakref_impl;
154
155 RefBase(const RefBase& o);
156 RefBase& operator=(const RefBase& o);
Mathias Agopianad099652011-08-10 21:07:02 -0700157
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800158 weakref_impl* const mRefs;
159};
160
161// ---------------------------------------------------------------------------
162
163template <class T>
164class LightRefBase
165{
166public:
167 inline LightRefBase() : mCount(0) { }
Igor Murashkina27c1e02012-12-05 16:10:26 -0800168 inline void incStrong(__attribute__((unused)) const void* id) const {
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800169 android_atomic_inc(&mCount);
170 }
Igor Murashkina27c1e02012-12-05 16:10:26 -0800171 inline void decStrong(__attribute__((unused)) const void* id) const {
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800172 if (android_atomic_dec(&mCount) == 1) {
173 delete static_cast<const T*>(this);
174 }
175 }
Mathias Agopian019f8ed2009-05-04 14:17:04 -0700176 //! DEBUGGING ONLY: Get current strong ref count.
177 inline int32_t getStrongCount() const {
178 return mCount;
179 }
Mathias Agopianb26ea8b2011-02-16 20:23:43 -0800180
181 typedef LightRefBase<T> basetype;
182
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800183protected:
184 inline ~LightRefBase() { }
Mathias Agopianb26ea8b2011-02-16 20:23:43 -0800185
186private:
187 friend class ReferenceMover;
188 inline static void moveReferences(void* d, void const* s, size_t n,
189 const ReferenceConverterBase& caster) { }
190
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800191private:
192 mutable volatile int32_t mCount;
193};
194
195// ---------------------------------------------------------------------------
196
197template <typename T>
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800198class wp
199{
200public:
201 typedef typename RefBase::weakref_type weakref_type;
202
203 inline wp() : m_ptr(0) { }
204
205 wp(T* other);
206 wp(const wp<T>& other);
207 wp(const sp<T>& other);
208 template<typename U> wp(U* other);
209 template<typename U> wp(const sp<U>& other);
210 template<typename U> wp(const wp<U>& other);
211
212 ~wp();
213
214 // Assignment
215
216 wp& operator = (T* other);
217 wp& operator = (const wp<T>& other);
218 wp& operator = (const sp<T>& other);
219
220 template<typename U> wp& operator = (U* other);
221 template<typename U> wp& operator = (const wp<U>& other);
222 template<typename U> wp& operator = (const sp<U>& other);
223
224 void set_object_and_refs(T* other, weakref_type* refs);
225
226 // promotion to sp
227
228 sp<T> promote() const;
229
230 // Reset
231
232 void clear();
233
234 // Accessors
235
236 inline weakref_type* get_refs() const { return m_refs; }
237
238 inline T* unsafe_get() const { return m_ptr; }
239
240 // Operators
Mathias Agopianff49de72011-02-09 18:38:55 -0800241
242 COMPARE_WEAK(==)
243 COMPARE_WEAK(!=)
244 COMPARE_WEAK(>)
245 COMPARE_WEAK(<)
246 COMPARE_WEAK(<=)
247 COMPARE_WEAK(>=)
248
249 inline bool operator == (const wp<T>& o) const {
250 return (m_ptr == o.m_ptr) && (m_refs == o.m_refs);
251 }
252 template<typename U>
253 inline bool operator == (const wp<U>& o) const {
254 return m_ptr == o.m_ptr;
255 }
256
257 inline bool operator > (const wp<T>& o) const {
258 return (m_ptr == o.m_ptr) ? (m_refs > o.m_refs) : (m_ptr > o.m_ptr);
259 }
260 template<typename U>
261 inline bool operator > (const wp<U>& o) const {
262 return (m_ptr == o.m_ptr) ? (m_refs > o.m_refs) : (m_ptr > o.m_ptr);
263 }
264
265 inline bool operator < (const wp<T>& o) const {
266 return (m_ptr == o.m_ptr) ? (m_refs < o.m_refs) : (m_ptr < o.m_ptr);
267 }
268 template<typename U>
269 inline bool operator < (const wp<U>& o) const {
270 return (m_ptr == o.m_ptr) ? (m_refs < o.m_refs) : (m_ptr < o.m_ptr);
271 }
272 inline bool operator != (const wp<T>& o) const { return m_refs != o.m_refs; }
273 template<typename U> inline bool operator != (const wp<U>& o) const { return !operator == (o); }
274 inline bool operator <= (const wp<T>& o) const { return !operator > (o); }
275 template<typename U> inline bool operator <= (const wp<U>& o) const { return !operator > (o); }
276 inline bool operator >= (const wp<T>& o) const { return !operator < (o); }
277 template<typename U> inline bool operator >= (const wp<U>& o) const { return !operator < (o); }
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800278
279private:
280 template<typename Y> friend class sp;
281 template<typename Y> friend class wp;
282
283 T* m_ptr;
284 weakref_type* m_refs;
285};
286
287template <typename T>
288TextOutput& operator<<(TextOutput& to, const wp<T>& val);
289
Mathias Agopianff49de72011-02-09 18:38:55 -0800290#undef COMPARE_WEAK
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800291
292// ---------------------------------------------------------------------------
293// No user serviceable parts below here.
294
295template<typename T>
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800296wp<T>::wp(T* other)
297 : m_ptr(other)
298{
299 if (other) m_refs = other->createWeak(this);
300}
301
302template<typename T>
303wp<T>::wp(const wp<T>& other)
304 : m_ptr(other.m_ptr), m_refs(other.m_refs)
305{
306 if (m_ptr) m_refs->incWeak(this);
307}
308
309template<typename T>
310wp<T>::wp(const sp<T>& other)
311 : m_ptr(other.m_ptr)
312{
313 if (m_ptr) {
314 m_refs = m_ptr->createWeak(this);
315 }
316}
317
318template<typename T> template<typename U>
319wp<T>::wp(U* other)
320 : m_ptr(other)
321{
322 if (other) m_refs = other->createWeak(this);
323}
324
325template<typename T> template<typename U>
326wp<T>::wp(const wp<U>& other)
327 : m_ptr(other.m_ptr)
328{
329 if (m_ptr) {
330 m_refs = other.m_refs;
331 m_refs->incWeak(this);
332 }
333}
334
335template<typename T> template<typename U>
336wp<T>::wp(const sp<U>& other)
337 : m_ptr(other.m_ptr)
338{
339 if (m_ptr) {
340 m_refs = m_ptr->createWeak(this);
341 }
342}
343
344template<typename T>
345wp<T>::~wp()
346{
347 if (m_ptr) m_refs->decWeak(this);
348}
349
350template<typename T>
351wp<T>& wp<T>::operator = (T* other)
352{
353 weakref_type* newRefs =
354 other ? other->createWeak(this) : 0;
355 if (m_ptr) m_refs->decWeak(this);
356 m_ptr = other;
357 m_refs = newRefs;
358 return *this;
359}
360
361template<typename T>
362wp<T>& wp<T>::operator = (const wp<T>& other)
363{
Mathias Agopian7b151672010-06-24 21:49:02 -0700364 weakref_type* otherRefs(other.m_refs);
365 T* otherPtr(other.m_ptr);
366 if (otherPtr) otherRefs->incWeak(this);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800367 if (m_ptr) m_refs->decWeak(this);
Mathias Agopian7b151672010-06-24 21:49:02 -0700368 m_ptr = otherPtr;
369 m_refs = otherRefs;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800370 return *this;
371}
372
373template<typename T>
374wp<T>& wp<T>::operator = (const sp<T>& other)
375{
376 weakref_type* newRefs =
377 other != NULL ? other->createWeak(this) : 0;
Mathias Agopian7b151672010-06-24 21:49:02 -0700378 T* otherPtr(other.m_ptr);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800379 if (m_ptr) m_refs->decWeak(this);
Mathias Agopian7b151672010-06-24 21:49:02 -0700380 m_ptr = otherPtr;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800381 m_refs = newRefs;
382 return *this;
383}
384
385template<typename T> template<typename U>
386wp<T>& wp<T>::operator = (U* other)
387{
388 weakref_type* newRefs =
389 other ? other->createWeak(this) : 0;
390 if (m_ptr) m_refs->decWeak(this);
391 m_ptr = other;
392 m_refs = newRefs;
393 return *this;
394}
395
396template<typename T> template<typename U>
397wp<T>& wp<T>::operator = (const wp<U>& other)
398{
Mathias Agopian7b151672010-06-24 21:49:02 -0700399 weakref_type* otherRefs(other.m_refs);
400 U* otherPtr(other.m_ptr);
401 if (otherPtr) otherRefs->incWeak(this);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800402 if (m_ptr) m_refs->decWeak(this);
Mathias Agopian7b151672010-06-24 21:49:02 -0700403 m_ptr = otherPtr;
404 m_refs = otherRefs;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800405 return *this;
406}
407
408template<typename T> template<typename U>
409wp<T>& wp<T>::operator = (const sp<U>& other)
410{
411 weakref_type* newRefs =
412 other != NULL ? other->createWeak(this) : 0;
Mathias Agopian7b151672010-06-24 21:49:02 -0700413 U* otherPtr(other.m_ptr);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800414 if (m_ptr) m_refs->decWeak(this);
Mathias Agopian7b151672010-06-24 21:49:02 -0700415 m_ptr = otherPtr;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800416 m_refs = newRefs;
417 return *this;
418}
419
420template<typename T>
421void wp<T>::set_object_and_refs(T* other, weakref_type* refs)
422{
423 if (other) refs->incWeak(this);
424 if (m_ptr) m_refs->decWeak(this);
425 m_ptr = other;
426 m_refs = refs;
427}
428
429template<typename T>
430sp<T> wp<T>::promote() const
431{
Mathias Agopian3e0f8752011-02-24 18:12:34 -0800432 sp<T> result;
433 if (m_ptr && m_refs->attemptIncStrong(&result)) {
434 result.set_pointer(m_ptr);
435 }
436 return result;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800437}
438
439template<typename T>
440void wp<T>::clear()
441{
442 if (m_ptr) {
443 m_refs->decWeak(this);
444 m_ptr = 0;
445 }
446}
447
448template <typename T>
449inline TextOutput& operator<<(TextOutput& to, const wp<T>& val)
450{
Mathias Agopian84a23fa2011-02-16 15:23:08 -0800451 return printWeakPointer(to, val.unsafe_get());
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800452}
453
Mathias Agopianb26ea8b2011-02-16 20:23:43 -0800454// ---------------------------------------------------------------------------
455
456// this class just serves as a namespace so TYPE::moveReferences can stay
457// private.
458
459class ReferenceMover {
460 // StrongReferenceCast and WeakReferenceCast do the impedance matching
461 // between the generic (void*) implementation in Refbase and the strongly typed
462 // template specializations below.
463
464 template <typename TYPE>
465 struct StrongReferenceCast : public ReferenceConverterBase {
466 virtual size_t getReferenceTypeSize() const { return sizeof( sp<TYPE> ); }
467 virtual void* getReferenceBase(void const* p) const {
468 sp<TYPE> const* sptr(reinterpret_cast<sp<TYPE> const*>(p));
469 return static_cast<typename TYPE::basetype *>(sptr->get());
470 }
471 };
472
473 template <typename TYPE>
474 struct WeakReferenceCast : public ReferenceConverterBase {
475 virtual size_t getReferenceTypeSize() const { return sizeof( wp<TYPE> ); }
476 virtual void* getReferenceBase(void const* p) const {
477 wp<TYPE> const* sptr(reinterpret_cast<wp<TYPE> const*>(p));
478 return static_cast<typename TYPE::basetype *>(sptr->unsafe_get());
479 }
480 };
481
482public:
483 template<typename TYPE> static inline
484 void move_references(sp<TYPE>* d, sp<TYPE> const* s, size_t n) {
485 memmove(d, s, n*sizeof(sp<TYPE>));
486 StrongReferenceCast<TYPE> caster;
487 TYPE::moveReferences(d, s, n, caster);
488 }
489 template<typename TYPE> static inline
490 void move_references(wp<TYPE>* d, wp<TYPE> const* s, size_t n) {
491 memmove(d, s, n*sizeof(wp<TYPE>));
492 WeakReferenceCast<TYPE> caster;
493 TYPE::moveReferences(d, s, n, caster);
494 }
495};
496
497// specialization for moving sp<> and wp<> types.
498// these are used by the [Sorted|Keyed]Vector<> implementations
499// sp<> and wp<> need to be handled specially, because they do not
500// have trivial copy operation in the general case (see RefBase.cpp
501// when DEBUG ops are enabled), but can be implemented very
502// efficiently in most cases.
503
504template<typename TYPE> inline
505void move_forward_type(sp<TYPE>* d, sp<TYPE> const* s, size_t n) {
506 ReferenceMover::move_references(d, s, n);
507}
508
509template<typename TYPE> inline
510void move_backward_type(sp<TYPE>* d, sp<TYPE> const* s, size_t n) {
511 ReferenceMover::move_references(d, s, n);
512}
513
514template<typename TYPE> inline
515void move_forward_type(wp<TYPE>* d, wp<TYPE> const* s, size_t n) {
516 ReferenceMover::move_references(d, s, n);
517}
518
519template<typename TYPE> inline
520void move_backward_type(wp<TYPE>* d, wp<TYPE> const* s, size_t n) {
521 ReferenceMover::move_references(d, s, n);
522}
523
524
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800525}; // namespace android
526
527// ---------------------------------------------------------------------------
528
529#endif // ANDROID_REF_BASE_H