2 * Copyright (C) 2005 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef ANDROID_REF_BASE_H
18 #define ANDROID_REF_BASE_H
20 #include <cutils/atomic.h>
23 #include <sys/types.h>
27 #include <utils/StrongPointer.h>
28 #include <utils/TypeHelpers.h>
30 // ---------------------------------------------------------------------------
34 TextOutput& printWeakPointer(TextOutput& to, const void* val);
36 // ---------------------------------------------------------------------------
38 #define COMPARE_WEAK(_op_) \
39 inline bool operator _op_ (const sp<T>& o) const { \
40 return m_ptr _op_ o.m_ptr; \
42 inline bool operator _op_ (const T* o) const { \
43 return m_ptr _op_ o; \
45 template<typename U> \
46 inline bool operator _op_ (const sp<U>& o) const { \
47 return m_ptr _op_ o.m_ptr; \
49 template<typename U> \
50 inline bool operator _op_ (const U* o) const { \
51 return m_ptr _op_ o; \
54 // ---------------------------------------------------------------------------
56 class ReferenceRenamer {
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
62 ~ReferenceRenamer() { }
64 virtual void operator()(size_t i) const = 0;
67 // ---------------------------------------------------------------------------
72 void incStrong(const void* id) const;
73 void decStrong(const void* id) const;
75 void forceIncStrong(const void* id) const;
77 //! DEBUGGING ONLY: Get current strong ref count.
78 int32_t getStrongCount() const;
83 RefBase* refBase() const;
85 void incWeak(const void* id);
86 void decWeak(const void* id);
88 // acquires a strong reference if there is already one.
89 bool attemptIncStrong(const void* id);
91 // acquires a weak reference if there is already one.
92 // This is not always safe. see ProcessState.cpp and BpBinder.cpp
94 bool attemptIncWeak(const void* id);
96 //! DEBUGGING ONLY: Get current weak ref count.
97 int32_t getWeakCount() const;
99 //! DEBUGGING ONLY: Print references held on object.
100 void printRefs() const;
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
109 void trackMe(bool enable, bool retain);
112 weakref_type* createWeak(const void* id) const;
114 weakref_type* getWeakRefs() const;
116 //! DEBUGGING ONLY: Print references held on object.
117 inline void printRefs() const { getWeakRefs()->printRefs(); }
119 //! DEBUGGING ONLY: Enable tracking of object.
120 inline void trackMe(bool enable, bool retain)
122 getWeakRefs()->trackMe(enable, retain);
125 typedef RefBase basetype;
131 //! Flags for extendObjectLifetime()
133 OBJECT_LIFETIME_STRONG = 0x0000,
134 OBJECT_LIFETIME_WEAK = 0x0001,
135 OBJECT_LIFETIME_MASK = 0x0001
138 void extendObjectLifetime(int32_t mode);
140 //! Flags for onIncStrongAttempted()
142 FIRST_INC_STRONG = 0x0001
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);
151 friend class weakref_type;
154 RefBase(const RefBase& o);
155 RefBase& operator=(const RefBase& o);
158 friend class ReferenceMover;
160 static void renameRefs(size_t n, const ReferenceRenamer& renamer);
162 static void renameRefId(weakref_type* ref,
163 const void* old_id, const void* new_id);
165 static void renameRefId(RefBase* ref,
166 const void* old_id, const void* new_id);
168 weakref_impl* const mRefs;
171 // ---------------------------------------------------------------------------
177 inline LightRefBase() : mCount(0) { }
178 inline void incStrong(__attribute__((unused)) const void* id) const {
179 android_atomic_inc(&mCount);
181 inline void decStrong(__attribute__((unused)) const void* id) const {
182 if (android_atomic_dec(&mCount) == 1) {
183 delete static_cast<const T*>(this);
186 //! DEBUGGING ONLY: Get current strong ref count.
187 inline int32_t getStrongCount() const {
191 typedef LightRefBase<T> basetype;
194 inline ~LightRefBase() { }
197 friend class ReferenceMover;
198 inline static void renameRefs(size_t n, const ReferenceRenamer& renamer) { }
199 inline static void renameRefId(T* ref,
200 const void* old_id, const void* new_id) { }
203 mutable volatile int32_t mCount;
206 // ---------------------------------------------------------------------------
208 template <typename T>
212 typedef typename RefBase::weakref_type weakref_type;
214 inline wp() : m_ptr(0) { }
217 wp(const wp<T>& other);
218 wp(const sp<T>& other);
219 template<typename U> wp(U* other);
220 template<typename U> wp(const sp<U>& other);
221 template<typename U> wp(const wp<U>& other);
227 wp& operator = (T* other);
228 wp& operator = (const wp<T>& other);
229 wp& operator = (const sp<T>& other);
231 template<typename U> wp& operator = (U* other);
232 template<typename U> wp& operator = (const wp<U>& other);
233 template<typename U> wp& operator = (const sp<U>& other);
235 void set_object_and_refs(T* other, weakref_type* refs);
239 sp<T> promote() const;
247 inline weakref_type* get_refs() const { return m_refs; }
249 inline T* unsafe_get() const { return m_ptr; }
260 inline bool operator == (const wp<T>& o) const {
261 return (m_ptr == o.m_ptr) && (m_refs == o.m_refs);
264 inline bool operator == (const wp<U>& o) const {
265 return m_ptr == o.m_ptr;
268 inline bool operator > (const wp<T>& o) const {
269 return (m_ptr == o.m_ptr) ? (m_refs > o.m_refs) : (m_ptr > o.m_ptr);
272 inline bool operator > (const wp<U>& o) const {
273 return (m_ptr == o.m_ptr) ? (m_refs > o.m_refs) : (m_ptr > o.m_ptr);
276 inline bool operator < (const wp<T>& o) const {
277 return (m_ptr == o.m_ptr) ? (m_refs < o.m_refs) : (m_ptr < o.m_ptr);
280 inline bool operator < (const wp<U>& o) const {
281 return (m_ptr == o.m_ptr) ? (m_refs < o.m_refs) : (m_ptr < o.m_ptr);
283 inline bool operator != (const wp<T>& o) const { return m_refs != o.m_refs; }
284 template<typename U> inline bool operator != (const wp<U>& o) const { return !operator == (o); }
285 inline bool operator <= (const wp<T>& o) const { return !operator > (o); }
286 template<typename U> inline bool operator <= (const wp<U>& o) const { return !operator > (o); }
287 inline bool operator >= (const wp<T>& o) const { return !operator < (o); }
288 template<typename U> inline bool operator >= (const wp<U>& o) const { return !operator < (o); }
291 template<typename Y> friend class sp;
292 template<typename Y> friend class wp;
295 weakref_type* m_refs;
298 template <typename T>
299 TextOutput& operator<<(TextOutput& to, const wp<T>& val);
303 // ---------------------------------------------------------------------------
304 // No user serviceable parts below here.
310 if (other) m_refs = other->createWeak(this);
314 wp<T>::wp(const wp<T>& other)
315 : m_ptr(other.m_ptr), m_refs(other.m_refs)
317 if (m_ptr) m_refs->incWeak(this);
321 wp<T>::wp(const sp<T>& other)
325 m_refs = m_ptr->createWeak(this);
329 template<typename T> template<typename U>
333 if (other) m_refs = other->createWeak(this);
336 template<typename T> template<typename U>
337 wp<T>::wp(const wp<U>& other)
341 m_refs = other.m_refs;
342 m_refs->incWeak(this);
346 template<typename T> template<typename U>
347 wp<T>::wp(const sp<U>& other)
351 m_refs = m_ptr->createWeak(this);
358 if (m_ptr) m_refs->decWeak(this);
362 wp<T>& wp<T>::operator = (T* other)
364 weakref_type* newRefs =
365 other ? other->createWeak(this) : 0;
366 if (m_ptr) m_refs->decWeak(this);
373 wp<T>& wp<T>::operator = (const wp<T>& other)
375 weakref_type* otherRefs(other.m_refs);
376 T* otherPtr(other.m_ptr);
377 if (otherPtr) otherRefs->incWeak(this);
378 if (m_ptr) m_refs->decWeak(this);
385 wp<T>& wp<T>::operator = (const sp<T>& other)
387 weakref_type* newRefs =
388 other != NULL ? other->createWeak(this) : 0;
389 T* otherPtr(other.m_ptr);
390 if (m_ptr) m_refs->decWeak(this);
396 template<typename T> template<typename U>
397 wp<T>& wp<T>::operator = (U* other)
399 weakref_type* newRefs =
400 other ? other->createWeak(this) : 0;
401 if (m_ptr) m_refs->decWeak(this);
407 template<typename T> template<typename U>
408 wp<T>& wp<T>::operator = (const wp<U>& other)
410 weakref_type* otherRefs(other.m_refs);
411 U* otherPtr(other.m_ptr);
412 if (otherPtr) otherRefs->incWeak(this);
413 if (m_ptr) m_refs->decWeak(this);
419 template<typename T> template<typename U>
420 wp<T>& wp<T>::operator = (const sp<U>& other)
422 weakref_type* newRefs =
423 other != NULL ? other->createWeak(this) : 0;
424 U* otherPtr(other.m_ptr);
425 if (m_ptr) m_refs->decWeak(this);
432 void wp<T>::set_object_and_refs(T* other, weakref_type* refs)
434 if (other) refs->incWeak(this);
435 if (m_ptr) m_refs->decWeak(this);
441 sp<T> wp<T>::promote() const
444 if (m_ptr && m_refs->attemptIncStrong(&result)) {
445 result.set_pointer(m_ptr);
454 m_refs->decWeak(this);
459 template <typename T>
460 inline TextOutput& operator<<(TextOutput& to, const wp<T>& val)
462 return printWeakPointer(to, val.unsafe_get());
465 // ---------------------------------------------------------------------------
467 // this class just serves as a namespace so TYPE::moveReferences can stay
469 class ReferenceMover {
471 // it would be nice if we could make sure no extra code is generated
472 // for sp<TYPE> or wp<TYPE> when TYPE is a descendant of RefBase:
473 // Using a sp<RefBase> override doesn't work; it's a bit like we wanted
474 // a template<typename TYPE inherits RefBase> template...
476 template<typename TYPE> static inline
477 void move_references(sp<TYPE>* d, sp<TYPE> const* s, size_t n) {
479 class Renamer : public ReferenceRenamer {
482 virtual void operator()(size_t i) const {
483 // The id are known to be the sp<>'s this pointer
484 TYPE::renameRefId(d[i].get(), &s[i], &d[i]);
487 Renamer(sp<TYPE>* d, sp<TYPE> const* s) : s(s), d(d) { }
490 memmove(d, s, n*sizeof(sp<TYPE>));
491 TYPE::renameRefs(n, Renamer(d, s));
495 template<typename TYPE> static inline
496 void move_references(wp<TYPE>* d, wp<TYPE> const* s, size_t n) {
498 class Renamer : public ReferenceRenamer {
501 virtual void operator()(size_t i) const {
502 // The id are known to be the wp<>'s this pointer
503 TYPE::renameRefId(d[i].get_refs(), &s[i], &d[i]);
506 Renamer(wp<TYPE>* d, wp<TYPE> const* s) : s(s), d(d) { }
509 memmove(d, s, n*sizeof(wp<TYPE>));
510 TYPE::renameRefs(n, Renamer(d, s));
514 // specialization for moving sp<> and wp<> types.
515 // these are used by the [Sorted|Keyed]Vector<> implementations
516 // sp<> and wp<> need to be handled specially, because they do not
517 // have trivial copy operation in the general case (see RefBase.cpp
518 // when DEBUG ops are enabled), but can be implemented very
519 // efficiently in most cases.
521 template<typename TYPE> inline
522 void move_forward_type(sp<TYPE>* d, sp<TYPE> const* s, size_t n) {
523 ReferenceMover::move_references(d, s, n);
526 template<typename TYPE> inline
527 void move_backward_type(sp<TYPE>* d, sp<TYPE> const* s, size_t n) {
528 ReferenceMover::move_references(d, s, n);
531 template<typename TYPE> inline
532 void move_forward_type(wp<TYPE>* d, wp<TYPE> const* s, size_t n) {
533 ReferenceMover::move_references(d, s, n);
536 template<typename TYPE> inline
537 void move_backward_type(wp<TYPE>* d, wp<TYPE> const* s, size_t n) {
538 ReferenceMover::move_references(d, s, n);
542 }; // namespace android
544 // ---------------------------------------------------------------------------
546 #endif // ANDROID_REF_BASE_H