blob: ab122c73213e6d3f68786e6217b1ce6ba2c9a490 [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
Steven Moreland377adea2022-10-08 05:06:52 +000024#include <fcntl.h>
Christopher Ferris0e691602020-10-14 14:13:58 -070025#include <log/log.h>
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080026
Christopher Ferris0e691602020-10-14 14:13:58 -070027#include <utils/RefBase.h>
Steven Moreland377adea2022-10-08 05:06:52 +000028#include <utils/String8.h>
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080029
Hans Boehm2a019ec2018-08-07 23:45:25 +000030#include <utils/Mutex.h>
31
Mark Salyzyn5bed8032014-04-30 11:10:46 -070032#ifndef __unused
33#define __unused __attribute__((__unused__))
34#endif
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080035
Hans Boehm2a019ec2018-08-07 23:45:25 +000036// Compile with refcounting debugging enabled.
Steven Moreland377adea2022-10-08 05:06:52 +000037#ifndef DEBUG_REFS
Hans Boehm2a019ec2018-08-07 23:45:25 +000038#define DEBUG_REFS 0
Steven Moreland377adea2022-10-08 05:06:52 +000039#endif
Hans Boehm2a019ec2018-08-07 23:45:25 +000040
41// The following three are ignored unless DEBUG_REFS is set.
Mathias Agopian6d4419d2013-03-18 20:31:18 -070042
43// whether ref-tracking is enabled by default, if not, trackMe(true, false)
44// needs to be called explicitly
Hans Boehm2a019ec2018-08-07 23:45:25 +000045#define DEBUG_REFS_ENABLED_BY_DEFAULT 0
Mathias Agopian6d4419d2013-03-18 20:31:18 -070046
47// whether callstack are collected (significantly slows things down)
Hans Boehm2a019ec2018-08-07 23:45:25 +000048#define DEBUG_REFS_CALLSTACK_ENABLED 1
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080049
Mathias Agopian6d4419d2013-03-18 20:31:18 -070050// folder where stack traces are saved when DEBUG_REFS is enabled
51// this folder needs to exist and be writable
Steven Morelandb7412c82022-10-08 05:07:52 +000052#ifdef __ANDROID__
Hans Boehm2a019ec2018-08-07 23:45:25 +000053#define DEBUG_REFS_CALLSTACK_PATH "/data/debug"
Steven Morelandb7412c82022-10-08 05:07:52 +000054#else
55#define DEBUG_REFS_CALLSTACK_PATH "."
56#endif
Mathias Agopian6d4419d2013-03-18 20:31:18 -070057
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080058// log all reference counting operations
Hans Boehm2a019ec2018-08-07 23:45:25 +000059#define PRINT_REFS 0
60
Andrei Homescu5c15de22021-12-10 05:32:17 +000061#if defined(__linux__)
Christopher Ferris0e691602020-10-14 14:13:58 -070062// CallStack is only supported on linux type platforms.
63#define CALLSTACK_ENABLED 1
64#else
65#define CALLSTACK_ENABLED 0
66#endif
67
68#if CALLSTACK_ENABLED
69#include <utils/CallStack.h>
70#endif
71
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080072// ---------------------------------------------------------------------------
73
74namespace android {
75
Hans Boehm9ba71922016-07-21 18:56:55 -070076// Observations, invariants, etc:
Hans Boehme263e6c2016-05-11 18:15:12 -070077
Hans Boehm9ba71922016-07-21 18:56:55 -070078// By default, obects are destroyed when the last strong reference disappears
79// or, if the object never had a strong reference, when the last weak reference
80// disappears.
81//
Hans Boehme263e6c2016-05-11 18:15:12 -070082// OBJECT_LIFETIME_WEAK changes this behavior to retain the object
83// unconditionally until the last reference of either kind disappears. The
84// client ensures that the extendObjectLifetime call happens before the dec
85// call that would otherwise have deallocated the object, or before an
86// attemptIncStrong call that might rely on it. We do not worry about
87// concurrent changes to the object lifetime.
Hans Boehm9ba71922016-07-21 18:56:55 -070088//
89// AttemptIncStrong will succeed if the object has a strong reference, or if it
90// has a weak reference and has never had a strong reference.
91// AttemptIncWeak really does succeed only if there is already a WEAK
92// reference, and thus may fail when attemptIncStrong would succeed.
93//
Hans Boehme263e6c2016-05-11 18:15:12 -070094// mStrong is the strong reference count. mWeak is the weak reference count.
95// Between calls, and ignoring memory ordering effects, mWeak includes strong
96// references, and is thus >= mStrong.
97//
Hans Boehm9ba71922016-07-21 18:56:55 -070098// A weakref_impl holds all the information, including both reference counts,
99// required to perform wp<> operations. Thus these can continue to be performed
100// after the RefBase object has been destroyed.
101//
Hans Boehme263e6c2016-05-11 18:15:12 -0700102// A weakref_impl is allocated as the value of mRefs in a RefBase object on
103// construction.
Hans Boehm23c857e2016-08-02 18:39:30 -0700104// In the OBJECT_LIFETIME_STRONG case, it is normally deallocated in decWeak,
105// and hence lives as long as the last weak reference. (It can also be
106// deallocated in the RefBase destructor iff the strong reference count was
107// never incremented and the weak count is zero, e.g. if the RefBase object is
108// explicitly destroyed without decrementing the strong count. This should be
109// avoided.) In this case, the RefBase destructor should be invoked from
110// decStrong.
111// In the OBJECT_LIFETIME_WEAK case, the weakref_impl is always deallocated in
112// the RefBase destructor, which is always invoked by decWeak. DecStrong
113// explicitly avoids the deletion in this case.
Hans Boehme263e6c2016-05-11 18:15:12 -0700114//
115// Memory ordering:
116// The client must ensure that every inc() call, together with all other
117// accesses to the object, happens before the corresponding dec() call.
118//
119// We try to keep memory ordering constraints on atomics as weak as possible,
120// since memory fences or ordered memory accesses are likely to be a major
121// performance cost for this code. All accesses to mStrong, mWeak, and mFlags
122// explicitly relax memory ordering in some way.
123//
124// The only operations that are not memory_order_relaxed are reference count
125// decrements. All reference count decrements are release operations. In
126// addition, the final decrement leading the deallocation is followed by an
127// acquire fence, which we can view informally as also turning it into an
128// acquire operation. (See 29.8p4 [atomics.fences] for details. We could
129// alternatively use acq_rel operations for all decrements. This is probably
130// slower on most current (2016) hardware, especially on ARMv7, but that may
131// not be true indefinitely.)
132//
133// This convention ensures that the second-to-last decrement synchronizes with
134// (in the language of 1.10 in the C++ standard) the final decrement of a
135// reference count. Since reference counts are only updated using atomic
136// read-modify-write operations, this also extends to any earlier decrements.
137// (See "release sequence" in 1.10.)
138//
139// Since all operations on an object happen before the corresponding reference
140// count decrement, and all reference count decrements happen before the final
141// one, we are guaranteed that all other object accesses happen before the
142// object is destroyed.
143
144
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800145#define INITIAL_STRONG_VALUE (1<<28)
146
Hans Boehm23c857e2016-08-02 18:39:30 -0700147#define MAX_COUNT 0xfffff
148
149// Test whether the argument is a clearly invalid strong reference count.
150// Used only for error checking on the value before an atomic decrement.
151// Intended to be very cheap.
152// Note that we cannot just check for excess decrements by comparing to zero
153// since the object would be deallocated before that.
154#define BAD_STRONG(c) \
155 ((c) == 0 || ((c) & (~(MAX_COUNT | INITIAL_STRONG_VALUE))) != 0)
156
157// Same for weak counts.
158#define BAD_WEAK(c) ((c) == 0 || ((c) & (~MAX_COUNT)) != 0)
159
Steven Morelandcd4ef872022-07-29 00:40:29 +0000160// name kept because prebuilts used to use it from inlining sp<> code
161void sp_report_stack_pointer() { LOG_ALWAYS_FATAL("RefBase used with stack pointer argument"); }
Steven Morelandc340a082022-07-26 17:09:50 +0000162
163// Check whether address is definitely on the calling stack. We actually check whether it is on
164// the same 4K page as the frame pointer.
165//
166// Assumptions:
167// - Pages are never smaller than 4K (MIN_PAGE_SIZE)
168// - Malloced memory never shares a page with a stack.
169//
170// It does not appear safe to broaden this check to include adjacent pages; apparently this code
171// is used in environments where there may not be a guard page below (at higher addresses than)
172// the bottom of the stack.
173static void check_not_on_stack(const void* ptr) {
174 static constexpr int MIN_PAGE_SIZE = 0x1000; // 4K. Safer than including sys/user.h.
175 static constexpr uintptr_t MIN_PAGE_MASK = ~static_cast<uintptr_t>(MIN_PAGE_SIZE - 1);
176 uintptr_t my_frame_address =
177 reinterpret_cast<uintptr_t>(__builtin_frame_address(0 /* this frame */));
178 if (((reinterpret_cast<uintptr_t>(ptr) ^ my_frame_address) & MIN_PAGE_MASK) == 0) {
179 sp_report_stack_pointer();
180 }
181}
182
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800183// ---------------------------------------------------------------------------
184
185class RefBase::weakref_impl : public RefBase::weakref_type
186{
187public:
Hans Boehme263e6c2016-05-11 18:15:12 -0700188 std::atomic<int32_t> mStrong;
189 std::atomic<int32_t> mWeak;
190 RefBase* const mBase;
191 std::atomic<int32_t> mFlags;
Mathias Agopian9c8fa9e2011-06-15 20:42:47 -0700192
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800193#if !DEBUG_REFS
194
Chih-Hung Hsieh1c563d92016-04-29 15:44:04 -0700195 explicit weakref_impl(RefBase* base)
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800196 : mStrong(INITIAL_STRONG_VALUE)
197 , mWeak(0)
198 , mBase(base)
陈冠有0cbef722021-08-22 16:36:28 +0000199 , mFlags(OBJECT_LIFETIME_STRONG)
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800200 {
201 }
202
203 void addStrongRef(const void* /*id*/) { }
204 void removeStrongRef(const void* /*id*/) { }
Mathias Agopianad099652011-08-10 21:07:02 -0700205 void renameStrongRefId(const void* /*old_id*/, const void* /*new_id*/) { }
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800206 void addWeakRef(const void* /*id*/) { }
207 void removeWeakRef(const void* /*id*/) { }
Mathias Agopianad099652011-08-10 21:07:02 -0700208 void renameWeakRefId(const void* /*old_id*/, const void* /*new_id*/) { }
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800209 void printRefs() const { }
210 void trackMe(bool, bool) { }
211
212#else
213
214 weakref_impl(RefBase* base)
215 : mStrong(INITIAL_STRONG_VALUE)
216 , mWeak(0)
217 , mBase(base)
陈冠有0cbef722021-08-22 16:36:28 +0000218 , mFlags(OBJECT_LIFETIME_STRONG)
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800219 , mStrongRefs(NULL)
220 , mWeakRefs(NULL)
221 , mTrackEnabled(!!DEBUG_REFS_ENABLED_BY_DEFAULT)
222 , mRetain(false)
223 {
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800224 }
Christopher Ferris0e691602020-10-14 14:13:58 -0700225
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800226 ~weakref_impl()
227 {
Mathias Agopianad099652011-08-10 21:07:02 -0700228 bool dumpStack = false;
229 if (!mRetain && mStrongRefs != NULL) {
230 dumpStack = true;
Steve Block1b781ab2012-01-06 19:20:56 +0000231 ALOGE("Strong references remain:");
Mathias Agopianad099652011-08-10 21:07:02 -0700232 ref_entry* refs = mStrongRefs;
233 while (refs) {
234 char inc = refs->ref >= 0 ? '+' : '-';
Steve Blockeb095332011-12-20 16:23:08 +0000235 ALOGD("\t%c ID %p (ref %d):", inc, refs->id, refs->ref);
Christopher Ferris0e691602020-10-14 14:13:58 -0700236#if DEBUG_REFS_CALLSTACK_ENABLED && CALLSTACK_ENABLED
Hans Boehm2a019ec2018-08-07 23:45:25 +0000237 CallStack::logStack(LOG_TAG, refs->stack.get());
Mathias Agopianad099652011-08-10 21:07:02 -0700238#endif
239 refs = refs->next;
240 }
241 }
242
243 if (!mRetain && mWeakRefs != NULL) {
244 dumpStack = true;
Steve Block1b781ab2012-01-06 19:20:56 +0000245 ALOGE("Weak references remain!");
Mathias Agopianad099652011-08-10 21:07:02 -0700246 ref_entry* refs = mWeakRefs;
247 while (refs) {
248 char inc = refs->ref >= 0 ? '+' : '-';
Steve Blockeb095332011-12-20 16:23:08 +0000249 ALOGD("\t%c ID %p (ref %d):", inc, refs->id, refs->ref);
Christopher Ferris0e691602020-10-14 14:13:58 -0700250#if DEBUG_REFS_CALLSTACK_ENABLED && CALLSTACK_ENABLED
Hans Boehm2a019ec2018-08-07 23:45:25 +0000251 CallStack::logStack(LOG_TAG, refs->stack.get());
Mathias Agopianad099652011-08-10 21:07:02 -0700252#endif
253 refs = refs->next;
254 }
255 }
256 if (dumpStack) {
Steve Block1b781ab2012-01-06 19:20:56 +0000257 ALOGE("above errors at:");
Christopher Ferris0e691602020-10-14 14:13:58 -0700258#if CALLSTACK_ENABLED
Hans Boehm2a019ec2018-08-07 23:45:25 +0000259 CallStack::logStack(LOG_TAG);
Christopher Ferris0e691602020-10-14 14:13:58 -0700260#endif
Mathias Agopianad099652011-08-10 21:07:02 -0700261 }
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800262 }
263
Mathias Agopianad099652011-08-10 21:07:02 -0700264 void addStrongRef(const void* id) {
Steve Blockeb095332011-12-20 16:23:08 +0000265 //ALOGD_IF(mTrackEnabled,
Mathias Agopianad099652011-08-10 21:07:02 -0700266 // "addStrongRef: RefBase=%p, id=%p", mBase, id);
Hans Boehme263e6c2016-05-11 18:15:12 -0700267 addRef(&mStrongRefs, id, mStrong.load(std::memory_order_relaxed));
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800268 }
269
Mathias Agopianad099652011-08-10 21:07:02 -0700270 void removeStrongRef(const void* id) {
Steve Blockeb095332011-12-20 16:23:08 +0000271 //ALOGD_IF(mTrackEnabled,
Mathias Agopianad099652011-08-10 21:07:02 -0700272 // "removeStrongRef: RefBase=%p, id=%p", mBase, id);
273 if (!mRetain) {
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800274 removeRef(&mStrongRefs, id);
Mathias Agopianad099652011-08-10 21:07:02 -0700275 } else {
Hans Boehme263e6c2016-05-11 18:15:12 -0700276 addRef(&mStrongRefs, id, -mStrong.load(std::memory_order_relaxed));
Mathias Agopianad099652011-08-10 21:07:02 -0700277 }
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800278 }
279
Mathias Agopianad099652011-08-10 21:07:02 -0700280 void renameStrongRefId(const void* old_id, const void* new_id) {
Steve Blockeb095332011-12-20 16:23:08 +0000281 //ALOGD_IF(mTrackEnabled,
Mathias Agopianad099652011-08-10 21:07:02 -0700282 // "renameStrongRefId: RefBase=%p, oid=%p, nid=%p",
283 // mBase, old_id, new_id);
284 renameRefsId(mStrongRefs, old_id, new_id);
285 }
286
287 void addWeakRef(const void* id) {
Hans Boehme263e6c2016-05-11 18:15:12 -0700288 addRef(&mWeakRefs, id, mWeak.load(std::memory_order_relaxed));
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800289 }
290
Mathias Agopianad099652011-08-10 21:07:02 -0700291 void removeWeakRef(const void* id) {
292 if (!mRetain) {
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800293 removeRef(&mWeakRefs, id);
Mathias Agopianad099652011-08-10 21:07:02 -0700294 } else {
Hans Boehme263e6c2016-05-11 18:15:12 -0700295 addRef(&mWeakRefs, id, -mWeak.load(std::memory_order_relaxed));
Mathias Agopianad099652011-08-10 21:07:02 -0700296 }
297 }
298
299 void renameWeakRefId(const void* old_id, const void* new_id) {
300 renameRefsId(mWeakRefs, old_id, new_id);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800301 }
302
Christopher Ferris0e691602020-10-14 14:13:58 -0700303 void trackMe(bool track, bool retain) {
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800304 mTrackEnabled = track;
305 mRetain = retain;
306 }
307
308 void printRefs() const
309 {
310 String8 text;
311
312 {
Mathias Agopianad099652011-08-10 21:07:02 -0700313 Mutex::Autolock _l(mMutex);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800314 char buf[128];
George Burgess IVe7aa2b22016-03-02 14:02:55 -0800315 snprintf(buf, sizeof(buf),
316 "Strong references on RefBase %p (weakref_type %p):\n",
317 mBase, this);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800318 text.append(buf);
319 printRefsLocked(&text, mStrongRefs);
George Burgess IVe7aa2b22016-03-02 14:02:55 -0800320 snprintf(buf, sizeof(buf),
321 "Weak references on RefBase %p (weakref_type %p):\n",
322 mBase, this);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800323 text.append(buf);
324 printRefsLocked(&text, mWeakRefs);
325 }
326
327 {
328 char name[100];
George Burgess IVe7aa2b22016-03-02 14:02:55 -0800329 snprintf(name, sizeof(name), DEBUG_REFS_CALLSTACK_PATH "/%p.stack",
330 this);
Steven Morelandb7412c82022-10-08 05:07:52 +0000331 int rc = open(name, O_RDWR | O_CREAT | O_APPEND, 0644);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800332 if (rc >= 0) {
Hans Boehm2a019ec2018-08-07 23:45:25 +0000333 (void)write(rc, text.string(), text.length());
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800334 close(rc);
Steven Morelandb7412c82022-10-08 05:07:52 +0000335 ALOGI("STACK TRACE for %p saved in %s", this, name);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800336 }
Steve Block1b781ab2012-01-06 19:20:56 +0000337 else ALOGE("FAILED TO PRINT STACK TRACE for %p in %s: %s", this,
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800338 name, strerror(errno));
339 }
340 }
341
342private:
343 struct ref_entry
344 {
345 ref_entry* next;
346 const void* id;
Christopher Ferris0e691602020-10-14 14:13:58 -0700347#if DEBUG_REFS_CALLSTACK_ENABLED && CALLSTACK_ENABLED
Hans Boehm2a019ec2018-08-07 23:45:25 +0000348 CallStack::CallStackUPtr stack;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800349#endif
350 int32_t ref;
351 };
352
353 void addRef(ref_entry** refs, const void* id, int32_t mRef)
354 {
355 if (mTrackEnabled) {
356 AutoMutex _l(mMutex);
Mathias Agopianad099652011-08-10 21:07:02 -0700357
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800358 ref_entry* ref = new ref_entry;
359 // Reference count at the time of the snapshot, but before the
360 // update. Positive value means we increment, negative--we
361 // decrement the reference count.
362 ref->ref = mRef;
363 ref->id = id;
Christopher Ferris0e691602020-10-14 14:13:58 -0700364#if DEBUG_REFS_CALLSTACK_ENABLED && CALLSTACK_ENABLED
Hans Boehm2a019ec2018-08-07 23:45:25 +0000365 ref->stack = CallStack::getCurrent(2);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800366#endif
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800367 ref->next = *refs;
368 *refs = ref;
369 }
370 }
371
372 void removeRef(ref_entry** refs, const void* id)
373 {
374 if (mTrackEnabled) {
375 AutoMutex _l(mMutex);
Christopher Ferris0e691602020-10-14 14:13:58 -0700376
Mathias Agopianad099652011-08-10 21:07:02 -0700377 ref_entry* const head = *refs;
378 ref_entry* ref = head;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800379 while (ref != NULL) {
380 if (ref->id == id) {
381 *refs = ref->next;
382 delete ref;
383 return;
384 }
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800385 refs = &ref->next;
386 ref = *refs;
387 }
Mathias Agopianad099652011-08-10 21:07:02 -0700388
Steve Block1b781ab2012-01-06 19:20:56 +0000389 ALOGE("RefBase: removing id %p on RefBase %p"
Mathias Agopianad099652011-08-10 21:07:02 -0700390 "(weakref_type %p) that doesn't exist!",
391 id, mBase, this);
392
393 ref = head;
394 while (ref) {
395 char inc = ref->ref >= 0 ? '+' : '-';
Steve Blockeb095332011-12-20 16:23:08 +0000396 ALOGD("\t%c ID %p (ref %d):", inc, ref->id, ref->ref);
Mathias Agopianad099652011-08-10 21:07:02 -0700397 ref = ref->next;
398 }
399
Christopher Ferris0e691602020-10-14 14:13:58 -0700400#if CALLSTACK_ENABLED
Hans Boehm2a019ec2018-08-07 23:45:25 +0000401 CallStack::logStack(LOG_TAG);
Christopher Ferris0e691602020-10-14 14:13:58 -0700402#endif
Mathias Agopianad099652011-08-10 21:07:02 -0700403 }
404 }
405
406 void renameRefsId(ref_entry* r, const void* old_id, const void* new_id)
407 {
408 if (mTrackEnabled) {
409 AutoMutex _l(mMutex);
410 ref_entry* ref = r;
411 while (ref != NULL) {
412 if (ref->id == old_id) {
413 ref->id = new_id;
414 }
415 ref = ref->next;
416 }
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800417 }
418 }
419
420 void printRefsLocked(String8* out, const ref_entry* refs) const
421 {
422 char buf[128];
423 while (refs) {
424 char inc = refs->ref >= 0 ? '+' : '-';
George Burgess IVe7aa2b22016-03-02 14:02:55 -0800425 snprintf(buf, sizeof(buf), "\t%c ID %p (ref %d):\n",
426 inc, refs->id, refs->ref);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800427 out->append(buf);
Christopher Ferris0e691602020-10-14 14:13:58 -0700428#if DEBUG_REFS_CALLSTACK_ENABLED && CALLSTACK_ENABLED
Hans Boehm2a019ec2018-08-07 23:45:25 +0000429 out->append(CallStack::stackToString("\t\t", refs->stack.get()));
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800430#else
431 out->append("\t\t(call stacks disabled)");
432#endif
433 refs = refs->next;
434 }
435 }
436
Mathias Agopianad099652011-08-10 21:07:02 -0700437 mutable Mutex mMutex;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800438 ref_entry* mStrongRefs;
439 ref_entry* mWeakRefs;
440
441 bool mTrackEnabled;
442 // Collect stack traces on addref and removeref, instead of deleting the stack references
443 // on removeref that match the address ones.
444 bool mRetain;
445
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800446#endif
447};
448
449// ---------------------------------------------------------------------------
450
451void RefBase::incStrong(const void* id) const
452{
453 weakref_impl* const refs = mRefs;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800454 refs->incWeak(id);
Christopher Ferris0e691602020-10-14 14:13:58 -0700455
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800456 refs->addStrongRef(id);
Hans Boehme263e6c2016-05-11 18:15:12 -0700457 const int32_t c = refs->mStrong.fetch_add(1, std::memory_order_relaxed);
Steve Blockae074452012-01-09 18:35:44 +0000458 ALOG_ASSERT(c > 0, "incStrong() called on %p after last strong ref", refs);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800459#if PRINT_REFS
Steve Blockeb095332011-12-20 16:23:08 +0000460 ALOGD("incStrong of %p from %p: cnt=%d\n", this, id, c);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800461#endif
462 if (c != INITIAL_STRONG_VALUE) {
463 return;
464 }
465
Steven Morelandc340a082022-07-26 17:09:50 +0000466 check_not_on_stack(this);
467
Chih-Hung Hsieh122352d2017-10-02 15:20:07 -0700468 int32_t old __unused = refs->mStrong.fetch_sub(INITIAL_STRONG_VALUE, std::memory_order_relaxed);
Hans Boehme263e6c2016-05-11 18:15:12 -0700469 // A decStrong() must still happen after us.
470 ALOG_ASSERT(old > INITIAL_STRONG_VALUE, "0x%x too small", old);
Mathias Agopianad099652011-08-10 21:07:02 -0700471 refs->mBase->onFirstRef();
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800472}
473
Steven Morelandda75cef2021-03-31 16:05:04 +0000474void RefBase::incStrongRequireStrong(const void* id) const {
475 weakref_impl* const refs = mRefs;
476 refs->incWeak(id);
477
478 refs->addStrongRef(id);
479 const int32_t c = refs->mStrong.fetch_add(1, std::memory_order_relaxed);
480
481 LOG_ALWAYS_FATAL_IF(c <= 0 || c == INITIAL_STRONG_VALUE,
482 "incStrongRequireStrong() called on %p which isn't already owned", refs);
483#if PRINT_REFS
484 ALOGD("incStrong (requiring strong) of %p from %p: cnt=%d\n", this, id, c);
485#endif
486}
487
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800488void RefBase::decStrong(const void* id) const
489{
490 weakref_impl* const refs = mRefs;
491 refs->removeStrongRef(id);
Hans Boehme263e6c2016-05-11 18:15:12 -0700492 const int32_t c = refs->mStrong.fetch_sub(1, std::memory_order_release);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800493#if PRINT_REFS
Steve Blockeb095332011-12-20 16:23:08 +0000494 ALOGD("decStrong of %p from %p: cnt=%d\n", this, id, c);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800495#endif
Hans Boehm23c857e2016-08-02 18:39:30 -0700496 LOG_ALWAYS_FATAL_IF(BAD_STRONG(c), "decStrong() called on %p too many times",
497 refs);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800498 if (c == 1) {
Hans Boehme263e6c2016-05-11 18:15:12 -0700499 std::atomic_thread_fence(std::memory_order_acquire);
Mathias Agopianad099652011-08-10 21:07:02 -0700500 refs->mBase->onLastStrongRef(id);
Hans Boehme263e6c2016-05-11 18:15:12 -0700501 int32_t flags = refs->mFlags.load(std::memory_order_relaxed);
502 if ((flags&OBJECT_LIFETIME_MASK) == OBJECT_LIFETIME_STRONG) {
Mathias Agopian9c8fa9e2011-06-15 20:42:47 -0700503 delete this;
Hans Boehm23c857e2016-08-02 18:39:30 -0700504 // The destructor does not delete refs in this case.
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800505 }
506 }
Hans Boehme263e6c2016-05-11 18:15:12 -0700507 // Note that even with only strong reference operations, the thread
508 // deallocating this may not be the same as the thread deallocating refs.
509 // That's OK: all accesses to this happen before its deletion here,
510 // and all accesses to refs happen before its deletion in the final decWeak.
511 // The destructor can safely access mRefs because either it's deleting
512 // mRefs itself, or it's running entirely before the final mWeak decrement.
George Burgess IV6753bc42017-10-01 12:38:44 -0700513 //
514 // Since we're doing atomic loads of `flags`, the static analyzer assumes
515 // they can change between `delete this;` and `refs->decWeak(id);`. This is
516 // not the case. The analyzer may become more okay with this patten when
517 // https://bugs.llvm.org/show_bug.cgi?id=34365 gets resolved. NOLINTNEXTLINE
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800518 refs->decWeak(id);
519}
520
521void RefBase::forceIncStrong(const void* id) const
522{
Hans Boehme263e6c2016-05-11 18:15:12 -0700523 // Allows initial mStrong of 0 in addition to INITIAL_STRONG_VALUE.
524 // TODO: Better document assumptions.
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800525 weakref_impl* const refs = mRefs;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800526 refs->incWeak(id);
Christopher Ferris0e691602020-10-14 14:13:58 -0700527
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800528 refs->addStrongRef(id);
Hans Boehme263e6c2016-05-11 18:15:12 -0700529 const int32_t c = refs->mStrong.fetch_add(1, std::memory_order_relaxed);
Steve Blockae074452012-01-09 18:35:44 +0000530 ALOG_ASSERT(c >= 0, "forceIncStrong called on %p after ref count underflow",
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800531 refs);
532#if PRINT_REFS
Steve Blockeb095332011-12-20 16:23:08 +0000533 ALOGD("forceIncStrong of %p from %p: cnt=%d\n", this, id, c);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800534#endif
535
536 switch (c) {
537 case INITIAL_STRONG_VALUE:
Hans Boehme263e6c2016-05-11 18:15:12 -0700538 refs->mStrong.fetch_sub(INITIAL_STRONG_VALUE,
539 std::memory_order_relaxed);
Chih-Hung Hsieh502f4862018-09-13 11:08:41 -0700540 FALLTHROUGH_INTENDED;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800541 case 0:
Mathias Agopianad099652011-08-10 21:07:02 -0700542 refs->mBase->onFirstRef();
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800543 }
544}
545
546int32_t RefBase::getStrongCount() const
547{
Hans Boehme263e6c2016-05-11 18:15:12 -0700548 // Debugging only; No memory ordering guarantees.
549 return mRefs->mStrong.load(std::memory_order_relaxed);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800550}
551
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800552RefBase* RefBase::weakref_type::refBase() const
553{
554 return static_cast<const weakref_impl*>(this)->mBase;
555}
556
557void RefBase::weakref_type::incWeak(const void* id)
558{
559 weakref_impl* const impl = static_cast<weakref_impl*>(this);
560 impl->addWeakRef(id);
Hans Boehme263e6c2016-05-11 18:15:12 -0700561 const int32_t c __unused = impl->mWeak.fetch_add(1,
562 std::memory_order_relaxed);
Steve Blockae074452012-01-09 18:35:44 +0000563 ALOG_ASSERT(c >= 0, "incWeak called on %p after last weak ref", this);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800564}
565
Steven Morelandda75cef2021-03-31 16:05:04 +0000566void RefBase::weakref_type::incWeakRequireWeak(const void* id)
567{
568 weakref_impl* const impl = static_cast<weakref_impl*>(this);
569 impl->addWeakRef(id);
570 const int32_t c __unused = impl->mWeak.fetch_add(1,
571 std::memory_order_relaxed);
572 LOG_ALWAYS_FATAL_IF(c <= 0, "incWeakRequireWeak called on %p which has no weak refs", this);
573}
Mathias Agopianad099652011-08-10 21:07:02 -0700574
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800575void RefBase::weakref_type::decWeak(const void* id)
576{
577 weakref_impl* const impl = static_cast<weakref_impl*>(this);
578 impl->removeWeakRef(id);
Hans Boehme263e6c2016-05-11 18:15:12 -0700579 const int32_t c = impl->mWeak.fetch_sub(1, std::memory_order_release);
Hans Boehm23c857e2016-08-02 18:39:30 -0700580 LOG_ALWAYS_FATAL_IF(BAD_WEAK(c), "decWeak called on %p too many times",
581 this);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800582 if (c != 1) return;
Hans Boehme263e6c2016-05-11 18:15:12 -0700583 atomic_thread_fence(std::memory_order_acquire);
Mathias Agopianad099652011-08-10 21:07:02 -0700584
Hans Boehme263e6c2016-05-11 18:15:12 -0700585 int32_t flags = impl->mFlags.load(std::memory_order_relaxed);
586 if ((flags&OBJECT_LIFETIME_MASK) == OBJECT_LIFETIME_STRONG) {
Mathias Agopianad099652011-08-10 21:07:02 -0700587 // This is the regular lifetime case. The object is destroyed
588 // when the last strong reference goes away. Since weakref_impl
Hans Boehm23c857e2016-08-02 18:39:30 -0700589 // outlives the object, it is not destroyed in the dtor, and
Mathias Agopianad099652011-08-10 21:07:02 -0700590 // we'll have to do it here.
Hans Boehme263e6c2016-05-11 18:15:12 -0700591 if (impl->mStrong.load(std::memory_order_relaxed)
592 == INITIAL_STRONG_VALUE) {
Hans Boehm23c857e2016-08-02 18:39:30 -0700593 // Decrementing a weak count to zero when object never had a strong
594 // reference. We assume it acquired a weak reference early, e.g.
595 // in the constructor, and will eventually be properly destroyed,
596 // usually via incrementing and decrementing the strong count.
597 // Thus we no longer do anything here. We log this case, since it
598 // seems to be extremely rare, and should not normally occur. We
599 // used to deallocate mBase here, so this may now indicate a leak.
600 ALOGW("RefBase: Object at %p lost last weak reference "
601 "before it had a strong reference", impl->mBase);
Mathias Agopianad099652011-08-10 21:07:02 -0700602 } else {
Steve Blockb37fbe92011-10-20 11:56:00 +0100603 // ALOGV("Freeing refs %p of old RefBase %p\n", this, impl->mBase);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800604 delete impl;
605 }
606 } else {
Hans Boehme263e6c2016-05-11 18:15:12 -0700607 // This is the OBJECT_LIFETIME_WEAK case. The last weak-reference
608 // is gone, we can destroy the object.
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800609 impl->mBase->onLastWeakRef(id);
Hans Boehme263e6c2016-05-11 18:15:12 -0700610 delete impl->mBase;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800611 }
612}
613
614bool RefBase::weakref_type::attemptIncStrong(const void* id)
615{
616 incWeak(id);
Christopher Ferris0e691602020-10-14 14:13:58 -0700617
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800618 weakref_impl* const impl = static_cast<weakref_impl*>(this);
Hans Boehme263e6c2016-05-11 18:15:12 -0700619 int32_t curCount = impl->mStrong.load(std::memory_order_relaxed);
Dianne Hackborna729ab12013-03-14 15:26:30 -0700620
621 ALOG_ASSERT(curCount >= 0,
622 "attemptIncStrong called on %p after underflow", this);
623
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800624 while (curCount > 0 && curCount != INITIAL_STRONG_VALUE) {
Dianne Hackborna729ab12013-03-14 15:26:30 -0700625 // we're in the easy/common case of promoting a weak-reference
626 // from an existing strong reference.
Hans Boehme263e6c2016-05-11 18:15:12 -0700627 if (impl->mStrong.compare_exchange_weak(curCount, curCount+1,
628 std::memory_order_relaxed)) {
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800629 break;
630 }
Dianne Hackborna729ab12013-03-14 15:26:30 -0700631 // the strong count has changed on us, we need to re-assert our
Hans Boehme263e6c2016-05-11 18:15:12 -0700632 // situation. curCount was updated by compare_exchange_weak.
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800633 }
Christopher Ferris0e691602020-10-14 14:13:58 -0700634
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800635 if (curCount <= 0 || curCount == INITIAL_STRONG_VALUE) {
Dianne Hackborna729ab12013-03-14 15:26:30 -0700636 // we're now in the harder case of either:
637 // - there never was a strong reference on us
638 // - or, all strong references have been released
Hans Boehme263e6c2016-05-11 18:15:12 -0700639 int32_t flags = impl->mFlags.load(std::memory_order_relaxed);
640 if ((flags&OBJECT_LIFETIME_MASK) == OBJECT_LIFETIME_STRONG) {
Dianne Hackborna729ab12013-03-14 15:26:30 -0700641 // this object has a "normal" life-time, i.e.: it gets destroyed
642 // when the last strong reference goes away
643 if (curCount <= 0) {
644 // the last strong-reference got released, the object cannot
645 // be revived.
646 decWeak(id);
647 return false;
648 }
649
650 // here, curCount == INITIAL_STRONG_VALUE, which means
651 // there never was a strong-reference, so we can try to
652 // promote this object; we need to do that atomically.
653 while (curCount > 0) {
Hans Boehme263e6c2016-05-11 18:15:12 -0700654 if (impl->mStrong.compare_exchange_weak(curCount, curCount+1,
655 std::memory_order_relaxed)) {
Dianne Hackborna729ab12013-03-14 15:26:30 -0700656 break;
657 }
658 // the strong count has changed on us, we need to re-assert our
659 // situation (e.g.: another thread has inc/decStrong'ed us)
Hans Boehme263e6c2016-05-11 18:15:12 -0700660 // curCount has been updated.
Dianne Hackborna729ab12013-03-14 15:26:30 -0700661 }
662
663 if (curCount <= 0) {
664 // promote() failed, some other thread destroyed us in the
665 // meantime (i.e.: strong count reached zero).
666 decWeak(id);
667 return false;
668 }
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800669 } else {
Dianne Hackborna729ab12013-03-14 15:26:30 -0700670 // this object has an "extended" life-time, i.e.: it can be
671 // revived from a weak-reference only.
672 // Ask the object's implementation if it agrees to be revived
673 if (!impl->mBase->onIncStrongAttempted(FIRST_INC_STRONG, id)) {
674 // it didn't so give-up.
675 decWeak(id);
676 return false;
677 }
678 // grab a strong-reference, which is always safe due to the
679 // extended life-time.
Hans Boehme263e6c2016-05-11 18:15:12 -0700680 curCount = impl->mStrong.fetch_add(1, std::memory_order_relaxed);
Hans Boehm7f27cbc2016-07-29 14:39:10 -0700681 // If the strong reference count has already been incremented by
682 // someone else, the implementor of onIncStrongAttempted() is holding
683 // an unneeded reference. So call onLastStrongRef() here to remove it.
684 // (No, this is not pretty.) Note that we MUST NOT do this if we
685 // are in fact acquiring the first reference.
686 if (curCount != 0 && curCount != INITIAL_STRONG_VALUE) {
687 impl->mBase->onLastStrongRef(id);
688 }
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800689 }
690 }
Christopher Ferris0e691602020-10-14 14:13:58 -0700691
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800692 impl->addStrongRef(id);
693
694#if PRINT_REFS
Steve Blockeb095332011-12-20 16:23:08 +0000695 ALOGD("attemptIncStrong of %p from %p: cnt=%d\n", this, id, curCount);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800696#endif
697
Hans Boehm7f27cbc2016-07-29 14:39:10 -0700698 // curCount is the value of mStrong before we incremented it.
Hans Boehme263e6c2016-05-11 18:15:12 -0700699 // Now we need to fix-up the count if it was INITIAL_STRONG_VALUE.
700 // This must be done safely, i.e.: handle the case where several threads
Dianne Hackborna729ab12013-03-14 15:26:30 -0700701 // were here in attemptIncStrong().
Hans Boehme263e6c2016-05-11 18:15:12 -0700702 // curCount > INITIAL_STRONG_VALUE is OK, and can happen if we're doing
703 // this in the middle of another incStrong. The subtraction is handled
704 // by the thread that started with INITIAL_STRONG_VALUE.
705 if (curCount == INITIAL_STRONG_VALUE) {
706 impl->mStrong.fetch_sub(INITIAL_STRONG_VALUE,
707 std::memory_order_relaxed);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800708 }
Dianne Hackborna729ab12013-03-14 15:26:30 -0700709
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800710 return true;
711}
712
713bool RefBase::weakref_type::attemptIncWeak(const void* id)
714{
715 weakref_impl* const impl = static_cast<weakref_impl*>(this);
Mathias Agopianad099652011-08-10 21:07:02 -0700716
Hans Boehme263e6c2016-05-11 18:15:12 -0700717 int32_t curCount = impl->mWeak.load(std::memory_order_relaxed);
Steve Blockae074452012-01-09 18:35:44 +0000718 ALOG_ASSERT(curCount >= 0, "attemptIncWeak called on %p after underflow",
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800719 this);
720 while (curCount > 0) {
Hans Boehme263e6c2016-05-11 18:15:12 -0700721 if (impl->mWeak.compare_exchange_weak(curCount, curCount+1,
722 std::memory_order_relaxed)) {
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800723 break;
724 }
Hans Boehme263e6c2016-05-11 18:15:12 -0700725 // curCount has been updated.
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800726 }
727
728 if (curCount > 0) {
729 impl->addWeakRef(id);
730 }
731
732 return curCount > 0;
733}
734
735int32_t RefBase::weakref_type::getWeakCount() const
736{
Hans Boehme263e6c2016-05-11 18:15:12 -0700737 // Debug only!
738 return static_cast<const weakref_impl*>(this)->mWeak
739 .load(std::memory_order_relaxed);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800740}
741
742void RefBase::weakref_type::printRefs() const
743{
744 static_cast<const weakref_impl*>(this)->printRefs();
745}
746
747void RefBase::weakref_type::trackMe(bool enable, bool retain)
748{
Mathias Agopianad099652011-08-10 21:07:02 -0700749 static_cast<weakref_impl*>(this)->trackMe(enable, retain);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800750}
751
752RefBase::weakref_type* RefBase::createWeak(const void* id) const
753{
754 mRefs->incWeak(id);
755 return mRefs;
756}
757
758RefBase::weakref_type* RefBase::getWeakRefs() const
759{
760 return mRefs;
761}
762
763RefBase::RefBase()
764 : mRefs(new weakref_impl(this))
765{
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800766}
767
768RefBase::~RefBase()
769{
Hans Boehm23c857e2016-08-02 18:39:30 -0700770 int32_t flags = mRefs->mFlags.load(std::memory_order_relaxed);
771 // Life-time of this object is extended to WEAK, in
772 // which case weakref_impl doesn't out-live the object and we
773 // can free it now.
774 if ((flags & OBJECT_LIFETIME_MASK) == OBJECT_LIFETIME_WEAK) {
775 // It's possible that the weak count is not 0 if the object
776 // re-acquired a weak reference in its destructor
777 if (mRefs->mWeak.load(std::memory_order_relaxed) == 0) {
778 delete mRefs;
779 }
Steven Moreland483a2de2022-07-26 17:57:39 +0000780 } else {
781 int32_t strongs = mRefs->mStrong.load(std::memory_order_relaxed);
Steven Morelandf164de82022-03-11 02:28:57 +0000782
Steven Moreland483a2de2022-07-26 17:57:39 +0000783 if (strongs == INITIAL_STRONG_VALUE) {
784 // We never acquired a strong reference on this object.
785
786 // It would be nice to make this fatal, but many places use RefBase on the stack.
787 // However, this is dangerous because it's also common for code to use the
788 // sp<T>(T*) constructor, assuming that if the object is around, it is already
789 // owned by an sp<>.
790 ALOGW("RefBase: Explicit destruction, weak count = %d (in %p). Use sp<> to manage this "
791 "object.",
792 mRefs->mWeak.load(), this);
Christopher Ferris0e691602020-10-14 14:13:58 -0700793
794#if CALLSTACK_ENABLED
Steven Moreland483a2de2022-07-26 17:57:39 +0000795 CallStack::logStack(LOG_TAG);
Christopher Ferris0e691602020-10-14 14:13:58 -0700796#endif
Steven Moreland483a2de2022-07-26 17:57:39 +0000797 } else if (strongs != 0) {
798 LOG_ALWAYS_FATAL("RefBase: object %p with strong count %d deleted. Double owned?", this,
799 strongs);
800 }
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800801 }
Hans Boehm23c857e2016-08-02 18:39:30 -0700802 // For debugging purposes, clear mRefs. Ineffective against outstanding wp's.
Yi Konge1731a42018-07-16 18:11:34 -0700803 const_cast<weakref_impl*&>(mRefs) = nullptr;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800804}
805
806void RefBase::extendObjectLifetime(int32_t mode)
807{
Steven Morelandd086fe52022-07-26 22:11:13 +0000808 check_not_on_stack(this);
809
Hans Boehme263e6c2016-05-11 18:15:12 -0700810 // Must be happens-before ordered with respect to construction or any
811 // operation that could destroy the object.
812 mRefs->mFlags.fetch_or(mode, std::memory_order_relaxed);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800813}
814
815void RefBase::onFirstRef()
816{
817}
818
819void RefBase::onLastStrongRef(const void* /*id*/)
820{
821}
822
Mark Salyzyn5bed8032014-04-30 11:10:46 -0700823bool RefBase::onIncStrongAttempted(uint32_t flags, const void* /*id*/)
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800824{
825 return (flags&FIRST_INC_STRONG) ? true : false;
826}
827
828void RefBase::onLastWeakRef(const void* /*id*/)
829{
830}
Mathias Agopianad099652011-08-10 21:07:02 -0700831
832// ---------------------------------------------------------------------------
833
Mathias Agopianad099652011-08-10 21:07:02 -0700834#if DEBUG_REFS
Mark Salyzyn5bed8032014-04-30 11:10:46 -0700835void RefBase::renameRefs(size_t n, const ReferenceRenamer& renamer) {
Mathias Agopianad099652011-08-10 21:07:02 -0700836 for (size_t i=0 ; i<n ; i++) {
Mathias Agopian6cd548c2013-03-18 22:27:41 -0700837 renamer(i);
Mathias Agopianad099652011-08-10 21:07:02 -0700838 }
Mathias Agopianad099652011-08-10 21:07:02 -0700839}
Mark Salyzyn5bed8032014-04-30 11:10:46 -0700840#else
841void RefBase::renameRefs(size_t /*n*/, const ReferenceRenamer& /*renamer*/) { }
842#endif
Mathias Agopianad099652011-08-10 21:07:02 -0700843
Mathias Agopian6cd548c2013-03-18 22:27:41 -0700844void RefBase::renameRefId(weakref_type* ref,
845 const void* old_id, const void* new_id) {
846 weakref_impl* const impl = static_cast<weakref_impl*>(ref);
847 impl->renameStrongRefId(old_id, new_id);
848 impl->renameWeakRefId(old_id, new_id);
849}
850
851void RefBase::renameRefId(RefBase* ref,
852 const void* old_id, const void* new_id) {
853 ref->mRefs->renameStrongRefId(old_id, new_id);
854 ref->mRefs->renameWeakRefId(old_id, new_id);
855}
856
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800857}; // namespace android