blob: c6b0cb7b01ba4ed3edcc9d16ada8507ab55228b7 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2008 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 "IMemory"
18
Hans Boehm7e202972016-07-12 18:05:49 -070019#include <atomic>
Mathias Agopiane9e9fe42017-02-28 16:25:16 -080020#include <stdatomic.h>
21
Mark Salyzyna5e161b2016-09-29 08:08:05 -070022#include <fcntl.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080023#include <stdint.h>
24#include <stdio.h>
25#include <stdlib.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080026#include <sys/types.h>
27#include <sys/mman.h>
Mark Salyzyna5e161b2016-09-29 08:08:05 -070028#include <unistd.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080029
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070030#include <binder/IMemory.h>
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070031#include <binder/Parcel.h>
Mark Salyzyn7823e122016-09-29 08:08:05 -070032#include <log/log.h>
Mathias Agopiane9e9fe42017-02-28 16:25:16 -080033
Andrei Homescu8028ff42022-03-14 22:11:54 +000034#include <utils/Mutex.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080035
Jiyong Park5970d0a2022-03-08 16:56:13 +090036#include <map>
37
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080038#define VERBOSE 0
39
40namespace android {
41// ---------------------------------------------------------------------------
42
43class HeapCache : public IBinder::DeathRecipient
44{
45public:
46 HeapCache();
47 virtual ~HeapCache();
Anu Sundararajan5728a922011-06-22 15:58:59 -050048
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080049 virtual void binderDied(const wp<IBinder>& who);
50
Anu Sundararajan5728a922011-06-22 15:58:59 -050051 sp<IMemoryHeap> find_heap(const sp<IBinder>& binder);
52 void free_heap(const sp<IBinder>& binder);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080053 sp<IMemoryHeap> get_heap(const sp<IBinder>& binder);
54 void dump_heaps();
55
56private:
57 // For IMemory.cpp
58 struct heap_info_t {
59 sp<IMemoryHeap> heap;
60 int32_t count;
Hans Boehm7e202972016-07-12 18:05:49 -070061 // Note that this cannot be meaningfully copied.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080062 };
63
Anu Sundararajan5728a922011-06-22 15:58:59 -050064 void free_heap(const wp<IBinder>& binder);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080065
Hans Boehm7e202972016-07-12 18:05:49 -070066 Mutex mHeapCacheLock; // Protects entire vector below.
Jiyong Park5970d0a2022-03-08 16:56:13 +090067 std::map<wp<IBinder>, heap_info_t> mHeapCache;
Hans Boehm7e202972016-07-12 18:05:49 -070068 // We do not use the copy-on-write capabilities of KeyedVector.
69 // TODO: Reimplemement based on standard C++ container?
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080070};
71
Steven Moreland1a3a8ef2021-04-02 02:52:46 +000072static sp<HeapCache> gHeapCache = sp<HeapCache>::make();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080073
74/******************************************************************************/
75
76enum {
77 HEAP_ID = IBinder::FIRST_CALL_TRANSACTION
78};
79
80class BpMemoryHeap : public BpInterface<IMemoryHeap>
81{
82public:
Chih-Hung Hsiehe2347b72016-04-25 15:41:05 -070083 explicit BpMemoryHeap(const sp<IBinder>& impl);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080084 virtual ~BpMemoryHeap();
85
Yi Kongf4bb6ff2020-02-25 15:37:53 +080086 int getHeapID() const override;
87 void* getBase() const override;
88 size_t getSize() const override;
89 uint32_t getFlags() const override;
Andy Hung5b028522018-10-22 15:29:49 -070090 off_t getOffset() const override;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080091
92private:
93 friend class IMemory;
94 friend class HeapCache;
Anu Sundararajan5728a922011-06-22 15:58:59 -050095
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080096 // for debugging in this module
97 static inline sp<IMemoryHeap> find_heap(const sp<IBinder>& binder) {
98 return gHeapCache->find_heap(binder);
99 }
100 static inline void free_heap(const sp<IBinder>& binder) {
101 gHeapCache->free_heap(binder);
102 }
103 static inline sp<IMemoryHeap> get_heap(const sp<IBinder>& binder) {
104 return gHeapCache->get_heap(binder);
105 }
106 static inline void dump_heaps() {
Anu Sundararajan5728a922011-06-22 15:58:59 -0500107 gHeapCache->dump_heaps();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800108 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800109
110 void assertMapped() const;
111 void assertReallyMapped() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800112
Hans Boehm7e202972016-07-12 18:05:49 -0700113 mutable std::atomic<int32_t> mHeapId;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800114 mutable void* mBase;
115 mutable size_t mSize;
116 mutable uint32_t mFlags;
Andy Hung5b028522018-10-22 15:29:49 -0700117 mutable off_t mOffset;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800118 mutable bool mRealHeap;
119 mutable Mutex mLock;
120};
121
122// ----------------------------------------------------------------------------
123
124enum {
125 GET_MEMORY = IBinder::FIRST_CALL_TRANSACTION
126};
127
128class BpMemory : public BpInterface<IMemory>
129{
130public:
Chih-Hung Hsiehe2347b72016-04-25 15:41:05 -0700131 explicit BpMemory(const sp<IBinder>& impl);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800132 virtual ~BpMemory();
Jiyong Parkb86c8662018-10-29 23:01:57 +0900133 // NOLINTNEXTLINE(google-default-arguments)
Yi Kongfdd8da92018-06-07 17:52:27 -0700134 virtual sp<IMemoryHeap> getMemory(ssize_t* offset=nullptr, size_t* size=nullptr) const;
Anu Sundararajan5728a922011-06-22 15:58:59 -0500135
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800136private:
137 mutable sp<IMemoryHeap> mHeap;
138 mutable ssize_t mOffset;
139 mutable size_t mSize;
140};
141
142/******************************************************************************/
143
144void* IMemory::fastPointer(const sp<IBinder>& binder, ssize_t offset) const
145{
146 sp<IMemoryHeap> realHeap = BpMemoryHeap::get_heap(binder);
147 void* const base = realHeap->base();
148 if (base == MAP_FAILED)
Yi Kongfdd8da92018-06-07 17:52:27 -0700149 return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800150 return static_cast<char*>(base) + offset;
151}
152
Ytai Ben-Tsviacb72992019-09-05 15:14:30 -0700153void* IMemory::unsecurePointer() const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800154 ssize_t offset;
155 sp<IMemoryHeap> heap = getMemory(&offset);
Yi Kongfdd8da92018-06-07 17:52:27 -0700156 void* const base = heap!=nullptr ? heap->base() : MAP_FAILED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800157 if (base == MAP_FAILED)
Yi Kongfdd8da92018-06-07 17:52:27 -0700158 return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800159 return static_cast<char*>(base) + offset;
160}
161
Ytai Ben-Tsviacb72992019-09-05 15:14:30 -0700162void* IMemory::pointer() const { return unsecurePointer(); }
163
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800164size_t IMemory::size() const {
165 size_t size;
Yi Kongfdd8da92018-06-07 17:52:27 -0700166 getMemory(nullptr, &size);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800167 return size;
168}
169
170ssize_t IMemory::offset() const {
171 ssize_t offset;
172 getMemory(&offset);
173 return offset;
174}
175
176/******************************************************************************/
177
178BpMemory::BpMemory(const sp<IBinder>& impl)
179 : BpInterface<IMemory>(impl), mOffset(0), mSize(0)
180{
181}
182
183BpMemory::~BpMemory()
184{
185}
186
Jiyong Parkb86c8662018-10-29 23:01:57 +0900187// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800188sp<IMemoryHeap> BpMemory::getMemory(ssize_t* offset, size_t* size) const
189{
Yi Kongfdd8da92018-06-07 17:52:27 -0700190 if (mHeap == nullptr) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800191 Parcel data, reply;
192 data.writeInterfaceToken(IMemory::getInterfaceDescriptor());
193 if (remote()->transact(GET_MEMORY, data, &reply) == NO_ERROR) {
194 sp<IBinder> heap = reply.readStrongBinder();
Yi Kongfdd8da92018-06-07 17:52:27 -0700195 if (heap != nullptr) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800196 mHeap = interface_cast<IMemoryHeap>(heap);
Yi Kongfdd8da92018-06-07 17:52:27 -0700197 if (mHeap != nullptr) {
Andy Hung5b028522018-10-22 15:29:49 -0700198 const int64_t offset64 = reply.readInt64();
199 const uint64_t size64 = reply.readUint64();
200 const ssize_t o = (ssize_t)offset64;
201 const size_t s = (size_t)size64;
Christopher Tate94b0d4e2016-02-05 19:02:56 -0800202 size_t heapSize = mHeap->getSize();
Andy Hung5b028522018-10-22 15:29:49 -0700203 if (s == size64 && o == offset64 // ILP32 bounds check
204 && s <= heapSize
Christopher Tate94b0d4e2016-02-05 19:02:56 -0800205 && o >= 0
206 && (static_cast<size_t>(o) <= heapSize - s)) {
207 mOffset = o;
208 mSize = s;
209 } else {
210 // Hm.
211 android_errorWriteWithInfoLog(0x534e4554,
Yi Kongfdd8da92018-06-07 17:52:27 -0700212 "26877992", -1, nullptr, 0);
Christopher Tate94b0d4e2016-02-05 19:02:56 -0800213 mOffset = 0;
214 mSize = 0;
215 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800216 }
217 }
218 }
219 }
220 if (offset) *offset = mOffset;
221 if (size) *size = mSize;
Yi Kongfdd8da92018-06-07 17:52:27 -0700222 return (mSize > 0) ? mHeap : nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800223}
224
225// ---------------------------------------------------------------------------
226
Jooyung Hanc91e3cb2020-11-25 06:38:17 +0900227IMPLEMENT_META_INTERFACE(Memory, "android.utils.IMemory")
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800228
Mathias Agopian83c04462009-05-22 19:00:22 -0700229BnMemory::BnMemory() {
230}
231
Anu Sundararajan5728a922011-06-22 15:58:59 -0500232BnMemory::~BnMemory() {
Mathias Agopian83c04462009-05-22 19:00:22 -0700233}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800234
Jiyong Parkb86c8662018-10-29 23:01:57 +0900235// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800236status_t BnMemory::onTransact(
237 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
238{
239 switch(code) {
240 case GET_MEMORY: {
241 CHECK_INTERFACE(IMemory, data, reply);
242 ssize_t offset;
243 size_t size;
Marco Nelissen097ca272014-11-14 08:01:01 -0800244 reply->writeStrongBinder( IInterface::asBinder(getMemory(&offset, &size)) );
Andy Hung5b028522018-10-22 15:29:49 -0700245 reply->writeInt64(offset);
246 reply->writeUint64(size);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800247 return NO_ERROR;
248 } break;
249 default:
250 return BBinder::onTransact(code, data, reply, flags);
251 }
252}
253
254
255/******************************************************************************/
256
257BpMemoryHeap::BpMemoryHeap(const sp<IBinder>& impl)
258 : BpInterface<IMemoryHeap>(impl),
Anu Sundararajan5728a922011-06-22 15:58:59 -0500259 mHeapId(-1), mBase(MAP_FAILED), mSize(0), mFlags(0), mOffset(0), mRealHeap(false)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800260{
261}
262
263BpMemoryHeap::~BpMemoryHeap() {
Hans Boehm7e202972016-07-12 18:05:49 -0700264 int32_t heapId = mHeapId.load(memory_order_relaxed);
265 if (heapId != -1) {
266 close(heapId);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800267 if (mRealHeap) {
268 // by construction we're the last one
269 if (mBase != MAP_FAILED) {
Marco Nelissen097ca272014-11-14 08:01:01 -0800270 sp<IBinder> binder = IInterface::asBinder(this);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800271
272 if (VERBOSE) {
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800273 ALOGD("UNMAPPING binder=%p, heap=%p, size=%zu, fd=%d",
Hans Boehm7e202972016-07-12 18:05:49 -0700274 binder.get(), this, mSize, heapId);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800275 }
276
277 munmap(mBase, mSize);
278 }
279 } else {
280 // remove from list only if it was mapped before
Marco Nelissen097ca272014-11-14 08:01:01 -0800281 sp<IBinder> binder = IInterface::asBinder(this);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800282 free_heap(binder);
283 }
284 }
285}
286
287void BpMemoryHeap::assertMapped() const
288{
Hans Boehm7e202972016-07-12 18:05:49 -0700289 int32_t heapId = mHeapId.load(memory_order_acquire);
290 if (heapId == -1) {
Marco Nelissen097ca272014-11-14 08:01:01 -0800291 sp<IBinder> binder(IInterface::asBinder(const_cast<BpMemoryHeap*>(this)));
Steven Moreland1a3a8ef2021-04-02 02:52:46 +0000292 sp<BpMemoryHeap> heap = sp<BpMemoryHeap>::cast(find_heap(binder));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800293 heap->assertReallyMapped();
294 if (heap->mBase != MAP_FAILED) {
295 Mutex::Autolock _l(mLock);
Hans Boehm7e202972016-07-12 18:05:49 -0700296 if (mHeapId.load(memory_order_relaxed) == -1) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800297 mBase = heap->mBase;
298 mSize = heap->mSize;
Anu Sundararajan5728a922011-06-22 15:58:59 -0500299 mOffset = heap->mOffset;
Nick Kralevichec9ec7d2016-12-17 19:47:27 -0800300 int fd = fcntl(heap->mHeapId.load(memory_order_relaxed), F_DUPFD_CLOEXEC, 0);
Hans Boehm7e202972016-07-12 18:05:49 -0700301 ALOGE_IF(fd==-1, "cannot dup fd=%d",
302 heap->mHeapId.load(memory_order_relaxed));
303 mHeapId.store(fd, memory_order_release);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800304 }
305 } else {
306 // something went wrong
307 free_heap(binder);
308 }
309 }
310}
311
312void BpMemoryHeap::assertReallyMapped() const
313{
Hans Boehm7e202972016-07-12 18:05:49 -0700314 int32_t heapId = mHeapId.load(memory_order_acquire);
315 if (heapId == -1) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800316
317 // remote call without mLock held, worse case scenario, we end up
318 // calling transact() from multiple threads, but that's not a problem,
319 // only mmap below must be in the critical section.
Anu Sundararajan5728a922011-06-22 15:58:59 -0500320
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800321 Parcel data, reply;
322 data.writeInterfaceToken(IMemoryHeap::getInterfaceDescriptor());
323 status_t err = remote()->transact(HEAP_ID, data, &reply);
324 int parcel_fd = reply.readFileDescriptor();
Andy Hung5b028522018-10-22 15:29:49 -0700325 const uint64_t size64 = reply.readUint64();
326 const int64_t offset64 = reply.readInt64();
327 const uint32_t flags = reply.readUint32();
328 const size_t size = (size_t)size64;
329 const off_t offset = (off_t)offset64;
330 if (err != NO_ERROR || // failed transaction
331 size != size64 || offset != offset64) { // ILP32 size check
332 ALOGE("binder=%p transaction failed fd=%d, size=%zu, err=%d (%s)",
333 IInterface::asBinder(this).get(),
334 parcel_fd, size, err, strerror(-err));
335 return;
336 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800337
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800338 Mutex::Autolock _l(mLock);
Hans Boehm7e202972016-07-12 18:05:49 -0700339 if (mHeapId.load(memory_order_relaxed) == -1) {
Nick Kralevichec9ec7d2016-12-17 19:47:27 -0800340 int fd = fcntl(parcel_fd, F_DUPFD_CLOEXEC, 0);
Andy Hung5b028522018-10-22 15:29:49 -0700341 ALOGE_IF(fd == -1, "cannot dup fd=%d, size=%zu, err=%d (%s)",
John Eckerdal6b0b0632016-04-21 15:04:14 +0200342 parcel_fd, size, err, strerror(errno));
343
344 int access = PROT_READ;
345 if (!(flags & READ_ONLY)) {
346 access |= PROT_WRITE;
347 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800348 mRealHeap = true;
Yi Kongfdd8da92018-06-07 17:52:27 -0700349 mBase = mmap(nullptr, size, access, MAP_SHARED, fd, offset);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800350 if (mBase == MAP_FAILED) {
Andy Hung5b028522018-10-22 15:29:49 -0700351 ALOGE("cannot map BpMemoryHeap (binder=%p), size=%zu, fd=%d (%s)",
Marco Nelissen097ca272014-11-14 08:01:01 -0800352 IInterface::asBinder(this).get(), size, fd, strerror(errno));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800353 close(fd);
354 } else {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800355 mSize = size;
356 mFlags = flags;
Anu Sundararajan5728a922011-06-22 15:58:59 -0500357 mOffset = offset;
Hans Boehm7e202972016-07-12 18:05:49 -0700358 mHeapId.store(fd, memory_order_release);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800359 }
360 }
361 }
362}
363
364int BpMemoryHeap::getHeapID() const {
365 assertMapped();
Hans Boehm7e202972016-07-12 18:05:49 -0700366 // We either stored mHeapId ourselves, or loaded it with acquire semantics.
367 return mHeapId.load(memory_order_relaxed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800368}
369
370void* BpMemoryHeap::getBase() const {
371 assertMapped();
372 return mBase;
373}
374
375size_t BpMemoryHeap::getSize() const {
376 assertMapped();
377 return mSize;
378}
379
380uint32_t BpMemoryHeap::getFlags() const {
381 assertMapped();
382 return mFlags;
383}
384
Andy Hung5b028522018-10-22 15:29:49 -0700385off_t BpMemoryHeap::getOffset() const {
Anu Sundararajan5728a922011-06-22 15:58:59 -0500386 assertMapped();
387 return mOffset;
388}
389
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800390// ---------------------------------------------------------------------------
391
Jooyung Hanc91e3cb2020-11-25 06:38:17 +0900392IMPLEMENT_META_INTERFACE(MemoryHeap, "android.utils.IMemoryHeap")
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800393
Anu Sundararajan5728a922011-06-22 15:58:59 -0500394BnMemoryHeap::BnMemoryHeap() {
Mathias Agopian83c04462009-05-22 19:00:22 -0700395}
396
Anu Sundararajan5728a922011-06-22 15:58:59 -0500397BnMemoryHeap::~BnMemoryHeap() {
Mathias Agopian83c04462009-05-22 19:00:22 -0700398}
399
Jiyong Parkb86c8662018-10-29 23:01:57 +0900400// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800401status_t BnMemoryHeap::onTransact(
Mathias Agopian83c04462009-05-22 19:00:22 -0700402 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800403{
404 switch(code) {
405 case HEAP_ID: {
406 CHECK_INTERFACE(IMemoryHeap, data, reply);
407 reply->writeFileDescriptor(getHeapID());
Andy Hung5b028522018-10-22 15:29:49 -0700408 reply->writeUint64(getSize());
409 reply->writeInt64(getOffset());
410 reply->writeUint32(getFlags());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800411 return NO_ERROR;
412 } break;
413 default:
414 return BBinder::onTransact(code, data, reply, flags);
415 }
416}
417
418/*****************************************************************************/
419
420HeapCache::HeapCache()
421 : DeathRecipient()
422{
423}
424
425HeapCache::~HeapCache()
426{
427}
428
429void HeapCache::binderDied(const wp<IBinder>& binder)
430{
Steve Block9d453682011-12-20 16:23:08 +0000431 //ALOGD("binderDied binder=%p", binder.unsafe_get());
Anu Sundararajan5728a922011-06-22 15:58:59 -0500432 free_heap(binder);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800433}
434
Anu Sundararajan5728a922011-06-22 15:58:59 -0500435sp<IMemoryHeap> HeapCache::find_heap(const sp<IBinder>& binder)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800436{
437 Mutex::Autolock _l(mHeapCacheLock);
Jiyong Park5970d0a2022-03-08 16:56:13 +0900438 auto i = mHeapCache.find(binder);
439 if (i != mHeapCache.end()) {
440 heap_info_t& info = i->second;
Steve Block9d453682011-12-20 16:23:08 +0000441 ALOGD_IF(VERBOSE,
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800442 "found binder=%p, heap=%p, size=%zu, fd=%d, count=%d",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800443 binder.get(), info.heap.get(),
444 static_cast<BpMemoryHeap*>(info.heap.get())->mSize,
Hans Boehm7e202972016-07-12 18:05:49 -0700445 static_cast<BpMemoryHeap*>(info.heap.get())
446 ->mHeapId.load(memory_order_relaxed),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800447 info.count);
Hans Boehm7e202972016-07-12 18:05:49 -0700448 ++info.count;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800449 return info.heap;
450 } else {
451 heap_info_t info;
452 info.heap = interface_cast<IMemoryHeap>(binder);
453 info.count = 1;
Steve Block9d453682011-12-20 16:23:08 +0000454 //ALOGD("adding binder=%p, heap=%p, count=%d",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800455 // binder.get(), info.heap.get(), info.count);
Jiyong Park5970d0a2022-03-08 16:56:13 +0900456 mHeapCache.insert({binder, info});
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800457 return info.heap;
458 }
459}
460
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800461void HeapCache::free_heap(const sp<IBinder>& binder) {
462 free_heap( wp<IBinder>(binder) );
463}
464
Anu Sundararajan5728a922011-06-22 15:58:59 -0500465void HeapCache::free_heap(const wp<IBinder>& binder)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800466{
467 sp<IMemoryHeap> rel;
468 {
469 Mutex::Autolock _l(mHeapCacheLock);
Jiyong Park5970d0a2022-03-08 16:56:13 +0900470 auto i = mHeapCache.find(binder);
471 if (i != mHeapCache.end()) {
472 heap_info_t& info = i->second;
Hans Boehm7e202972016-07-12 18:05:49 -0700473 if (--info.count == 0) {
Steve Block9d453682011-12-20 16:23:08 +0000474 ALOGD_IF(VERBOSE,
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800475 "removing binder=%p, heap=%p, size=%zu, fd=%d, count=%d",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800476 binder.unsafe_get(), info.heap.get(),
477 static_cast<BpMemoryHeap*>(info.heap.get())->mSize,
Hans Boehm7e202972016-07-12 18:05:49 -0700478 static_cast<BpMemoryHeap*>(info.heap.get())
479 ->mHeapId.load(memory_order_relaxed),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800480 info.count);
Jiyong Park5970d0a2022-03-08 16:56:13 +0900481 rel = i->second.heap;
482 mHeapCache.erase(i);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800483 }
484 } else {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000485 ALOGE("free_heap binder=%p not found!!!", binder.unsafe_get());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800486 }
487 }
488}
489
490sp<IMemoryHeap> HeapCache::get_heap(const sp<IBinder>& binder)
491{
492 sp<IMemoryHeap> realHeap;
493 Mutex::Autolock _l(mHeapCacheLock);
Jiyong Park5970d0a2022-03-08 16:56:13 +0900494 auto i = mHeapCache.find(binder);
495 if (i != mHeapCache.end())
496 realHeap = i->second.heap;
497 else
498 realHeap = interface_cast<IMemoryHeap>(binder);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800499 return realHeap;
500}
501
Anu Sundararajan5728a922011-06-22 15:58:59 -0500502void HeapCache::dump_heaps()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800503{
504 Mutex::Autolock _l(mHeapCacheLock);
Jiyong Park5970d0a2022-03-08 16:56:13 +0900505 for (const auto& i : mHeapCache) {
506 const heap_info_t& info = i.second;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800507 BpMemoryHeap const* h(static_cast<BpMemoryHeap const *>(info.heap.get()));
Jiyong Park5970d0a2022-03-08 16:56:13 +0900508 ALOGD("hey=%p, heap=%p, count=%d, (fd=%d, base=%p, size=%zu)", i.first.unsafe_get(),
509 info.heap.get(), info.count, h->mHeapId.load(memory_order_relaxed), h->mBase,
510 h->mSize);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800511 }
512}
513
514
515// ---------------------------------------------------------------------------
Steven Moreland6511af52019-09-26 16:05:45 -0700516} // namespace android