blob: f8c3216b4618b3ea6f53fc58167921ccabed6a20 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-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 Projectedbf3b62009-03-03 19:31:44 -080021
22#include <stdint.h>
23#include <sys/types.h>
24#include <stdlib.h>
Mathias Agopianf14a1042011-02-16 20:23:43 -080025#include <string.h>
26
27#include <utils/StrongPointer.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080028
29// ---------------------------------------------------------------------------
30namespace android {
31
Mathias Agopiana08ef492011-02-16 15:23:08 -080032class TextOutput;
Mathias Agopiana08ef492011-02-16 15:23:08 -080033TextOutput& printWeakPointer(TextOutput& to, const void* val);
34
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080035// ---------------------------------------------------------------------------
36
Mathias Agopian7802bbd2011-02-09 18:38:55 -080037#define COMPARE_WEAK(_op_) \
The Android Open Source Projectedbf3b62009-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 Projectedbf3b62009-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 Agopian7802bbd2011-02-09 18:38:55 -080049inline bool operator _op_ (const U* o) const { \
50 return m_ptr _op_ o; \
51}
52
Mathias Agopianf14a1042011-02-16 20:23:43 -080053// ---------------------------------------------------------------------------
Mathias Agopianf14a1042011-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 Projectedbf3b62009-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 Agopianf14a1042011-02-16 20:23:43 -0800117 typedef RefBase basetype;
118
Mathias Agopianddc31c32011-06-12 18:05:53 -0700119 // used to override the RefBase destruction.
120 class Destroyer {
121 friend class RefBase;
122 public:
123 virtual ~Destroyer();
124 private:
125 virtual void destroy(RefBase const* base) = 0;
126 };
127
128 // Make sure to never acquire a strong reference from this function. The
129 // same restrictions than for destructors apply.
130 void setDestroyer(Destroyer* destroyer);
131
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800132protected:
133 RefBase();
134 virtual ~RefBase();
Mathias Agopian20aeb1c2011-05-19 18:03:31 -0700135
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800136 //! Flags for extendObjectLifetime()
137 enum {
138 OBJECT_LIFETIME_WEAK = 0x0001,
139 OBJECT_LIFETIME_FOREVER = 0x0003
140 };
141
142 void extendObjectLifetime(int32_t mode);
143
144 //! Flags for onIncStrongAttempted()
145 enum {
146 FIRST_INC_STRONG = 0x0001
147 };
148
149 virtual void onFirstRef();
150 virtual void onLastStrongRef(const void* id);
151 virtual bool onIncStrongAttempted(uint32_t flags, const void* id);
152 virtual void onLastWeakRef(const void* id);
153
154private:
Mathias Agopianf14a1042011-02-16 20:23:43 -0800155 friend class ReferenceMover;
156 static void moveReferences(void* d, void const* s, size_t n,
157 const ReferenceConverterBase& caster);
158
159private:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800160 friend class weakref_type;
161 class weakref_impl;
162
163 RefBase(const RefBase& o);
164 RefBase& operator=(const RefBase& o);
165
166 weakref_impl* const mRefs;
167};
168
169// ---------------------------------------------------------------------------
170
171template <class T>
172class LightRefBase
173{
174public:
175 inline LightRefBase() : mCount(0) { }
176 inline void incStrong(const void* id) const {
177 android_atomic_inc(&mCount);
178 }
179 inline void decStrong(const void* id) const {
180 if (android_atomic_dec(&mCount) == 1) {
181 delete static_cast<const T*>(this);
182 }
183 }
Mathias Agopian0926f502009-05-04 14:17:04 -0700184 //! DEBUGGING ONLY: Get current strong ref count.
185 inline int32_t getStrongCount() const {
186 return mCount;
187 }
Mathias Agopianf14a1042011-02-16 20:23:43 -0800188
189 typedef LightRefBase<T> basetype;
190
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800191protected:
192 inline ~LightRefBase() { }
Mathias Agopianf14a1042011-02-16 20:23:43 -0800193
194private:
195 friend class ReferenceMover;
196 inline static void moveReferences(void* d, void const* s, size_t n,
197 const ReferenceConverterBase& caster) { }
198
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800199private:
200 mutable volatile int32_t mCount;
201};
202
203// ---------------------------------------------------------------------------
204
205template <typename T>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800206class wp
207{
208public:
209 typedef typename RefBase::weakref_type weakref_type;
210
211 inline wp() : m_ptr(0) { }
212
213 wp(T* other);
214 wp(const wp<T>& other);
215 wp(const sp<T>& other);
216 template<typename U> wp(U* other);
217 template<typename U> wp(const sp<U>& other);
218 template<typename U> wp(const wp<U>& other);
219
220 ~wp();
221
222 // Assignment
223
224 wp& operator = (T* other);
225 wp& operator = (const wp<T>& other);
226 wp& operator = (const sp<T>& other);
227
228 template<typename U> wp& operator = (U* other);
229 template<typename U> wp& operator = (const wp<U>& other);
230 template<typename U> wp& operator = (const sp<U>& other);
231
232 void set_object_and_refs(T* other, weakref_type* refs);
233
234 // promotion to sp
235
236 sp<T> promote() const;
237
238 // Reset
239
240 void clear();
241
242 // Accessors
243
244 inline weakref_type* get_refs() const { return m_refs; }
245
246 inline T* unsafe_get() const { return m_ptr; }
247
248 // Operators
Mathias Agopian7802bbd2011-02-09 18:38:55 -0800249
250 COMPARE_WEAK(==)
251 COMPARE_WEAK(!=)
252 COMPARE_WEAK(>)
253 COMPARE_WEAK(<)
254 COMPARE_WEAK(<=)
255 COMPARE_WEAK(>=)
256
257 inline bool operator == (const wp<T>& o) const {
258 return (m_ptr == o.m_ptr) && (m_refs == o.m_refs);
259 }
260 template<typename U>
261 inline bool operator == (const wp<U>& o) const {
262 return 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
273 inline bool operator < (const wp<T>& o) const {
274 return (m_ptr == o.m_ptr) ? (m_refs < o.m_refs) : (m_ptr < o.m_ptr);
275 }
276 template<typename U>
277 inline bool operator < (const wp<U>& o) const {
278 return (m_ptr == o.m_ptr) ? (m_refs < o.m_refs) : (m_ptr < o.m_ptr);
279 }
280 inline bool operator != (const wp<T>& o) const { return m_refs != o.m_refs; }
281 template<typename U> inline bool operator != (const wp<U>& o) const { return !operator == (o); }
282 inline bool operator <= (const wp<T>& o) const { return !operator > (o); }
283 template<typename U> inline bool operator <= (const wp<U>& o) const { return !operator > (o); }
284 inline bool operator >= (const wp<T>& o) const { return !operator < (o); }
285 template<typename U> inline bool operator >= (const wp<U>& o) const { return !operator < (o); }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800286
287private:
288 template<typename Y> friend class sp;
289 template<typename Y> friend class wp;
290
291 T* m_ptr;
292 weakref_type* m_refs;
293};
294
295template <typename T>
296TextOutput& operator<<(TextOutput& to, const wp<T>& val);
297
Mathias Agopian7802bbd2011-02-09 18:38:55 -0800298#undef COMPARE_WEAK
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800299
300// ---------------------------------------------------------------------------
301// No user serviceable parts below here.
302
303template<typename T>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800304wp<T>::wp(T* other)
305 : m_ptr(other)
306{
307 if (other) m_refs = other->createWeak(this);
308}
309
310template<typename T>
311wp<T>::wp(const wp<T>& other)
312 : m_ptr(other.m_ptr), m_refs(other.m_refs)
313{
314 if (m_ptr) m_refs->incWeak(this);
315}
316
317template<typename T>
318wp<T>::wp(const sp<T>& other)
319 : m_ptr(other.m_ptr)
320{
321 if (m_ptr) {
322 m_refs = m_ptr->createWeak(this);
323 }
324}
325
326template<typename T> template<typename U>
327wp<T>::wp(U* other)
328 : m_ptr(other)
329{
330 if (other) m_refs = other->createWeak(this);
331}
332
333template<typename T> template<typename U>
334wp<T>::wp(const wp<U>& other)
335 : m_ptr(other.m_ptr)
336{
337 if (m_ptr) {
338 m_refs = other.m_refs;
339 m_refs->incWeak(this);
340 }
341}
342
343template<typename T> template<typename U>
344wp<T>::wp(const sp<U>& other)
345 : m_ptr(other.m_ptr)
346{
347 if (m_ptr) {
348 m_refs = m_ptr->createWeak(this);
349 }
350}
351
352template<typename T>
353wp<T>::~wp()
354{
355 if (m_ptr) m_refs->decWeak(this);
356}
357
358template<typename T>
359wp<T>& wp<T>::operator = (T* other)
360{
361 weakref_type* newRefs =
362 other ? other->createWeak(this) : 0;
363 if (m_ptr) m_refs->decWeak(this);
364 m_ptr = other;
365 m_refs = newRefs;
366 return *this;
367}
368
369template<typename T>
370wp<T>& wp<T>::operator = (const wp<T>& other)
371{
Mathias Agopian8c3c51b2010-06-24 21:49:02 -0700372 weakref_type* otherRefs(other.m_refs);
373 T* otherPtr(other.m_ptr);
374 if (otherPtr) otherRefs->incWeak(this);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800375 if (m_ptr) m_refs->decWeak(this);
Mathias Agopian8c3c51b2010-06-24 21:49:02 -0700376 m_ptr = otherPtr;
377 m_refs = otherRefs;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800378 return *this;
379}
380
381template<typename T>
382wp<T>& wp<T>::operator = (const sp<T>& other)
383{
384 weakref_type* newRefs =
385 other != NULL ? other->createWeak(this) : 0;
Mathias Agopian8c3c51b2010-06-24 21:49:02 -0700386 T* otherPtr(other.m_ptr);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800387 if (m_ptr) m_refs->decWeak(this);
Mathias Agopian8c3c51b2010-06-24 21:49:02 -0700388 m_ptr = otherPtr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800389 m_refs = newRefs;
390 return *this;
391}
392
393template<typename T> template<typename U>
394wp<T>& wp<T>::operator = (U* other)
395{
396 weakref_type* newRefs =
397 other ? other->createWeak(this) : 0;
398 if (m_ptr) m_refs->decWeak(this);
399 m_ptr = other;
400 m_refs = newRefs;
401 return *this;
402}
403
404template<typename T> template<typename U>
405wp<T>& wp<T>::operator = (const wp<U>& other)
406{
Mathias Agopian8c3c51b2010-06-24 21:49:02 -0700407 weakref_type* otherRefs(other.m_refs);
408 U* otherPtr(other.m_ptr);
409 if (otherPtr) otherRefs->incWeak(this);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800410 if (m_ptr) m_refs->decWeak(this);
Mathias Agopian8c3c51b2010-06-24 21:49:02 -0700411 m_ptr = otherPtr;
412 m_refs = otherRefs;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800413 return *this;
414}
415
416template<typename T> template<typename U>
417wp<T>& wp<T>::operator = (const sp<U>& other)
418{
419 weakref_type* newRefs =
420 other != NULL ? other->createWeak(this) : 0;
Mathias Agopian8c3c51b2010-06-24 21:49:02 -0700421 U* otherPtr(other.m_ptr);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800422 if (m_ptr) m_refs->decWeak(this);
Mathias Agopian8c3c51b2010-06-24 21:49:02 -0700423 m_ptr = otherPtr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800424 m_refs = newRefs;
425 return *this;
426}
427
428template<typename T>
429void wp<T>::set_object_and_refs(T* other, weakref_type* refs)
430{
431 if (other) refs->incWeak(this);
432 if (m_ptr) m_refs->decWeak(this);
433 m_ptr = other;
434 m_refs = refs;
435}
436
437template<typename T>
438sp<T> wp<T>::promote() const
439{
Mathias Agopiand0050042011-02-24 18:12:34 -0800440 sp<T> result;
441 if (m_ptr && m_refs->attemptIncStrong(&result)) {
442 result.set_pointer(m_ptr);
443 }
444 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800445}
446
447template<typename T>
448void wp<T>::clear()
449{
450 if (m_ptr) {
451 m_refs->decWeak(this);
452 m_ptr = 0;
453 }
454}
455
456template <typename T>
457inline TextOutput& operator<<(TextOutput& to, const wp<T>& val)
458{
Mathias Agopiana08ef492011-02-16 15:23:08 -0800459 return printWeakPointer(to, val.unsafe_get());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800460}
461
Mathias Agopianf14a1042011-02-16 20:23:43 -0800462// ---------------------------------------------------------------------------
463
464// this class just serves as a namespace so TYPE::moveReferences can stay
465// private.
466
467class ReferenceMover {
468 // StrongReferenceCast and WeakReferenceCast do the impedance matching
469 // between the generic (void*) implementation in Refbase and the strongly typed
470 // template specializations below.
471
472 template <typename TYPE>
473 struct StrongReferenceCast : public ReferenceConverterBase {
474 virtual size_t getReferenceTypeSize() const { return sizeof( sp<TYPE> ); }
475 virtual void* getReferenceBase(void const* p) const {
476 sp<TYPE> const* sptr(reinterpret_cast<sp<TYPE> const*>(p));
477 return static_cast<typename TYPE::basetype *>(sptr->get());
478 }
479 };
480
481 template <typename TYPE>
482 struct WeakReferenceCast : public ReferenceConverterBase {
483 virtual size_t getReferenceTypeSize() const { return sizeof( wp<TYPE> ); }
484 virtual void* getReferenceBase(void const* p) const {
485 wp<TYPE> const* sptr(reinterpret_cast<wp<TYPE> const*>(p));
486 return static_cast<typename TYPE::basetype *>(sptr->unsafe_get());
487 }
488 };
489
490public:
491 template<typename TYPE> static inline
492 void move_references(sp<TYPE>* d, sp<TYPE> const* s, size_t n) {
493 memmove(d, s, n*sizeof(sp<TYPE>));
494 StrongReferenceCast<TYPE> caster;
495 TYPE::moveReferences(d, s, n, caster);
496 }
497 template<typename TYPE> static inline
498 void move_references(wp<TYPE>* d, wp<TYPE> const* s, size_t n) {
499 memmove(d, s, n*sizeof(wp<TYPE>));
500 WeakReferenceCast<TYPE> caster;
501 TYPE::moveReferences(d, s, n, caster);
502 }
503};
504
505// specialization for moving sp<> and wp<> types.
506// these are used by the [Sorted|Keyed]Vector<> implementations
507// sp<> and wp<> need to be handled specially, because they do not
508// have trivial copy operation in the general case (see RefBase.cpp
509// when DEBUG ops are enabled), but can be implemented very
510// efficiently in most cases.
511
512template<typename TYPE> inline
513void move_forward_type(sp<TYPE>* d, sp<TYPE> const* s, size_t n) {
514 ReferenceMover::move_references(d, s, n);
515}
516
517template<typename TYPE> inline
518void move_backward_type(sp<TYPE>* d, sp<TYPE> const* s, size_t n) {
519 ReferenceMover::move_references(d, s, n);
520}
521
522template<typename TYPE> inline
523void move_forward_type(wp<TYPE>* d, wp<TYPE> const* s, size_t n) {
524 ReferenceMover::move_references(d, s, n);
525}
526
527template<typename TYPE> inline
528void move_backward_type(wp<TYPE>* d, wp<TYPE> const* s, size_t n) {
529 ReferenceMover::move_references(d, s, n);
530}
531
532
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800533}; // namespace android
534
535// ---------------------------------------------------------------------------
536
537#endif // ANDROID_REF_BASE_H