blob: 6b5b1afc00890c5d7c4fe65b37593a066928a04e [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>
Mark Salyzyna5e161b2016-09-29 08:08:05 -070020#include <fcntl.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080021#include <stdint.h>
22#include <stdio.h>
23#include <stdlib.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080024#include <sys/types.h>
25#include <sys/mman.h>
Mark Salyzyna5e161b2016-09-29 08:08:05 -070026#include <unistd.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080027
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070028#include <binder/IMemory.h>
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070029#include <binder/Parcel.h>
Mark Salyzyn7823e122016-09-29 08:08:05 -070030#include <log/log.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080031#include <utils/CallStack.h>
Mark Salyzyna5e161b2016-09-29 08:08:05 -070032#include <utils/KeyedVector.h>
33#include <utils/threads.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080034
35#define VERBOSE 0
36
37namespace android {
38// ---------------------------------------------------------------------------
39
40class HeapCache : public IBinder::DeathRecipient
41{
42public:
43 HeapCache();
44 virtual ~HeapCache();
Anu Sundararajan5728a922011-06-22 15:58:59 -050045
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080046 virtual void binderDied(const wp<IBinder>& who);
47
Anu Sundararajan5728a922011-06-22 15:58:59 -050048 sp<IMemoryHeap> find_heap(const sp<IBinder>& binder);
49 void free_heap(const sp<IBinder>& binder);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080050 sp<IMemoryHeap> get_heap(const sp<IBinder>& binder);
51 void dump_heaps();
52
53private:
54 // For IMemory.cpp
55 struct heap_info_t {
56 sp<IMemoryHeap> heap;
57 int32_t count;
Hans Boehm4ce4db72016-07-12 18:05:49 -070058 // Note that this cannot be meaningfully copied.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080059 };
60
Anu Sundararajan5728a922011-06-22 15:58:59 -050061 void free_heap(const wp<IBinder>& binder);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080062
Hans Boehm4ce4db72016-07-12 18:05:49 -070063 Mutex mHeapCacheLock; // Protects entire vector below.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080064 KeyedVector< wp<IBinder>, heap_info_t > mHeapCache;
Hans Boehm4ce4db72016-07-12 18:05:49 -070065 // We do not use the copy-on-write capabilities of KeyedVector.
66 // TODO: Reimplemement based on standard C++ container?
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080067};
68
69static sp<HeapCache> gHeapCache = new HeapCache();
70
71/******************************************************************************/
72
73enum {
74 HEAP_ID = IBinder::FIRST_CALL_TRANSACTION
75};
76
77class BpMemoryHeap : public BpInterface<IMemoryHeap>
78{
79public:
Chih-Hung Hsiehe2347b72016-04-25 15:41:05 -070080 explicit BpMemoryHeap(const sp<IBinder>& impl);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080081 virtual ~BpMemoryHeap();
82
83 virtual int getHeapID() const;
84 virtual void* getBase() const;
85 virtual size_t getSize() const;
86 virtual uint32_t getFlags() const;
Anu Sundararajan5728a922011-06-22 15:58:59 -050087 virtual uint32_t getOffset() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080088
89private:
90 friend class IMemory;
91 friend class HeapCache;
Anu Sundararajan5728a922011-06-22 15:58:59 -050092
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080093 // for debugging in this module
94 static inline sp<IMemoryHeap> find_heap(const sp<IBinder>& binder) {
95 return gHeapCache->find_heap(binder);
96 }
97 static inline void free_heap(const sp<IBinder>& binder) {
98 gHeapCache->free_heap(binder);
99 }
100 static inline sp<IMemoryHeap> get_heap(const sp<IBinder>& binder) {
101 return gHeapCache->get_heap(binder);
102 }
103 static inline void dump_heaps() {
Anu Sundararajan5728a922011-06-22 15:58:59 -0500104 gHeapCache->dump_heaps();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800105 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800106
107 void assertMapped() const;
108 void assertReallyMapped() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800109
Hans Boehm4ce4db72016-07-12 18:05:49 -0700110 mutable std::atomic<int32_t> mHeapId;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800111 mutable void* mBase;
112 mutable size_t mSize;
113 mutable uint32_t mFlags;
Anu Sundararajan5728a922011-06-22 15:58:59 -0500114 mutable uint32_t mOffset;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800115 mutable bool mRealHeap;
116 mutable Mutex mLock;
117};
118
119// ----------------------------------------------------------------------------
120
121enum {
122 GET_MEMORY = IBinder::FIRST_CALL_TRANSACTION
123};
124
125class BpMemory : public BpInterface<IMemory>
126{
127public:
Chih-Hung Hsiehe2347b72016-04-25 15:41:05 -0700128 explicit BpMemory(const sp<IBinder>& impl);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800129 virtual ~BpMemory();
130 virtual sp<IMemoryHeap> getMemory(ssize_t* offset=0, size_t* size=0) const;
Anu Sundararajan5728a922011-06-22 15:58:59 -0500131
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800132private:
133 mutable sp<IMemoryHeap> mHeap;
134 mutable ssize_t mOffset;
135 mutable size_t mSize;
136};
137
138/******************************************************************************/
139
140void* IMemory::fastPointer(const sp<IBinder>& binder, ssize_t offset) const
141{
142 sp<IMemoryHeap> realHeap = BpMemoryHeap::get_heap(binder);
143 void* const base = realHeap->base();
144 if (base == MAP_FAILED)
145 return 0;
146 return static_cast<char*>(base) + offset;
147}
148
149void* IMemory::pointer() const {
150 ssize_t offset;
151 sp<IMemoryHeap> heap = getMemory(&offset);
152 void* const base = heap!=0 ? heap->base() : MAP_FAILED;
153 if (base == MAP_FAILED)
154 return 0;
155 return static_cast<char*>(base) + offset;
156}
157
158size_t IMemory::size() const {
159 size_t size;
160 getMemory(NULL, &size);
161 return size;
162}
163
164ssize_t IMemory::offset() const {
165 ssize_t offset;
166 getMemory(&offset);
167 return offset;
168}
169
170/******************************************************************************/
171
172BpMemory::BpMemory(const sp<IBinder>& impl)
173 : BpInterface<IMemory>(impl), mOffset(0), mSize(0)
174{
175}
176
177BpMemory::~BpMemory()
178{
179}
180
181sp<IMemoryHeap> BpMemory::getMemory(ssize_t* offset, size_t* size) const
182{
183 if (mHeap == 0) {
184 Parcel data, reply;
185 data.writeInterfaceToken(IMemory::getInterfaceDescriptor());
186 if (remote()->transact(GET_MEMORY, data, &reply) == NO_ERROR) {
187 sp<IBinder> heap = reply.readStrongBinder();
188 ssize_t o = reply.readInt32();
189 size_t s = reply.readInt32();
190 if (heap != 0) {
191 mHeap = interface_cast<IMemoryHeap>(heap);
192 if (mHeap != 0) {
Christopher Tate94b0d4e2016-02-05 19:02:56 -0800193 size_t heapSize = mHeap->getSize();
194 if (s <= heapSize
195 && o >= 0
196 && (static_cast<size_t>(o) <= heapSize - s)) {
197 mOffset = o;
198 mSize = s;
199 } else {
200 // Hm.
201 android_errorWriteWithInfoLog(0x534e4554,
202 "26877992", -1, NULL, 0);
203 mOffset = 0;
204 mSize = 0;
205 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800206 }
207 }
208 }
209 }
210 if (offset) *offset = mOffset;
211 if (size) *size = mSize;
Christopher Tate94b0d4e2016-02-05 19:02:56 -0800212 return (mSize > 0) ? mHeap : 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800213}
214
215// ---------------------------------------------------------------------------
216
217IMPLEMENT_META_INTERFACE(Memory, "android.utils.IMemory");
218
Mathias Agopian83c04462009-05-22 19:00:22 -0700219BnMemory::BnMemory() {
220}
221
Anu Sundararajan5728a922011-06-22 15:58:59 -0500222BnMemory::~BnMemory() {
Mathias Agopian83c04462009-05-22 19:00:22 -0700223}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800224
225status_t BnMemory::onTransact(
226 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
227{
228 switch(code) {
229 case GET_MEMORY: {
230 CHECK_INTERFACE(IMemory, data, reply);
231 ssize_t offset;
232 size_t size;
Marco Nelissen097ca272014-11-14 08:01:01 -0800233 reply->writeStrongBinder( IInterface::asBinder(getMemory(&offset, &size)) );
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800234 reply->writeInt32(offset);
235 reply->writeInt32(size);
236 return NO_ERROR;
237 } break;
238 default:
239 return BBinder::onTransact(code, data, reply, flags);
240 }
241}
242
243
244/******************************************************************************/
245
246BpMemoryHeap::BpMemoryHeap(const sp<IBinder>& impl)
247 : BpInterface<IMemoryHeap>(impl),
Anu Sundararajan5728a922011-06-22 15:58:59 -0500248 mHeapId(-1), mBase(MAP_FAILED), mSize(0), mFlags(0), mOffset(0), mRealHeap(false)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800249{
250}
251
252BpMemoryHeap::~BpMemoryHeap() {
Hans Boehm4ce4db72016-07-12 18:05:49 -0700253 int32_t heapId = mHeapId.load(memory_order_relaxed);
254 if (heapId != -1) {
255 close(heapId);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800256 if (mRealHeap) {
257 // by construction we're the last one
258 if (mBase != MAP_FAILED) {
Marco Nelissen097ca272014-11-14 08:01:01 -0800259 sp<IBinder> binder = IInterface::asBinder(this);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800260
261 if (VERBOSE) {
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800262 ALOGD("UNMAPPING binder=%p, heap=%p, size=%zu, fd=%d",
Hans Boehm4ce4db72016-07-12 18:05:49 -0700263 binder.get(), this, mSize, heapId);
Mathias Agopiancab25d62013-03-21 17:12:40 -0700264 CallStack stack(LOG_TAG);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800265 }
266
267 munmap(mBase, mSize);
268 }
269 } else {
270 // remove from list only if it was mapped before
Marco Nelissen097ca272014-11-14 08:01:01 -0800271 sp<IBinder> binder = IInterface::asBinder(this);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800272 free_heap(binder);
273 }
274 }
275}
276
277void BpMemoryHeap::assertMapped() const
278{
Hans Boehm4ce4db72016-07-12 18:05:49 -0700279 int32_t heapId = mHeapId.load(memory_order_acquire);
280 if (heapId == -1) {
Marco Nelissen097ca272014-11-14 08:01:01 -0800281 sp<IBinder> binder(IInterface::asBinder(const_cast<BpMemoryHeap*>(this)));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800282 sp<BpMemoryHeap> heap(static_cast<BpMemoryHeap*>(find_heap(binder).get()));
283 heap->assertReallyMapped();
284 if (heap->mBase != MAP_FAILED) {
285 Mutex::Autolock _l(mLock);
Hans Boehm4ce4db72016-07-12 18:05:49 -0700286 if (mHeapId.load(memory_order_relaxed) == -1) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800287 mBase = heap->mBase;
288 mSize = heap->mSize;
Anu Sundararajan5728a922011-06-22 15:58:59 -0500289 mOffset = heap->mOffset;
Hans Boehm4ce4db72016-07-12 18:05:49 -0700290 int fd = dup(heap->mHeapId.load(memory_order_relaxed));
291 ALOGE_IF(fd==-1, "cannot dup fd=%d",
292 heap->mHeapId.load(memory_order_relaxed));
293 mHeapId.store(fd, memory_order_release);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800294 }
295 } else {
296 // something went wrong
297 free_heap(binder);
298 }
299 }
300}
301
302void BpMemoryHeap::assertReallyMapped() const
303{
Hans Boehm4ce4db72016-07-12 18:05:49 -0700304 int32_t heapId = mHeapId.load(memory_order_acquire);
305 if (heapId == -1) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800306
307 // remote call without mLock held, worse case scenario, we end up
308 // calling transact() from multiple threads, but that's not a problem,
309 // only mmap below must be in the critical section.
Anu Sundararajan5728a922011-06-22 15:58:59 -0500310
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800311 Parcel data, reply;
312 data.writeInterfaceToken(IMemoryHeap::getInterfaceDescriptor());
313 status_t err = remote()->transact(HEAP_ID, data, &reply);
314 int parcel_fd = reply.readFileDescriptor();
315 ssize_t size = reply.readInt32();
316 uint32_t flags = reply.readInt32();
Anu Sundararajan5728a922011-06-22 15:58:59 -0500317 uint32_t offset = reply.readInt32();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800318
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800319 ALOGE_IF(err, "binder=%p transaction failed fd=%d, size=%zd, err=%d (%s)",
Marco Nelissen097ca272014-11-14 08:01:01 -0800320 IInterface::asBinder(this).get(),
321 parcel_fd, size, err, strerror(-err));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800322
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800323 Mutex::Autolock _l(mLock);
Hans Boehm4ce4db72016-07-12 18:05:49 -0700324 if (mHeapId.load(memory_order_relaxed) == -1) {
John Eckerdal6b0b0632016-04-21 15:04:14 +0200325 int fd = dup( parcel_fd );
326 ALOGE_IF(fd==-1, "cannot dup fd=%d, size=%zd, err=%d (%s)",
327 parcel_fd, size, err, strerror(errno));
328
329 int access = PROT_READ;
330 if (!(flags & READ_ONLY)) {
331 access |= PROT_WRITE;
332 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800333 mRealHeap = true;
Anu Sundararajan5728a922011-06-22 15:58:59 -0500334 mBase = mmap(0, size, access, MAP_SHARED, fd, offset);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800335 if (mBase == MAP_FAILED) {
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800336 ALOGE("cannot map BpMemoryHeap (binder=%p), size=%zd, fd=%d (%s)",
Marco Nelissen097ca272014-11-14 08:01:01 -0800337 IInterface::asBinder(this).get(), size, fd, strerror(errno));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800338 close(fd);
339 } else {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800340 mSize = size;
341 mFlags = flags;
Anu Sundararajan5728a922011-06-22 15:58:59 -0500342 mOffset = offset;
Hans Boehm4ce4db72016-07-12 18:05:49 -0700343 mHeapId.store(fd, memory_order_release);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800344 }
345 }
346 }
347}
348
349int BpMemoryHeap::getHeapID() const {
350 assertMapped();
Hans Boehm4ce4db72016-07-12 18:05:49 -0700351 // We either stored mHeapId ourselves, or loaded it with acquire semantics.
352 return mHeapId.load(memory_order_relaxed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800353}
354
355void* BpMemoryHeap::getBase() const {
356 assertMapped();
357 return mBase;
358}
359
360size_t BpMemoryHeap::getSize() const {
361 assertMapped();
362 return mSize;
363}
364
365uint32_t BpMemoryHeap::getFlags() const {
366 assertMapped();
367 return mFlags;
368}
369
Anu Sundararajan5728a922011-06-22 15:58:59 -0500370uint32_t BpMemoryHeap::getOffset() const {
371 assertMapped();
372 return mOffset;
373}
374
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800375// ---------------------------------------------------------------------------
376
377IMPLEMENT_META_INTERFACE(MemoryHeap, "android.utils.IMemoryHeap");
378
Anu Sundararajan5728a922011-06-22 15:58:59 -0500379BnMemoryHeap::BnMemoryHeap() {
Mathias Agopian83c04462009-05-22 19:00:22 -0700380}
381
Anu Sundararajan5728a922011-06-22 15:58:59 -0500382BnMemoryHeap::~BnMemoryHeap() {
Mathias Agopian83c04462009-05-22 19:00:22 -0700383}
384
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800385status_t BnMemoryHeap::onTransact(
Mathias Agopian83c04462009-05-22 19:00:22 -0700386 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800387{
388 switch(code) {
389 case HEAP_ID: {
390 CHECK_INTERFACE(IMemoryHeap, data, reply);
391 reply->writeFileDescriptor(getHeapID());
392 reply->writeInt32(getSize());
393 reply->writeInt32(getFlags());
Anu Sundararajan5728a922011-06-22 15:58:59 -0500394 reply->writeInt32(getOffset());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800395 return NO_ERROR;
396 } break;
397 default:
398 return BBinder::onTransact(code, data, reply, flags);
399 }
400}
401
402/*****************************************************************************/
403
404HeapCache::HeapCache()
405 : DeathRecipient()
406{
407}
408
409HeapCache::~HeapCache()
410{
411}
412
413void HeapCache::binderDied(const wp<IBinder>& binder)
414{
Steve Block9d453682011-12-20 16:23:08 +0000415 //ALOGD("binderDied binder=%p", binder.unsafe_get());
Anu Sundararajan5728a922011-06-22 15:58:59 -0500416 free_heap(binder);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800417}
418
Anu Sundararajan5728a922011-06-22 15:58:59 -0500419sp<IMemoryHeap> HeapCache::find_heap(const sp<IBinder>& binder)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800420{
421 Mutex::Autolock _l(mHeapCacheLock);
422 ssize_t i = mHeapCache.indexOfKey(binder);
423 if (i>=0) {
424 heap_info_t& info = mHeapCache.editValueAt(i);
Steve Block9d453682011-12-20 16:23:08 +0000425 ALOGD_IF(VERBOSE,
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800426 "found binder=%p, heap=%p, size=%zu, fd=%d, count=%d",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800427 binder.get(), info.heap.get(),
428 static_cast<BpMemoryHeap*>(info.heap.get())->mSize,
Hans Boehm4ce4db72016-07-12 18:05:49 -0700429 static_cast<BpMemoryHeap*>(info.heap.get())
430 ->mHeapId.load(memory_order_relaxed),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800431 info.count);
Hans Boehm4ce4db72016-07-12 18:05:49 -0700432 ++info.count;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800433 return info.heap;
434 } else {
435 heap_info_t info;
436 info.heap = interface_cast<IMemoryHeap>(binder);
437 info.count = 1;
Steve Block9d453682011-12-20 16:23:08 +0000438 //ALOGD("adding binder=%p, heap=%p, count=%d",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800439 // binder.get(), info.heap.get(), info.count);
440 mHeapCache.add(binder, info);
441 return info.heap;
442 }
443}
444
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800445void HeapCache::free_heap(const sp<IBinder>& binder) {
446 free_heap( wp<IBinder>(binder) );
447}
448
Anu Sundararajan5728a922011-06-22 15:58:59 -0500449void HeapCache::free_heap(const wp<IBinder>& binder)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800450{
451 sp<IMemoryHeap> rel;
452 {
453 Mutex::Autolock _l(mHeapCacheLock);
454 ssize_t i = mHeapCache.indexOfKey(binder);
455 if (i>=0) {
456 heap_info_t& info(mHeapCache.editValueAt(i));
Hans Boehm4ce4db72016-07-12 18:05:49 -0700457 if (--info.count == 0) {
Steve Block9d453682011-12-20 16:23:08 +0000458 ALOGD_IF(VERBOSE,
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800459 "removing binder=%p, heap=%p, size=%zu, fd=%d, count=%d",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800460 binder.unsafe_get(), info.heap.get(),
461 static_cast<BpMemoryHeap*>(info.heap.get())->mSize,
Hans Boehm4ce4db72016-07-12 18:05:49 -0700462 static_cast<BpMemoryHeap*>(info.heap.get())
463 ->mHeapId.load(memory_order_relaxed),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800464 info.count);
465 rel = mHeapCache.valueAt(i).heap;
466 mHeapCache.removeItemsAt(i);
467 }
468 } else {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000469 ALOGE("free_heap binder=%p not found!!!", binder.unsafe_get());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800470 }
471 }
472}
473
474sp<IMemoryHeap> HeapCache::get_heap(const sp<IBinder>& binder)
475{
476 sp<IMemoryHeap> realHeap;
477 Mutex::Autolock _l(mHeapCacheLock);
478 ssize_t i = mHeapCache.indexOfKey(binder);
479 if (i>=0) realHeap = mHeapCache.valueAt(i).heap;
480 else realHeap = interface_cast<IMemoryHeap>(binder);
481 return realHeap;
482}
483
Anu Sundararajan5728a922011-06-22 15:58:59 -0500484void HeapCache::dump_heaps()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800485{
486 Mutex::Autolock _l(mHeapCacheLock);
487 int c = mHeapCache.size();
488 for (int i=0 ; i<c ; i++) {
489 const heap_info_t& info = mHeapCache.valueAt(i);
490 BpMemoryHeap const* h(static_cast<BpMemoryHeap const *>(info.heap.get()));
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800491 ALOGD("hey=%p, heap=%p, count=%d, (fd=%d, base=%p, size=%zu)",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800492 mHeapCache.keyAt(i).unsafe_get(),
Anu Sundararajan5728a922011-06-22 15:58:59 -0500493 info.heap.get(), info.count,
Hans Boehm4ce4db72016-07-12 18:05:49 -0700494 h->mHeapId.load(memory_order_relaxed), h->mBase, h->mSize);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800495 }
496}
497
498
499// ---------------------------------------------------------------------------
500}; // namespace android