blob: 4b0cc161bc459891efc673f9d07e557db4732aa1 [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>
Steven Moreland7a1ca592023-10-07 01:09:39 +000021#include <mutex>
Hans Boehm2a019ec2018-08-07 23:45:25 +000022
Steven Moreland377adea2022-10-08 05:06:52 +000023#include <fcntl.h>
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>
Steven Moreland377adea2022-10-08 05:06:52 +000027#include <utils/String8.h>
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080028
Mark Salyzyn5bed8032014-04-30 11:10:46 -070029#ifndef __unused
30#define __unused __attribute__((__unused__))
31#endif
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080032
Hans Boehm2a019ec2018-08-07 23:45:25 +000033// Compile with refcounting debugging enabled.
Steven Moreland377adea2022-10-08 05:06:52 +000034#ifndef DEBUG_REFS
Hans Boehm2a019ec2018-08-07 23:45:25 +000035#define DEBUG_REFS 0
Steven Moreland377adea2022-10-08 05:06:52 +000036#endif
Hans Boehm2a019ec2018-08-07 23:45:25 +000037
38// The following three are ignored unless DEBUG_REFS is set.
Mathias Agopian6d4419d2013-03-18 20:31:18 -070039
40// whether ref-tracking is enabled by default, if not, trackMe(true, false)
41// needs to be called explicitly
Hans Boehm2a019ec2018-08-07 23:45:25 +000042#define DEBUG_REFS_ENABLED_BY_DEFAULT 0
Mathias Agopian6d4419d2013-03-18 20:31:18 -070043
44// whether callstack are collected (significantly slows things down)
Hans Boehm2a019ec2018-08-07 23:45:25 +000045#define DEBUG_REFS_CALLSTACK_ENABLED 1
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080046
Mathias Agopian6d4419d2013-03-18 20:31:18 -070047// folder where stack traces are saved when DEBUG_REFS is enabled
48// this folder needs to exist and be writable
Steven Morelandb7412c82022-10-08 05:07:52 +000049#ifdef __ANDROID__
Hans Boehm2a019ec2018-08-07 23:45:25 +000050#define DEBUG_REFS_CALLSTACK_PATH "/data/debug"
Steven Morelandb7412c82022-10-08 05:07:52 +000051#else
52#define DEBUG_REFS_CALLSTACK_PATH "."
53#endif
Mathias Agopian6d4419d2013-03-18 20:31:18 -070054
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080055// log all reference counting operations
Hans Boehm2a019ec2018-08-07 23:45:25 +000056#define PRINT_REFS 0
57
Andrei Homescu5c15de22021-12-10 05:32:17 +000058#if defined(__linux__)
Christopher Ferris0e691602020-10-14 14:13:58 -070059// CallStack is only supported on linux type platforms.
60#define CALLSTACK_ENABLED 1
61#else
62#define CALLSTACK_ENABLED 0
63#endif
64
65#if CALLSTACK_ENABLED
Steven Morelandc7383702023-10-21 00:43:52 +000066#include "../../include/utils/CallStack.h"
Christopher Ferris0e691602020-10-14 14:13:58 -070067#endif
68
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080069// ---------------------------------------------------------------------------
70
71namespace android {
72
Hans Boehm9ba71922016-07-21 18:56:55 -070073// Observations, invariants, etc:
Hans Boehme263e6c2016-05-11 18:15:12 -070074
Hans Boehm9ba71922016-07-21 18:56:55 -070075// By default, obects are destroyed when the last strong reference disappears
76// or, if the object never had a strong reference, when the last weak reference
77// disappears.
78//
Hans Boehme263e6c2016-05-11 18:15:12 -070079// OBJECT_LIFETIME_WEAK changes this behavior to retain the object
80// unconditionally until the last reference of either kind disappears. The
81// client ensures that the extendObjectLifetime call happens before the dec
82// call that would otherwise have deallocated the object, or before an
83// attemptIncStrong call that might rely on it. We do not worry about
84// concurrent changes to the object lifetime.
Hans Boehm9ba71922016-07-21 18:56:55 -070085//
86// AttemptIncStrong will succeed if the object has a strong reference, or if it
87// has a weak reference and has never had a strong reference.
88// AttemptIncWeak really does succeed only if there is already a WEAK
89// reference, and thus may fail when attemptIncStrong would succeed.
90//
Hans Boehme263e6c2016-05-11 18:15:12 -070091// mStrong is the strong reference count. mWeak is the weak reference count.
92// Between calls, and ignoring memory ordering effects, mWeak includes strong
93// references, and is thus >= mStrong.
94//
Hans Boehm9ba71922016-07-21 18:56:55 -070095// A weakref_impl holds all the information, including both reference counts,
96// required to perform wp<> operations. Thus these can continue to be performed
97// after the RefBase object has been destroyed.
98//
Hans Boehme263e6c2016-05-11 18:15:12 -070099// A weakref_impl is allocated as the value of mRefs in a RefBase object on
100// construction.
Hans Boehm23c857e2016-08-02 18:39:30 -0700101// In the OBJECT_LIFETIME_STRONG case, it is normally deallocated in decWeak,
102// and hence lives as long as the last weak reference. (It can also be
103// deallocated in the RefBase destructor iff the strong reference count was
104// never incremented and the weak count is zero, e.g. if the RefBase object is
105// explicitly destroyed without decrementing the strong count. This should be
106// avoided.) In this case, the RefBase destructor should be invoked from
107// decStrong.
108// In the OBJECT_LIFETIME_WEAK case, the weakref_impl is always deallocated in
109// the RefBase destructor, which is always invoked by decWeak. DecStrong
110// explicitly avoids the deletion in this case.
Hans Boehme263e6c2016-05-11 18:15:12 -0700111//
112// Memory ordering:
113// The client must ensure that every inc() call, together with all other
114// accesses to the object, happens before the corresponding dec() call.
115//
116// We try to keep memory ordering constraints on atomics as weak as possible,
117// since memory fences or ordered memory accesses are likely to be a major
118// performance cost for this code. All accesses to mStrong, mWeak, and mFlags
119// explicitly relax memory ordering in some way.
120//
121// The only operations that are not memory_order_relaxed are reference count
122// decrements. All reference count decrements are release operations. In
123// addition, the final decrement leading the deallocation is followed by an
124// acquire fence, which we can view informally as also turning it into an
125// acquire operation. (See 29.8p4 [atomics.fences] for details. We could
126// alternatively use acq_rel operations for all decrements. This is probably
127// slower on most current (2016) hardware, especially on ARMv7, but that may
128// not be true indefinitely.)
129//
130// This convention ensures that the second-to-last decrement synchronizes with
131// (in the language of 1.10 in the C++ standard) the final decrement of a
132// reference count. Since reference counts are only updated using atomic
133// read-modify-write operations, this also extends to any earlier decrements.
134// (See "release sequence" in 1.10.)
135//
136// Since all operations on an object happen before the corresponding reference
137// count decrement, and all reference count decrements happen before the final
138// one, we are guaranteed that all other object accesses happen before the
139// object is destroyed.
140
141
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800142#define INITIAL_STRONG_VALUE (1<<28)
143
Hans Boehm23c857e2016-08-02 18:39:30 -0700144#define MAX_COUNT 0xfffff
145
146// Test whether the argument is a clearly invalid strong reference count.
147// Used only for error checking on the value before an atomic decrement.
148// Intended to be very cheap.
149// Note that we cannot just check for excess decrements by comparing to zero
150// since the object would be deallocated before that.
151#define BAD_STRONG(c) \
152 ((c) == 0 || ((c) & (~(MAX_COUNT | INITIAL_STRONG_VALUE))) != 0)
153
154// Same for weak counts.
155#define BAD_WEAK(c) ((c) == 0 || ((c) & (~MAX_COUNT)) != 0)
156
Steven Morelandcd4ef872022-07-29 00:40:29 +0000157// name kept because prebuilts used to use it from inlining sp<> code
158void sp_report_stack_pointer() { LOG_ALWAYS_FATAL("RefBase used with stack pointer argument"); }
Steven Morelandc340a082022-07-26 17:09:50 +0000159
160// Check whether address is definitely on the calling stack. We actually check whether it is on
161// the same 4K page as the frame pointer.
162//
163// Assumptions:
164// - Pages are never smaller than 4K (MIN_PAGE_SIZE)
165// - Malloced memory never shares a page with a stack.
166//
167// It does not appear safe to broaden this check to include adjacent pages; apparently this code
168// is used in environments where there may not be a guard page below (at higher addresses than)
169// the bottom of the stack.
170static void check_not_on_stack(const void* ptr) {
171 static constexpr int MIN_PAGE_SIZE = 0x1000; // 4K. Safer than including sys/user.h.
172 static constexpr uintptr_t MIN_PAGE_MASK = ~static_cast<uintptr_t>(MIN_PAGE_SIZE - 1);
173 uintptr_t my_frame_address =
174 reinterpret_cast<uintptr_t>(__builtin_frame_address(0 /* this frame */));
175 if (((reinterpret_cast<uintptr_t>(ptr) ^ my_frame_address) & MIN_PAGE_MASK) == 0) {
176 sp_report_stack_pointer();
177 }
178}
179
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800180// ---------------------------------------------------------------------------
181
182class RefBase::weakref_impl : public RefBase::weakref_type
183{
184public:
Hans Boehme263e6c2016-05-11 18:15:12 -0700185 std::atomic<int32_t> mStrong;
186 std::atomic<int32_t> mWeak;
187 RefBase* const mBase;
188 std::atomic<int32_t> mFlags;
Mathias Agopian9c8fa9e2011-06-15 20:42:47 -0700189
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800190#if !DEBUG_REFS
191
Chih-Hung Hsieh1c563d92016-04-29 15:44:04 -0700192 explicit weakref_impl(RefBase* base)
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800193 : mStrong(INITIAL_STRONG_VALUE)
194 , mWeak(0)
195 , mBase(base)
陈冠有0cbef722021-08-22 16:36:28 +0000196 , mFlags(OBJECT_LIFETIME_STRONG)
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800197 {
198 }
199
200 void addStrongRef(const void* /*id*/) { }
201 void removeStrongRef(const void* /*id*/) { }
Mathias Agopianad099652011-08-10 21:07:02 -0700202 void renameStrongRefId(const void* /*old_id*/, const void* /*new_id*/) { }
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800203 void addWeakRef(const void* /*id*/) { }
204 void removeWeakRef(const void* /*id*/) { }
Mathias Agopianad099652011-08-10 21:07:02 -0700205 void renameWeakRefId(const void* /*old_id*/, const void* /*new_id*/) { }
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800206 void printRefs() const { }
207 void trackMe(bool, bool) { }
208
209#else
210
211 weakref_impl(RefBase* base)
212 : mStrong(INITIAL_STRONG_VALUE)
213 , mWeak(0)
214 , mBase(base)
陈冠有0cbef722021-08-22 16:36:28 +0000215 , mFlags(OBJECT_LIFETIME_STRONG)
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800216 , mStrongRefs(NULL)
217 , mWeakRefs(NULL)
218 , mTrackEnabled(!!DEBUG_REFS_ENABLED_BY_DEFAULT)
219 , mRetain(false)
220 {
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800221 }
Christopher Ferris0e691602020-10-14 14:13:58 -0700222
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800223 ~weakref_impl()
224 {
Mathias Agopianad099652011-08-10 21:07:02 -0700225 bool dumpStack = false;
226 if (!mRetain && mStrongRefs != NULL) {
227 dumpStack = true;
Steve Block1b781ab2012-01-06 19:20:56 +0000228 ALOGE("Strong references remain:");
Mathias Agopianad099652011-08-10 21:07:02 -0700229 ref_entry* refs = mStrongRefs;
230 while (refs) {
231 char inc = refs->ref >= 0 ? '+' : '-';
Steve Blockeb095332011-12-20 16:23:08 +0000232 ALOGD("\t%c ID %p (ref %d):", inc, refs->id, refs->ref);
Christopher Ferris0e691602020-10-14 14:13:58 -0700233#if DEBUG_REFS_CALLSTACK_ENABLED && CALLSTACK_ENABLED
Hans Boehm2a019ec2018-08-07 23:45:25 +0000234 CallStack::logStack(LOG_TAG, refs->stack.get());
Mathias Agopianad099652011-08-10 21:07:02 -0700235#endif
236 refs = refs->next;
237 }
238 }
239
240 if (!mRetain && mWeakRefs != NULL) {
241 dumpStack = true;
Steve Block1b781ab2012-01-06 19:20:56 +0000242 ALOGE("Weak references remain!");
Mathias Agopianad099652011-08-10 21:07:02 -0700243 ref_entry* refs = mWeakRefs;
244 while (refs) {
245 char inc = refs->ref >= 0 ? '+' : '-';
Steve Blockeb095332011-12-20 16:23:08 +0000246 ALOGD("\t%c ID %p (ref %d):", inc, refs->id, refs->ref);
Christopher Ferris0e691602020-10-14 14:13:58 -0700247#if DEBUG_REFS_CALLSTACK_ENABLED && CALLSTACK_ENABLED
Hans Boehm2a019ec2018-08-07 23:45:25 +0000248 CallStack::logStack(LOG_TAG, refs->stack.get());
Mathias Agopianad099652011-08-10 21:07:02 -0700249#endif
250 refs = refs->next;
251 }
252 }
253 if (dumpStack) {
Steve Block1b781ab2012-01-06 19:20:56 +0000254 ALOGE("above errors at:");
Christopher Ferris0e691602020-10-14 14:13:58 -0700255#if CALLSTACK_ENABLED
Hans Boehm2a019ec2018-08-07 23:45:25 +0000256 CallStack::logStack(LOG_TAG);
Christopher Ferris0e691602020-10-14 14:13:58 -0700257#endif
Mathias Agopianad099652011-08-10 21:07:02 -0700258 }
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800259 }
260
Mathias Agopianad099652011-08-10 21:07:02 -0700261 void addStrongRef(const void* id) {
Steve Blockeb095332011-12-20 16:23:08 +0000262 //ALOGD_IF(mTrackEnabled,
Mathias Agopianad099652011-08-10 21:07:02 -0700263 // "addStrongRef: RefBase=%p, id=%p", mBase, id);
Hans Boehme263e6c2016-05-11 18:15:12 -0700264 addRef(&mStrongRefs, id, mStrong.load(std::memory_order_relaxed));
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800265 }
266
Mathias Agopianad099652011-08-10 21:07:02 -0700267 void removeStrongRef(const void* id) {
Steve Blockeb095332011-12-20 16:23:08 +0000268 //ALOGD_IF(mTrackEnabled,
Mathias Agopianad099652011-08-10 21:07:02 -0700269 // "removeStrongRef: RefBase=%p, id=%p", mBase, id);
270 if (!mRetain) {
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800271 removeRef(&mStrongRefs, id);
Mathias Agopianad099652011-08-10 21:07:02 -0700272 } else {
Hans Boehme263e6c2016-05-11 18:15:12 -0700273 addRef(&mStrongRefs, id, -mStrong.load(std::memory_order_relaxed));
Mathias Agopianad099652011-08-10 21:07:02 -0700274 }
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800275 }
276
Mathias Agopianad099652011-08-10 21:07:02 -0700277 void renameStrongRefId(const void* old_id, const void* new_id) {
Steve Blockeb095332011-12-20 16:23:08 +0000278 //ALOGD_IF(mTrackEnabled,
Mathias Agopianad099652011-08-10 21:07:02 -0700279 // "renameStrongRefId: RefBase=%p, oid=%p, nid=%p",
280 // mBase, old_id, new_id);
281 renameRefsId(mStrongRefs, old_id, new_id);
282 }
283
284 void addWeakRef(const void* id) {
Hans Boehme263e6c2016-05-11 18:15:12 -0700285 addRef(&mWeakRefs, id, mWeak.load(std::memory_order_relaxed));
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800286 }
287
Mathias Agopianad099652011-08-10 21:07:02 -0700288 void removeWeakRef(const void* id) {
289 if (!mRetain) {
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800290 removeRef(&mWeakRefs, id);
Mathias Agopianad099652011-08-10 21:07:02 -0700291 } else {
Hans Boehme263e6c2016-05-11 18:15:12 -0700292 addRef(&mWeakRefs, id, -mWeak.load(std::memory_order_relaxed));
Mathias Agopianad099652011-08-10 21:07:02 -0700293 }
294 }
295
296 void renameWeakRefId(const void* old_id, const void* new_id) {
297 renameRefsId(mWeakRefs, old_id, new_id);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800298 }
299
Christopher Ferris0e691602020-10-14 14:13:58 -0700300 void trackMe(bool track, bool retain) {
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800301 mTrackEnabled = track;
302 mRetain = retain;
303 }
304
305 void printRefs() const
306 {
307 String8 text;
308
309 {
Steven Moreland7a1ca592023-10-07 01:09:39 +0000310 std::lock_guard<std::mutex> _l(mMutex);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800311 char buf[128];
George Burgess IVe7aa2b22016-03-02 14:02:55 -0800312 snprintf(buf, sizeof(buf),
313 "Strong 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, mStrongRefs);
George Burgess IVe7aa2b22016-03-02 14:02:55 -0800317 snprintf(buf, sizeof(buf),
318 "Weak references on RefBase %p (weakref_type %p):\n",
319 mBase, this);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800320 text.append(buf);
321 printRefsLocked(&text, mWeakRefs);
322 }
323
324 {
325 char name[100];
George Burgess IVe7aa2b22016-03-02 14:02:55 -0800326 snprintf(name, sizeof(name), DEBUG_REFS_CALLSTACK_PATH "/%p.stack",
327 this);
Steven Morelandb7412c82022-10-08 05:07:52 +0000328 int rc = open(name, O_RDWR | O_CREAT | O_APPEND, 0644);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800329 if (rc >= 0) {
Tomasz Wasilczyk18b74612023-08-10 23:29:50 +0000330 (void)write(rc, text.c_str(), text.length());
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800331 close(rc);
Steven Morelandb7412c82022-10-08 05:07:52 +0000332 ALOGI("STACK TRACE for %p saved in %s", this, name);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800333 }
Steve Block1b781ab2012-01-06 19:20:56 +0000334 else ALOGE("FAILED TO PRINT STACK TRACE for %p in %s: %s", this,
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800335 name, strerror(errno));
336 }
337 }
338
339private:
340 struct ref_entry
341 {
342 ref_entry* next;
343 const void* id;
Christopher Ferris0e691602020-10-14 14:13:58 -0700344#if DEBUG_REFS_CALLSTACK_ENABLED && CALLSTACK_ENABLED
Hans Boehm2a019ec2018-08-07 23:45:25 +0000345 CallStack::CallStackUPtr stack;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800346#endif
347 int32_t ref;
348 };
349
350 void addRef(ref_entry** refs, const void* id, int32_t mRef)
351 {
352 if (mTrackEnabled) {
Steven Moreland7a1ca592023-10-07 01:09:39 +0000353 std::lock_guard<std::mutex> _l(mMutex);
Mathias Agopianad099652011-08-10 21:07:02 -0700354
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800355 ref_entry* ref = new ref_entry;
356 // Reference count at the time of the snapshot, but before the
357 // update. Positive value means we increment, negative--we
358 // decrement the reference count.
359 ref->ref = mRef;
360 ref->id = id;
Christopher Ferris0e691602020-10-14 14:13:58 -0700361#if DEBUG_REFS_CALLSTACK_ENABLED && CALLSTACK_ENABLED
Hans Boehm2a019ec2018-08-07 23:45:25 +0000362 ref->stack = CallStack::getCurrent(2);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800363#endif
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800364 ref->next = *refs;
365 *refs = ref;
366 }
367 }
368
369 void removeRef(ref_entry** refs, const void* id)
370 {
371 if (mTrackEnabled) {
Steven Moreland7a1ca592023-10-07 01:09:39 +0000372 std::lock_guard<std::mutex> _l(mMutex);
Christopher Ferris0e691602020-10-14 14:13:58 -0700373
Mathias Agopianad099652011-08-10 21:07:02 -0700374 ref_entry* const head = *refs;
375 ref_entry* ref = head;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800376 while (ref != NULL) {
377 if (ref->id == id) {
378 *refs = ref->next;
379 delete ref;
380 return;
381 }
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800382 refs = &ref->next;
383 ref = *refs;
384 }
Mathias Agopianad099652011-08-10 21:07:02 -0700385
Steve Block1b781ab2012-01-06 19:20:56 +0000386 ALOGE("RefBase: removing id %p on RefBase %p"
Mathias Agopianad099652011-08-10 21:07:02 -0700387 "(weakref_type %p) that doesn't exist!",
388 id, mBase, this);
389
390 ref = head;
391 while (ref) {
392 char inc = ref->ref >= 0 ? '+' : '-';
Steve Blockeb095332011-12-20 16:23:08 +0000393 ALOGD("\t%c ID %p (ref %d):", inc, ref->id, ref->ref);
Mathias Agopianad099652011-08-10 21:07:02 -0700394 ref = ref->next;
395 }
396
Christopher Ferris0e691602020-10-14 14:13:58 -0700397#if CALLSTACK_ENABLED
Hans Boehm2a019ec2018-08-07 23:45:25 +0000398 CallStack::logStack(LOG_TAG);
Christopher Ferris0e691602020-10-14 14:13:58 -0700399#endif
Mathias Agopianad099652011-08-10 21:07:02 -0700400 }
401 }
402
403 void renameRefsId(ref_entry* r, const void* old_id, const void* new_id)
404 {
405 if (mTrackEnabled) {
Steven Moreland7a1ca592023-10-07 01:09:39 +0000406 std::lock_guard<std::mutex> _l(mMutex);
Mathias Agopianad099652011-08-10 21:07:02 -0700407 ref_entry* ref = r;
408 while (ref != NULL) {
409 if (ref->id == old_id) {
410 ref->id = new_id;
411 }
412 ref = ref->next;
413 }
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800414 }
415 }
416
417 void printRefsLocked(String8* out, const ref_entry* refs) const
418 {
419 char buf[128];
420 while (refs) {
421 char inc = refs->ref >= 0 ? '+' : '-';
George Burgess IVe7aa2b22016-03-02 14:02:55 -0800422 snprintf(buf, sizeof(buf), "\t%c ID %p (ref %d):\n",
423 inc, refs->id, refs->ref);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800424 out->append(buf);
Christopher Ferris0e691602020-10-14 14:13:58 -0700425#if DEBUG_REFS_CALLSTACK_ENABLED && CALLSTACK_ENABLED
Hans Boehm2a019ec2018-08-07 23:45:25 +0000426 out->append(CallStack::stackToString("\t\t", refs->stack.get()));
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800427#else
428 out->append("\t\t(call stacks disabled)");
429#endif
430 refs = refs->next;
431 }
432 }
433
Steven Moreland7a1ca592023-10-07 01:09:39 +0000434 mutable std::mutex mMutex;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800435 ref_entry* mStrongRefs;
436 ref_entry* mWeakRefs;
437
438 bool mTrackEnabled;
439 // Collect stack traces on addref and removeref, instead of deleting the stack references
440 // on removeref that match the address ones.
441 bool mRetain;
442
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800443#endif
444};
445
446// ---------------------------------------------------------------------------
447
448void RefBase::incStrong(const void* id) const
449{
450 weakref_impl* const refs = mRefs;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800451 refs->incWeak(id);
Christopher Ferris0e691602020-10-14 14:13:58 -0700452
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800453 refs->addStrongRef(id);
Hans Boehme263e6c2016-05-11 18:15:12 -0700454 const int32_t c = refs->mStrong.fetch_add(1, std::memory_order_relaxed);
Steve Blockae074452012-01-09 18:35:44 +0000455 ALOG_ASSERT(c > 0, "incStrong() called on %p after last strong ref", refs);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800456#if PRINT_REFS
Steve Blockeb095332011-12-20 16:23:08 +0000457 ALOGD("incStrong of %p from %p: cnt=%d\n", this, id, c);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800458#endif
459 if (c != INITIAL_STRONG_VALUE) {
460 return;
461 }
462
Steven Morelandc340a082022-07-26 17:09:50 +0000463 check_not_on_stack(this);
464
Chih-Hung Hsieh122352d2017-10-02 15:20:07 -0700465 int32_t old __unused = refs->mStrong.fetch_sub(INITIAL_STRONG_VALUE, std::memory_order_relaxed);
Hans Boehme263e6c2016-05-11 18:15:12 -0700466 // A decStrong() must still happen after us.
467 ALOG_ASSERT(old > INITIAL_STRONG_VALUE, "0x%x too small", old);
Mathias Agopianad099652011-08-10 21:07:02 -0700468 refs->mBase->onFirstRef();
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800469}
470
Steven Morelandda75cef2021-03-31 16:05:04 +0000471void RefBase::incStrongRequireStrong(const void* id) const {
472 weakref_impl* const refs = mRefs;
473 refs->incWeak(id);
474
475 refs->addStrongRef(id);
476 const int32_t c = refs->mStrong.fetch_add(1, std::memory_order_relaxed);
477
478 LOG_ALWAYS_FATAL_IF(c <= 0 || c == INITIAL_STRONG_VALUE,
479 "incStrongRequireStrong() called on %p which isn't already owned", refs);
480#if PRINT_REFS
481 ALOGD("incStrong (requiring strong) of %p from %p: cnt=%d\n", this, id, c);
482#endif
483}
484
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800485void RefBase::decStrong(const void* id) const
486{
487 weakref_impl* const refs = mRefs;
488 refs->removeStrongRef(id);
Hans Boehme263e6c2016-05-11 18:15:12 -0700489 const int32_t c = refs->mStrong.fetch_sub(1, std::memory_order_release);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800490#if PRINT_REFS
Steve Blockeb095332011-12-20 16:23:08 +0000491 ALOGD("decStrong of %p from %p: cnt=%d\n", this, id, c);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800492#endif
Hans Boehm23c857e2016-08-02 18:39:30 -0700493 LOG_ALWAYS_FATAL_IF(BAD_STRONG(c), "decStrong() called on %p too many times",
494 refs);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800495 if (c == 1) {
Hans Boehme263e6c2016-05-11 18:15:12 -0700496 std::atomic_thread_fence(std::memory_order_acquire);
Mathias Agopianad099652011-08-10 21:07:02 -0700497 refs->mBase->onLastStrongRef(id);
Hans Boehme263e6c2016-05-11 18:15:12 -0700498 int32_t flags = refs->mFlags.load(std::memory_order_relaxed);
499 if ((flags&OBJECT_LIFETIME_MASK) == OBJECT_LIFETIME_STRONG) {
Mathias Agopian9c8fa9e2011-06-15 20:42:47 -0700500 delete this;
Hans Boehm23c857e2016-08-02 18:39:30 -0700501 // The destructor does not delete refs in this case.
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800502 }
503 }
Hans Boehme263e6c2016-05-11 18:15:12 -0700504 // Note that even with only strong reference operations, the thread
505 // deallocating this may not be the same as the thread deallocating refs.
506 // That's OK: all accesses to this happen before its deletion here,
507 // and all accesses to refs happen before its deletion in the final decWeak.
508 // The destructor can safely access mRefs because either it's deleting
509 // mRefs itself, or it's running entirely before the final mWeak decrement.
George Burgess IV6753bc42017-10-01 12:38:44 -0700510 //
511 // Since we're doing atomic loads of `flags`, the static analyzer assumes
512 // they can change between `delete this;` and `refs->decWeak(id);`. This is
513 // not the case. The analyzer may become more okay with this patten when
514 // https://bugs.llvm.org/show_bug.cgi?id=34365 gets resolved. NOLINTNEXTLINE
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800515 refs->decWeak(id);
516}
517
518void RefBase::forceIncStrong(const void* id) const
519{
Hans Boehme263e6c2016-05-11 18:15:12 -0700520 // Allows initial mStrong of 0 in addition to INITIAL_STRONG_VALUE.
521 // TODO: Better document assumptions.
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800522 weakref_impl* const refs = mRefs;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800523 refs->incWeak(id);
Christopher Ferris0e691602020-10-14 14:13:58 -0700524
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800525 refs->addStrongRef(id);
Hans Boehme263e6c2016-05-11 18:15:12 -0700526 const int32_t c = refs->mStrong.fetch_add(1, std::memory_order_relaxed);
Steve Blockae074452012-01-09 18:35:44 +0000527 ALOG_ASSERT(c >= 0, "forceIncStrong called on %p after ref count underflow",
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800528 refs);
529#if PRINT_REFS
Steve Blockeb095332011-12-20 16:23:08 +0000530 ALOGD("forceIncStrong of %p from %p: cnt=%d\n", this, id, c);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800531#endif
532
533 switch (c) {
534 case INITIAL_STRONG_VALUE:
Hans Boehme263e6c2016-05-11 18:15:12 -0700535 refs->mStrong.fetch_sub(INITIAL_STRONG_VALUE,
536 std::memory_order_relaxed);
Steven Morelandc7383702023-10-21 00:43:52 +0000537 [[fallthrough]];
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800538 case 0:
Mathias Agopianad099652011-08-10 21:07:02 -0700539 refs->mBase->onFirstRef();
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800540 }
541}
542
543int32_t RefBase::getStrongCount() const
544{
Hans Boehme263e6c2016-05-11 18:15:12 -0700545 // Debugging only; No memory ordering guarantees.
546 return mRefs->mStrong.load(std::memory_order_relaxed);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800547}
548
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800549RefBase* RefBase::weakref_type::refBase() const
550{
551 return static_cast<const weakref_impl*>(this)->mBase;
552}
553
554void RefBase::weakref_type::incWeak(const void* id)
555{
556 weakref_impl* const impl = static_cast<weakref_impl*>(this);
557 impl->addWeakRef(id);
Hans Boehme263e6c2016-05-11 18:15:12 -0700558 const int32_t c __unused = impl->mWeak.fetch_add(1,
559 std::memory_order_relaxed);
Steve Blockae074452012-01-09 18:35:44 +0000560 ALOG_ASSERT(c >= 0, "incWeak called on %p after last weak ref", this);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800561}
562
Steven Morelandda75cef2021-03-31 16:05:04 +0000563void RefBase::weakref_type::incWeakRequireWeak(const void* id)
564{
565 weakref_impl* const impl = static_cast<weakref_impl*>(this);
566 impl->addWeakRef(id);
567 const int32_t c __unused = impl->mWeak.fetch_add(1,
568 std::memory_order_relaxed);
569 LOG_ALWAYS_FATAL_IF(c <= 0, "incWeakRequireWeak called on %p which has no weak refs", this);
570}
Mathias Agopianad099652011-08-10 21:07:02 -0700571
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800572void RefBase::weakref_type::decWeak(const void* id)
573{
574 weakref_impl* const impl = static_cast<weakref_impl*>(this);
575 impl->removeWeakRef(id);
Hans Boehme263e6c2016-05-11 18:15:12 -0700576 const int32_t c = impl->mWeak.fetch_sub(1, std::memory_order_release);
Hans Boehm23c857e2016-08-02 18:39:30 -0700577 LOG_ALWAYS_FATAL_IF(BAD_WEAK(c), "decWeak called on %p too many times",
578 this);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800579 if (c != 1) return;
Hans Boehme263e6c2016-05-11 18:15:12 -0700580 atomic_thread_fence(std::memory_order_acquire);
Mathias Agopianad099652011-08-10 21:07:02 -0700581
Hans Boehme263e6c2016-05-11 18:15:12 -0700582 int32_t flags = impl->mFlags.load(std::memory_order_relaxed);
583 if ((flags&OBJECT_LIFETIME_MASK) == OBJECT_LIFETIME_STRONG) {
Mathias Agopianad099652011-08-10 21:07:02 -0700584 // This is the regular lifetime case. The object is destroyed
585 // when the last strong reference goes away. Since weakref_impl
Hans Boehm23c857e2016-08-02 18:39:30 -0700586 // outlives the object, it is not destroyed in the dtor, and
Mathias Agopianad099652011-08-10 21:07:02 -0700587 // we'll have to do it here.
Hans Boehme263e6c2016-05-11 18:15:12 -0700588 if (impl->mStrong.load(std::memory_order_relaxed)
589 == INITIAL_STRONG_VALUE) {
Hans Boehm23c857e2016-08-02 18:39:30 -0700590 // Decrementing a weak count to zero when object never had a strong
591 // reference. We assume it acquired a weak reference early, e.g.
592 // in the constructor, and will eventually be properly destroyed,
593 // usually via incrementing and decrementing the strong count.
594 // Thus we no longer do anything here. We log this case, since it
595 // seems to be extremely rare, and should not normally occur. We
596 // used to deallocate mBase here, so this may now indicate a leak.
597 ALOGW("RefBase: Object at %p lost last weak reference "
598 "before it had a strong reference", impl->mBase);
Mathias Agopianad099652011-08-10 21:07:02 -0700599 } else {
Steve Blockb37fbe92011-10-20 11:56:00 +0100600 // ALOGV("Freeing refs %p of old RefBase %p\n", this, impl->mBase);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800601 delete impl;
602 }
603 } else {
Hans Boehme263e6c2016-05-11 18:15:12 -0700604 // This is the OBJECT_LIFETIME_WEAK case. The last weak-reference
605 // is gone, we can destroy the object.
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800606 impl->mBase->onLastWeakRef(id);
Hans Boehme263e6c2016-05-11 18:15:12 -0700607 delete impl->mBase;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800608 }
609}
610
611bool RefBase::weakref_type::attemptIncStrong(const void* id)
612{
613 incWeak(id);
Christopher Ferris0e691602020-10-14 14:13:58 -0700614
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800615 weakref_impl* const impl = static_cast<weakref_impl*>(this);
Hans Boehme263e6c2016-05-11 18:15:12 -0700616 int32_t curCount = impl->mStrong.load(std::memory_order_relaxed);
Dianne Hackborna729ab12013-03-14 15:26:30 -0700617
618 ALOG_ASSERT(curCount >= 0,
619 "attemptIncStrong called on %p after underflow", this);
620
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800621 while (curCount > 0 && curCount != INITIAL_STRONG_VALUE) {
Dianne Hackborna729ab12013-03-14 15:26:30 -0700622 // we're in the easy/common case of promoting a weak-reference
623 // from an existing strong reference.
Hans Boehme263e6c2016-05-11 18:15:12 -0700624 if (impl->mStrong.compare_exchange_weak(curCount, curCount+1,
625 std::memory_order_relaxed)) {
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800626 break;
627 }
Dianne Hackborna729ab12013-03-14 15:26:30 -0700628 // the strong count has changed on us, we need to re-assert our
Hans Boehme263e6c2016-05-11 18:15:12 -0700629 // situation. curCount was updated by compare_exchange_weak.
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800630 }
Christopher Ferris0e691602020-10-14 14:13:58 -0700631
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800632 if (curCount <= 0 || curCount == INITIAL_STRONG_VALUE) {
Dianne Hackborna729ab12013-03-14 15:26:30 -0700633 // we're now in the harder case of either:
634 // - there never was a strong reference on us
635 // - or, all strong references have been released
Hans Boehme263e6c2016-05-11 18:15:12 -0700636 int32_t flags = impl->mFlags.load(std::memory_order_relaxed);
637 if ((flags&OBJECT_LIFETIME_MASK) == OBJECT_LIFETIME_STRONG) {
Dianne Hackborna729ab12013-03-14 15:26:30 -0700638 // this object has a "normal" life-time, i.e.: it gets destroyed
639 // when the last strong reference goes away
640 if (curCount <= 0) {
641 // the last strong-reference got released, the object cannot
642 // be revived.
643 decWeak(id);
644 return false;
645 }
646
647 // here, curCount == INITIAL_STRONG_VALUE, which means
648 // there never was a strong-reference, so we can try to
649 // promote this object; we need to do that atomically.
650 while (curCount > 0) {
Hans Boehme263e6c2016-05-11 18:15:12 -0700651 if (impl->mStrong.compare_exchange_weak(curCount, curCount+1,
652 std::memory_order_relaxed)) {
Dianne Hackborna729ab12013-03-14 15:26:30 -0700653 break;
654 }
655 // the strong count has changed on us, we need to re-assert our
656 // situation (e.g.: another thread has inc/decStrong'ed us)
Hans Boehme263e6c2016-05-11 18:15:12 -0700657 // curCount has been updated.
Dianne Hackborna729ab12013-03-14 15:26:30 -0700658 }
659
660 if (curCount <= 0) {
661 // promote() failed, some other thread destroyed us in the
662 // meantime (i.e.: strong count reached zero).
663 decWeak(id);
664 return false;
665 }
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800666 } else {
Dianne Hackborna729ab12013-03-14 15:26:30 -0700667 // this object has an "extended" life-time, i.e.: it can be
668 // revived from a weak-reference only.
669 // Ask the object's implementation if it agrees to be revived
670 if (!impl->mBase->onIncStrongAttempted(FIRST_INC_STRONG, id)) {
671 // it didn't so give-up.
672 decWeak(id);
673 return false;
674 }
675 // grab a strong-reference, which is always safe due to the
676 // extended life-time.
Hans Boehme263e6c2016-05-11 18:15:12 -0700677 curCount = impl->mStrong.fetch_add(1, std::memory_order_relaxed);
Hans Boehm7f27cbc2016-07-29 14:39:10 -0700678 // If the strong reference count has already been incremented by
679 // someone else, the implementor of onIncStrongAttempted() is holding
680 // an unneeded reference. So call onLastStrongRef() here to remove it.
681 // (No, this is not pretty.) Note that we MUST NOT do this if we
682 // are in fact acquiring the first reference.
683 if (curCount != 0 && curCount != INITIAL_STRONG_VALUE) {
684 impl->mBase->onLastStrongRef(id);
685 }
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800686 }
687 }
Christopher Ferris0e691602020-10-14 14:13:58 -0700688
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800689 impl->addStrongRef(id);
690
691#if PRINT_REFS
Steve Blockeb095332011-12-20 16:23:08 +0000692 ALOGD("attemptIncStrong of %p from %p: cnt=%d\n", this, id, curCount);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800693#endif
694
Hans Boehm7f27cbc2016-07-29 14:39:10 -0700695 // curCount is the value of mStrong before we incremented it.
Hans Boehme263e6c2016-05-11 18:15:12 -0700696 // Now we need to fix-up the count if it was INITIAL_STRONG_VALUE.
697 // This must be done safely, i.e.: handle the case where several threads
Dianne Hackborna729ab12013-03-14 15:26:30 -0700698 // were here in attemptIncStrong().
Hans Boehme263e6c2016-05-11 18:15:12 -0700699 // curCount > INITIAL_STRONG_VALUE is OK, and can happen if we're doing
700 // this in the middle of another incStrong. The subtraction is handled
701 // by the thread that started with INITIAL_STRONG_VALUE.
702 if (curCount == INITIAL_STRONG_VALUE) {
703 impl->mStrong.fetch_sub(INITIAL_STRONG_VALUE,
704 std::memory_order_relaxed);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800705 }
Dianne Hackborna729ab12013-03-14 15:26:30 -0700706
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800707 return true;
708}
709
710bool RefBase::weakref_type::attemptIncWeak(const void* id)
711{
712 weakref_impl* const impl = static_cast<weakref_impl*>(this);
Mathias Agopianad099652011-08-10 21:07:02 -0700713
Hans Boehme263e6c2016-05-11 18:15:12 -0700714 int32_t curCount = impl->mWeak.load(std::memory_order_relaxed);
Steve Blockae074452012-01-09 18:35:44 +0000715 ALOG_ASSERT(curCount >= 0, "attemptIncWeak called on %p after underflow",
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800716 this);
717 while (curCount > 0) {
Hans Boehme263e6c2016-05-11 18:15:12 -0700718 if (impl->mWeak.compare_exchange_weak(curCount, curCount+1,
719 std::memory_order_relaxed)) {
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800720 break;
721 }
Hans Boehme263e6c2016-05-11 18:15:12 -0700722 // curCount has been updated.
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800723 }
724
725 if (curCount > 0) {
726 impl->addWeakRef(id);
727 }
728
729 return curCount > 0;
730}
731
732int32_t RefBase::weakref_type::getWeakCount() const
733{
Hans Boehme263e6c2016-05-11 18:15:12 -0700734 // Debug only!
735 return static_cast<const weakref_impl*>(this)->mWeak
736 .load(std::memory_order_relaxed);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800737}
738
739void RefBase::weakref_type::printRefs() const
740{
741 static_cast<const weakref_impl*>(this)->printRefs();
742}
743
744void RefBase::weakref_type::trackMe(bool enable, bool retain)
745{
Mathias Agopianad099652011-08-10 21:07:02 -0700746 static_cast<weakref_impl*>(this)->trackMe(enable, retain);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800747}
748
749RefBase::weakref_type* RefBase::createWeak(const void* id) const
750{
751 mRefs->incWeak(id);
752 return mRefs;
753}
754
755RefBase::weakref_type* RefBase::getWeakRefs() const
756{
757 return mRefs;
758}
759
760RefBase::RefBase()
761 : mRefs(new weakref_impl(this))
762{
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800763}
764
765RefBase::~RefBase()
766{
Hans Boehm23c857e2016-08-02 18:39:30 -0700767 int32_t flags = mRefs->mFlags.load(std::memory_order_relaxed);
768 // Life-time of this object is extended to WEAK, in
769 // which case weakref_impl doesn't out-live the object and we
770 // can free it now.
771 if ((flags & OBJECT_LIFETIME_MASK) == OBJECT_LIFETIME_WEAK) {
772 // It's possible that the weak count is not 0 if the object
773 // re-acquired a weak reference in its destructor
774 if (mRefs->mWeak.load(std::memory_order_relaxed) == 0) {
775 delete mRefs;
776 }
Steven Moreland483a2de2022-07-26 17:57:39 +0000777 } else {
778 int32_t strongs = mRefs->mStrong.load(std::memory_order_relaxed);
Steven Morelandf164de82022-03-11 02:28:57 +0000779
Steven Moreland483a2de2022-07-26 17:57:39 +0000780 if (strongs == INITIAL_STRONG_VALUE) {
781 // We never acquired a strong reference on this object.
782
783 // It would be nice to make this fatal, but many places use RefBase on the stack.
784 // However, this is dangerous because it's also common for code to use the
785 // sp<T>(T*) constructor, assuming that if the object is around, it is already
786 // owned by an sp<>.
787 ALOGW("RefBase: Explicit destruction, weak count = %d (in %p). Use sp<> to manage this "
788 "object.",
789 mRefs->mWeak.load(), this);
Christopher Ferris0e691602020-10-14 14:13:58 -0700790
791#if CALLSTACK_ENABLED
Steven Moreland483a2de2022-07-26 17:57:39 +0000792 CallStack::logStack(LOG_TAG);
Christopher Ferris0e691602020-10-14 14:13:58 -0700793#endif
Steven Moreland483a2de2022-07-26 17:57:39 +0000794 } else if (strongs != 0) {
795 LOG_ALWAYS_FATAL("RefBase: object %p with strong count %d deleted. Double owned?", this,
796 strongs);
797 }
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800798 }
Hans Boehm23c857e2016-08-02 18:39:30 -0700799 // For debugging purposes, clear mRefs. Ineffective against outstanding wp's.
Yi Konge1731a42018-07-16 18:11:34 -0700800 const_cast<weakref_impl*&>(mRefs) = nullptr;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800801}
802
803void RefBase::extendObjectLifetime(int32_t mode)
804{
Steven Morelandd086fe52022-07-26 22:11:13 +0000805 check_not_on_stack(this);
806
Hans Boehme263e6c2016-05-11 18:15:12 -0700807 // Must be happens-before ordered with respect to construction or any
808 // operation that could destroy the object.
809 mRefs->mFlags.fetch_or(mode, std::memory_order_relaxed);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800810}
811
812void RefBase::onFirstRef()
813{
814}
815
816void RefBase::onLastStrongRef(const void* /*id*/)
817{
818}
819
Mark Salyzyn5bed8032014-04-30 11:10:46 -0700820bool RefBase::onIncStrongAttempted(uint32_t flags, const void* /*id*/)
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800821{
822 return (flags&FIRST_INC_STRONG) ? true : false;
823}
824
825void RefBase::onLastWeakRef(const void* /*id*/)
826{
827}
Mathias Agopianad099652011-08-10 21:07:02 -0700828
829// ---------------------------------------------------------------------------
830
Mathias Agopianad099652011-08-10 21:07:02 -0700831#if DEBUG_REFS
Mark Salyzyn5bed8032014-04-30 11:10:46 -0700832void RefBase::renameRefs(size_t n, const ReferenceRenamer& renamer) {
Mathias Agopianad099652011-08-10 21:07:02 -0700833 for (size_t i=0 ; i<n ; i++) {
Mathias Agopian6cd548c2013-03-18 22:27:41 -0700834 renamer(i);
Mathias Agopianad099652011-08-10 21:07:02 -0700835 }
Mathias Agopianad099652011-08-10 21:07:02 -0700836}
Mark Salyzyn5bed8032014-04-30 11:10:46 -0700837#else
838void RefBase::renameRefs(size_t /*n*/, const ReferenceRenamer& /*renamer*/) { }
839#endif
Mathias Agopianad099652011-08-10 21:07:02 -0700840
Mathias Agopian6cd548c2013-03-18 22:27:41 -0700841void RefBase::renameRefId(weakref_type* ref,
842 const void* old_id, const void* new_id) {
843 weakref_impl* const impl = static_cast<weakref_impl*>(ref);
844 impl->renameStrongRefId(old_id, new_id);
845 impl->renameWeakRefId(old_id, new_id);
846}
847
848void RefBase::renameRefId(RefBase* ref,
849 const void* old_id, const void* new_id) {
850 ref->mRefs->renameStrongRefId(old_id, new_id);
851 ref->mRefs->renameWeakRefId(old_id, new_id);
852}
853
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800854}; // namespace android