blob: 5199ef60e326c82d703998bcf7945bbeeeecebf2 [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#define LOG_TAG "RefBase"
Mathias Agopianda8ec4b2013-03-19 17:36:57 -070018// #define LOG_NDEBUG 0
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080019
Hans Boehm2a019ec2018-08-07 23:45:25 +000020#include <memory>
21
Chih-Hung Hsieh502f4862018-09-13 11:08:41 -070022#include <android-base/macros.h>
23
Christopher Ferris0e691602020-10-14 14:13:58 -070024#include <log/log.h>
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080025
Christopher Ferris0e691602020-10-14 14:13:58 -070026#include <utils/RefBase.h>
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080027
Hans Boehm2a019ec2018-08-07 23:45:25 +000028#include <utils/Mutex.h>
29
Mark Salyzyn5bed8032014-04-30 11:10:46 -070030#ifndef __unused
31#define __unused __attribute__((__unused__))
32#endif
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080033
Hans Boehm2a019ec2018-08-07 23:45:25 +000034// Compile with refcounting debugging enabled.
35#define DEBUG_REFS 0
36
37// The following three are ignored unless DEBUG_REFS is set.
Mathias Agopian6d4419d2013-03-18 20:31:18 -070038
39// whether ref-tracking is enabled by default, if not, trackMe(true, false)
40// needs to be called explicitly
Hans Boehm2a019ec2018-08-07 23:45:25 +000041#define DEBUG_REFS_ENABLED_BY_DEFAULT 0
Mathias Agopian6d4419d2013-03-18 20:31:18 -070042
43// whether callstack are collected (significantly slows things down)
Hans Boehm2a019ec2018-08-07 23:45:25 +000044#define DEBUG_REFS_CALLSTACK_ENABLED 1
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080045
Mathias Agopian6d4419d2013-03-18 20:31:18 -070046// folder where stack traces are saved when DEBUG_REFS is enabled
47// this folder needs to exist and be writable
Hans Boehm2a019ec2018-08-07 23:45:25 +000048#define DEBUG_REFS_CALLSTACK_PATH "/data/debug"
Mathias Agopian6d4419d2013-03-18 20:31:18 -070049
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080050// log all reference counting operations
Hans Boehm2a019ec2018-08-07 23:45:25 +000051#define PRINT_REFS 0
52
Andrei Homescu5c15de22021-12-10 05:32:17 +000053#if defined(__linux__)
Christopher Ferris0e691602020-10-14 14:13:58 -070054// CallStack is only supported on linux type platforms.
55#define CALLSTACK_ENABLED 1
56#else
57#define CALLSTACK_ENABLED 0
58#endif
59
60#if CALLSTACK_ENABLED
61#include <utils/CallStack.h>
62#endif
63
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080064// ---------------------------------------------------------------------------
65
66namespace android {
67
Hans Boehm9ba71922016-07-21 18:56:55 -070068// Observations, invariants, etc:
Hans Boehme263e6c2016-05-11 18:15:12 -070069
Hans Boehm9ba71922016-07-21 18:56:55 -070070// By default, obects are destroyed when the last strong reference disappears
71// or, if the object never had a strong reference, when the last weak reference
72// disappears.
73//
Hans Boehme263e6c2016-05-11 18:15:12 -070074// OBJECT_LIFETIME_WEAK changes this behavior to retain the object
75// unconditionally until the last reference of either kind disappears. The
76// client ensures that the extendObjectLifetime call happens before the dec
77// call that would otherwise have deallocated the object, or before an
78// attemptIncStrong call that might rely on it. We do not worry about
79// concurrent changes to the object lifetime.
Hans Boehm9ba71922016-07-21 18:56:55 -070080//
81// AttemptIncStrong will succeed if the object has a strong reference, or if it
82// has a weak reference and has never had a strong reference.
83// AttemptIncWeak really does succeed only if there is already a WEAK
84// reference, and thus may fail when attemptIncStrong would succeed.
85//
Hans Boehme263e6c2016-05-11 18:15:12 -070086// mStrong is the strong reference count. mWeak is the weak reference count.
87// Between calls, and ignoring memory ordering effects, mWeak includes strong
88// references, and is thus >= mStrong.
89//
Hans Boehm9ba71922016-07-21 18:56:55 -070090// A weakref_impl holds all the information, including both reference counts,
91// required to perform wp<> operations. Thus these can continue to be performed
92// after the RefBase object has been destroyed.
93//
Hans Boehme263e6c2016-05-11 18:15:12 -070094// A weakref_impl is allocated as the value of mRefs in a RefBase object on
95// construction.
Hans Boehm23c857e2016-08-02 18:39:30 -070096// In the OBJECT_LIFETIME_STRONG case, it is normally deallocated in decWeak,
97// and hence lives as long as the last weak reference. (It can also be
98// deallocated in the RefBase destructor iff the strong reference count was
99// never incremented and the weak count is zero, e.g. if the RefBase object is
100// explicitly destroyed without decrementing the strong count. This should be
101// avoided.) In this case, the RefBase destructor should be invoked from
102// decStrong.
103// In the OBJECT_LIFETIME_WEAK case, the weakref_impl is always deallocated in
104// the RefBase destructor, which is always invoked by decWeak. DecStrong
105// explicitly avoids the deletion in this case.
Hans Boehme263e6c2016-05-11 18:15:12 -0700106//
107// Memory ordering:
108// The client must ensure that every inc() call, together with all other
109// accesses to the object, happens before the corresponding dec() call.
110//
111// We try to keep memory ordering constraints on atomics as weak as possible,
112// since memory fences or ordered memory accesses are likely to be a major
113// performance cost for this code. All accesses to mStrong, mWeak, and mFlags
114// explicitly relax memory ordering in some way.
115//
116// The only operations that are not memory_order_relaxed are reference count
117// decrements. All reference count decrements are release operations. In
118// addition, the final decrement leading the deallocation is followed by an
119// acquire fence, which we can view informally as also turning it into an
120// acquire operation. (See 29.8p4 [atomics.fences] for details. We could
121// alternatively use acq_rel operations for all decrements. This is probably
122// slower on most current (2016) hardware, especially on ARMv7, but that may
123// not be true indefinitely.)
124//
125// This convention ensures that the second-to-last decrement synchronizes with
126// (in the language of 1.10 in the C++ standard) the final decrement of a
127// reference count. Since reference counts are only updated using atomic
128// read-modify-write operations, this also extends to any earlier decrements.
129// (See "release sequence" in 1.10.)
130//
131// Since all operations on an object happen before the corresponding reference
132// count decrement, and all reference count decrements happen before the final
133// one, we are guaranteed that all other object accesses happen before the
134// object is destroyed.
135
136
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800137#define INITIAL_STRONG_VALUE (1<<28)
138
Hans Boehm23c857e2016-08-02 18:39:30 -0700139#define MAX_COUNT 0xfffff
140
141// Test whether the argument is a clearly invalid strong reference count.
142// Used only for error checking on the value before an atomic decrement.
143// Intended to be very cheap.
144// Note that we cannot just check for excess decrements by comparing to zero
145// since the object would be deallocated before that.
146#define BAD_STRONG(c) \
147 ((c) == 0 || ((c) & (~(MAX_COUNT | INITIAL_STRONG_VALUE))) != 0)
148
149// Same for weak counts.
150#define BAD_WEAK(c) ((c) == 0 || ((c) & (~MAX_COUNT)) != 0)
151
Steven Morelandc340a082022-07-26 17:09:50 +0000152// see utils/StrongPointer.h - declared there for legacy reasons
153void sp_report_stack_pointer();
154
155// Check whether address is definitely on the calling stack. We actually check whether it is on
156// the same 4K page as the frame pointer.
157//
158// Assumptions:
159// - Pages are never smaller than 4K (MIN_PAGE_SIZE)
160// - Malloced memory never shares a page with a stack.
161//
162// It does not appear safe to broaden this check to include adjacent pages; apparently this code
163// is used in environments where there may not be a guard page below (at higher addresses than)
164// the bottom of the stack.
165static void check_not_on_stack(const void* ptr) {
166 static constexpr int MIN_PAGE_SIZE = 0x1000; // 4K. Safer than including sys/user.h.
167 static constexpr uintptr_t MIN_PAGE_MASK = ~static_cast<uintptr_t>(MIN_PAGE_SIZE - 1);
168 uintptr_t my_frame_address =
169 reinterpret_cast<uintptr_t>(__builtin_frame_address(0 /* this frame */));
170 if (((reinterpret_cast<uintptr_t>(ptr) ^ my_frame_address) & MIN_PAGE_MASK) == 0) {
171 sp_report_stack_pointer();
172 }
173}
174
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800175// ---------------------------------------------------------------------------
176
177class RefBase::weakref_impl : public RefBase::weakref_type
178{
179public:
Hans Boehme263e6c2016-05-11 18:15:12 -0700180 std::atomic<int32_t> mStrong;
181 std::atomic<int32_t> mWeak;
182 RefBase* const mBase;
183 std::atomic<int32_t> mFlags;
Mathias Agopian9c8fa9e2011-06-15 20:42:47 -0700184
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800185#if !DEBUG_REFS
186
Chih-Hung Hsieh1c563d92016-04-29 15:44:04 -0700187 explicit weakref_impl(RefBase* base)
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800188 : mStrong(INITIAL_STRONG_VALUE)
189 , mWeak(0)
190 , mBase(base)
陈冠有0cbef722021-08-22 16:36:28 +0000191 , mFlags(OBJECT_LIFETIME_STRONG)
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800192 {
193 }
194
195 void addStrongRef(const void* /*id*/) { }
196 void removeStrongRef(const void* /*id*/) { }
Mathias Agopianad099652011-08-10 21:07:02 -0700197 void renameStrongRefId(const void* /*old_id*/, const void* /*new_id*/) { }
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800198 void addWeakRef(const void* /*id*/) { }
199 void removeWeakRef(const void* /*id*/) { }
Mathias Agopianad099652011-08-10 21:07:02 -0700200 void renameWeakRefId(const void* /*old_id*/, const void* /*new_id*/) { }
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800201 void printRefs() const { }
202 void trackMe(bool, bool) { }
203
204#else
205
206 weakref_impl(RefBase* base)
207 : mStrong(INITIAL_STRONG_VALUE)
208 , mWeak(0)
209 , mBase(base)
陈冠有0cbef722021-08-22 16:36:28 +0000210 , mFlags(OBJECT_LIFETIME_STRONG)
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800211 , mStrongRefs(NULL)
212 , mWeakRefs(NULL)
213 , mTrackEnabled(!!DEBUG_REFS_ENABLED_BY_DEFAULT)
214 , mRetain(false)
215 {
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800216 }
Christopher Ferris0e691602020-10-14 14:13:58 -0700217
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800218 ~weakref_impl()
219 {
Mathias Agopianad099652011-08-10 21:07:02 -0700220 bool dumpStack = false;
221 if (!mRetain && mStrongRefs != NULL) {
222 dumpStack = true;
Steve Block1b781ab2012-01-06 19:20:56 +0000223 ALOGE("Strong references remain:");
Mathias Agopianad099652011-08-10 21:07:02 -0700224 ref_entry* refs = mStrongRefs;
225 while (refs) {
226 char inc = refs->ref >= 0 ? '+' : '-';
Steve Blockeb095332011-12-20 16:23:08 +0000227 ALOGD("\t%c ID %p (ref %d):", inc, refs->id, refs->ref);
Christopher Ferris0e691602020-10-14 14:13:58 -0700228#if DEBUG_REFS_CALLSTACK_ENABLED && CALLSTACK_ENABLED
Hans Boehm2a019ec2018-08-07 23:45:25 +0000229 CallStack::logStack(LOG_TAG, refs->stack.get());
Mathias Agopianad099652011-08-10 21:07:02 -0700230#endif
231 refs = refs->next;
232 }
233 }
234
235 if (!mRetain && mWeakRefs != NULL) {
236 dumpStack = true;
Steve Block1b781ab2012-01-06 19:20:56 +0000237 ALOGE("Weak references remain!");
Mathias Agopianad099652011-08-10 21:07:02 -0700238 ref_entry* refs = mWeakRefs;
239 while (refs) {
240 char inc = refs->ref >= 0 ? '+' : '-';
Steve Blockeb095332011-12-20 16:23:08 +0000241 ALOGD("\t%c ID %p (ref %d):", inc, refs->id, refs->ref);
Christopher Ferris0e691602020-10-14 14:13:58 -0700242#if DEBUG_REFS_CALLSTACK_ENABLED && CALLSTACK_ENABLED
Hans Boehm2a019ec2018-08-07 23:45:25 +0000243 CallStack::logStack(LOG_TAG, refs->stack.get());
Mathias Agopianad099652011-08-10 21:07:02 -0700244#endif
245 refs = refs->next;
246 }
247 }
248 if (dumpStack) {
Steve Block1b781ab2012-01-06 19:20:56 +0000249 ALOGE("above errors at:");
Christopher Ferris0e691602020-10-14 14:13:58 -0700250#if CALLSTACK_ENABLED
Hans Boehm2a019ec2018-08-07 23:45:25 +0000251 CallStack::logStack(LOG_TAG);
Christopher Ferris0e691602020-10-14 14:13:58 -0700252#endif
Mathias Agopianad099652011-08-10 21:07:02 -0700253 }
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800254 }
255
Mathias Agopianad099652011-08-10 21:07:02 -0700256 void addStrongRef(const void* id) {
Steve Blockeb095332011-12-20 16:23:08 +0000257 //ALOGD_IF(mTrackEnabled,
Mathias Agopianad099652011-08-10 21:07:02 -0700258 // "addStrongRef: RefBase=%p, id=%p", mBase, id);
Hans Boehme263e6c2016-05-11 18:15:12 -0700259 addRef(&mStrongRefs, id, mStrong.load(std::memory_order_relaxed));
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800260 }
261
Mathias Agopianad099652011-08-10 21:07:02 -0700262 void removeStrongRef(const void* id) {
Steve Blockeb095332011-12-20 16:23:08 +0000263 //ALOGD_IF(mTrackEnabled,
Mathias Agopianad099652011-08-10 21:07:02 -0700264 // "removeStrongRef: RefBase=%p, id=%p", mBase, id);
265 if (!mRetain) {
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800266 removeRef(&mStrongRefs, id);
Mathias Agopianad099652011-08-10 21:07:02 -0700267 } else {
Hans Boehme263e6c2016-05-11 18:15:12 -0700268 addRef(&mStrongRefs, id, -mStrong.load(std::memory_order_relaxed));
Mathias Agopianad099652011-08-10 21:07:02 -0700269 }
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800270 }
271
Mathias Agopianad099652011-08-10 21:07:02 -0700272 void renameStrongRefId(const void* old_id, const void* new_id) {
Steve Blockeb095332011-12-20 16:23:08 +0000273 //ALOGD_IF(mTrackEnabled,
Mathias Agopianad099652011-08-10 21:07:02 -0700274 // "renameStrongRefId: RefBase=%p, oid=%p, nid=%p",
275 // mBase, old_id, new_id);
276 renameRefsId(mStrongRefs, old_id, new_id);
277 }
278
279 void addWeakRef(const void* id) {
Hans Boehme263e6c2016-05-11 18:15:12 -0700280 addRef(&mWeakRefs, id, mWeak.load(std::memory_order_relaxed));
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800281 }
282
Mathias Agopianad099652011-08-10 21:07:02 -0700283 void removeWeakRef(const void* id) {
284 if (!mRetain) {
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800285 removeRef(&mWeakRefs, id);
Mathias Agopianad099652011-08-10 21:07:02 -0700286 } else {
Hans Boehme263e6c2016-05-11 18:15:12 -0700287 addRef(&mWeakRefs, id, -mWeak.load(std::memory_order_relaxed));
Mathias Agopianad099652011-08-10 21:07:02 -0700288 }
289 }
290
291 void renameWeakRefId(const void* old_id, const void* new_id) {
292 renameRefsId(mWeakRefs, old_id, new_id);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800293 }
294
Christopher Ferris0e691602020-10-14 14:13:58 -0700295 void trackMe(bool track, bool retain) {
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800296 mTrackEnabled = track;
297 mRetain = retain;
298 }
299
300 void printRefs() const
301 {
302 String8 text;
303
304 {
Mathias Agopianad099652011-08-10 21:07:02 -0700305 Mutex::Autolock _l(mMutex);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800306 char buf[128];
George Burgess IVe7aa2b22016-03-02 14:02:55 -0800307 snprintf(buf, sizeof(buf),
308 "Strong references on RefBase %p (weakref_type %p):\n",
309 mBase, this);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800310 text.append(buf);
311 printRefsLocked(&text, mStrongRefs);
George Burgess IVe7aa2b22016-03-02 14:02:55 -0800312 snprintf(buf, sizeof(buf),
313 "Weak references on RefBase %p (weakref_type %p):\n",
314 mBase, this);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800315 text.append(buf);
316 printRefsLocked(&text, mWeakRefs);
317 }
318
319 {
320 char name[100];
George Burgess IVe7aa2b22016-03-02 14:02:55 -0800321 snprintf(name, sizeof(name), DEBUG_REFS_CALLSTACK_PATH "/%p.stack",
322 this);
Mathias Agopian769828d2013-03-06 17:51:15 -0800323 int rc = open(name, O_RDWR | O_CREAT | O_APPEND, 644);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800324 if (rc >= 0) {
Hans Boehm2a019ec2018-08-07 23:45:25 +0000325 (void)write(rc, text.string(), text.length());
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800326 close(rc);
Steve Blockeb095332011-12-20 16:23:08 +0000327 ALOGD("STACK TRACE for %p saved in %s", this, name);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800328 }
Steve Block1b781ab2012-01-06 19:20:56 +0000329 else ALOGE("FAILED TO PRINT STACK TRACE for %p in %s: %s", this,
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800330 name, strerror(errno));
331 }
332 }
333
334private:
335 struct ref_entry
336 {
337 ref_entry* next;
338 const void* id;
Christopher Ferris0e691602020-10-14 14:13:58 -0700339#if DEBUG_REFS_CALLSTACK_ENABLED && CALLSTACK_ENABLED
Hans Boehm2a019ec2018-08-07 23:45:25 +0000340 CallStack::CallStackUPtr stack;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800341#endif
342 int32_t ref;
343 };
344
345 void addRef(ref_entry** refs, const void* id, int32_t mRef)
346 {
347 if (mTrackEnabled) {
348 AutoMutex _l(mMutex);
Mathias Agopianad099652011-08-10 21:07:02 -0700349
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800350 ref_entry* ref = new ref_entry;
351 // Reference count at the time of the snapshot, but before the
352 // update. Positive value means we increment, negative--we
353 // decrement the reference count.
354 ref->ref = mRef;
355 ref->id = id;
Christopher Ferris0e691602020-10-14 14:13:58 -0700356#if DEBUG_REFS_CALLSTACK_ENABLED && CALLSTACK_ENABLED
Hans Boehm2a019ec2018-08-07 23:45:25 +0000357 ref->stack = CallStack::getCurrent(2);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800358#endif
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800359 ref->next = *refs;
360 *refs = ref;
361 }
362 }
363
364 void removeRef(ref_entry** refs, const void* id)
365 {
366 if (mTrackEnabled) {
367 AutoMutex _l(mMutex);
Christopher Ferris0e691602020-10-14 14:13:58 -0700368
Mathias Agopianad099652011-08-10 21:07:02 -0700369 ref_entry* const head = *refs;
370 ref_entry* ref = head;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800371 while (ref != NULL) {
372 if (ref->id == id) {
373 *refs = ref->next;
374 delete ref;
375 return;
376 }
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800377 refs = &ref->next;
378 ref = *refs;
379 }
Mathias Agopianad099652011-08-10 21:07:02 -0700380
Steve Block1b781ab2012-01-06 19:20:56 +0000381 ALOGE("RefBase: removing id %p on RefBase %p"
Mathias Agopianad099652011-08-10 21:07:02 -0700382 "(weakref_type %p) that doesn't exist!",
383 id, mBase, this);
384
385 ref = head;
386 while (ref) {
387 char inc = ref->ref >= 0 ? '+' : '-';
Steve Blockeb095332011-12-20 16:23:08 +0000388 ALOGD("\t%c ID %p (ref %d):", inc, ref->id, ref->ref);
Mathias Agopianad099652011-08-10 21:07:02 -0700389 ref = ref->next;
390 }
391
Christopher Ferris0e691602020-10-14 14:13:58 -0700392#if CALLSTACK_ENABLED
Hans Boehm2a019ec2018-08-07 23:45:25 +0000393 CallStack::logStack(LOG_TAG);
Christopher Ferris0e691602020-10-14 14:13:58 -0700394#endif
Mathias Agopianad099652011-08-10 21:07:02 -0700395 }
396 }
397
398 void renameRefsId(ref_entry* r, const void* old_id, const void* new_id)
399 {
400 if (mTrackEnabled) {
401 AutoMutex _l(mMutex);
402 ref_entry* ref = r;
403 while (ref != NULL) {
404 if (ref->id == old_id) {
405 ref->id = new_id;
406 }
407 ref = ref->next;
408 }
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800409 }
410 }
411
412 void printRefsLocked(String8* out, const ref_entry* refs) const
413 {
414 char buf[128];
415 while (refs) {
416 char inc = refs->ref >= 0 ? '+' : '-';
George Burgess IVe7aa2b22016-03-02 14:02:55 -0800417 snprintf(buf, sizeof(buf), "\t%c ID %p (ref %d):\n",
418 inc, refs->id, refs->ref);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800419 out->append(buf);
Christopher Ferris0e691602020-10-14 14:13:58 -0700420#if DEBUG_REFS_CALLSTACK_ENABLED && CALLSTACK_ENABLED
Hans Boehm2a019ec2018-08-07 23:45:25 +0000421 out->append(CallStack::stackToString("\t\t", refs->stack.get()));
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800422#else
423 out->append("\t\t(call stacks disabled)");
424#endif
425 refs = refs->next;
426 }
427 }
428
Mathias Agopianad099652011-08-10 21:07:02 -0700429 mutable Mutex mMutex;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800430 ref_entry* mStrongRefs;
431 ref_entry* mWeakRefs;
432
433 bool mTrackEnabled;
434 // Collect stack traces on addref and removeref, instead of deleting the stack references
435 // on removeref that match the address ones.
436 bool mRetain;
437
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800438#endif
439};
440
441// ---------------------------------------------------------------------------
442
443void RefBase::incStrong(const void* id) const
444{
445 weakref_impl* const refs = mRefs;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800446 refs->incWeak(id);
Christopher Ferris0e691602020-10-14 14:13:58 -0700447
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800448 refs->addStrongRef(id);
Hans Boehme263e6c2016-05-11 18:15:12 -0700449 const int32_t c = refs->mStrong.fetch_add(1, std::memory_order_relaxed);
Steve Blockae074452012-01-09 18:35:44 +0000450 ALOG_ASSERT(c > 0, "incStrong() called on %p after last strong ref", refs);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800451#if PRINT_REFS
Steve Blockeb095332011-12-20 16:23:08 +0000452 ALOGD("incStrong of %p from %p: cnt=%d\n", this, id, c);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800453#endif
454 if (c != INITIAL_STRONG_VALUE) {
455 return;
456 }
457
Steven Morelandc340a082022-07-26 17:09:50 +0000458 check_not_on_stack(this);
459
Chih-Hung Hsieh122352d2017-10-02 15:20:07 -0700460 int32_t old __unused = refs->mStrong.fetch_sub(INITIAL_STRONG_VALUE, std::memory_order_relaxed);
Hans Boehme263e6c2016-05-11 18:15:12 -0700461 // A decStrong() must still happen after us.
462 ALOG_ASSERT(old > INITIAL_STRONG_VALUE, "0x%x too small", old);
Mathias Agopianad099652011-08-10 21:07:02 -0700463 refs->mBase->onFirstRef();
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800464}
465
Steven Morelandda75cef2021-03-31 16:05:04 +0000466void RefBase::incStrongRequireStrong(const void* id) const {
467 weakref_impl* const refs = mRefs;
468 refs->incWeak(id);
469
470 refs->addStrongRef(id);
471 const int32_t c = refs->mStrong.fetch_add(1, std::memory_order_relaxed);
472
473 LOG_ALWAYS_FATAL_IF(c <= 0 || c == INITIAL_STRONG_VALUE,
474 "incStrongRequireStrong() called on %p which isn't already owned", refs);
475#if PRINT_REFS
476 ALOGD("incStrong (requiring strong) of %p from %p: cnt=%d\n", this, id, c);
477#endif
478}
479
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800480void RefBase::decStrong(const void* id) const
481{
482 weakref_impl* const refs = mRefs;
483 refs->removeStrongRef(id);
Hans Boehme263e6c2016-05-11 18:15:12 -0700484 const int32_t c = refs->mStrong.fetch_sub(1, std::memory_order_release);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800485#if PRINT_REFS
Steve Blockeb095332011-12-20 16:23:08 +0000486 ALOGD("decStrong of %p from %p: cnt=%d\n", this, id, c);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800487#endif
Hans Boehm23c857e2016-08-02 18:39:30 -0700488 LOG_ALWAYS_FATAL_IF(BAD_STRONG(c), "decStrong() called on %p too many times",
489 refs);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800490 if (c == 1) {
Hans Boehme263e6c2016-05-11 18:15:12 -0700491 std::atomic_thread_fence(std::memory_order_acquire);
Mathias Agopianad099652011-08-10 21:07:02 -0700492 refs->mBase->onLastStrongRef(id);
Hans Boehme263e6c2016-05-11 18:15:12 -0700493 int32_t flags = refs->mFlags.load(std::memory_order_relaxed);
494 if ((flags&OBJECT_LIFETIME_MASK) == OBJECT_LIFETIME_STRONG) {
Mathias Agopian9c8fa9e2011-06-15 20:42:47 -0700495 delete this;
Hans Boehm23c857e2016-08-02 18:39:30 -0700496 // The destructor does not delete refs in this case.
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800497 }
498 }
Hans Boehme263e6c2016-05-11 18:15:12 -0700499 // Note that even with only strong reference operations, the thread
500 // deallocating this may not be the same as the thread deallocating refs.
501 // That's OK: all accesses to this happen before its deletion here,
502 // and all accesses to refs happen before its deletion in the final decWeak.
503 // The destructor can safely access mRefs because either it's deleting
504 // mRefs itself, or it's running entirely before the final mWeak decrement.
George Burgess IV6753bc42017-10-01 12:38:44 -0700505 //
506 // Since we're doing atomic loads of `flags`, the static analyzer assumes
507 // they can change between `delete this;` and `refs->decWeak(id);`. This is
508 // not the case. The analyzer may become more okay with this patten when
509 // https://bugs.llvm.org/show_bug.cgi?id=34365 gets resolved. NOLINTNEXTLINE
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800510 refs->decWeak(id);
511}
512
513void RefBase::forceIncStrong(const void* id) const
514{
Hans Boehme263e6c2016-05-11 18:15:12 -0700515 // Allows initial mStrong of 0 in addition to INITIAL_STRONG_VALUE.
516 // TODO: Better document assumptions.
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800517 weakref_impl* const refs = mRefs;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800518 refs->incWeak(id);
Christopher Ferris0e691602020-10-14 14:13:58 -0700519
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800520 refs->addStrongRef(id);
Hans Boehme263e6c2016-05-11 18:15:12 -0700521 const int32_t c = refs->mStrong.fetch_add(1, std::memory_order_relaxed);
Steve Blockae074452012-01-09 18:35:44 +0000522 ALOG_ASSERT(c >= 0, "forceIncStrong called on %p after ref count underflow",
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800523 refs);
524#if PRINT_REFS
Steve Blockeb095332011-12-20 16:23:08 +0000525 ALOGD("forceIncStrong of %p from %p: cnt=%d\n", this, id, c);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800526#endif
527
528 switch (c) {
529 case INITIAL_STRONG_VALUE:
Hans Boehme263e6c2016-05-11 18:15:12 -0700530 refs->mStrong.fetch_sub(INITIAL_STRONG_VALUE,
531 std::memory_order_relaxed);
Chih-Hung Hsieh502f4862018-09-13 11:08:41 -0700532 FALLTHROUGH_INTENDED;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800533 case 0:
Mathias Agopianad099652011-08-10 21:07:02 -0700534 refs->mBase->onFirstRef();
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800535 }
536}
537
538int32_t RefBase::getStrongCount() const
539{
Hans Boehme263e6c2016-05-11 18:15:12 -0700540 // Debugging only; No memory ordering guarantees.
541 return mRefs->mStrong.load(std::memory_order_relaxed);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800542}
543
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800544RefBase* RefBase::weakref_type::refBase() const
545{
546 return static_cast<const weakref_impl*>(this)->mBase;
547}
548
549void RefBase::weakref_type::incWeak(const void* id)
550{
551 weakref_impl* const impl = static_cast<weakref_impl*>(this);
552 impl->addWeakRef(id);
Hans Boehme263e6c2016-05-11 18:15:12 -0700553 const int32_t c __unused = impl->mWeak.fetch_add(1,
554 std::memory_order_relaxed);
Steve Blockae074452012-01-09 18:35:44 +0000555 ALOG_ASSERT(c >= 0, "incWeak called on %p after last weak ref", this);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800556}
557
Steven Morelandda75cef2021-03-31 16:05:04 +0000558void RefBase::weakref_type::incWeakRequireWeak(const void* id)
559{
560 weakref_impl* const impl = static_cast<weakref_impl*>(this);
561 impl->addWeakRef(id);
562 const int32_t c __unused = impl->mWeak.fetch_add(1,
563 std::memory_order_relaxed);
564 LOG_ALWAYS_FATAL_IF(c <= 0, "incWeakRequireWeak called on %p which has no weak refs", this);
565}
Mathias Agopianad099652011-08-10 21:07:02 -0700566
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800567void RefBase::weakref_type::decWeak(const void* id)
568{
569 weakref_impl* const impl = static_cast<weakref_impl*>(this);
570 impl->removeWeakRef(id);
Hans Boehme263e6c2016-05-11 18:15:12 -0700571 const int32_t c = impl->mWeak.fetch_sub(1, std::memory_order_release);
Hans Boehm23c857e2016-08-02 18:39:30 -0700572 LOG_ALWAYS_FATAL_IF(BAD_WEAK(c), "decWeak called on %p too many times",
573 this);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800574 if (c != 1) return;
Hans Boehme263e6c2016-05-11 18:15:12 -0700575 atomic_thread_fence(std::memory_order_acquire);
Mathias Agopianad099652011-08-10 21:07:02 -0700576
Hans Boehme263e6c2016-05-11 18:15:12 -0700577 int32_t flags = impl->mFlags.load(std::memory_order_relaxed);
578 if ((flags&OBJECT_LIFETIME_MASK) == OBJECT_LIFETIME_STRONG) {
Mathias Agopianad099652011-08-10 21:07:02 -0700579 // This is the regular lifetime case. The object is destroyed
580 // when the last strong reference goes away. Since weakref_impl
Hans Boehm23c857e2016-08-02 18:39:30 -0700581 // outlives the object, it is not destroyed in the dtor, and
Mathias Agopianad099652011-08-10 21:07:02 -0700582 // we'll have to do it here.
Hans Boehme263e6c2016-05-11 18:15:12 -0700583 if (impl->mStrong.load(std::memory_order_relaxed)
584 == INITIAL_STRONG_VALUE) {
Hans Boehm23c857e2016-08-02 18:39:30 -0700585 // Decrementing a weak count to zero when object never had a strong
586 // reference. We assume it acquired a weak reference early, e.g.
587 // in the constructor, and will eventually be properly destroyed,
588 // usually via incrementing and decrementing the strong count.
589 // Thus we no longer do anything here. We log this case, since it
590 // seems to be extremely rare, and should not normally occur. We
591 // used to deallocate mBase here, so this may now indicate a leak.
592 ALOGW("RefBase: Object at %p lost last weak reference "
593 "before it had a strong reference", impl->mBase);
Mathias Agopianad099652011-08-10 21:07:02 -0700594 } else {
Steve Blockb37fbe92011-10-20 11:56:00 +0100595 // ALOGV("Freeing refs %p of old RefBase %p\n", this, impl->mBase);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800596 delete impl;
597 }
598 } else {
Hans Boehme263e6c2016-05-11 18:15:12 -0700599 // This is the OBJECT_LIFETIME_WEAK case. The last weak-reference
600 // is gone, we can destroy the object.
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800601 impl->mBase->onLastWeakRef(id);
Hans Boehme263e6c2016-05-11 18:15:12 -0700602 delete impl->mBase;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800603 }
604}
605
606bool RefBase::weakref_type::attemptIncStrong(const void* id)
607{
608 incWeak(id);
Christopher Ferris0e691602020-10-14 14:13:58 -0700609
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800610 weakref_impl* const impl = static_cast<weakref_impl*>(this);
Hans Boehme263e6c2016-05-11 18:15:12 -0700611 int32_t curCount = impl->mStrong.load(std::memory_order_relaxed);
Dianne Hackborna729ab12013-03-14 15:26:30 -0700612
613 ALOG_ASSERT(curCount >= 0,
614 "attemptIncStrong called on %p after underflow", this);
615
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800616 while (curCount > 0 && curCount != INITIAL_STRONG_VALUE) {
Dianne Hackborna729ab12013-03-14 15:26:30 -0700617 // we're in the easy/common case of promoting a weak-reference
618 // from an existing strong reference.
Hans Boehme263e6c2016-05-11 18:15:12 -0700619 if (impl->mStrong.compare_exchange_weak(curCount, curCount+1,
620 std::memory_order_relaxed)) {
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800621 break;
622 }
Dianne Hackborna729ab12013-03-14 15:26:30 -0700623 // the strong count has changed on us, we need to re-assert our
Hans Boehme263e6c2016-05-11 18:15:12 -0700624 // situation. curCount was updated by compare_exchange_weak.
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800625 }
Christopher Ferris0e691602020-10-14 14:13:58 -0700626
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800627 if (curCount <= 0 || curCount == INITIAL_STRONG_VALUE) {
Dianne Hackborna729ab12013-03-14 15:26:30 -0700628 // we're now in the harder case of either:
629 // - there never was a strong reference on us
630 // - or, all strong references have been released
Hans Boehme263e6c2016-05-11 18:15:12 -0700631 int32_t flags = impl->mFlags.load(std::memory_order_relaxed);
632 if ((flags&OBJECT_LIFETIME_MASK) == OBJECT_LIFETIME_STRONG) {
Dianne Hackborna729ab12013-03-14 15:26:30 -0700633 // this object has a "normal" life-time, i.e.: it gets destroyed
634 // when the last strong reference goes away
635 if (curCount <= 0) {
636 // the last strong-reference got released, the object cannot
637 // be revived.
638 decWeak(id);
639 return false;
640 }
641
642 // here, curCount == INITIAL_STRONG_VALUE, which means
643 // there never was a strong-reference, so we can try to
644 // promote this object; we need to do that atomically.
645 while (curCount > 0) {
Hans Boehme263e6c2016-05-11 18:15:12 -0700646 if (impl->mStrong.compare_exchange_weak(curCount, curCount+1,
647 std::memory_order_relaxed)) {
Dianne Hackborna729ab12013-03-14 15:26:30 -0700648 break;
649 }
650 // the strong count has changed on us, we need to re-assert our
651 // situation (e.g.: another thread has inc/decStrong'ed us)
Hans Boehme263e6c2016-05-11 18:15:12 -0700652 // curCount has been updated.
Dianne Hackborna729ab12013-03-14 15:26:30 -0700653 }
654
655 if (curCount <= 0) {
656 // promote() failed, some other thread destroyed us in the
657 // meantime (i.e.: strong count reached zero).
658 decWeak(id);
659 return false;
660 }
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800661 } else {
Dianne Hackborna729ab12013-03-14 15:26:30 -0700662 // this object has an "extended" life-time, i.e.: it can be
663 // revived from a weak-reference only.
664 // Ask the object's implementation if it agrees to be revived
665 if (!impl->mBase->onIncStrongAttempted(FIRST_INC_STRONG, id)) {
666 // it didn't so give-up.
667 decWeak(id);
668 return false;
669 }
670 // grab a strong-reference, which is always safe due to the
671 // extended life-time.
Hans Boehme263e6c2016-05-11 18:15:12 -0700672 curCount = impl->mStrong.fetch_add(1, std::memory_order_relaxed);
Hans Boehm7f27cbc2016-07-29 14:39:10 -0700673 // If the strong reference count has already been incremented by
674 // someone else, the implementor of onIncStrongAttempted() is holding
675 // an unneeded reference. So call onLastStrongRef() here to remove it.
676 // (No, this is not pretty.) Note that we MUST NOT do this if we
677 // are in fact acquiring the first reference.
678 if (curCount != 0 && curCount != INITIAL_STRONG_VALUE) {
679 impl->mBase->onLastStrongRef(id);
680 }
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800681 }
682 }
Christopher Ferris0e691602020-10-14 14:13:58 -0700683
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800684 impl->addStrongRef(id);
685
686#if PRINT_REFS
Steve Blockeb095332011-12-20 16:23:08 +0000687 ALOGD("attemptIncStrong of %p from %p: cnt=%d\n", this, id, curCount);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800688#endif
689
Hans Boehm7f27cbc2016-07-29 14:39:10 -0700690 // curCount is the value of mStrong before we incremented it.
Hans Boehme263e6c2016-05-11 18:15:12 -0700691 // Now we need to fix-up the count if it was INITIAL_STRONG_VALUE.
692 // This must be done safely, i.e.: handle the case where several threads
Dianne Hackborna729ab12013-03-14 15:26:30 -0700693 // were here in attemptIncStrong().
Hans Boehme263e6c2016-05-11 18:15:12 -0700694 // curCount > INITIAL_STRONG_VALUE is OK, and can happen if we're doing
695 // this in the middle of another incStrong. The subtraction is handled
696 // by the thread that started with INITIAL_STRONG_VALUE.
697 if (curCount == INITIAL_STRONG_VALUE) {
698 impl->mStrong.fetch_sub(INITIAL_STRONG_VALUE,
699 std::memory_order_relaxed);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800700 }
Dianne Hackborna729ab12013-03-14 15:26:30 -0700701
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800702 return true;
703}
704
705bool RefBase::weakref_type::attemptIncWeak(const void* id)
706{
707 weakref_impl* const impl = static_cast<weakref_impl*>(this);
Mathias Agopianad099652011-08-10 21:07:02 -0700708
Hans Boehme263e6c2016-05-11 18:15:12 -0700709 int32_t curCount = impl->mWeak.load(std::memory_order_relaxed);
Steve Blockae074452012-01-09 18:35:44 +0000710 ALOG_ASSERT(curCount >= 0, "attemptIncWeak called on %p after underflow",
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800711 this);
712 while (curCount > 0) {
Hans Boehme263e6c2016-05-11 18:15:12 -0700713 if (impl->mWeak.compare_exchange_weak(curCount, curCount+1,
714 std::memory_order_relaxed)) {
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800715 break;
716 }
Hans Boehme263e6c2016-05-11 18:15:12 -0700717 // curCount has been updated.
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800718 }
719
720 if (curCount > 0) {
721 impl->addWeakRef(id);
722 }
723
724 return curCount > 0;
725}
726
727int32_t RefBase::weakref_type::getWeakCount() const
728{
Hans Boehme263e6c2016-05-11 18:15:12 -0700729 // Debug only!
730 return static_cast<const weakref_impl*>(this)->mWeak
731 .load(std::memory_order_relaxed);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800732}
733
734void RefBase::weakref_type::printRefs() const
735{
736 static_cast<const weakref_impl*>(this)->printRefs();
737}
738
739void RefBase::weakref_type::trackMe(bool enable, bool retain)
740{
Mathias Agopianad099652011-08-10 21:07:02 -0700741 static_cast<weakref_impl*>(this)->trackMe(enable, retain);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800742}
743
744RefBase::weakref_type* RefBase::createWeak(const void* id) const
745{
746 mRefs->incWeak(id);
747 return mRefs;
748}
749
750RefBase::weakref_type* RefBase::getWeakRefs() const
751{
752 return mRefs;
753}
754
755RefBase::RefBase()
756 : mRefs(new weakref_impl(this))
757{
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800758}
759
760RefBase::~RefBase()
761{
Hans Boehm23c857e2016-08-02 18:39:30 -0700762 int32_t flags = mRefs->mFlags.load(std::memory_order_relaxed);
763 // Life-time of this object is extended to WEAK, in
764 // which case weakref_impl doesn't out-live the object and we
765 // can free it now.
766 if ((flags & OBJECT_LIFETIME_MASK) == OBJECT_LIFETIME_WEAK) {
767 // It's possible that the weak count is not 0 if the object
768 // re-acquired a weak reference in its destructor
769 if (mRefs->mWeak.load(std::memory_order_relaxed) == 0) {
770 delete mRefs;
771 }
Steven Moreland483a2de2022-07-26 17:57:39 +0000772 } else {
773 int32_t strongs = mRefs->mStrong.load(std::memory_order_relaxed);
Steven Morelandf164de82022-03-11 02:28:57 +0000774
Steven Moreland483a2de2022-07-26 17:57:39 +0000775 if (strongs == INITIAL_STRONG_VALUE) {
776 // We never acquired a strong reference on this object.
777
778 // It would be nice to make this fatal, but many places use RefBase on the stack.
779 // However, this is dangerous because it's also common for code to use the
780 // sp<T>(T*) constructor, assuming that if the object is around, it is already
781 // owned by an sp<>.
782 ALOGW("RefBase: Explicit destruction, weak count = %d (in %p). Use sp<> to manage this "
783 "object.",
784 mRefs->mWeak.load(), this);
Christopher Ferris0e691602020-10-14 14:13:58 -0700785
786#if CALLSTACK_ENABLED
Steven Moreland483a2de2022-07-26 17:57:39 +0000787 CallStack::logStack(LOG_TAG);
Christopher Ferris0e691602020-10-14 14:13:58 -0700788#endif
Steven Moreland483a2de2022-07-26 17:57:39 +0000789 } else if (strongs != 0) {
790 LOG_ALWAYS_FATAL("RefBase: object %p with strong count %d deleted. Double owned?", this,
791 strongs);
792 }
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800793 }
Hans Boehm23c857e2016-08-02 18:39:30 -0700794 // For debugging purposes, clear mRefs. Ineffective against outstanding wp's.
Yi Konge1731a42018-07-16 18:11:34 -0700795 const_cast<weakref_impl*&>(mRefs) = nullptr;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800796}
797
798void RefBase::extendObjectLifetime(int32_t mode)
799{
Hans Boehme263e6c2016-05-11 18:15:12 -0700800 // Must be happens-before ordered with respect to construction or any
801 // operation that could destroy the object.
802 mRefs->mFlags.fetch_or(mode, std::memory_order_relaxed);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800803}
804
805void RefBase::onFirstRef()
806{
807}
808
809void RefBase::onLastStrongRef(const void* /*id*/)
810{
811}
812
Mark Salyzyn5bed8032014-04-30 11:10:46 -0700813bool RefBase::onIncStrongAttempted(uint32_t flags, const void* /*id*/)
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800814{
815 return (flags&FIRST_INC_STRONG) ? true : false;
816}
817
818void RefBase::onLastWeakRef(const void* /*id*/)
819{
820}
Mathias Agopianad099652011-08-10 21:07:02 -0700821
822// ---------------------------------------------------------------------------
823
Mathias Agopianad099652011-08-10 21:07:02 -0700824#if DEBUG_REFS
Mark Salyzyn5bed8032014-04-30 11:10:46 -0700825void RefBase::renameRefs(size_t n, const ReferenceRenamer& renamer) {
Mathias Agopianad099652011-08-10 21:07:02 -0700826 for (size_t i=0 ; i<n ; i++) {
Mathias Agopian6cd548c2013-03-18 22:27:41 -0700827 renamer(i);
Mathias Agopianad099652011-08-10 21:07:02 -0700828 }
Mathias Agopianad099652011-08-10 21:07:02 -0700829}
Mark Salyzyn5bed8032014-04-30 11:10:46 -0700830#else
831void RefBase::renameRefs(size_t /*n*/, const ReferenceRenamer& /*renamer*/) { }
832#endif
Mathias Agopianad099652011-08-10 21:07:02 -0700833
Mathias Agopian6cd548c2013-03-18 22:27:41 -0700834void RefBase::renameRefId(weakref_type* ref,
835 const void* old_id, const void* new_id) {
836 weakref_impl* const impl = static_cast<weakref_impl*>(ref);
837 impl->renameStrongRefId(old_id, new_id);
838 impl->renameWeakRefId(old_id, new_id);
839}
840
841void RefBase::renameRefId(RefBase* ref,
842 const void* old_id, const void* new_id) {
843 ref->mRefs->renameStrongRefId(old_id, new_id);
844 ref->mRefs->renameWeakRefId(old_id, new_id);
845}
846
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800847}; // namespace android