blob: 790fa8c9319b7f660e6fff0e2f8287afc5535852 [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 Boehm4ce4db72016-07-12 18:05:49 -070019#include <atomic>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080020#include <stdint.h>
21#include <stdio.h>
22#include <stdlib.h>
23#include <fcntl.h>
24#include <unistd.h>
25
26#include <sys/types.h>
27#include <sys/mman.h>
28
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070029#include <binder/IMemory.h>
Christopher Tate94b0d4e2016-02-05 19:02:56 -080030#include <cutils/log.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080031#include <utils/KeyedVector.h>
32#include <utils/threads.h>
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070033#include <binder/Parcel.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080034#include <utils/CallStack.h>
35
36#define VERBOSE 0
37
38namespace android {
39// ---------------------------------------------------------------------------
40
41class HeapCache : public IBinder::DeathRecipient
42{
43public:
44 HeapCache();
45 virtual ~HeapCache();
Anu Sundararajan5728a922011-06-22 15:58:59 -050046
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080047 virtual void binderDied(const wp<IBinder>& who);
48
Anu Sundararajan5728a922011-06-22 15:58:59 -050049 sp<IMemoryHeap> find_heap(const sp<IBinder>& binder);
50 void free_heap(const sp<IBinder>& binder);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080051 sp<IMemoryHeap> get_heap(const sp<IBinder>& binder);
52 void dump_heaps();
53
54private:
55 // For IMemory.cpp
56 struct heap_info_t {
57 sp<IMemoryHeap> heap;
58 int32_t count;
Hans Boehm4ce4db72016-07-12 18:05:49 -070059 // Note that this cannot be meaningfully copied.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080060 };
61
Anu Sundararajan5728a922011-06-22 15:58:59 -050062 void free_heap(const wp<IBinder>& binder);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080063
Hans Boehm4ce4db72016-07-12 18:05:49 -070064 Mutex mHeapCacheLock; // Protects entire vector below.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080065 KeyedVector< wp<IBinder>, heap_info_t > mHeapCache;
Hans Boehm4ce4db72016-07-12 18:05:49 -070066 // We do not use the copy-on-write capabilities of KeyedVector.
67 // TODO: Reimplemement based on standard C++ container?
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080068};
69
70static sp<HeapCache> gHeapCache = new HeapCache();
71
72/******************************************************************************/
73
74enum {
75 HEAP_ID = IBinder::FIRST_CALL_TRANSACTION
76};
77
78class BpMemoryHeap : public BpInterface<IMemoryHeap>
79{
80public:
Chih-Hung Hsiehe2347b72016-04-25 15:41:05 -070081 explicit BpMemoryHeap(const sp<IBinder>& impl);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080082 virtual ~BpMemoryHeap();
83
84 virtual int getHeapID() const;
85 virtual void* getBase() const;
86 virtual size_t getSize() const;
87 virtual uint32_t getFlags() const;
Anu Sundararajan5728a922011-06-22 15:58:59 -050088 virtual uint32_t getOffset() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080089
90private:
91 friend class IMemory;
92 friend class HeapCache;
Anu Sundararajan5728a922011-06-22 15:58:59 -050093
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080094 // for debugging in this module
95 static inline sp<IMemoryHeap> find_heap(const sp<IBinder>& binder) {
96 return gHeapCache->find_heap(binder);
97 }
98 static inline void free_heap(const sp<IBinder>& binder) {
99 gHeapCache->free_heap(binder);
100 }
101 static inline sp<IMemoryHeap> get_heap(const sp<IBinder>& binder) {
102 return gHeapCache->get_heap(binder);
103 }
104 static inline void dump_heaps() {
Anu Sundararajan5728a922011-06-22 15:58:59 -0500105 gHeapCache->dump_heaps();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800106 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800107
108 void assertMapped() const;
109 void assertReallyMapped() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800110
Hans Boehm4ce4db72016-07-12 18:05:49 -0700111 mutable std::atomic<int32_t> mHeapId;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800112 mutable void* mBase;
113 mutable size_t mSize;
114 mutable uint32_t mFlags;
Anu Sundararajan5728a922011-06-22 15:58:59 -0500115 mutable uint32_t mOffset;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800116 mutable bool mRealHeap;
117 mutable Mutex mLock;
118};
119
120// ----------------------------------------------------------------------------
121
122enum {
123 GET_MEMORY = IBinder::FIRST_CALL_TRANSACTION
124};
125
126class BpMemory : public BpInterface<IMemory>
127{
128public:
Chih-Hung Hsiehe2347b72016-04-25 15:41:05 -0700129 explicit BpMemory(const sp<IBinder>& impl);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800130 virtual ~BpMemory();
131 virtual sp<IMemoryHeap> getMemory(ssize_t* offset=0, size_t* size=0) const;
Anu Sundararajan5728a922011-06-22 15:58:59 -0500132
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800133private:
134 mutable sp<IMemoryHeap> mHeap;
135 mutable ssize_t mOffset;
136 mutable size_t mSize;
137};
138
139/******************************************************************************/
140
141void* IMemory::fastPointer(const sp<IBinder>& binder, ssize_t offset) const
142{
143 sp<IMemoryHeap> realHeap = BpMemoryHeap::get_heap(binder);
144 void* const base = realHeap->base();
145 if (base == MAP_FAILED)
146 return 0;
147 return static_cast<char*>(base) + offset;
148}
149
150void* IMemory::pointer() const {
151 ssize_t offset;
152 sp<IMemoryHeap> heap = getMemory(&offset);
153 void* const base = heap!=0 ? heap->base() : MAP_FAILED;
154 if (base == MAP_FAILED)
155 return 0;
156 return static_cast<char*>(base) + offset;
157}
158
159size_t IMemory::size() const {
160 size_t size;
161 getMemory(NULL, &size);
162 return size;
163}
164
165ssize_t IMemory::offset() const {
166 ssize_t offset;
167 getMemory(&offset);
168 return offset;
169}
170
171/******************************************************************************/
172
173BpMemory::BpMemory(const sp<IBinder>& impl)
174 : BpInterface<IMemory>(impl), mOffset(0), mSize(0)
175{
176}
177
178BpMemory::~BpMemory()
179{
180}
181
182sp<IMemoryHeap> BpMemory::getMemory(ssize_t* offset, size_t* size) const
183{
184 if (mHeap == 0) {
185 Parcel data, reply;
186 data.writeInterfaceToken(IMemory::getInterfaceDescriptor());
187 if (remote()->transact(GET_MEMORY, data, &reply) == NO_ERROR) {
188 sp<IBinder> heap = reply.readStrongBinder();
189 ssize_t o = reply.readInt32();
190 size_t s = reply.readInt32();
191 if (heap != 0) {
192 mHeap = interface_cast<IMemoryHeap>(heap);
193 if (mHeap != 0) {
Christopher Tate94b0d4e2016-02-05 19:02:56 -0800194 size_t heapSize = mHeap->getSize();
195 if (s <= heapSize
196 && o >= 0
197 && (static_cast<size_t>(o) <= heapSize - s)) {
198 mOffset = o;
199 mSize = s;
200 } else {
201 // Hm.
202 android_errorWriteWithInfoLog(0x534e4554,
203 "26877992", -1, NULL, 0);
204 mOffset = 0;
205 mSize = 0;
206 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800207 }
208 }
209 }
210 }
211 if (offset) *offset = mOffset;
212 if (size) *size = mSize;
Christopher Tate94b0d4e2016-02-05 19:02:56 -0800213 return (mSize > 0) ? mHeap : 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800214}
215
216// ---------------------------------------------------------------------------
217
218IMPLEMENT_META_INTERFACE(Memory, "android.utils.IMemory");
219
Mathias Agopian83c04462009-05-22 19:00:22 -0700220BnMemory::BnMemory() {
221}
222
Anu Sundararajan5728a922011-06-22 15:58:59 -0500223BnMemory::~BnMemory() {
Mathias Agopian83c04462009-05-22 19:00:22 -0700224}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800225
226status_t BnMemory::onTransact(
227 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
228{
229 switch(code) {
230 case GET_MEMORY: {
231 CHECK_INTERFACE(IMemory, data, reply);
232 ssize_t offset;
233 size_t size;
Marco Nelissen097ca272014-11-14 08:01:01 -0800234 reply->writeStrongBinder( IInterface::asBinder(getMemory(&offset, &size)) );
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800235 reply->writeInt32(offset);
236 reply->writeInt32(size);
237 return NO_ERROR;
238 } break;
239 default:
240 return BBinder::onTransact(code, data, reply, flags);
241 }
242}
243
244
245/******************************************************************************/
246
247BpMemoryHeap::BpMemoryHeap(const sp<IBinder>& impl)
248 : BpInterface<IMemoryHeap>(impl),
Anu Sundararajan5728a922011-06-22 15:58:59 -0500249 mHeapId(-1), mBase(MAP_FAILED), mSize(0), mFlags(0), mOffset(0), mRealHeap(false)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800250{
251}
252
253BpMemoryHeap::~BpMemoryHeap() {
Hans Boehm4ce4db72016-07-12 18:05:49 -0700254 int32_t heapId = mHeapId.load(memory_order_relaxed);
255 if (heapId != -1) {
256 close(heapId);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800257 if (mRealHeap) {
258 // by construction we're the last one
259 if (mBase != MAP_FAILED) {
Marco Nelissen097ca272014-11-14 08:01:01 -0800260 sp<IBinder> binder = IInterface::asBinder(this);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800261
262 if (VERBOSE) {
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800263 ALOGD("UNMAPPING binder=%p, heap=%p, size=%zu, fd=%d",
Hans Boehm4ce4db72016-07-12 18:05:49 -0700264 binder.get(), this, mSize, heapId);
Mathias Agopiancab25d62013-03-21 17:12:40 -0700265 CallStack stack(LOG_TAG);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800266 }
267
268 munmap(mBase, mSize);
269 }
270 } else {
271 // remove from list only if it was mapped before
Marco Nelissen097ca272014-11-14 08:01:01 -0800272 sp<IBinder> binder = IInterface::asBinder(this);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800273 free_heap(binder);
274 }
275 }
276}
277
278void BpMemoryHeap::assertMapped() const
279{
Hans Boehm4ce4db72016-07-12 18:05:49 -0700280 int32_t heapId = mHeapId.load(memory_order_acquire);
281 if (heapId == -1) {
Marco Nelissen097ca272014-11-14 08:01:01 -0800282 sp<IBinder> binder(IInterface::asBinder(const_cast<BpMemoryHeap*>(this)));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800283 sp<BpMemoryHeap> heap(static_cast<BpMemoryHeap*>(find_heap(binder).get()));
284 heap->assertReallyMapped();
285 if (heap->mBase != MAP_FAILED) {
286 Mutex::Autolock _l(mLock);
Hans Boehm4ce4db72016-07-12 18:05:49 -0700287 if (mHeapId.load(memory_order_relaxed) == -1) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800288 mBase = heap->mBase;
289 mSize = heap->mSize;
Anu Sundararajan5728a922011-06-22 15:58:59 -0500290 mOffset = heap->mOffset;
Hans Boehm4ce4db72016-07-12 18:05:49 -0700291 int fd = dup(heap->mHeapId.load(memory_order_relaxed));
292 ALOGE_IF(fd==-1, "cannot dup fd=%d",
293 heap->mHeapId.load(memory_order_relaxed));
294 mHeapId.store(fd, memory_order_release);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800295 }
296 } else {
297 // something went wrong
298 free_heap(binder);
299 }
300 }
301}
302
303void BpMemoryHeap::assertReallyMapped() const
304{
Hans Boehm4ce4db72016-07-12 18:05:49 -0700305 int32_t heapId = mHeapId.load(memory_order_acquire);
306 if (heapId == -1) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800307
308 // remote call without mLock held, worse case scenario, we end up
309 // calling transact() from multiple threads, but that's not a problem,
310 // only mmap below must be in the critical section.
Anu Sundararajan5728a922011-06-22 15:58:59 -0500311
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800312 Parcel data, reply;
313 data.writeInterfaceToken(IMemoryHeap::getInterfaceDescriptor());
314 status_t err = remote()->transact(HEAP_ID, data, &reply);
315 int parcel_fd = reply.readFileDescriptor();
316 ssize_t size = reply.readInt32();
317 uint32_t flags = reply.readInt32();
Anu Sundararajan5728a922011-06-22 15:58:59 -0500318 uint32_t offset = reply.readInt32();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800319
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800320 ALOGE_IF(err, "binder=%p transaction failed fd=%d, size=%zd, err=%d (%s)",
Marco Nelissen097ca272014-11-14 08:01:01 -0800321 IInterface::asBinder(this).get(),
322 parcel_fd, size, err, strerror(-err));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800323
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800324 Mutex::Autolock _l(mLock);
Hans Boehm4ce4db72016-07-12 18:05:49 -0700325 if (mHeapId.load(memory_order_relaxed) == -1) {
John Eckerdal6b0b0632016-04-21 15:04:14 +0200326 int fd = dup( parcel_fd );
327 ALOGE_IF(fd==-1, "cannot dup fd=%d, size=%zd, err=%d (%s)",
328 parcel_fd, size, err, strerror(errno));
329
330 int access = PROT_READ;
331 if (!(flags & READ_ONLY)) {
332 access |= PROT_WRITE;
333 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800334 mRealHeap = true;
Anu Sundararajan5728a922011-06-22 15:58:59 -0500335 mBase = mmap(0, size, access, MAP_SHARED, fd, offset);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800336 if (mBase == MAP_FAILED) {
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800337 ALOGE("cannot map BpMemoryHeap (binder=%p), size=%zd, fd=%d (%s)",
Marco Nelissen097ca272014-11-14 08:01:01 -0800338 IInterface::asBinder(this).get(), size, fd, strerror(errno));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800339 close(fd);
340 } else {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800341 mSize = size;
342 mFlags = flags;
Anu Sundararajan5728a922011-06-22 15:58:59 -0500343 mOffset = offset;
Hans Boehm4ce4db72016-07-12 18:05:49 -0700344 mHeapId.store(fd, memory_order_release);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800345 }
346 }
347 }
348}
349
350int BpMemoryHeap::getHeapID() const {
351 assertMapped();
Hans Boehm4ce4db72016-07-12 18:05:49 -0700352 // We either stored mHeapId ourselves, or loaded it with acquire semantics.
353 return mHeapId.load(memory_order_relaxed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800354}
355
356void* BpMemoryHeap::getBase() const {
357 assertMapped();
358 return mBase;
359}
360
361size_t BpMemoryHeap::getSize() const {
362 assertMapped();
363 return mSize;
364}
365
366uint32_t BpMemoryHeap::getFlags() const {
367 assertMapped();
368 return mFlags;
369}
370
Anu Sundararajan5728a922011-06-22 15:58:59 -0500371uint32_t BpMemoryHeap::getOffset() const {
372 assertMapped();
373 return mOffset;
374}
375
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800376// ---------------------------------------------------------------------------
377
378IMPLEMENT_META_INTERFACE(MemoryHeap, "android.utils.IMemoryHeap");
379
Anu Sundararajan5728a922011-06-22 15:58:59 -0500380BnMemoryHeap::BnMemoryHeap() {
Mathias Agopian83c04462009-05-22 19:00:22 -0700381}
382
Anu Sundararajan5728a922011-06-22 15:58:59 -0500383BnMemoryHeap::~BnMemoryHeap() {
Mathias Agopian83c04462009-05-22 19:00:22 -0700384}
385
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800386status_t BnMemoryHeap::onTransact(
Mathias Agopian83c04462009-05-22 19:00:22 -0700387 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800388{
389 switch(code) {
390 case HEAP_ID: {
391 CHECK_INTERFACE(IMemoryHeap, data, reply);
392 reply->writeFileDescriptor(getHeapID());
393 reply->writeInt32(getSize());
394 reply->writeInt32(getFlags());
Anu Sundararajan5728a922011-06-22 15:58:59 -0500395 reply->writeInt32(getOffset());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800396 return NO_ERROR;
397 } break;
398 default:
399 return BBinder::onTransact(code, data, reply, flags);
400 }
401}
402
403/*****************************************************************************/
404
405HeapCache::HeapCache()
406 : DeathRecipient()
407{
408}
409
410HeapCache::~HeapCache()
411{
412}
413
414void HeapCache::binderDied(const wp<IBinder>& binder)
415{
Steve Block9d453682011-12-20 16:23:08 +0000416 //ALOGD("binderDied binder=%p", binder.unsafe_get());
Anu Sundararajan5728a922011-06-22 15:58:59 -0500417 free_heap(binder);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800418}
419
Anu Sundararajan5728a922011-06-22 15:58:59 -0500420sp<IMemoryHeap> HeapCache::find_heap(const sp<IBinder>& binder)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800421{
422 Mutex::Autolock _l(mHeapCacheLock);
423 ssize_t i = mHeapCache.indexOfKey(binder);
424 if (i>=0) {
425 heap_info_t& info = mHeapCache.editValueAt(i);
Steve Block9d453682011-12-20 16:23:08 +0000426 ALOGD_IF(VERBOSE,
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800427 "found binder=%p, heap=%p, size=%zu, fd=%d, count=%d",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800428 binder.get(), info.heap.get(),
429 static_cast<BpMemoryHeap*>(info.heap.get())->mSize,
Hans Boehm4ce4db72016-07-12 18:05:49 -0700430 static_cast<BpMemoryHeap*>(info.heap.get())
431 ->mHeapId.load(memory_order_relaxed),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800432 info.count);
Hans Boehm4ce4db72016-07-12 18:05:49 -0700433 ++info.count;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800434 return info.heap;
435 } else {
436 heap_info_t info;
437 info.heap = interface_cast<IMemoryHeap>(binder);
438 info.count = 1;
Steve Block9d453682011-12-20 16:23:08 +0000439 //ALOGD("adding binder=%p, heap=%p, count=%d",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800440 // binder.get(), info.heap.get(), info.count);
441 mHeapCache.add(binder, info);
442 return info.heap;
443 }
444}
445
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800446void HeapCache::free_heap(const sp<IBinder>& binder) {
447 free_heap( wp<IBinder>(binder) );
448}
449
Anu Sundararajan5728a922011-06-22 15:58:59 -0500450void HeapCache::free_heap(const wp<IBinder>& binder)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800451{
452 sp<IMemoryHeap> rel;
453 {
454 Mutex::Autolock _l(mHeapCacheLock);
455 ssize_t i = mHeapCache.indexOfKey(binder);
456 if (i>=0) {
457 heap_info_t& info(mHeapCache.editValueAt(i));
Hans Boehm4ce4db72016-07-12 18:05:49 -0700458 if (--info.count == 0) {
Steve Block9d453682011-12-20 16:23:08 +0000459 ALOGD_IF(VERBOSE,
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800460 "removing binder=%p, heap=%p, size=%zu, fd=%d, count=%d",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800461 binder.unsafe_get(), info.heap.get(),
462 static_cast<BpMemoryHeap*>(info.heap.get())->mSize,
Hans Boehm4ce4db72016-07-12 18:05:49 -0700463 static_cast<BpMemoryHeap*>(info.heap.get())
464 ->mHeapId.load(memory_order_relaxed),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800465 info.count);
466 rel = mHeapCache.valueAt(i).heap;
467 mHeapCache.removeItemsAt(i);
468 }
469 } else {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000470 ALOGE("free_heap binder=%p not found!!!", binder.unsafe_get());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800471 }
472 }
473}
474
475sp<IMemoryHeap> HeapCache::get_heap(const sp<IBinder>& binder)
476{
477 sp<IMemoryHeap> realHeap;
478 Mutex::Autolock _l(mHeapCacheLock);
479 ssize_t i = mHeapCache.indexOfKey(binder);
480 if (i>=0) realHeap = mHeapCache.valueAt(i).heap;
481 else realHeap = interface_cast<IMemoryHeap>(binder);
482 return realHeap;
483}
484
Anu Sundararajan5728a922011-06-22 15:58:59 -0500485void HeapCache::dump_heaps()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800486{
487 Mutex::Autolock _l(mHeapCacheLock);
488 int c = mHeapCache.size();
489 for (int i=0 ; i<c ; i++) {
490 const heap_info_t& info = mHeapCache.valueAt(i);
491 BpMemoryHeap const* h(static_cast<BpMemoryHeap const *>(info.heap.get()));
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800492 ALOGD("hey=%p, heap=%p, count=%d, (fd=%d, base=%p, size=%zu)",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800493 mHeapCache.keyAt(i).unsafe_get(),
Anu Sundararajan5728a922011-06-22 15:58:59 -0500494 info.heap.get(), info.count,
Hans Boehm4ce4db72016-07-12 18:05:49 -0700495 h->mHeapId.load(memory_order_relaxed), h->mBase, h->mSize);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800496 }
497}
498
499
500// ---------------------------------------------------------------------------
501}; // namespace android