blob: c2bb811e9fee6877652f2024edb5039e9adcb2cd [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
Mark Salyzyna5e161b2016-09-29 08:08:05 -070034#include <utils/KeyedVector.h>
35#include <utils/threads.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080036
37#define VERBOSE 0
38
39namespace android {
40// ---------------------------------------------------------------------------
41
42class HeapCache : public IBinder::DeathRecipient
43{
44public:
45 HeapCache();
46 virtual ~HeapCache();
Anu Sundararajan5728a922011-06-22 15:58:59 -050047
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080048 virtual void binderDied(const wp<IBinder>& who);
49
Anu Sundararajan5728a922011-06-22 15:58:59 -050050 sp<IMemoryHeap> find_heap(const sp<IBinder>& binder);
51 void free_heap(const sp<IBinder>& binder);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080052 sp<IMemoryHeap> get_heap(const sp<IBinder>& binder);
53 void dump_heaps();
54
55private:
56 // For IMemory.cpp
57 struct heap_info_t {
58 sp<IMemoryHeap> heap;
59 int32_t count;
Hans Boehm7e202972016-07-12 18:05:49 -070060 // Note that this cannot be meaningfully copied.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080061 };
62
Anu Sundararajan5728a922011-06-22 15:58:59 -050063 void free_heap(const wp<IBinder>& binder);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080064
Hans Boehm7e202972016-07-12 18:05:49 -070065 Mutex mHeapCacheLock; // Protects entire vector below.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080066 KeyedVector< wp<IBinder>, heap_info_t > mHeapCache;
Hans Boehm7e202972016-07-12 18:05:49 -070067 // We do not use the copy-on-write capabilities of KeyedVector.
68 // TODO: Reimplemement based on standard C++ container?
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080069};
70
71static sp<HeapCache> gHeapCache = new HeapCache();
72
73/******************************************************************************/
74
75enum {
76 HEAP_ID = IBinder::FIRST_CALL_TRANSACTION
77};
78
79class BpMemoryHeap : public BpInterface<IMemoryHeap>
80{
81public:
Chih-Hung Hsiehe2347b72016-04-25 15:41:05 -070082 explicit BpMemoryHeap(const sp<IBinder>& impl);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080083 virtual ~BpMemoryHeap();
84
85 virtual int getHeapID() const;
86 virtual void* getBase() const;
87 virtual size_t getSize() const;
88 virtual uint32_t getFlags() const;
Andy Hung5b028522018-10-22 15:29:49 -070089 off_t getOffset() const override;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080090
91private:
92 friend class IMemory;
93 friend class HeapCache;
Anu Sundararajan5728a922011-06-22 15:58:59 -050094
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080095 // for debugging in this module
96 static inline sp<IMemoryHeap> find_heap(const sp<IBinder>& binder) {
97 return gHeapCache->find_heap(binder);
98 }
99 static inline void free_heap(const sp<IBinder>& binder) {
100 gHeapCache->free_heap(binder);
101 }
102 static inline sp<IMemoryHeap> get_heap(const sp<IBinder>& binder) {
103 return gHeapCache->get_heap(binder);
104 }
105 static inline void dump_heaps() {
Anu Sundararajan5728a922011-06-22 15:58:59 -0500106 gHeapCache->dump_heaps();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800107 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800108
109 void assertMapped() const;
110 void assertReallyMapped() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800111
Hans Boehm7e202972016-07-12 18:05:49 -0700112 mutable std::atomic<int32_t> mHeapId;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800113 mutable void* mBase;
114 mutable size_t mSize;
115 mutable uint32_t mFlags;
Andy Hung5b028522018-10-22 15:29:49 -0700116 mutable off_t mOffset;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800117 mutable bool mRealHeap;
118 mutable Mutex mLock;
119};
120
121// ----------------------------------------------------------------------------
122
123enum {
124 GET_MEMORY = IBinder::FIRST_CALL_TRANSACTION
125};
126
127class BpMemory : public BpInterface<IMemory>
128{
129public:
Chih-Hung Hsiehe2347b72016-04-25 15:41:05 -0700130 explicit BpMemory(const sp<IBinder>& impl);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800131 virtual ~BpMemory();
Jiyong Parkb86c8662018-10-29 23:01:57 +0900132 // NOLINTNEXTLINE(google-default-arguments)
Yi Kongfdd8da92018-06-07 17:52:27 -0700133 virtual sp<IMemoryHeap> getMemory(ssize_t* offset=nullptr, size_t* size=nullptr) const;
Anu Sundararajan5728a922011-06-22 15:58:59 -0500134
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800135private:
136 mutable sp<IMemoryHeap> mHeap;
137 mutable ssize_t mOffset;
138 mutable size_t mSize;
139};
140
141/******************************************************************************/
142
143void* IMemory::fastPointer(const sp<IBinder>& binder, ssize_t offset) const
144{
145 sp<IMemoryHeap> realHeap = BpMemoryHeap::get_heap(binder);
146 void* const base = realHeap->base();
147 if (base == MAP_FAILED)
Yi Kongfdd8da92018-06-07 17:52:27 -0700148 return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800149 return static_cast<char*>(base) + offset;
150}
151
Ytai Ben-Tsviacb72992019-09-05 15:14:30 -0700152void* IMemory::unsecurePointer() const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800153 ssize_t offset;
154 sp<IMemoryHeap> heap = getMemory(&offset);
Yi Kongfdd8da92018-06-07 17:52:27 -0700155 void* const base = heap!=nullptr ? heap->base() : MAP_FAILED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800156 if (base == MAP_FAILED)
Yi Kongfdd8da92018-06-07 17:52:27 -0700157 return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800158 return static_cast<char*>(base) + offset;
159}
160
Ytai Ben-Tsviacb72992019-09-05 15:14:30 -0700161void* IMemory::pointer() const { return unsecurePointer(); }
162
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800163size_t IMemory::size() const {
164 size_t size;
Yi Kongfdd8da92018-06-07 17:52:27 -0700165 getMemory(nullptr, &size);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800166 return size;
167}
168
169ssize_t IMemory::offset() const {
170 ssize_t offset;
171 getMemory(&offset);
172 return offset;
173}
174
175/******************************************************************************/
176
177BpMemory::BpMemory(const sp<IBinder>& impl)
178 : BpInterface<IMemory>(impl), mOffset(0), mSize(0)
179{
180}
181
182BpMemory::~BpMemory()
183{
184}
185
Jiyong Parkb86c8662018-10-29 23:01:57 +0900186// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800187sp<IMemoryHeap> BpMemory::getMemory(ssize_t* offset, size_t* size) const
188{
Yi Kongfdd8da92018-06-07 17:52:27 -0700189 if (mHeap == nullptr) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800190 Parcel data, reply;
191 data.writeInterfaceToken(IMemory::getInterfaceDescriptor());
192 if (remote()->transact(GET_MEMORY, data, &reply) == NO_ERROR) {
193 sp<IBinder> heap = reply.readStrongBinder();
Yi Kongfdd8da92018-06-07 17:52:27 -0700194 if (heap != nullptr) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800195 mHeap = interface_cast<IMemoryHeap>(heap);
Yi Kongfdd8da92018-06-07 17:52:27 -0700196 if (mHeap != nullptr) {
Andy Hung5b028522018-10-22 15:29:49 -0700197 const int64_t offset64 = reply.readInt64();
198 const uint64_t size64 = reply.readUint64();
199 const ssize_t o = (ssize_t)offset64;
200 const size_t s = (size_t)size64;
Christopher Tate94b0d4e2016-02-05 19:02:56 -0800201 size_t heapSize = mHeap->getSize();
Andy Hung5b028522018-10-22 15:29:49 -0700202 if (s == size64 && o == offset64 // ILP32 bounds check
203 && s <= heapSize
Christopher Tate94b0d4e2016-02-05 19:02:56 -0800204 && o >= 0
205 && (static_cast<size_t>(o) <= heapSize - s)) {
206 mOffset = o;
207 mSize = s;
208 } else {
209 // Hm.
210 android_errorWriteWithInfoLog(0x534e4554,
Yi Kongfdd8da92018-06-07 17:52:27 -0700211 "26877992", -1, nullptr, 0);
Christopher Tate94b0d4e2016-02-05 19:02:56 -0800212 mOffset = 0;
213 mSize = 0;
214 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800215 }
216 }
217 }
218 }
219 if (offset) *offset = mOffset;
220 if (size) *size = mSize;
Yi Kongfdd8da92018-06-07 17:52:27 -0700221 return (mSize > 0) ? mHeap : nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800222}
223
224// ---------------------------------------------------------------------------
225
226IMPLEMENT_META_INTERFACE(Memory, "android.utils.IMemory");
227
Mathias Agopian83c04462009-05-22 19:00:22 -0700228BnMemory::BnMemory() {
229}
230
Anu Sundararajan5728a922011-06-22 15:58:59 -0500231BnMemory::~BnMemory() {
Mathias Agopian83c04462009-05-22 19:00:22 -0700232}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800233
Jiyong Parkb86c8662018-10-29 23:01:57 +0900234// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800235status_t BnMemory::onTransact(
236 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
237{
238 switch(code) {
239 case GET_MEMORY: {
240 CHECK_INTERFACE(IMemory, data, reply);
241 ssize_t offset;
242 size_t size;
Marco Nelissen097ca272014-11-14 08:01:01 -0800243 reply->writeStrongBinder( IInterface::asBinder(getMemory(&offset, &size)) );
Andy Hung5b028522018-10-22 15:29:49 -0700244 reply->writeInt64(offset);
245 reply->writeUint64(size);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800246 return NO_ERROR;
247 } break;
248 default:
249 return BBinder::onTransact(code, data, reply, flags);
250 }
251}
252
253
254/******************************************************************************/
255
256BpMemoryHeap::BpMemoryHeap(const sp<IBinder>& impl)
257 : BpInterface<IMemoryHeap>(impl),
Anu Sundararajan5728a922011-06-22 15:58:59 -0500258 mHeapId(-1), mBase(MAP_FAILED), mSize(0), mFlags(0), mOffset(0), mRealHeap(false)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800259{
260}
261
262BpMemoryHeap::~BpMemoryHeap() {
Hans Boehm7e202972016-07-12 18:05:49 -0700263 int32_t heapId = mHeapId.load(memory_order_relaxed);
264 if (heapId != -1) {
265 close(heapId);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800266 if (mRealHeap) {
267 // by construction we're the last one
268 if (mBase != MAP_FAILED) {
Marco Nelissen097ca272014-11-14 08:01:01 -0800269 sp<IBinder> binder = IInterface::asBinder(this);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800270
271 if (VERBOSE) {
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800272 ALOGD("UNMAPPING binder=%p, heap=%p, size=%zu, fd=%d",
Hans Boehm7e202972016-07-12 18:05:49 -0700273 binder.get(), this, mSize, heapId);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800274 }
275
276 munmap(mBase, mSize);
277 }
278 } else {
279 // remove from list only if it was mapped before
Marco Nelissen097ca272014-11-14 08:01:01 -0800280 sp<IBinder> binder = IInterface::asBinder(this);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800281 free_heap(binder);
282 }
283 }
284}
285
286void BpMemoryHeap::assertMapped() const
287{
Hans Boehm7e202972016-07-12 18:05:49 -0700288 int32_t heapId = mHeapId.load(memory_order_acquire);
289 if (heapId == -1) {
Marco Nelissen097ca272014-11-14 08:01:01 -0800290 sp<IBinder> binder(IInterface::asBinder(const_cast<BpMemoryHeap*>(this)));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800291 sp<BpMemoryHeap> heap(static_cast<BpMemoryHeap*>(find_heap(binder).get()));
292 heap->assertReallyMapped();
293 if (heap->mBase != MAP_FAILED) {
294 Mutex::Autolock _l(mLock);
Hans Boehm7e202972016-07-12 18:05:49 -0700295 if (mHeapId.load(memory_order_relaxed) == -1) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800296 mBase = heap->mBase;
297 mSize = heap->mSize;
Anu Sundararajan5728a922011-06-22 15:58:59 -0500298 mOffset = heap->mOffset;
Nick Kralevichec9ec7d2016-12-17 19:47:27 -0800299 int fd = fcntl(heap->mHeapId.load(memory_order_relaxed), F_DUPFD_CLOEXEC, 0);
Hans Boehm7e202972016-07-12 18:05:49 -0700300 ALOGE_IF(fd==-1, "cannot dup fd=%d",
301 heap->mHeapId.load(memory_order_relaxed));
302 mHeapId.store(fd, memory_order_release);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800303 }
304 } else {
305 // something went wrong
306 free_heap(binder);
307 }
308 }
309}
310
311void BpMemoryHeap::assertReallyMapped() const
312{
Hans Boehm7e202972016-07-12 18:05:49 -0700313 int32_t heapId = mHeapId.load(memory_order_acquire);
314 if (heapId == -1) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800315
316 // remote call without mLock held, worse case scenario, we end up
317 // calling transact() from multiple threads, but that's not a problem,
318 // only mmap below must be in the critical section.
Anu Sundararajan5728a922011-06-22 15:58:59 -0500319
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800320 Parcel data, reply;
321 data.writeInterfaceToken(IMemoryHeap::getInterfaceDescriptor());
322 status_t err = remote()->transact(HEAP_ID, data, &reply);
323 int parcel_fd = reply.readFileDescriptor();
Andy Hung5b028522018-10-22 15:29:49 -0700324 const uint64_t size64 = reply.readUint64();
325 const int64_t offset64 = reply.readInt64();
326 const uint32_t flags = reply.readUint32();
327 const size_t size = (size_t)size64;
328 const off_t offset = (off_t)offset64;
329 if (err != NO_ERROR || // failed transaction
330 size != size64 || offset != offset64) { // ILP32 size check
331 ALOGE("binder=%p transaction failed fd=%d, size=%zu, err=%d (%s)",
332 IInterface::asBinder(this).get(),
333 parcel_fd, size, err, strerror(-err));
334 return;
335 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800336
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800337 Mutex::Autolock _l(mLock);
Hans Boehm7e202972016-07-12 18:05:49 -0700338 if (mHeapId.load(memory_order_relaxed) == -1) {
Nick Kralevichec9ec7d2016-12-17 19:47:27 -0800339 int fd = fcntl(parcel_fd, F_DUPFD_CLOEXEC, 0);
Andy Hung5b028522018-10-22 15:29:49 -0700340 ALOGE_IF(fd == -1, "cannot dup fd=%d, size=%zu, err=%d (%s)",
John Eckerdal6b0b0632016-04-21 15:04:14 +0200341 parcel_fd, size, err, strerror(errno));
342
343 int access = PROT_READ;
344 if (!(flags & READ_ONLY)) {
345 access |= PROT_WRITE;
346 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800347 mRealHeap = true;
Yi Kongfdd8da92018-06-07 17:52:27 -0700348 mBase = mmap(nullptr, size, access, MAP_SHARED, fd, offset);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800349 if (mBase == MAP_FAILED) {
Andy Hung5b028522018-10-22 15:29:49 -0700350 ALOGE("cannot map BpMemoryHeap (binder=%p), size=%zu, fd=%d (%s)",
Marco Nelissen097ca272014-11-14 08:01:01 -0800351 IInterface::asBinder(this).get(), size, fd, strerror(errno));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800352 close(fd);
353 } else {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800354 mSize = size;
355 mFlags = flags;
Anu Sundararajan5728a922011-06-22 15:58:59 -0500356 mOffset = offset;
Hans Boehm7e202972016-07-12 18:05:49 -0700357 mHeapId.store(fd, memory_order_release);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800358 }
359 }
360 }
361}
362
363int BpMemoryHeap::getHeapID() const {
364 assertMapped();
Hans Boehm7e202972016-07-12 18:05:49 -0700365 // We either stored mHeapId ourselves, or loaded it with acquire semantics.
366 return mHeapId.load(memory_order_relaxed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800367}
368
369void* BpMemoryHeap::getBase() const {
370 assertMapped();
371 return mBase;
372}
373
374size_t BpMemoryHeap::getSize() const {
375 assertMapped();
376 return mSize;
377}
378
379uint32_t BpMemoryHeap::getFlags() const {
380 assertMapped();
381 return mFlags;
382}
383
Andy Hung5b028522018-10-22 15:29:49 -0700384off_t BpMemoryHeap::getOffset() const {
Anu Sundararajan5728a922011-06-22 15:58:59 -0500385 assertMapped();
386 return mOffset;
387}
388
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800389// ---------------------------------------------------------------------------
390
391IMPLEMENT_META_INTERFACE(MemoryHeap, "android.utils.IMemoryHeap");
392
Anu Sundararajan5728a922011-06-22 15:58:59 -0500393BnMemoryHeap::BnMemoryHeap() {
Mathias Agopian83c04462009-05-22 19:00:22 -0700394}
395
Anu Sundararajan5728a922011-06-22 15:58:59 -0500396BnMemoryHeap::~BnMemoryHeap() {
Mathias Agopian83c04462009-05-22 19:00:22 -0700397}
398
Jiyong Parkb86c8662018-10-29 23:01:57 +0900399// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800400status_t BnMemoryHeap::onTransact(
Mathias Agopian83c04462009-05-22 19:00:22 -0700401 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800402{
403 switch(code) {
404 case HEAP_ID: {
405 CHECK_INTERFACE(IMemoryHeap, data, reply);
406 reply->writeFileDescriptor(getHeapID());
Andy Hung5b028522018-10-22 15:29:49 -0700407 reply->writeUint64(getSize());
408 reply->writeInt64(getOffset());
409 reply->writeUint32(getFlags());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800410 return NO_ERROR;
411 } break;
412 default:
413 return BBinder::onTransact(code, data, reply, flags);
414 }
415}
416
417/*****************************************************************************/
418
419HeapCache::HeapCache()
420 : DeathRecipient()
421{
422}
423
424HeapCache::~HeapCache()
425{
426}
427
428void HeapCache::binderDied(const wp<IBinder>& binder)
429{
Steve Block9d453682011-12-20 16:23:08 +0000430 //ALOGD("binderDied binder=%p", binder.unsafe_get());
Anu Sundararajan5728a922011-06-22 15:58:59 -0500431 free_heap(binder);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800432}
433
Anu Sundararajan5728a922011-06-22 15:58:59 -0500434sp<IMemoryHeap> HeapCache::find_heap(const sp<IBinder>& binder)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800435{
436 Mutex::Autolock _l(mHeapCacheLock);
437 ssize_t i = mHeapCache.indexOfKey(binder);
438 if (i>=0) {
439 heap_info_t& info = mHeapCache.editValueAt(i);
Steve Block9d453682011-12-20 16:23:08 +0000440 ALOGD_IF(VERBOSE,
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800441 "found binder=%p, heap=%p, size=%zu, fd=%d, count=%d",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800442 binder.get(), info.heap.get(),
443 static_cast<BpMemoryHeap*>(info.heap.get())->mSize,
Hans Boehm7e202972016-07-12 18:05:49 -0700444 static_cast<BpMemoryHeap*>(info.heap.get())
445 ->mHeapId.load(memory_order_relaxed),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800446 info.count);
Hans Boehm7e202972016-07-12 18:05:49 -0700447 ++info.count;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800448 return info.heap;
449 } else {
450 heap_info_t info;
451 info.heap = interface_cast<IMemoryHeap>(binder);
452 info.count = 1;
Steve Block9d453682011-12-20 16:23:08 +0000453 //ALOGD("adding binder=%p, heap=%p, count=%d",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800454 // binder.get(), info.heap.get(), info.count);
455 mHeapCache.add(binder, info);
456 return info.heap;
457 }
458}
459
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800460void HeapCache::free_heap(const sp<IBinder>& binder) {
461 free_heap( wp<IBinder>(binder) );
462}
463
Anu Sundararajan5728a922011-06-22 15:58:59 -0500464void HeapCache::free_heap(const wp<IBinder>& binder)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800465{
466 sp<IMemoryHeap> rel;
467 {
468 Mutex::Autolock _l(mHeapCacheLock);
469 ssize_t i = mHeapCache.indexOfKey(binder);
470 if (i>=0) {
471 heap_info_t& info(mHeapCache.editValueAt(i));
Hans Boehm7e202972016-07-12 18:05:49 -0700472 if (--info.count == 0) {
Steve Block9d453682011-12-20 16:23:08 +0000473 ALOGD_IF(VERBOSE,
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800474 "removing binder=%p, heap=%p, size=%zu, fd=%d, count=%d",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800475 binder.unsafe_get(), info.heap.get(),
476 static_cast<BpMemoryHeap*>(info.heap.get())->mSize,
Hans Boehm7e202972016-07-12 18:05:49 -0700477 static_cast<BpMemoryHeap*>(info.heap.get())
478 ->mHeapId.load(memory_order_relaxed),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800479 info.count);
480 rel = mHeapCache.valueAt(i).heap;
481 mHeapCache.removeItemsAt(i);
482 }
483 } else {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000484 ALOGE("free_heap binder=%p not found!!!", binder.unsafe_get());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800485 }
486 }
487}
488
489sp<IMemoryHeap> HeapCache::get_heap(const sp<IBinder>& binder)
490{
491 sp<IMemoryHeap> realHeap;
492 Mutex::Autolock _l(mHeapCacheLock);
493 ssize_t i = mHeapCache.indexOfKey(binder);
494 if (i>=0) realHeap = mHeapCache.valueAt(i).heap;
495 else realHeap = interface_cast<IMemoryHeap>(binder);
496 return realHeap;
497}
498
Anu Sundararajan5728a922011-06-22 15:58:59 -0500499void HeapCache::dump_heaps()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800500{
501 Mutex::Autolock _l(mHeapCacheLock);
502 int c = mHeapCache.size();
503 for (int i=0 ; i<c ; i++) {
504 const heap_info_t& info = mHeapCache.valueAt(i);
505 BpMemoryHeap const* h(static_cast<BpMemoryHeap const *>(info.heap.get()));
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800506 ALOGD("hey=%p, heap=%p, count=%d, (fd=%d, base=%p, size=%zu)",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800507 mHeapCache.keyAt(i).unsafe_get(),
Anu Sundararajan5728a922011-06-22 15:58:59 -0500508 info.heap.get(), info.count,
Hans Boehm7e202972016-07-12 18:05:49 -0700509 h->mHeapId.load(memory_order_relaxed), h->mBase, h->mSize);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800510 }
511}
512
513
514// ---------------------------------------------------------------------------
Steven Moreland6511af52019-09-26 16:05:45 -0700515} // namespace android