blob: c82e7d9563cb010cd8e9852f4c77029388609380 [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
Hans Boehme263e6c2016-05-11 18:15:12 -070020#include <atomic>
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 Agopian6cd548c2013-03-18 22:27:41 -070055
56class ReferenceRenamer {
57protected:
58 // destructor is purposedly not virtual so we avoid code overhead from
59 // subclasses; we have to make it protected to guarantee that it
60 // cannot be called from this base class (and to make strict compilers
61 // happy).
62 ~ReferenceRenamer() { }
Mathias Agopianb26ea8b2011-02-16 20:23:43 -080063public:
Mathias Agopian6cd548c2013-03-18 22:27:41 -070064 virtual void operator()(size_t i) const = 0;
Mathias Agopianb26ea8b2011-02-16 20:23:43 -080065};
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080066
67// ---------------------------------------------------------------------------
68
69class RefBase
70{
71public:
72 void incStrong(const void* id) const;
73 void decStrong(const void* id) const;
74
75 void forceIncStrong(const void* id) const;
76
77 //! DEBUGGING ONLY: Get current strong ref count.
78 int32_t getStrongCount() const;
79
80 class weakref_type
81 {
82 public:
83 RefBase* refBase() const;
84
85 void incWeak(const void* id);
86 void decWeak(const void* id);
87
Mathias Agopianad099652011-08-10 21:07:02 -070088 // acquires a strong reference if there is already one.
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080089 bool attemptIncStrong(const void* id);
90
Mathias Agopianad099652011-08-10 21:07:02 -070091 // acquires a weak reference if there is already one.
92 // This is not always safe. see ProcessState.cpp and BpBinder.cpp
93 // for proper use.
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080094 bool attemptIncWeak(const void* id);
95
96 //! DEBUGGING ONLY: Get current weak ref count.
97 int32_t getWeakCount() const;
98
99 //! DEBUGGING ONLY: Print references held on object.
100 void printRefs() const;
101
102 //! DEBUGGING ONLY: Enable tracking for this object.
103 // enable -- enable/disable tracking
104 // retain -- when tracking is enable, if true, then we save a stack trace
105 // for each reference and dereference; when retain == false, we
106 // match up references and dereferences and keep only the
107 // outstanding ones.
108
109 void trackMe(bool enable, bool retain);
110 };
111
112 weakref_type* createWeak(const void* id) const;
113
114 weakref_type* getWeakRefs() const;
115
116 //! DEBUGGING ONLY: Print references held on object.
117 inline void printRefs() const { getWeakRefs()->printRefs(); }
118
119 //! DEBUGGING ONLY: Enable tracking of object.
120 inline void trackMe(bool enable, bool retain)
121 {
122 getWeakRefs()->trackMe(enable, retain);
123 }
124
Mathias Agopianb26ea8b2011-02-16 20:23:43 -0800125 typedef RefBase basetype;
126
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800127protected:
128 RefBase();
129 virtual ~RefBase();
Mathias Agopian7f57eac2011-06-16 17:15:51 -0700130
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800131 //! Flags for extendObjectLifetime()
132 enum {
Mathias Agopianad099652011-08-10 21:07:02 -0700133 OBJECT_LIFETIME_STRONG = 0x0000,
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800134 OBJECT_LIFETIME_WEAK = 0x0001,
Mathias Agopianad099652011-08-10 21:07:02 -0700135 OBJECT_LIFETIME_MASK = 0x0001
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800136 };
137
138 void extendObjectLifetime(int32_t mode);
139
140 //! Flags for onIncStrongAttempted()
141 enum {
142 FIRST_INC_STRONG = 0x0001
143 };
144
145 virtual void onFirstRef();
146 virtual void onLastStrongRef(const void* id);
147 virtual bool onIncStrongAttempted(uint32_t flags, const void* id);
148 virtual void onLastWeakRef(const void* id);
149
150private:
151 friend class weakref_type;
152 class weakref_impl;
153
154 RefBase(const RefBase& o);
155 RefBase& operator=(const RefBase& o);
Mathias Agopianad099652011-08-10 21:07:02 -0700156
Mathias Agopian6cd548c2013-03-18 22:27:41 -0700157private:
158 friend class ReferenceMover;
159
160 static void renameRefs(size_t n, const ReferenceRenamer& renamer);
161
162 static void renameRefId(weakref_type* ref,
163 const void* old_id, const void* new_id);
164
165 static void renameRefId(RefBase* ref,
166 const void* old_id, const void* new_id);
167
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800168 weakref_impl* const mRefs;
169};
170
171// ---------------------------------------------------------------------------
172
173template <class T>
174class LightRefBase
175{
176public:
177 inline LightRefBase() : mCount(0) { }
Igor Murashkina27c1e02012-12-05 16:10:26 -0800178 inline void incStrong(__attribute__((unused)) const void* id) const {
Hans Boehme263e6c2016-05-11 18:15:12 -0700179 mCount.fetch_add(1, std::memory_order_relaxed);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800180 }
Igor Murashkina27c1e02012-12-05 16:10:26 -0800181 inline void decStrong(__attribute__((unused)) const void* id) const {
Hans Boehme263e6c2016-05-11 18:15:12 -0700182 if (mCount.fetch_sub(1, std::memory_order_release) == 1) {
183 std::atomic_thread_fence(std::memory_order_acquire);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800184 delete static_cast<const T*>(this);
185 }
186 }
Mathias Agopian019f8ed2009-05-04 14:17:04 -0700187 //! DEBUGGING ONLY: Get current strong ref count.
188 inline int32_t getStrongCount() const {
Hans Boehme263e6c2016-05-11 18:15:12 -0700189 return mCount.load(std::memory_order_relaxed);
Mathias Agopian019f8ed2009-05-04 14:17:04 -0700190 }
Mathias Agopianb26ea8b2011-02-16 20:23:43 -0800191
192 typedef LightRefBase<T> basetype;
193
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800194protected:
195 inline ~LightRefBase() { }
Mathias Agopianb26ea8b2011-02-16 20:23:43 -0800196
197private:
198 friend class ReferenceMover;
Greg Kaiserd9885e72016-07-08 17:18:07 -0700199 inline static void renameRefs(size_t /*n*/,
200 const ReferenceRenamer& /*renamer*/) { }
201 inline static void renameRefId(T* /*ref*/,
202 const void* /*old_id*/ , const void* /*new_id*/) { }
Mathias Agopianb26ea8b2011-02-16 20:23:43 -0800203
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800204private:
Hans Boehme263e6c2016-05-11 18:15:12 -0700205 mutable std::atomic<int32_t> mCount;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800206};
207
John Reckd83186c2014-05-09 15:27:22 -0700208// This is a wrapper around LightRefBase that simply enforces a virtual
209// destructor to eliminate the template requirement of LightRefBase
210class VirtualLightRefBase : public LightRefBase<VirtualLightRefBase> {
211public:
212 virtual ~VirtualLightRefBase() {}
213};
214
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800215// ---------------------------------------------------------------------------
216
217template <typename T>
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800218class wp
219{
220public:
221 typedef typename RefBase::weakref_type weakref_type;
222
223 inline wp() : m_ptr(0) { }
224
225 wp(T* other);
226 wp(const wp<T>& other);
227 wp(const sp<T>& other);
228 template<typename U> wp(U* other);
229 template<typename U> wp(const sp<U>& other);
230 template<typename U> wp(const wp<U>& other);
231
232 ~wp();
233
234 // Assignment
235
236 wp& operator = (T* other);
237 wp& operator = (const wp<T>& other);
238 wp& operator = (const sp<T>& other);
239
240 template<typename U> wp& operator = (U* other);
241 template<typename U> wp& operator = (const wp<U>& other);
242 template<typename U> wp& operator = (const sp<U>& other);
243
244 void set_object_and_refs(T* other, weakref_type* refs);
245
246 // promotion to sp
247
248 sp<T> promote() const;
249
250 // Reset
251
252 void clear();
253
254 // Accessors
255
256 inline weakref_type* get_refs() const { return m_refs; }
257
258 inline T* unsafe_get() const { return m_ptr; }
259
260 // Operators
Mathias Agopianff49de72011-02-09 18:38:55 -0800261
262 COMPARE_WEAK(==)
263 COMPARE_WEAK(!=)
264 COMPARE_WEAK(>)
265 COMPARE_WEAK(<)
266 COMPARE_WEAK(<=)
267 COMPARE_WEAK(>=)
268
269 inline bool operator == (const wp<T>& o) const {
270 return (m_ptr == o.m_ptr) && (m_refs == o.m_refs);
271 }
272 template<typename U>
273 inline bool operator == (const wp<U>& o) const {
274 return m_ptr == o.m_ptr;
275 }
276
277 inline bool operator > (const wp<T>& o) const {
278 return (m_ptr == o.m_ptr) ? (m_refs > o.m_refs) : (m_ptr > o.m_ptr);
279 }
280 template<typename U>
281 inline bool operator > (const wp<U>& o) const {
282 return (m_ptr == o.m_ptr) ? (m_refs > o.m_refs) : (m_ptr > o.m_ptr);
283 }
284
285 inline bool operator < (const wp<T>& o) const {
286 return (m_ptr == o.m_ptr) ? (m_refs < o.m_refs) : (m_ptr < o.m_ptr);
287 }
288 template<typename U>
289 inline bool operator < (const wp<U>& o) const {
290 return (m_ptr == o.m_ptr) ? (m_refs < o.m_refs) : (m_ptr < o.m_ptr);
291 }
292 inline bool operator != (const wp<T>& o) const { return m_refs != o.m_refs; }
293 template<typename U> inline bool operator != (const wp<U>& o) const { return !operator == (o); }
294 inline bool operator <= (const wp<T>& o) const { return !operator > (o); }
295 template<typename U> inline bool operator <= (const wp<U>& o) const { return !operator > (o); }
296 inline bool operator >= (const wp<T>& o) const { return !operator < (o); }
297 template<typename U> inline bool operator >= (const wp<U>& o) const { return !operator < (o); }
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800298
299private:
300 template<typename Y> friend class sp;
301 template<typename Y> friend class wp;
302
303 T* m_ptr;
304 weakref_type* m_refs;
305};
306
307template <typename T>
308TextOutput& operator<<(TextOutput& to, const wp<T>& val);
309
Mathias Agopianff49de72011-02-09 18:38:55 -0800310#undef COMPARE_WEAK
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800311
312// ---------------------------------------------------------------------------
313// No user serviceable parts below here.
314
315template<typename T>
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800316wp<T>::wp(T* other)
317 : m_ptr(other)
318{
319 if (other) m_refs = other->createWeak(this);
320}
321
322template<typename T>
323wp<T>::wp(const wp<T>& other)
324 : m_ptr(other.m_ptr), m_refs(other.m_refs)
325{
326 if (m_ptr) m_refs->incWeak(this);
327}
328
329template<typename T>
330wp<T>::wp(const sp<T>& other)
331 : m_ptr(other.m_ptr)
332{
333 if (m_ptr) {
334 m_refs = m_ptr->createWeak(this);
335 }
336}
337
338template<typename T> template<typename U>
339wp<T>::wp(U* other)
340 : m_ptr(other)
341{
342 if (other) m_refs = other->createWeak(this);
343}
344
345template<typename T> template<typename U>
346wp<T>::wp(const wp<U>& other)
347 : m_ptr(other.m_ptr)
348{
349 if (m_ptr) {
350 m_refs = other.m_refs;
351 m_refs->incWeak(this);
352 }
353}
354
355template<typename T> template<typename U>
356wp<T>::wp(const sp<U>& other)
357 : m_ptr(other.m_ptr)
358{
359 if (m_ptr) {
360 m_refs = m_ptr->createWeak(this);
361 }
362}
363
364template<typename T>
365wp<T>::~wp()
366{
367 if (m_ptr) m_refs->decWeak(this);
368}
369
370template<typename T>
371wp<T>& wp<T>::operator = (T* other)
372{
373 weakref_type* newRefs =
374 other ? other->createWeak(this) : 0;
375 if (m_ptr) m_refs->decWeak(this);
376 m_ptr = other;
377 m_refs = newRefs;
378 return *this;
379}
380
381template<typename T>
382wp<T>& wp<T>::operator = (const wp<T>& other)
383{
Mathias Agopian7b151672010-06-24 21:49:02 -0700384 weakref_type* otherRefs(other.m_refs);
385 T* otherPtr(other.m_ptr);
386 if (otherPtr) otherRefs->incWeak(this);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800387 if (m_ptr) m_refs->decWeak(this);
Mathias Agopian7b151672010-06-24 21:49:02 -0700388 m_ptr = otherPtr;
389 m_refs = otherRefs;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800390 return *this;
391}
392
393template<typename T>
394wp<T>& wp<T>::operator = (const sp<T>& other)
395{
396 weakref_type* newRefs =
397 other != NULL ? other->createWeak(this) : 0;
Mathias Agopian7b151672010-06-24 21:49:02 -0700398 T* otherPtr(other.m_ptr);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800399 if (m_ptr) m_refs->decWeak(this);
Mathias Agopian7b151672010-06-24 21:49:02 -0700400 m_ptr = otherPtr;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800401 m_refs = newRefs;
402 return *this;
403}
404
405template<typename T> template<typename U>
406wp<T>& wp<T>::operator = (U* other)
407{
408 weakref_type* newRefs =
409 other ? other->createWeak(this) : 0;
410 if (m_ptr) m_refs->decWeak(this);
411 m_ptr = other;
412 m_refs = newRefs;
413 return *this;
414}
415
416template<typename T> template<typename U>
417wp<T>& wp<T>::operator = (const wp<U>& other)
418{
Mathias Agopian7b151672010-06-24 21:49:02 -0700419 weakref_type* otherRefs(other.m_refs);
420 U* otherPtr(other.m_ptr);
421 if (otherPtr) otherRefs->incWeak(this);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800422 if (m_ptr) m_refs->decWeak(this);
Mathias Agopian7b151672010-06-24 21:49:02 -0700423 m_ptr = otherPtr;
424 m_refs = otherRefs;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800425 return *this;
426}
427
428template<typename T> template<typename U>
429wp<T>& wp<T>::operator = (const sp<U>& other)
430{
431 weakref_type* newRefs =
432 other != NULL ? other->createWeak(this) : 0;
Mathias Agopian7b151672010-06-24 21:49:02 -0700433 U* otherPtr(other.m_ptr);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800434 if (m_ptr) m_refs->decWeak(this);
Mathias Agopian7b151672010-06-24 21:49:02 -0700435 m_ptr = otherPtr;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800436 m_refs = newRefs;
437 return *this;
438}
439
440template<typename T>
441void wp<T>::set_object_and_refs(T* other, weakref_type* refs)
442{
443 if (other) refs->incWeak(this);
444 if (m_ptr) m_refs->decWeak(this);
445 m_ptr = other;
446 m_refs = refs;
447}
448
449template<typename T>
450sp<T> wp<T>::promote() const
451{
Mathias Agopian3e0f8752011-02-24 18:12:34 -0800452 sp<T> result;
453 if (m_ptr && m_refs->attemptIncStrong(&result)) {
454 result.set_pointer(m_ptr);
455 }
456 return result;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800457}
458
459template<typename T>
460void wp<T>::clear()
461{
462 if (m_ptr) {
463 m_refs->decWeak(this);
464 m_ptr = 0;
465 }
466}
467
468template <typename T>
469inline TextOutput& operator<<(TextOutput& to, const wp<T>& val)
470{
Mathias Agopian84a23fa2011-02-16 15:23:08 -0800471 return printWeakPointer(to, val.unsafe_get());
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800472}
473
Mathias Agopianb26ea8b2011-02-16 20:23:43 -0800474// ---------------------------------------------------------------------------
475
476// this class just serves as a namespace so TYPE::moveReferences can stay
477// private.
Mathias Agopianb26ea8b2011-02-16 20:23:43 -0800478class ReferenceMover {
Mathias Agopianb26ea8b2011-02-16 20:23:43 -0800479public:
Mathias Agopian6cd548c2013-03-18 22:27:41 -0700480 // it would be nice if we could make sure no extra code is generated
481 // for sp<TYPE> or wp<TYPE> when TYPE is a descendant of RefBase:
482 // Using a sp<RefBase> override doesn't work; it's a bit like we wanted
483 // a template<typename TYPE inherits RefBase> template...
484
Mathias Agopianb26ea8b2011-02-16 20:23:43 -0800485 template<typename TYPE> static inline
486 void move_references(sp<TYPE>* d, sp<TYPE> const* s, size_t n) {
Mathias Agopian6cd548c2013-03-18 22:27:41 -0700487
488 class Renamer : public ReferenceRenamer {
489 sp<TYPE>* d;
490 sp<TYPE> const* s;
491 virtual void operator()(size_t i) const {
492 // The id are known to be the sp<>'s this pointer
493 TYPE::renameRefId(d[i].get(), &s[i], &d[i]);
494 }
495 public:
Ukri Niemimuukko00e56a22014-04-29 06:25:28 +0800496 Renamer(sp<TYPE>* d, sp<TYPE> const* s) : d(d), s(s) { }
497 virtual ~Renamer() { }
Mathias Agopian6cd548c2013-03-18 22:27:41 -0700498 };
499
Mathias Agopianb26ea8b2011-02-16 20:23:43 -0800500 memmove(d, s, n*sizeof(sp<TYPE>));
Mathias Agopian6cd548c2013-03-18 22:27:41 -0700501 TYPE::renameRefs(n, Renamer(d, s));
Mathias Agopianb26ea8b2011-02-16 20:23:43 -0800502 }
Mathias Agopian6cd548c2013-03-18 22:27:41 -0700503
504
Mathias Agopianb26ea8b2011-02-16 20:23:43 -0800505 template<typename TYPE> static inline
506 void move_references(wp<TYPE>* d, wp<TYPE> const* s, size_t n) {
Mathias Agopian6cd548c2013-03-18 22:27:41 -0700507
508 class Renamer : public ReferenceRenamer {
509 wp<TYPE>* d;
510 wp<TYPE> const* s;
511 virtual void operator()(size_t i) const {
512 // The id are known to be the wp<>'s this pointer
513 TYPE::renameRefId(d[i].get_refs(), &s[i], &d[i]);
514 }
515 public:
Ukri Niemimuukko00e56a22014-04-29 06:25:28 +0800516 Renamer(wp<TYPE>* d, wp<TYPE> const* s) : d(d), s(s) { }
517 virtual ~Renamer() { }
Mathias Agopian6cd548c2013-03-18 22:27:41 -0700518 };
519
Mathias Agopianb26ea8b2011-02-16 20:23:43 -0800520 memmove(d, s, n*sizeof(wp<TYPE>));
Mathias Agopian6cd548c2013-03-18 22:27:41 -0700521 TYPE::renameRefs(n, Renamer(d, s));
Mathias Agopianb26ea8b2011-02-16 20:23:43 -0800522 }
523};
524
525// specialization for moving sp<> and wp<> types.
526// these are used by the [Sorted|Keyed]Vector<> implementations
527// sp<> and wp<> need to be handled specially, because they do not
528// have trivial copy operation in the general case (see RefBase.cpp
529// when DEBUG ops are enabled), but can be implemented very
530// efficiently in most cases.
531
532template<typename TYPE> inline
533void move_forward_type(sp<TYPE>* d, sp<TYPE> const* s, size_t n) {
534 ReferenceMover::move_references(d, s, n);
535}
536
537template<typename TYPE> inline
538void move_backward_type(sp<TYPE>* d, sp<TYPE> const* s, size_t n) {
539 ReferenceMover::move_references(d, s, n);
540}
541
542template<typename TYPE> inline
543void move_forward_type(wp<TYPE>* d, wp<TYPE> const* s, size_t n) {
544 ReferenceMover::move_references(d, s, n);
545}
546
547template<typename TYPE> inline
548void move_backward_type(wp<TYPE>* d, wp<TYPE> const* s, size_t n) {
549 ReferenceMover::move_references(d, s, n);
550}
551
552
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800553}; // namespace android
554
555// ---------------------------------------------------------------------------
556
557#endif // ANDROID_REF_BASE_H