blob: b5b3efd1abf6006f210a74f0a4548bf657e54b8f [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
Hans Boehm2a019ec2018-08-07 23:45:25 +000052#define DEBUG_REFS_CALLSTACK_PATH "/data/debug"
Mathias Agopian6d4419d2013-03-18 20:31:18 -070053
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080054// log all reference counting operations
Hans Boehm2a019ec2018-08-07 23:45:25 +000055#define PRINT_REFS 0
56
Andrei Homescu5c15de22021-12-10 05:32:17 +000057#if defined(__linux__)
Christopher Ferris0e691602020-10-14 14:13:58 -070058// CallStack is only supported on linux type platforms.
59#define CALLSTACK_ENABLED 1
60#else
61#define CALLSTACK_ENABLED 0
62#endif
63
64#if CALLSTACK_ENABLED
65#include <utils/CallStack.h>
66#endif
67
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080068// ---------------------------------------------------------------------------
69
70namespace android {
71
Hans Boehm9ba71922016-07-21 18:56:55 -070072// Observations, invariants, etc:
Hans Boehme263e6c2016-05-11 18:15:12 -070073
Hans Boehm9ba71922016-07-21 18:56:55 -070074// By default, obects are destroyed when the last strong reference disappears
75// or, if the object never had a strong reference, when the last weak reference
76// disappears.
77//
Hans Boehme263e6c2016-05-11 18:15:12 -070078// OBJECT_LIFETIME_WEAK changes this behavior to retain the object
79// unconditionally until the last reference of either kind disappears. The
80// client ensures that the extendObjectLifetime call happens before the dec
81// call that would otherwise have deallocated the object, or before an
82// attemptIncStrong call that might rely on it. We do not worry about
83// concurrent changes to the object lifetime.
Hans Boehm9ba71922016-07-21 18:56:55 -070084//
85// AttemptIncStrong will succeed if the object has a strong reference, or if it
86// has a weak reference and has never had a strong reference.
87// AttemptIncWeak really does succeed only if there is already a WEAK
88// reference, and thus may fail when attemptIncStrong would succeed.
89//
Hans Boehme263e6c2016-05-11 18:15:12 -070090// mStrong is the strong reference count. mWeak is the weak reference count.
91// Between calls, and ignoring memory ordering effects, mWeak includes strong
92// references, and is thus >= mStrong.
93//
Hans Boehm9ba71922016-07-21 18:56:55 -070094// A weakref_impl holds all the information, including both reference counts,
95// required to perform wp<> operations. Thus these can continue to be performed
96// after the RefBase object has been destroyed.
97//
Hans Boehme263e6c2016-05-11 18:15:12 -070098// A weakref_impl is allocated as the value of mRefs in a RefBase object on
99// construction.
Hans Boehm23c857e2016-08-02 18:39:30 -0700100// In the OBJECT_LIFETIME_STRONG case, it is normally deallocated in decWeak,
101// and hence lives as long as the last weak reference. (It can also be
102// deallocated in the RefBase destructor iff the strong reference count was
103// never incremented and the weak count is zero, e.g. if the RefBase object is
104// explicitly destroyed without decrementing the strong count. This should be
105// avoided.) In this case, the RefBase destructor should be invoked from
106// decStrong.
107// In the OBJECT_LIFETIME_WEAK case, the weakref_impl is always deallocated in
108// the RefBase destructor, which is always invoked by decWeak. DecStrong
109// explicitly avoids the deletion in this case.
Hans Boehme263e6c2016-05-11 18:15:12 -0700110//
111// Memory ordering:
112// The client must ensure that every inc() call, together with all other
113// accesses to the object, happens before the corresponding dec() call.
114//
115// We try to keep memory ordering constraints on atomics as weak as possible,
116// since memory fences or ordered memory accesses are likely to be a major
117// performance cost for this code. All accesses to mStrong, mWeak, and mFlags
118// explicitly relax memory ordering in some way.
119//
120// The only operations that are not memory_order_relaxed are reference count
121// decrements. All reference count decrements are release operations. In
122// addition, the final decrement leading the deallocation is followed by an
123// acquire fence, which we can view informally as also turning it into an
124// acquire operation. (See 29.8p4 [atomics.fences] for details. We could
125// alternatively use acq_rel operations for all decrements. This is probably
126// slower on most current (2016) hardware, especially on ARMv7, but that may
127// not be true indefinitely.)
128//
129// This convention ensures that the second-to-last decrement synchronizes with
130// (in the language of 1.10 in the C++ standard) the final decrement of a
131// reference count. Since reference counts are only updated using atomic
132// read-modify-write operations, this also extends to any earlier decrements.
133// (See "release sequence" in 1.10.)
134//
135// Since all operations on an object happen before the corresponding reference
136// count decrement, and all reference count decrements happen before the final
137// one, we are guaranteed that all other object accesses happen before the
138// object is destroyed.
139
140
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800141#define INITIAL_STRONG_VALUE (1<<28)
142
Hans Boehm23c857e2016-08-02 18:39:30 -0700143#define MAX_COUNT 0xfffff
144
145// Test whether the argument is a clearly invalid strong reference count.
146// Used only for error checking on the value before an atomic decrement.
147// Intended to be very cheap.
148// Note that we cannot just check for excess decrements by comparing to zero
149// since the object would be deallocated before that.
150#define BAD_STRONG(c) \
151 ((c) == 0 || ((c) & (~(MAX_COUNT | INITIAL_STRONG_VALUE))) != 0)
152
153// Same for weak counts.
154#define BAD_WEAK(c) ((c) == 0 || ((c) & (~MAX_COUNT)) != 0)
155
Steven Morelandcd4ef872022-07-29 00:40:29 +0000156// name kept because prebuilts used to use it from inlining sp<> code
157void sp_report_stack_pointer() { LOG_ALWAYS_FATAL("RefBase used with stack pointer argument"); }
Steven Morelandc340a082022-07-26 17:09:50 +0000158
159// Check whether address is definitely on the calling stack. We actually check whether it is on
160// the same 4K page as the frame pointer.
161//
162// Assumptions:
163// - Pages are never smaller than 4K (MIN_PAGE_SIZE)
164// - Malloced memory never shares a page with a stack.
165//
166// It does not appear safe to broaden this check to include adjacent pages; apparently this code
167// is used in environments where there may not be a guard page below (at higher addresses than)
168// the bottom of the stack.
169static void check_not_on_stack(const void* ptr) {
170 static constexpr int MIN_PAGE_SIZE = 0x1000; // 4K. Safer than including sys/user.h.
171 static constexpr uintptr_t MIN_PAGE_MASK = ~static_cast<uintptr_t>(MIN_PAGE_SIZE - 1);
172 uintptr_t my_frame_address =
173 reinterpret_cast<uintptr_t>(__builtin_frame_address(0 /* this frame */));
174 if (((reinterpret_cast<uintptr_t>(ptr) ^ my_frame_address) & MIN_PAGE_MASK) == 0) {
175 sp_report_stack_pointer();
176 }
177}
178
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800179// ---------------------------------------------------------------------------
180
181class RefBase::weakref_impl : public RefBase::weakref_type
182{
183public:
Hans Boehme263e6c2016-05-11 18:15:12 -0700184 std::atomic<int32_t> mStrong;
185 std::atomic<int32_t> mWeak;
186 RefBase* const mBase;
187 std::atomic<int32_t> mFlags;
Mathias Agopian9c8fa9e2011-06-15 20:42:47 -0700188
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800189#if !DEBUG_REFS
190
Chih-Hung Hsieh1c563d92016-04-29 15:44:04 -0700191 explicit weakref_impl(RefBase* base)
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800192 : mStrong(INITIAL_STRONG_VALUE)
193 , mWeak(0)
194 , mBase(base)
陈冠有0cbef722021-08-22 16:36:28 +0000195 , mFlags(OBJECT_LIFETIME_STRONG)
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800196 {
197 }
198
199 void addStrongRef(const void* /*id*/) { }
200 void removeStrongRef(const void* /*id*/) { }
Mathias Agopianad099652011-08-10 21:07:02 -0700201 void renameStrongRefId(const void* /*old_id*/, const void* /*new_id*/) { }
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800202 void addWeakRef(const void* /*id*/) { }
203 void removeWeakRef(const void* /*id*/) { }
Mathias Agopianad099652011-08-10 21:07:02 -0700204 void renameWeakRefId(const void* /*old_id*/, const void* /*new_id*/) { }
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800205 void printRefs() const { }
206 void trackMe(bool, bool) { }
207
208#else
209
210 weakref_impl(RefBase* base)
211 : mStrong(INITIAL_STRONG_VALUE)
212 , mWeak(0)
213 , mBase(base)
陈冠有0cbef722021-08-22 16:36:28 +0000214 , mFlags(OBJECT_LIFETIME_STRONG)
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800215 , mStrongRefs(NULL)
216 , mWeakRefs(NULL)
217 , mTrackEnabled(!!DEBUG_REFS_ENABLED_BY_DEFAULT)
218 , mRetain(false)
219 {
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800220 }
Christopher Ferris0e691602020-10-14 14:13:58 -0700221
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800222 ~weakref_impl()
223 {
Mathias Agopianad099652011-08-10 21:07:02 -0700224 bool dumpStack = false;
225 if (!mRetain && mStrongRefs != NULL) {
226 dumpStack = true;
Steve Block1b781ab2012-01-06 19:20:56 +0000227 ALOGE("Strong references remain:");
Mathias Agopianad099652011-08-10 21:07:02 -0700228 ref_entry* refs = mStrongRefs;
229 while (refs) {
230 char inc = refs->ref >= 0 ? '+' : '-';
Steve Blockeb095332011-12-20 16:23:08 +0000231 ALOGD("\t%c ID %p (ref %d):", inc, refs->id, refs->ref);
Christopher Ferris0e691602020-10-14 14:13:58 -0700232#if DEBUG_REFS_CALLSTACK_ENABLED && CALLSTACK_ENABLED
Hans Boehm2a019ec2018-08-07 23:45:25 +0000233 CallStack::logStack(LOG_TAG, refs->stack.get());
Mathias Agopianad099652011-08-10 21:07:02 -0700234#endif
235 refs = refs->next;
236 }
237 }
238
239 if (!mRetain && mWeakRefs != NULL) {
240 dumpStack = true;
Steve Block1b781ab2012-01-06 19:20:56 +0000241 ALOGE("Weak references remain!");
Mathias Agopianad099652011-08-10 21:07:02 -0700242 ref_entry* refs = mWeakRefs;
243 while (refs) {
244 char inc = refs->ref >= 0 ? '+' : '-';
Steve Blockeb095332011-12-20 16:23:08 +0000245 ALOGD("\t%c ID %p (ref %d):", inc, refs->id, refs->ref);
Christopher Ferris0e691602020-10-14 14:13:58 -0700246#if DEBUG_REFS_CALLSTACK_ENABLED && CALLSTACK_ENABLED
Hans Boehm2a019ec2018-08-07 23:45:25 +0000247 CallStack::logStack(LOG_TAG, refs->stack.get());
Mathias Agopianad099652011-08-10 21:07:02 -0700248#endif
249 refs = refs->next;
250 }
251 }
252 if (dumpStack) {
Steve Block1b781ab2012-01-06 19:20:56 +0000253 ALOGE("above errors at:");
Christopher Ferris0e691602020-10-14 14:13:58 -0700254#if CALLSTACK_ENABLED
Hans Boehm2a019ec2018-08-07 23:45:25 +0000255 CallStack::logStack(LOG_TAG);
Christopher Ferris0e691602020-10-14 14:13:58 -0700256#endif
Mathias Agopianad099652011-08-10 21:07:02 -0700257 }
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800258 }
259
Mathias Agopianad099652011-08-10 21:07:02 -0700260 void addStrongRef(const void* id) {
Steve Blockeb095332011-12-20 16:23:08 +0000261 //ALOGD_IF(mTrackEnabled,
Mathias Agopianad099652011-08-10 21:07:02 -0700262 // "addStrongRef: RefBase=%p, id=%p", mBase, id);
Hans Boehme263e6c2016-05-11 18:15:12 -0700263 addRef(&mStrongRefs, id, mStrong.load(std::memory_order_relaxed));
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800264 }
265
Mathias Agopianad099652011-08-10 21:07:02 -0700266 void removeStrongRef(const void* id) {
Steve Blockeb095332011-12-20 16:23:08 +0000267 //ALOGD_IF(mTrackEnabled,
Mathias Agopianad099652011-08-10 21:07:02 -0700268 // "removeStrongRef: RefBase=%p, id=%p", mBase, id);
269 if (!mRetain) {
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800270 removeRef(&mStrongRefs, id);
Mathias Agopianad099652011-08-10 21:07:02 -0700271 } else {
Hans Boehme263e6c2016-05-11 18:15:12 -0700272 addRef(&mStrongRefs, id, -mStrong.load(std::memory_order_relaxed));
Mathias Agopianad099652011-08-10 21:07:02 -0700273 }
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800274 }
275
Mathias Agopianad099652011-08-10 21:07:02 -0700276 void renameStrongRefId(const void* old_id, const void* new_id) {
Steve Blockeb095332011-12-20 16:23:08 +0000277 //ALOGD_IF(mTrackEnabled,
Mathias Agopianad099652011-08-10 21:07:02 -0700278 // "renameStrongRefId: RefBase=%p, oid=%p, nid=%p",
279 // mBase, old_id, new_id);
280 renameRefsId(mStrongRefs, old_id, new_id);
281 }
282
283 void addWeakRef(const void* id) {
Hans Boehme263e6c2016-05-11 18:15:12 -0700284 addRef(&mWeakRefs, id, mWeak.load(std::memory_order_relaxed));
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800285 }
286
Mathias Agopianad099652011-08-10 21:07:02 -0700287 void removeWeakRef(const void* id) {
288 if (!mRetain) {
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800289 removeRef(&mWeakRefs, id);
Mathias Agopianad099652011-08-10 21:07:02 -0700290 } else {
Hans Boehme263e6c2016-05-11 18:15:12 -0700291 addRef(&mWeakRefs, id, -mWeak.load(std::memory_order_relaxed));
Mathias Agopianad099652011-08-10 21:07:02 -0700292 }
293 }
294
295 void renameWeakRefId(const void* old_id, const void* new_id) {
296 renameRefsId(mWeakRefs, old_id, new_id);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800297 }
298
Christopher Ferris0e691602020-10-14 14:13:58 -0700299 void trackMe(bool track, bool retain) {
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800300 mTrackEnabled = track;
301 mRetain = retain;
302 }
303
304 void printRefs() const
305 {
306 String8 text;
307
308 {
Mathias Agopianad099652011-08-10 21:07:02 -0700309 Mutex::Autolock _l(mMutex);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800310 char buf[128];
George Burgess IVe7aa2b22016-03-02 14:02:55 -0800311 snprintf(buf, sizeof(buf),
312 "Strong references on RefBase %p (weakref_type %p):\n",
313 mBase, this);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800314 text.append(buf);
315 printRefsLocked(&text, mStrongRefs);
George Burgess IVe7aa2b22016-03-02 14:02:55 -0800316 snprintf(buf, sizeof(buf),
317 "Weak references on RefBase %p (weakref_type %p):\n",
318 mBase, this);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800319 text.append(buf);
320 printRefsLocked(&text, mWeakRefs);
321 }
322
323 {
324 char name[100];
George Burgess IVe7aa2b22016-03-02 14:02:55 -0800325 snprintf(name, sizeof(name), DEBUG_REFS_CALLSTACK_PATH "/%p.stack",
326 this);
Mathias Agopian769828d2013-03-06 17:51:15 -0800327 int rc = open(name, O_RDWR | O_CREAT | O_APPEND, 644);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800328 if (rc >= 0) {
Hans Boehm2a019ec2018-08-07 23:45:25 +0000329 (void)write(rc, text.string(), text.length());
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800330 close(rc);
Steve Blockeb095332011-12-20 16:23:08 +0000331 ALOGD("STACK TRACE for %p saved in %s", this, name);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800332 }
Steve Block1b781ab2012-01-06 19:20:56 +0000333 else ALOGE("FAILED TO PRINT STACK TRACE for %p in %s: %s", this,
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800334 name, strerror(errno));
335 }
336 }
337
338private:
339 struct ref_entry
340 {
341 ref_entry* next;
342 const void* id;
Christopher Ferris0e691602020-10-14 14:13:58 -0700343#if DEBUG_REFS_CALLSTACK_ENABLED && CALLSTACK_ENABLED
Hans Boehm2a019ec2018-08-07 23:45:25 +0000344 CallStack::CallStackUPtr stack;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800345#endif
346 int32_t ref;
347 };
348
349 void addRef(ref_entry** refs, const void* id, int32_t mRef)
350 {
351 if (mTrackEnabled) {
352 AutoMutex _l(mMutex);
Mathias Agopianad099652011-08-10 21:07:02 -0700353
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800354 ref_entry* ref = new ref_entry;
355 // Reference count at the time of the snapshot, but before the
356 // update. Positive value means we increment, negative--we
357 // decrement the reference count.
358 ref->ref = mRef;
359 ref->id = id;
Christopher Ferris0e691602020-10-14 14:13:58 -0700360#if DEBUG_REFS_CALLSTACK_ENABLED && CALLSTACK_ENABLED
Hans Boehm2a019ec2018-08-07 23:45:25 +0000361 ref->stack = CallStack::getCurrent(2);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800362#endif
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800363 ref->next = *refs;
364 *refs = ref;
365 }
366 }
367
368 void removeRef(ref_entry** refs, const void* id)
369 {
370 if (mTrackEnabled) {
371 AutoMutex _l(mMutex);
Christopher Ferris0e691602020-10-14 14:13:58 -0700372
Mathias Agopianad099652011-08-10 21:07:02 -0700373 ref_entry* const head = *refs;
374 ref_entry* ref = head;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800375 while (ref != NULL) {
376 if (ref->id == id) {
377 *refs = ref->next;
378 delete ref;
379 return;
380 }
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800381 refs = &ref->next;
382 ref = *refs;
383 }
Mathias Agopianad099652011-08-10 21:07:02 -0700384
Steve Block1b781ab2012-01-06 19:20:56 +0000385 ALOGE("RefBase: removing id %p on RefBase %p"
Mathias Agopianad099652011-08-10 21:07:02 -0700386 "(weakref_type %p) that doesn't exist!",
387 id, mBase, this);
388
389 ref = head;
390 while (ref) {
391 char inc = ref->ref >= 0 ? '+' : '-';
Steve Blockeb095332011-12-20 16:23:08 +0000392 ALOGD("\t%c ID %p (ref %d):", inc, ref->id, ref->ref);
Mathias Agopianad099652011-08-10 21:07:02 -0700393 ref = ref->next;
394 }
395
Christopher Ferris0e691602020-10-14 14:13:58 -0700396#if CALLSTACK_ENABLED
Hans Boehm2a019ec2018-08-07 23:45:25 +0000397 CallStack::logStack(LOG_TAG);
Christopher Ferris0e691602020-10-14 14:13:58 -0700398#endif
Mathias Agopianad099652011-08-10 21:07:02 -0700399 }
400 }
401
402 void renameRefsId(ref_entry* r, const void* old_id, const void* new_id)
403 {
404 if (mTrackEnabled) {
405 AutoMutex _l(mMutex);
406 ref_entry* ref = r;
407 while (ref != NULL) {
408 if (ref->id == old_id) {
409 ref->id = new_id;
410 }
411 ref = ref->next;
412 }
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800413 }
414 }
415
416 void printRefsLocked(String8* out, const ref_entry* refs) const
417 {
418 char buf[128];
419 while (refs) {
420 char inc = refs->ref >= 0 ? '+' : '-';
George Burgess IVe7aa2b22016-03-02 14:02:55 -0800421 snprintf(buf, sizeof(buf), "\t%c ID %p (ref %d):\n",
422 inc, refs->id, refs->ref);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800423 out->append(buf);
Christopher Ferris0e691602020-10-14 14:13:58 -0700424#if DEBUG_REFS_CALLSTACK_ENABLED && CALLSTACK_ENABLED
Hans Boehm2a019ec2018-08-07 23:45:25 +0000425 out->append(CallStack::stackToString("\t\t", refs->stack.get()));
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800426#else
427 out->append("\t\t(call stacks disabled)");
428#endif
429 refs = refs->next;
430 }
431 }
432
Mathias Agopianad099652011-08-10 21:07:02 -0700433 mutable Mutex mMutex;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800434 ref_entry* mStrongRefs;
435 ref_entry* mWeakRefs;
436
437 bool mTrackEnabled;
438 // Collect stack traces on addref and removeref, instead of deleting the stack references
439 // on removeref that match the address ones.
440 bool mRetain;
441
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800442#endif
443};
444
445// ---------------------------------------------------------------------------
446
447void RefBase::incStrong(const void* id) const
448{
449 weakref_impl* const refs = mRefs;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800450 refs->incWeak(id);
Christopher Ferris0e691602020-10-14 14:13:58 -0700451
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800452 refs->addStrongRef(id);
Hans Boehme263e6c2016-05-11 18:15:12 -0700453 const int32_t c = refs->mStrong.fetch_add(1, std::memory_order_relaxed);
Steve Blockae074452012-01-09 18:35:44 +0000454 ALOG_ASSERT(c > 0, "incStrong() called on %p after last strong ref", refs);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800455#if PRINT_REFS
Steve Blockeb095332011-12-20 16:23:08 +0000456 ALOGD("incStrong of %p from %p: cnt=%d\n", this, id, c);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800457#endif
458 if (c != INITIAL_STRONG_VALUE) {
459 return;
460 }
461
Steven Morelandc340a082022-07-26 17:09:50 +0000462 check_not_on_stack(this);
463
Chih-Hung Hsieh122352d2017-10-02 15:20:07 -0700464 int32_t old __unused = refs->mStrong.fetch_sub(INITIAL_STRONG_VALUE, std::memory_order_relaxed);
Hans Boehme263e6c2016-05-11 18:15:12 -0700465 // A decStrong() must still happen after us.
466 ALOG_ASSERT(old > INITIAL_STRONG_VALUE, "0x%x too small", old);
Mathias Agopianad099652011-08-10 21:07:02 -0700467 refs->mBase->onFirstRef();
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800468}
469
Steven Morelandda75cef2021-03-31 16:05:04 +0000470void RefBase::incStrongRequireStrong(const void* id) const {
471 weakref_impl* const refs = mRefs;
472 refs->incWeak(id);
473
474 refs->addStrongRef(id);
475 const int32_t c = refs->mStrong.fetch_add(1, std::memory_order_relaxed);
476
477 LOG_ALWAYS_FATAL_IF(c <= 0 || c == INITIAL_STRONG_VALUE,
478 "incStrongRequireStrong() called on %p which isn't already owned", refs);
479#if PRINT_REFS
480 ALOGD("incStrong (requiring strong) of %p from %p: cnt=%d\n", this, id, c);
481#endif
482}
483
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800484void RefBase::decStrong(const void* id) const
485{
486 weakref_impl* const refs = mRefs;
487 refs->removeStrongRef(id);
Hans Boehme263e6c2016-05-11 18:15:12 -0700488 const int32_t c = refs->mStrong.fetch_sub(1, std::memory_order_release);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800489#if PRINT_REFS
Steve Blockeb095332011-12-20 16:23:08 +0000490 ALOGD("decStrong of %p from %p: cnt=%d\n", this, id, c);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800491#endif
Hans Boehm23c857e2016-08-02 18:39:30 -0700492 LOG_ALWAYS_FATAL_IF(BAD_STRONG(c), "decStrong() called on %p too many times",
493 refs);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800494 if (c == 1) {
Hans Boehme263e6c2016-05-11 18:15:12 -0700495 std::atomic_thread_fence(std::memory_order_acquire);
Mathias Agopianad099652011-08-10 21:07:02 -0700496 refs->mBase->onLastStrongRef(id);
Hans Boehme263e6c2016-05-11 18:15:12 -0700497 int32_t flags = refs->mFlags.load(std::memory_order_relaxed);
498 if ((flags&OBJECT_LIFETIME_MASK) == OBJECT_LIFETIME_STRONG) {
Mathias Agopian9c8fa9e2011-06-15 20:42:47 -0700499 delete this;
Hans Boehm23c857e2016-08-02 18:39:30 -0700500 // The destructor does not delete refs in this case.
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800501 }
502 }
Hans Boehme263e6c2016-05-11 18:15:12 -0700503 // Note that even with only strong reference operations, the thread
504 // deallocating this may not be the same as the thread deallocating refs.
505 // That's OK: all accesses to this happen before its deletion here,
506 // and all accesses to refs happen before its deletion in the final decWeak.
507 // The destructor can safely access mRefs because either it's deleting
508 // mRefs itself, or it's running entirely before the final mWeak decrement.
George Burgess IV6753bc42017-10-01 12:38:44 -0700509 //
510 // Since we're doing atomic loads of `flags`, the static analyzer assumes
511 // they can change between `delete this;` and `refs->decWeak(id);`. This is
512 // not the case. The analyzer may become more okay with this patten when
513 // https://bugs.llvm.org/show_bug.cgi?id=34365 gets resolved. NOLINTNEXTLINE
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800514 refs->decWeak(id);
515}
516
517void RefBase::forceIncStrong(const void* id) const
518{
Hans Boehme263e6c2016-05-11 18:15:12 -0700519 // Allows initial mStrong of 0 in addition to INITIAL_STRONG_VALUE.
520 // TODO: Better document assumptions.
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800521 weakref_impl* const refs = mRefs;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800522 refs->incWeak(id);
Christopher Ferris0e691602020-10-14 14:13:58 -0700523
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800524 refs->addStrongRef(id);
Hans Boehme263e6c2016-05-11 18:15:12 -0700525 const int32_t c = refs->mStrong.fetch_add(1, std::memory_order_relaxed);
Steve Blockae074452012-01-09 18:35:44 +0000526 ALOG_ASSERT(c >= 0, "forceIncStrong called on %p after ref count underflow",
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800527 refs);
528#if PRINT_REFS
Steve Blockeb095332011-12-20 16:23:08 +0000529 ALOGD("forceIncStrong of %p from %p: cnt=%d\n", this, id, c);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800530#endif
531
532 switch (c) {
533 case INITIAL_STRONG_VALUE:
Hans Boehme263e6c2016-05-11 18:15:12 -0700534 refs->mStrong.fetch_sub(INITIAL_STRONG_VALUE,
535 std::memory_order_relaxed);
Chih-Hung Hsieh502f4862018-09-13 11:08:41 -0700536 FALLTHROUGH_INTENDED;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800537 case 0:
Mathias Agopianad099652011-08-10 21:07:02 -0700538 refs->mBase->onFirstRef();
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800539 }
540}
541
542int32_t RefBase::getStrongCount() const
543{
Hans Boehme263e6c2016-05-11 18:15:12 -0700544 // Debugging only; No memory ordering guarantees.
545 return mRefs->mStrong.load(std::memory_order_relaxed);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800546}
547
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800548RefBase* RefBase::weakref_type::refBase() const
549{
550 return static_cast<const weakref_impl*>(this)->mBase;
551}
552
553void RefBase::weakref_type::incWeak(const void* id)
554{
555 weakref_impl* const impl = static_cast<weakref_impl*>(this);
556 impl->addWeakRef(id);
Hans Boehme263e6c2016-05-11 18:15:12 -0700557 const int32_t c __unused = impl->mWeak.fetch_add(1,
558 std::memory_order_relaxed);
Steve Blockae074452012-01-09 18:35:44 +0000559 ALOG_ASSERT(c >= 0, "incWeak called on %p after last weak ref", this);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800560}
561
Steven Morelandda75cef2021-03-31 16:05:04 +0000562void RefBase::weakref_type::incWeakRequireWeak(const void* id)
563{
564 weakref_impl* const impl = static_cast<weakref_impl*>(this);
565 impl->addWeakRef(id);
566 const int32_t c __unused = impl->mWeak.fetch_add(1,
567 std::memory_order_relaxed);
568 LOG_ALWAYS_FATAL_IF(c <= 0, "incWeakRequireWeak called on %p which has no weak refs", this);
569}
Mathias Agopianad099652011-08-10 21:07:02 -0700570
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800571void RefBase::weakref_type::decWeak(const void* id)
572{
573 weakref_impl* const impl = static_cast<weakref_impl*>(this);
574 impl->removeWeakRef(id);
Hans Boehme263e6c2016-05-11 18:15:12 -0700575 const int32_t c = impl->mWeak.fetch_sub(1, std::memory_order_release);
Hans Boehm23c857e2016-08-02 18:39:30 -0700576 LOG_ALWAYS_FATAL_IF(BAD_WEAK(c), "decWeak called on %p too many times",
577 this);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800578 if (c != 1) return;
Hans Boehme263e6c2016-05-11 18:15:12 -0700579 atomic_thread_fence(std::memory_order_acquire);
Mathias Agopianad099652011-08-10 21:07:02 -0700580
Hans Boehme263e6c2016-05-11 18:15:12 -0700581 int32_t flags = impl->mFlags.load(std::memory_order_relaxed);
582 if ((flags&OBJECT_LIFETIME_MASK) == OBJECT_LIFETIME_STRONG) {
Mathias Agopianad099652011-08-10 21:07:02 -0700583 // This is the regular lifetime case. The object is destroyed
584 // when the last strong reference goes away. Since weakref_impl
Hans Boehm23c857e2016-08-02 18:39:30 -0700585 // outlives the object, it is not destroyed in the dtor, and
Mathias Agopianad099652011-08-10 21:07:02 -0700586 // we'll have to do it here.
Hans Boehme263e6c2016-05-11 18:15:12 -0700587 if (impl->mStrong.load(std::memory_order_relaxed)
588 == INITIAL_STRONG_VALUE) {
Hans Boehm23c857e2016-08-02 18:39:30 -0700589 // Decrementing a weak count to zero when object never had a strong
590 // reference. We assume it acquired a weak reference early, e.g.
591 // in the constructor, and will eventually be properly destroyed,
592 // usually via incrementing and decrementing the strong count.
593 // Thus we no longer do anything here. We log this case, since it
594 // seems to be extremely rare, and should not normally occur. We
595 // used to deallocate mBase here, so this may now indicate a leak.
596 ALOGW("RefBase: Object at %p lost last weak reference "
597 "before it had a strong reference", impl->mBase);
Mathias Agopianad099652011-08-10 21:07:02 -0700598 } else {
Steve Blockb37fbe92011-10-20 11:56:00 +0100599 // ALOGV("Freeing refs %p of old RefBase %p\n", this, impl->mBase);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800600 delete impl;
601 }
602 } else {
Hans Boehme263e6c2016-05-11 18:15:12 -0700603 // This is the OBJECT_LIFETIME_WEAK case. The last weak-reference
604 // is gone, we can destroy the object.
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800605 impl->mBase->onLastWeakRef(id);
Hans Boehme263e6c2016-05-11 18:15:12 -0700606 delete impl->mBase;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800607 }
608}
609
610bool RefBase::weakref_type::attemptIncStrong(const void* id)
611{
612 incWeak(id);
Christopher Ferris0e691602020-10-14 14:13:58 -0700613
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800614 weakref_impl* const impl = static_cast<weakref_impl*>(this);
Hans Boehme263e6c2016-05-11 18:15:12 -0700615 int32_t curCount = impl->mStrong.load(std::memory_order_relaxed);
Dianne Hackborna729ab12013-03-14 15:26:30 -0700616
617 ALOG_ASSERT(curCount >= 0,
618 "attemptIncStrong called on %p after underflow", this);
619
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800620 while (curCount > 0 && curCount != INITIAL_STRONG_VALUE) {
Dianne Hackborna729ab12013-03-14 15:26:30 -0700621 // we're in the easy/common case of promoting a weak-reference
622 // from an existing strong reference.
Hans Boehme263e6c2016-05-11 18:15:12 -0700623 if (impl->mStrong.compare_exchange_weak(curCount, curCount+1,
624 std::memory_order_relaxed)) {
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800625 break;
626 }
Dianne Hackborna729ab12013-03-14 15:26:30 -0700627 // the strong count has changed on us, we need to re-assert our
Hans Boehme263e6c2016-05-11 18:15:12 -0700628 // situation. curCount was updated by compare_exchange_weak.
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800629 }
Christopher Ferris0e691602020-10-14 14:13:58 -0700630
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800631 if (curCount <= 0 || curCount == INITIAL_STRONG_VALUE) {
Dianne Hackborna729ab12013-03-14 15:26:30 -0700632 // we're now in the harder case of either:
633 // - there never was a strong reference on us
634 // - or, all strong references have been released
Hans Boehme263e6c2016-05-11 18:15:12 -0700635 int32_t flags = impl->mFlags.load(std::memory_order_relaxed);
636 if ((flags&OBJECT_LIFETIME_MASK) == OBJECT_LIFETIME_STRONG) {
Dianne Hackborna729ab12013-03-14 15:26:30 -0700637 // this object has a "normal" life-time, i.e.: it gets destroyed
638 // when the last strong reference goes away
639 if (curCount <= 0) {
640 // the last strong-reference got released, the object cannot
641 // be revived.
642 decWeak(id);
643 return false;
644 }
645
646 // here, curCount == INITIAL_STRONG_VALUE, which means
647 // there never was a strong-reference, so we can try to
648 // promote this object; we need to do that atomically.
649 while (curCount > 0) {
Hans Boehme263e6c2016-05-11 18:15:12 -0700650 if (impl->mStrong.compare_exchange_weak(curCount, curCount+1,
651 std::memory_order_relaxed)) {
Dianne Hackborna729ab12013-03-14 15:26:30 -0700652 break;
653 }
654 // the strong count has changed on us, we need to re-assert our
655 // situation (e.g.: another thread has inc/decStrong'ed us)
Hans Boehme263e6c2016-05-11 18:15:12 -0700656 // curCount has been updated.
Dianne Hackborna729ab12013-03-14 15:26:30 -0700657 }
658
659 if (curCount <= 0) {
660 // promote() failed, some other thread destroyed us in the
661 // meantime (i.e.: strong count reached zero).
662 decWeak(id);
663 return false;
664 }
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800665 } else {
Dianne Hackborna729ab12013-03-14 15:26:30 -0700666 // this object has an "extended" life-time, i.e.: it can be
667 // revived from a weak-reference only.
668 // Ask the object's implementation if it agrees to be revived
669 if (!impl->mBase->onIncStrongAttempted(FIRST_INC_STRONG, id)) {
670 // it didn't so give-up.
671 decWeak(id);
672 return false;
673 }
674 // grab a strong-reference, which is always safe due to the
675 // extended life-time.
Hans Boehme263e6c2016-05-11 18:15:12 -0700676 curCount = impl->mStrong.fetch_add(1, std::memory_order_relaxed);
Hans Boehm7f27cbc2016-07-29 14:39:10 -0700677 // If the strong reference count has already been incremented by
678 // someone else, the implementor of onIncStrongAttempted() is holding
679 // an unneeded reference. So call onLastStrongRef() here to remove it.
680 // (No, this is not pretty.) Note that we MUST NOT do this if we
681 // are in fact acquiring the first reference.
682 if (curCount != 0 && curCount != INITIAL_STRONG_VALUE) {
683 impl->mBase->onLastStrongRef(id);
684 }
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800685 }
686 }
Christopher Ferris0e691602020-10-14 14:13:58 -0700687
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800688 impl->addStrongRef(id);
689
690#if PRINT_REFS
Steve Blockeb095332011-12-20 16:23:08 +0000691 ALOGD("attemptIncStrong of %p from %p: cnt=%d\n", this, id, curCount);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800692#endif
693
Hans Boehm7f27cbc2016-07-29 14:39:10 -0700694 // curCount is the value of mStrong before we incremented it.
Hans Boehme263e6c2016-05-11 18:15:12 -0700695 // Now we need to fix-up the count if it was INITIAL_STRONG_VALUE.
696 // This must be done safely, i.e.: handle the case where several threads
Dianne Hackborna729ab12013-03-14 15:26:30 -0700697 // were here in attemptIncStrong().
Hans Boehme263e6c2016-05-11 18:15:12 -0700698 // curCount > INITIAL_STRONG_VALUE is OK, and can happen if we're doing
699 // this in the middle of another incStrong. The subtraction is handled
700 // by the thread that started with INITIAL_STRONG_VALUE.
701 if (curCount == INITIAL_STRONG_VALUE) {
702 impl->mStrong.fetch_sub(INITIAL_STRONG_VALUE,
703 std::memory_order_relaxed);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800704 }
Dianne Hackborna729ab12013-03-14 15:26:30 -0700705
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800706 return true;
707}
708
709bool RefBase::weakref_type::attemptIncWeak(const void* id)
710{
711 weakref_impl* const impl = static_cast<weakref_impl*>(this);
Mathias Agopianad099652011-08-10 21:07:02 -0700712
Hans Boehme263e6c2016-05-11 18:15:12 -0700713 int32_t curCount = impl->mWeak.load(std::memory_order_relaxed);
Steve Blockae074452012-01-09 18:35:44 +0000714 ALOG_ASSERT(curCount >= 0, "attemptIncWeak called on %p after underflow",
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800715 this);
716 while (curCount > 0) {
Hans Boehme263e6c2016-05-11 18:15:12 -0700717 if (impl->mWeak.compare_exchange_weak(curCount, curCount+1,
718 std::memory_order_relaxed)) {
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800719 break;
720 }
Hans Boehme263e6c2016-05-11 18:15:12 -0700721 // curCount has been updated.
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800722 }
723
724 if (curCount > 0) {
725 impl->addWeakRef(id);
726 }
727
728 return curCount > 0;
729}
730
731int32_t RefBase::weakref_type::getWeakCount() const
732{
Hans Boehme263e6c2016-05-11 18:15:12 -0700733 // Debug only!
734 return static_cast<const weakref_impl*>(this)->mWeak
735 .load(std::memory_order_relaxed);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800736}
737
738void RefBase::weakref_type::printRefs() const
739{
740 static_cast<const weakref_impl*>(this)->printRefs();
741}
742
743void RefBase::weakref_type::trackMe(bool enable, bool retain)
744{
Mathias Agopianad099652011-08-10 21:07:02 -0700745 static_cast<weakref_impl*>(this)->trackMe(enable, retain);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800746}
747
748RefBase::weakref_type* RefBase::createWeak(const void* id) const
749{
750 mRefs->incWeak(id);
751 return mRefs;
752}
753
754RefBase::weakref_type* RefBase::getWeakRefs() const
755{
756 return mRefs;
757}
758
759RefBase::RefBase()
760 : mRefs(new weakref_impl(this))
761{
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800762}
763
764RefBase::~RefBase()
765{
Hans Boehm23c857e2016-08-02 18:39:30 -0700766 int32_t flags = mRefs->mFlags.load(std::memory_order_relaxed);
767 // Life-time of this object is extended to WEAK, in
768 // which case weakref_impl doesn't out-live the object and we
769 // can free it now.
770 if ((flags & OBJECT_LIFETIME_MASK) == OBJECT_LIFETIME_WEAK) {
771 // It's possible that the weak count is not 0 if the object
772 // re-acquired a weak reference in its destructor
773 if (mRefs->mWeak.load(std::memory_order_relaxed) == 0) {
774 delete mRefs;
775 }
Steven Moreland483a2de2022-07-26 17:57:39 +0000776 } else {
777 int32_t strongs = mRefs->mStrong.load(std::memory_order_relaxed);
Steven Morelandf164de82022-03-11 02:28:57 +0000778
Steven Moreland483a2de2022-07-26 17:57:39 +0000779 if (strongs == INITIAL_STRONG_VALUE) {
780 // We never acquired a strong reference on this object.
781
782 // It would be nice to make this fatal, but many places use RefBase on the stack.
783 // However, this is dangerous because it's also common for code to use the
784 // sp<T>(T*) constructor, assuming that if the object is around, it is already
785 // owned by an sp<>.
786 ALOGW("RefBase: Explicit destruction, weak count = %d (in %p). Use sp<> to manage this "
787 "object.",
788 mRefs->mWeak.load(), this);
Christopher Ferris0e691602020-10-14 14:13:58 -0700789
790#if CALLSTACK_ENABLED
Steven Moreland483a2de2022-07-26 17:57:39 +0000791 CallStack::logStack(LOG_TAG);
Christopher Ferris0e691602020-10-14 14:13:58 -0700792#endif
Steven Moreland483a2de2022-07-26 17:57:39 +0000793 } else if (strongs != 0) {
794 LOG_ALWAYS_FATAL("RefBase: object %p with strong count %d deleted. Double owned?", this,
795 strongs);
796 }
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800797 }
Hans Boehm23c857e2016-08-02 18:39:30 -0700798 // For debugging purposes, clear mRefs. Ineffective against outstanding wp's.
Yi Konge1731a42018-07-16 18:11:34 -0700799 const_cast<weakref_impl*&>(mRefs) = nullptr;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800800}
801
802void RefBase::extendObjectLifetime(int32_t mode)
803{
Steven Morelandd086fe52022-07-26 22:11:13 +0000804 check_not_on_stack(this);
805
Hans Boehme263e6c2016-05-11 18:15:12 -0700806 // Must be happens-before ordered with respect to construction or any
807 // operation that could destroy the object.
808 mRefs->mFlags.fetch_or(mode, std::memory_order_relaxed);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800809}
810
811void RefBase::onFirstRef()
812{
813}
814
815void RefBase::onLastStrongRef(const void* /*id*/)
816{
817}
818
Mark Salyzyn5bed8032014-04-30 11:10:46 -0700819bool RefBase::onIncStrongAttempted(uint32_t flags, const void* /*id*/)
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800820{
821 return (flags&FIRST_INC_STRONG) ? true : false;
822}
823
824void RefBase::onLastWeakRef(const void* /*id*/)
825{
826}
Mathias Agopianad099652011-08-10 21:07:02 -0700827
828// ---------------------------------------------------------------------------
829
Mathias Agopianad099652011-08-10 21:07:02 -0700830#if DEBUG_REFS
Mark Salyzyn5bed8032014-04-30 11:10:46 -0700831void RefBase::renameRefs(size_t n, const ReferenceRenamer& renamer) {
Mathias Agopianad099652011-08-10 21:07:02 -0700832 for (size_t i=0 ; i<n ; i++) {
Mathias Agopian6cd548c2013-03-18 22:27:41 -0700833 renamer(i);
Mathias Agopianad099652011-08-10 21:07:02 -0700834 }
Mathias Agopianad099652011-08-10 21:07:02 -0700835}
Mark Salyzyn5bed8032014-04-30 11:10:46 -0700836#else
837void RefBase::renameRefs(size_t /*n*/, const ReferenceRenamer& /*renamer*/) { }
838#endif
Mathias Agopianad099652011-08-10 21:07:02 -0700839
Mathias Agopian6cd548c2013-03-18 22:27:41 -0700840void RefBase::renameRefId(weakref_type* ref,
841 const void* old_id, const void* new_id) {
842 weakref_impl* const impl = static_cast<weakref_impl*>(ref);
843 impl->renameStrongRefId(old_id, new_id);
844 impl->renameWeakRefId(old_id, new_id);
845}
846
847void RefBase::renameRefId(RefBase* ref,
848 const void* old_id, const void* new_id) {
849 ref->mRefs->renameStrongRefId(old_id, new_id);
850 ref->mRefs->renameWeakRefId(old_id, new_id);
851}
852
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800853}; // namespace android