blob: e81cd0063a186849f9aba2f1e52f2aa315022514 [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>
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080028
29// ---------------------------------------------------------------------------
30namespace android {
31
Mathias Agopian84a23fa2011-02-16 15:23:08 -080032class TextOutput;
Mathias Agopian84a23fa2011-02-16 15:23:08 -080033TextOutput& printWeakPointer(TextOutput& to, const void* val);
34
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080035// ---------------------------------------------------------------------------
36
Mathias Agopianff49de72011-02-09 18:38:55 -080037#define COMPARE_WEAK(_op_) \
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080038inline bool operator _op_ (const sp<T>& o) const { \
39 return m_ptr _op_ o.m_ptr; \
40} \
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080041inline bool operator _op_ (const T* o) const { \
42 return m_ptr _op_ o; \
43} \
44template<typename U> \
45inline bool operator _op_ (const sp<U>& o) const { \
46 return m_ptr _op_ o.m_ptr; \
47} \
48template<typename U> \
Mathias Agopianff49de72011-02-09 18:38:55 -080049inline bool operator _op_ (const U* o) const { \
50 return m_ptr _op_ o; \
51}
52
Mathias Agopianb26ea8b2011-02-16 20:23:43 -080053// ---------------------------------------------------------------------------
Mathias Agopianb26ea8b2011-02-16 20:23:43 -080054class ReferenceMover;
55class ReferenceConverterBase {
56public:
57 virtual size_t getReferenceTypeSize() const = 0;
58 virtual void* getReferenceBase(void const*) const = 0;
59 inline virtual ~ReferenceConverterBase() { }
60};
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080061
62// ---------------------------------------------------------------------------
63
64class RefBase
65{
66public:
67 void incStrong(const void* id) const;
68 void decStrong(const void* id) const;
69
70 void forceIncStrong(const void* id) const;
71
72 //! DEBUGGING ONLY: Get current strong ref count.
73 int32_t getStrongCount() const;
74
75 class weakref_type
76 {
77 public:
78 RefBase* refBase() const;
79
80 void incWeak(const void* id);
81 void decWeak(const void* id);
82
83 bool attemptIncStrong(const void* id);
84
85 //! This is only safe if you have set OBJECT_LIFETIME_FOREVER.
86 bool attemptIncWeak(const void* id);
87
88 //! DEBUGGING ONLY: Get current weak ref count.
89 int32_t getWeakCount() const;
90
91 //! DEBUGGING ONLY: Print references held on object.
92 void printRefs() const;
93
94 //! DEBUGGING ONLY: Enable tracking for this object.
95 // enable -- enable/disable tracking
96 // retain -- when tracking is enable, if true, then we save a stack trace
97 // for each reference and dereference; when retain == false, we
98 // match up references and dereferences and keep only the
99 // outstanding ones.
100
101 void trackMe(bool enable, bool retain);
102 };
103
104 weakref_type* createWeak(const void* id) const;
105
106 weakref_type* getWeakRefs() const;
107
108 //! DEBUGGING ONLY: Print references held on object.
109 inline void printRefs() const { getWeakRefs()->printRefs(); }
110
111 //! DEBUGGING ONLY: Enable tracking of object.
112 inline void trackMe(bool enable, bool retain)
113 {
114 getWeakRefs()->trackMe(enable, retain);
115 }
116
Mathias Agopianb26ea8b2011-02-16 20:23:43 -0800117 typedef RefBase basetype;
118
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800119protected:
120 RefBase();
121 virtual ~RefBase();
Mathias Agopian7f57eac2011-06-16 17:15:51 -0700122
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800123 //! Flags for extendObjectLifetime()
124 enum {
125 OBJECT_LIFETIME_WEAK = 0x0001,
126 OBJECT_LIFETIME_FOREVER = 0x0003
127 };
128
129 void extendObjectLifetime(int32_t mode);
130
131 //! Flags for onIncStrongAttempted()
132 enum {
133 FIRST_INC_STRONG = 0x0001
134 };
135
136 virtual void onFirstRef();
137 virtual void onLastStrongRef(const void* id);
138 virtual bool onIncStrongAttempted(uint32_t flags, const void* id);
139 virtual void onLastWeakRef(const void* id);
140
141private:
Mathias Agopianb26ea8b2011-02-16 20:23:43 -0800142 friend class ReferenceMover;
143 static void moveReferences(void* d, void const* s, size_t n,
144 const ReferenceConverterBase& caster);
145
146private:
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800147 friend class weakref_type;
148 class weakref_impl;
149
150 RefBase(const RefBase& o);
151 RefBase& operator=(const RefBase& o);
152
153 weakref_impl* const mRefs;
154};
155
156// ---------------------------------------------------------------------------
157
158template <class T>
159class LightRefBase
160{
161public:
162 inline LightRefBase() : mCount(0) { }
163 inline void incStrong(const void* id) const {
164 android_atomic_inc(&mCount);
165 }
166 inline void decStrong(const void* id) const {
167 if (android_atomic_dec(&mCount) == 1) {
168 delete static_cast<const T*>(this);
169 }
170 }
Mathias Agopian019f8ed2009-05-04 14:17:04 -0700171 //! DEBUGGING ONLY: Get current strong ref count.
172 inline int32_t getStrongCount() const {
173 return mCount;
174 }
Mathias Agopianb26ea8b2011-02-16 20:23:43 -0800175
176 typedef LightRefBase<T> basetype;
177
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800178protected:
179 inline ~LightRefBase() { }
Mathias Agopianb26ea8b2011-02-16 20:23:43 -0800180
181private:
182 friend class ReferenceMover;
183 inline static void moveReferences(void* d, void const* s, size_t n,
184 const ReferenceConverterBase& caster) { }
185
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800186private:
187 mutable volatile int32_t mCount;
188};
189
190// ---------------------------------------------------------------------------
191
192template <typename T>
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800193class wp
194{
195public:
196 typedef typename RefBase::weakref_type weakref_type;
197
198 inline wp() : m_ptr(0) { }
199
200 wp(T* other);
201 wp(const wp<T>& other);
202 wp(const sp<T>& other);
203 template<typename U> wp(U* other);
204 template<typename U> wp(const sp<U>& other);
205 template<typename U> wp(const wp<U>& other);
206
207 ~wp();
208
209 // Assignment
210
211 wp& operator = (T* other);
212 wp& operator = (const wp<T>& other);
213 wp& operator = (const sp<T>& other);
214
215 template<typename U> wp& operator = (U* other);
216 template<typename U> wp& operator = (const wp<U>& other);
217 template<typename U> wp& operator = (const sp<U>& other);
218
219 void set_object_and_refs(T* other, weakref_type* refs);
220
221 // promotion to sp
222
223 sp<T> promote() const;
224
225 // Reset
226
227 void clear();
228
229 // Accessors
230
231 inline weakref_type* get_refs() const { return m_refs; }
232
233 inline T* unsafe_get() const { return m_ptr; }
234
235 // Operators
Mathias Agopianff49de72011-02-09 18:38:55 -0800236
237 COMPARE_WEAK(==)
238 COMPARE_WEAK(!=)
239 COMPARE_WEAK(>)
240 COMPARE_WEAK(<)
241 COMPARE_WEAK(<=)
242 COMPARE_WEAK(>=)
243
244 inline bool operator == (const wp<T>& o) const {
245 return (m_ptr == o.m_ptr) && (m_refs == o.m_refs);
246 }
247 template<typename U>
248 inline bool operator == (const wp<U>& o) const {
249 return m_ptr == o.m_ptr;
250 }
251
252 inline bool operator > (const wp<T>& o) const {
253 return (m_ptr == o.m_ptr) ? (m_refs > o.m_refs) : (m_ptr > o.m_ptr);
254 }
255 template<typename U>
256 inline bool operator > (const wp<U>& o) const {
257 return (m_ptr == o.m_ptr) ? (m_refs > o.m_refs) : (m_ptr > o.m_ptr);
258 }
259
260 inline bool operator < (const wp<T>& o) const {
261 return (m_ptr == o.m_ptr) ? (m_refs < o.m_refs) : (m_ptr < o.m_ptr);
262 }
263 template<typename U>
264 inline bool operator < (const wp<U>& o) const {
265 return (m_ptr == o.m_ptr) ? (m_refs < o.m_refs) : (m_ptr < o.m_ptr);
266 }
267 inline bool operator != (const wp<T>& o) const { return m_refs != o.m_refs; }
268 template<typename U> inline bool operator != (const wp<U>& o) const { return !operator == (o); }
269 inline bool operator <= (const wp<T>& o) const { return !operator > (o); }
270 template<typename U> inline bool operator <= (const wp<U>& o) const { return !operator > (o); }
271 inline bool operator >= (const wp<T>& o) const { return !operator < (o); }
272 template<typename U> inline bool operator >= (const wp<U>& o) const { return !operator < (o); }
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800273
274private:
275 template<typename Y> friend class sp;
276 template<typename Y> friend class wp;
277
278 T* m_ptr;
279 weakref_type* m_refs;
280};
281
282template <typename T>
283TextOutput& operator<<(TextOutput& to, const wp<T>& val);
284
Mathias Agopianff49de72011-02-09 18:38:55 -0800285#undef COMPARE_WEAK
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800286
287// ---------------------------------------------------------------------------
288// No user serviceable parts below here.
289
290template<typename T>
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800291wp<T>::wp(T* other)
292 : m_ptr(other)
293{
294 if (other) m_refs = other->createWeak(this);
295}
296
297template<typename T>
298wp<T>::wp(const wp<T>& other)
299 : m_ptr(other.m_ptr), m_refs(other.m_refs)
300{
301 if (m_ptr) m_refs->incWeak(this);
302}
303
304template<typename T>
305wp<T>::wp(const sp<T>& other)
306 : m_ptr(other.m_ptr)
307{
308 if (m_ptr) {
309 m_refs = m_ptr->createWeak(this);
310 }
311}
312
313template<typename T> template<typename U>
314wp<T>::wp(U* other)
315 : m_ptr(other)
316{
317 if (other) m_refs = other->createWeak(this);
318}
319
320template<typename T> template<typename U>
321wp<T>::wp(const wp<U>& other)
322 : m_ptr(other.m_ptr)
323{
324 if (m_ptr) {
325 m_refs = other.m_refs;
326 m_refs->incWeak(this);
327 }
328}
329
330template<typename T> template<typename U>
331wp<T>::wp(const sp<U>& other)
332 : m_ptr(other.m_ptr)
333{
334 if (m_ptr) {
335 m_refs = m_ptr->createWeak(this);
336 }
337}
338
339template<typename T>
340wp<T>::~wp()
341{
342 if (m_ptr) m_refs->decWeak(this);
343}
344
345template<typename T>
346wp<T>& wp<T>::operator = (T* other)
347{
348 weakref_type* newRefs =
349 other ? other->createWeak(this) : 0;
350 if (m_ptr) m_refs->decWeak(this);
351 m_ptr = other;
352 m_refs = newRefs;
353 return *this;
354}
355
356template<typename T>
357wp<T>& wp<T>::operator = (const wp<T>& other)
358{
Mathias Agopian7b151672010-06-24 21:49:02 -0700359 weakref_type* otherRefs(other.m_refs);
360 T* otherPtr(other.m_ptr);
361 if (otherPtr) otherRefs->incWeak(this);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800362 if (m_ptr) m_refs->decWeak(this);
Mathias Agopian7b151672010-06-24 21:49:02 -0700363 m_ptr = otherPtr;
364 m_refs = otherRefs;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800365 return *this;
366}
367
368template<typename T>
369wp<T>& wp<T>::operator = (const sp<T>& other)
370{
371 weakref_type* newRefs =
372 other != NULL ? other->createWeak(this) : 0;
Mathias Agopian7b151672010-06-24 21:49:02 -0700373 T* otherPtr(other.m_ptr);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800374 if (m_ptr) m_refs->decWeak(this);
Mathias Agopian7b151672010-06-24 21:49:02 -0700375 m_ptr = otherPtr;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800376 m_refs = newRefs;
377 return *this;
378}
379
380template<typename T> template<typename U>
381wp<T>& wp<T>::operator = (U* other)
382{
383 weakref_type* newRefs =
384 other ? other->createWeak(this) : 0;
385 if (m_ptr) m_refs->decWeak(this);
386 m_ptr = other;
387 m_refs = newRefs;
388 return *this;
389}
390
391template<typename T> template<typename U>
392wp<T>& wp<T>::operator = (const wp<U>& other)
393{
Mathias Agopian7b151672010-06-24 21:49:02 -0700394 weakref_type* otherRefs(other.m_refs);
395 U* otherPtr(other.m_ptr);
396 if (otherPtr) otherRefs->incWeak(this);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800397 if (m_ptr) m_refs->decWeak(this);
Mathias Agopian7b151672010-06-24 21:49:02 -0700398 m_ptr = otherPtr;
399 m_refs = otherRefs;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800400 return *this;
401}
402
403template<typename T> template<typename U>
404wp<T>& wp<T>::operator = (const sp<U>& other)
405{
406 weakref_type* newRefs =
407 other != NULL ? other->createWeak(this) : 0;
Mathias Agopian7b151672010-06-24 21:49:02 -0700408 U* otherPtr(other.m_ptr);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800409 if (m_ptr) m_refs->decWeak(this);
Mathias Agopian7b151672010-06-24 21:49:02 -0700410 m_ptr = otherPtr;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800411 m_refs = newRefs;
412 return *this;
413}
414
415template<typename T>
416void wp<T>::set_object_and_refs(T* other, weakref_type* refs)
417{
418 if (other) refs->incWeak(this);
419 if (m_ptr) m_refs->decWeak(this);
420 m_ptr = other;
421 m_refs = refs;
422}
423
424template<typename T>
425sp<T> wp<T>::promote() const
426{
Mathias Agopian3e0f8752011-02-24 18:12:34 -0800427 sp<T> result;
428 if (m_ptr && m_refs->attemptIncStrong(&result)) {
429 result.set_pointer(m_ptr);
430 }
431 return result;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800432}
433
434template<typename T>
435void wp<T>::clear()
436{
437 if (m_ptr) {
438 m_refs->decWeak(this);
439 m_ptr = 0;
440 }
441}
442
443template <typename T>
444inline TextOutput& operator<<(TextOutput& to, const wp<T>& val)
445{
Mathias Agopian84a23fa2011-02-16 15:23:08 -0800446 return printWeakPointer(to, val.unsafe_get());
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800447}
448
Mathias Agopianb26ea8b2011-02-16 20:23:43 -0800449// ---------------------------------------------------------------------------
450
451// this class just serves as a namespace so TYPE::moveReferences can stay
452// private.
453
454class ReferenceMover {
455 // StrongReferenceCast and WeakReferenceCast do the impedance matching
456 // between the generic (void*) implementation in Refbase and the strongly typed
457 // template specializations below.
458
459 template <typename TYPE>
460 struct StrongReferenceCast : public ReferenceConverterBase {
461 virtual size_t getReferenceTypeSize() const { return sizeof( sp<TYPE> ); }
462 virtual void* getReferenceBase(void const* p) const {
463 sp<TYPE> const* sptr(reinterpret_cast<sp<TYPE> const*>(p));
464 return static_cast<typename TYPE::basetype *>(sptr->get());
465 }
466 };
467
468 template <typename TYPE>
469 struct WeakReferenceCast : public ReferenceConverterBase {
470 virtual size_t getReferenceTypeSize() const { return sizeof( wp<TYPE> ); }
471 virtual void* getReferenceBase(void const* p) const {
472 wp<TYPE> const* sptr(reinterpret_cast<wp<TYPE> const*>(p));
473 return static_cast<typename TYPE::basetype *>(sptr->unsafe_get());
474 }
475 };
476
477public:
478 template<typename TYPE> static inline
479 void move_references(sp<TYPE>* d, sp<TYPE> const* s, size_t n) {
480 memmove(d, s, n*sizeof(sp<TYPE>));
481 StrongReferenceCast<TYPE> caster;
482 TYPE::moveReferences(d, s, n, caster);
483 }
484 template<typename TYPE> static inline
485 void move_references(wp<TYPE>* d, wp<TYPE> const* s, size_t n) {
486 memmove(d, s, n*sizeof(wp<TYPE>));
487 WeakReferenceCast<TYPE> caster;
488 TYPE::moveReferences(d, s, n, caster);
489 }
490};
491
492// specialization for moving sp<> and wp<> types.
493// these are used by the [Sorted|Keyed]Vector<> implementations
494// sp<> and wp<> need to be handled specially, because they do not
495// have trivial copy operation in the general case (see RefBase.cpp
496// when DEBUG ops are enabled), but can be implemented very
497// efficiently in most cases.
498
499template<typename TYPE> inline
500void move_forward_type(sp<TYPE>* d, sp<TYPE> const* s, size_t n) {
501 ReferenceMover::move_references(d, s, n);
502}
503
504template<typename TYPE> inline
505void move_backward_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_forward_type(wp<TYPE>* d, wp<TYPE> const* s, size_t n) {
511 ReferenceMover::move_references(d, s, n);
512}
513
514template<typename TYPE> inline
515void move_backward_type(wp<TYPE>* d, wp<TYPE> const* s, size_t n) {
516 ReferenceMover::move_references(d, s, n);
517}
518
519
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800520}; // namespace android
521
522// ---------------------------------------------------------------------------
523
524#endif // ANDROID_REF_BASE_H