IMemory.cpp revision 9d4536835248525f32f1504a3d28d5bbfa0a2910
183b8526436ba2e564dff99ec4c6cf46fabfdf22eYigit Boyar/*
283b8526436ba2e564dff99ec4c6cf46fabfdf22eYigit Boyar * Copyright (C) 2008 The Android Open Source Project
383b8526436ba2e564dff99ec4c6cf46fabfdf22eYigit Boyar *
483b8526436ba2e564dff99ec4c6cf46fabfdf22eYigit Boyar * Licensed under the Apache License, Version 2.0 (the "License");
583b8526436ba2e564dff99ec4c6cf46fabfdf22eYigit Boyar * you may not use this file except in compliance with the License.
683b8526436ba2e564dff99ec4c6cf46fabfdf22eYigit Boyar * You may obtain a copy of the License at
783b8526436ba2e564dff99ec4c6cf46fabfdf22eYigit Boyar *
883b8526436ba2e564dff99ec4c6cf46fabfdf22eYigit Boyar *      http://www.apache.org/licenses/LICENSE-2.0
983b8526436ba2e564dff99ec4c6cf46fabfdf22eYigit Boyar *
1083b8526436ba2e564dff99ec4c6cf46fabfdf22eYigit Boyar * Unless required by applicable law or agreed to in writing, software
1183b8526436ba2e564dff99ec4c6cf46fabfdf22eYigit Boyar * distributed under the License is distributed on an "AS IS" BASIS,
1283b8526436ba2e564dff99ec4c6cf46fabfdf22eYigit Boyar * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1383b8526436ba2e564dff99ec4c6cf46fabfdf22eYigit Boyar * See the License for the specific language governing permissions and
1483b8526436ba2e564dff99ec4c6cf46fabfdf22eYigit Boyar * limitations under the License.
1583b8526436ba2e564dff99ec4c6cf46fabfdf22eYigit Boyar */
1683b8526436ba2e564dff99ec4c6cf46fabfdf22eYigit Boyar
1783b8526436ba2e564dff99ec4c6cf46fabfdf22eYigit Boyar#define LOG_TAG "IMemory"
1883b8526436ba2e564dff99ec4c6cf46fabfdf22eYigit Boyar
1983b8526436ba2e564dff99ec4c6cf46fabfdf22eYigit Boyar#include <stdint.h>
2083b8526436ba2e564dff99ec4c6cf46fabfdf22eYigit Boyar#include <stdio.h>
2183b8526436ba2e564dff99ec4c6cf46fabfdf22eYigit Boyar#include <stdlib.h>
2283b8526436ba2e564dff99ec4c6cf46fabfdf22eYigit Boyar#include <fcntl.h>
2383b8526436ba2e564dff99ec4c6cf46fabfdf22eYigit Boyar#include <unistd.h>
2483b8526436ba2e564dff99ec4c6cf46fabfdf22eYigit Boyar
2583b8526436ba2e564dff99ec4c6cf46fabfdf22eYigit Boyar#include <sys/types.h>
2683b8526436ba2e564dff99ec4c6cf46fabfdf22eYigit Boyar#include <sys/mman.h>
27bdb07a1802c017efa64a5cfd8ab5a7ff4c4926b0Yigit Boyar
28bdb07a1802c017efa64a5cfd8ab5a7ff4c4926b0Yigit Boyar#include <binder/IMemory.h>
29bdb07a1802c017efa64a5cfd8ab5a7ff4c4926b0Yigit Boyar#include <utils/KeyedVector.h>
3083b8526436ba2e564dff99ec4c6cf46fabfdf22eYigit Boyar#include <utils/threads.h>
3183b8526436ba2e564dff99ec4c6cf46fabfdf22eYigit Boyar#include <utils/Atomic.h>
3283b8526436ba2e564dff99ec4c6cf46fabfdf22eYigit Boyar#include <binder/Parcel.h>
3383b8526436ba2e564dff99ec4c6cf46fabfdf22eYigit Boyar#include <utils/CallStack.h>
3483b8526436ba2e564dff99ec4c6cf46fabfdf22eYigit Boyar
3583b8526436ba2e564dff99ec4c6cf46fabfdf22eYigit Boyar#define VERBOSE   0
3683b8526436ba2e564dff99ec4c6cf46fabfdf22eYigit Boyar
37bdb07a1802c017efa64a5cfd8ab5a7ff4c4926b0Yigit Boyarnamespace android {
38bdb07a1802c017efa64a5cfd8ab5a7ff4c4926b0Yigit Boyar// ---------------------------------------------------------------------------
39bdb07a1802c017efa64a5cfd8ab5a7ff4c4926b0Yigit Boyar
40bdb07a1802c017efa64a5cfd8ab5a7ff4c4926b0Yigit Boyarclass HeapCache : public IBinder::DeathRecipient
41bdb07a1802c017efa64a5cfd8ab5a7ff4c4926b0Yigit Boyar{
42bdb07a1802c017efa64a5cfd8ab5a7ff4c4926b0Yigit Boyarpublic:
43c42ba8c000d1e6ce85e152dfc17089a0a69e739fYigit Boyar    HeapCache();
4483b8526436ba2e564dff99ec4c6cf46fabfdf22eYigit Boyar    virtual ~HeapCache();
45c42ba8c000d1e6ce85e152dfc17089a0a69e739fYigit Boyar
4683b8526436ba2e564dff99ec4c6cf46fabfdf22eYigit Boyar    virtual void binderDied(const wp<IBinder>& who);
4783b8526436ba2e564dff99ec4c6cf46fabfdf22eYigit Boyar
4883b8526436ba2e564dff99ec4c6cf46fabfdf22eYigit Boyar    sp<IMemoryHeap> find_heap(const sp<IBinder>& binder);
49bdb07a1802c017efa64a5cfd8ab5a7ff4c4926b0Yigit Boyar    void free_heap(const sp<IBinder>& binder);
50bdb07a1802c017efa64a5cfd8ab5a7ff4c4926b0Yigit Boyar    sp<IMemoryHeap> get_heap(const sp<IBinder>& binder);
51bdb07a1802c017efa64a5cfd8ab5a7ff4c4926b0Yigit Boyar    void dump_heaps();
52bdb07a1802c017efa64a5cfd8ab5a7ff4c4926b0Yigit Boyar
53c42ba8c000d1e6ce85e152dfc17089a0a69e739fYigit Boyarprivate:
54c42ba8c000d1e6ce85e152dfc17089a0a69e739fYigit Boyar    // For IMemory.cpp
55c42ba8c000d1e6ce85e152dfc17089a0a69e739fYigit Boyar    struct heap_info_t {
56bdb07a1802c017efa64a5cfd8ab5a7ff4c4926b0Yigit Boyar        sp<IMemoryHeap> heap;
57bdb07a1802c017efa64a5cfd8ab5a7ff4c4926b0Yigit Boyar        int32_t         count;
58bdb07a1802c017efa64a5cfd8ab5a7ff4c4926b0Yigit Boyar    };
59bdb07a1802c017efa64a5cfd8ab5a7ff4c4926b0Yigit Boyar
60c42ba8c000d1e6ce85e152dfc17089a0a69e739fYigit Boyar    void free_heap(const wp<IBinder>& binder);
61bdb07a1802c017efa64a5cfd8ab5a7ff4c4926b0Yigit Boyar
62bdb07a1802c017efa64a5cfd8ab5a7ff4c4926b0Yigit Boyar    Mutex mHeapCacheLock;
63bdb07a1802c017efa64a5cfd8ab5a7ff4c4926b0Yigit Boyar    KeyedVector< wp<IBinder>, heap_info_t > mHeapCache;
64bdb07a1802c017efa64a5cfd8ab5a7ff4c4926b0Yigit Boyar};
65bdb07a1802c017efa64a5cfd8ab5a7ff4c4926b0Yigit Boyar
66bdb07a1802c017efa64a5cfd8ab5a7ff4c4926b0Yigit Boyarstatic sp<HeapCache> gHeapCache = new HeapCache();
6783b8526436ba2e564dff99ec4c6cf46fabfdf22eYigit Boyar
6883b8526436ba2e564dff99ec4c6cf46fabfdf22eYigit Boyar/******************************************************************************/
6983b8526436ba2e564dff99ec4c6cf46fabfdf22eYigit Boyar
7083b8526436ba2e564dff99ec4c6cf46fabfdf22eYigit Boyarenum {
71bdb07a1802c017efa64a5cfd8ab5a7ff4c4926b0Yigit Boyar    HEAP_ID = IBinder::FIRST_CALL_TRANSACTION
72bdb07a1802c017efa64a5cfd8ab5a7ff4c4926b0Yigit Boyar};
73bdb07a1802c017efa64a5cfd8ab5a7ff4c4926b0Yigit Boyar
74bdb07a1802c017efa64a5cfd8ab5a7ff4c4926b0Yigit Boyarclass BpMemoryHeap : public BpInterface<IMemoryHeap>
75bdb07a1802c017efa64a5cfd8ab5a7ff4c4926b0Yigit Boyar{
76bdb07a1802c017efa64a5cfd8ab5a7ff4c4926b0Yigit Boyarpublic:
77bdb07a1802c017efa64a5cfd8ab5a7ff4c4926b0Yigit Boyar    BpMemoryHeap(const sp<IBinder>& impl);
78bdb07a1802c017efa64a5cfd8ab5a7ff4c4926b0Yigit Boyar    virtual ~BpMemoryHeap();
79bdb07a1802c017efa64a5cfd8ab5a7ff4c4926b0Yigit Boyar
80bdb07a1802c017efa64a5cfd8ab5a7ff4c4926b0Yigit Boyar    virtual int getHeapID() const;
81c42ba8c000d1e6ce85e152dfc17089a0a69e739fYigit Boyar    virtual void* getBase() const;
82c42ba8c000d1e6ce85e152dfc17089a0a69e739fYigit Boyar    virtual size_t getSize() const;
83bdb07a1802c017efa64a5cfd8ab5a7ff4c4926b0Yigit Boyar    virtual uint32_t getFlags() const;
84c42ba8c000d1e6ce85e152dfc17089a0a69e739fYigit Boyar    virtual uint32_t getOffset() const;
85c42ba8c000d1e6ce85e152dfc17089a0a69e739fYigit Boyar
86bdb07a1802c017efa64a5cfd8ab5a7ff4c4926b0Yigit Boyarprivate:
8783b8526436ba2e564dff99ec4c6cf46fabfdf22eYigit Boyar    friend class IMemory;
8883b8526436ba2e564dff99ec4c6cf46fabfdf22eYigit Boyar    friend class HeapCache;
8983b8526436ba2e564dff99ec4c6cf46fabfdf22eYigit Boyar
9083b8526436ba2e564dff99ec4c6cf46fabfdf22eYigit Boyar    // for debugging in this module
9183b8526436ba2e564dff99ec4c6cf46fabfdf22eYigit Boyar    static inline sp<IMemoryHeap> find_heap(const sp<IBinder>& binder) {
92bdb07a1802c017efa64a5cfd8ab5a7ff4c4926b0Yigit Boyar        return gHeapCache->find_heap(binder);
9383b8526436ba2e564dff99ec4c6cf46fabfdf22eYigit Boyar    }
9483b8526436ba2e564dff99ec4c6cf46fabfdf22eYigit Boyar    static inline void free_heap(const sp<IBinder>& binder) {
9583b8526436ba2e564dff99ec4c6cf46fabfdf22eYigit Boyar        gHeapCache->free_heap(binder);
96bf43be6ab14db8489f924d1673951f0c49014605Chris Craik    }
97bdb07a1802c017efa64a5cfd8ab5a7ff4c4926b0Yigit Boyar    static inline sp<IMemoryHeap> get_heap(const sp<IBinder>& binder) {
9883b8526436ba2e564dff99ec4c6cf46fabfdf22eYigit Boyar        return gHeapCache->get_heap(binder);
9983b8526436ba2e564dff99ec4c6cf46fabfdf22eYigit Boyar    }
100bdb07a1802c017efa64a5cfd8ab5a7ff4c4926b0Yigit Boyar    static inline void dump_heaps() {
10183b8526436ba2e564dff99ec4c6cf46fabfdf22eYigit Boyar        gHeapCache->dump_heaps();
10283b8526436ba2e564dff99ec4c6cf46fabfdf22eYigit Boyar    }
10383b8526436ba2e564dff99ec4c6cf46fabfdf22eYigit Boyar
10483b8526436ba2e564dff99ec4c6cf46fabfdf22eYigit Boyar    void assertMapped() const;
105bdb07a1802c017efa64a5cfd8ab5a7ff4c4926b0Yigit Boyar    void assertReallyMapped() const;
10683b8526436ba2e564dff99ec4c6cf46fabfdf22eYigit Boyar
10783b8526436ba2e564dff99ec4c6cf46fabfdf22eYigit Boyar    mutable volatile int32_t mHeapId;
10883b8526436ba2e564dff99ec4c6cf46fabfdf22eYigit Boyar    mutable void*       mBase;
10983b8526436ba2e564dff99ec4c6cf46fabfdf22eYigit Boyar    mutable size_t      mSize;
11083b8526436ba2e564dff99ec4c6cf46fabfdf22eYigit Boyar    mutable uint32_t    mFlags;
11183b8526436ba2e564dff99ec4c6cf46fabfdf22eYigit Boyar    mutable uint32_t    mOffset;
11283b8526436ba2e564dff99ec4c6cf46fabfdf22eYigit Boyar    mutable bool        mRealHeap;
11383b8526436ba2e564dff99ec4c6cf46fabfdf22eYigit Boyar    mutable Mutex       mLock;
11483b8526436ba2e564dff99ec4c6cf46fabfdf22eYigit Boyar};
11583b8526436ba2e564dff99ec4c6cf46fabfdf22eYigit Boyar
11683b8526436ba2e564dff99ec4c6cf46fabfdf22eYigit Boyar// ----------------------------------------------------------------------------
11783b8526436ba2e564dff99ec4c6cf46fabfdf22eYigit Boyar
11883b8526436ba2e564dff99ec4c6cf46fabfdf22eYigit Boyarenum {
11983b8526436ba2e564dff99ec4c6cf46fabfdf22eYigit Boyar    GET_MEMORY = IBinder::FIRST_CALL_TRANSACTION
12083b8526436ba2e564dff99ec4c6cf46fabfdf22eYigit Boyar};
121ad124ea5fa71f1b675c9c42b858adfbc42093d37Yigit Boyar
12283b8526436ba2e564dff99ec4c6cf46fabfdf22eYigit Boyarclass BpMemory : public BpInterface<IMemory>
12383b8526436ba2e564dff99ec4c6cf46fabfdf22eYigit Boyar{
12483b8526436ba2e564dff99ec4c6cf46fabfdf22eYigit Boyarpublic:
12583b8526436ba2e564dff99ec4c6cf46fabfdf22eYigit Boyar    BpMemory(const sp<IBinder>& impl);
12683b8526436ba2e564dff99ec4c6cf46fabfdf22eYigit Boyar    virtual ~BpMemory();
1274eb77f0ed24a9d300f7d12959de8cf7efd837e2fYigit Boyar    virtual sp<IMemoryHeap> getMemory(ssize_t* offset=0, size_t* size=0) const;
1284eb77f0ed24a9d300f7d12959de8cf7efd837e2fYigit Boyar
1294eb77f0ed24a9d300f7d12959de8cf7efd837e2fYigit Boyarprivate:
1304eb77f0ed24a9d300f7d12959de8cf7efd837e2fYigit Boyar    mutable sp<IMemoryHeap> mHeap;
1314eb77f0ed24a9d300f7d12959de8cf7efd837e2fYigit Boyar    mutable ssize_t mOffset;
13283b8526436ba2e564dff99ec4c6cf46fabfdf22eYigit Boyar    mutable size_t mSize;
133};
134
135/******************************************************************************/
136
137void* IMemory::fastPointer(const sp<IBinder>& binder, ssize_t offset) const
138{
139    sp<IMemoryHeap> realHeap = BpMemoryHeap::get_heap(binder);
140    void* const base = realHeap->base();
141    if (base == MAP_FAILED)
142        return 0;
143    return static_cast<char*>(base) + offset;
144}
145
146void* IMemory::pointer() const {
147    ssize_t offset;
148    sp<IMemoryHeap> heap = getMemory(&offset);
149    void* const base = heap!=0 ? heap->base() : MAP_FAILED;
150    if (base == MAP_FAILED)
151        return 0;
152    return static_cast<char*>(base) + offset;
153}
154
155size_t IMemory::size() const {
156    size_t size;
157    getMemory(NULL, &size);
158    return size;
159}
160
161ssize_t IMemory::offset() const {
162    ssize_t offset;
163    getMemory(&offset);
164    return offset;
165}
166
167/******************************************************************************/
168
169BpMemory::BpMemory(const sp<IBinder>& impl)
170    : BpInterface<IMemory>(impl), mOffset(0), mSize(0)
171{
172}
173
174BpMemory::~BpMemory()
175{
176}
177
178sp<IMemoryHeap> BpMemory::getMemory(ssize_t* offset, size_t* size) const
179{
180    if (mHeap == 0) {
181        Parcel data, reply;
182        data.writeInterfaceToken(IMemory::getInterfaceDescriptor());
183        if (remote()->transact(GET_MEMORY, data, &reply) == NO_ERROR) {
184            sp<IBinder> heap = reply.readStrongBinder();
185            ssize_t o = reply.readInt32();
186            size_t s = reply.readInt32();
187            if (heap != 0) {
188                mHeap = interface_cast<IMemoryHeap>(heap);
189                if (mHeap != 0) {
190                    mOffset = o;
191                    mSize = s;
192                }
193            }
194        }
195    }
196    if (offset) *offset = mOffset;
197    if (size) *size = mSize;
198    return mHeap;
199}
200
201// ---------------------------------------------------------------------------
202
203IMPLEMENT_META_INTERFACE(Memory, "android.utils.IMemory");
204
205BnMemory::BnMemory() {
206}
207
208BnMemory::~BnMemory() {
209}
210
211status_t BnMemory::onTransact(
212    uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
213{
214    switch(code) {
215        case GET_MEMORY: {
216            CHECK_INTERFACE(IMemory, data, reply);
217            ssize_t offset;
218            size_t size;
219            reply->writeStrongBinder( getMemory(&offset, &size)->asBinder() );
220            reply->writeInt32(offset);
221            reply->writeInt32(size);
222            return NO_ERROR;
223        } break;
224        default:
225            return BBinder::onTransact(code, data, reply, flags);
226    }
227}
228
229
230/******************************************************************************/
231
232BpMemoryHeap::BpMemoryHeap(const sp<IBinder>& impl)
233    : BpInterface<IMemoryHeap>(impl),
234        mHeapId(-1), mBase(MAP_FAILED), mSize(0), mFlags(0), mOffset(0), mRealHeap(false)
235{
236}
237
238BpMemoryHeap::~BpMemoryHeap() {
239    if (mHeapId != -1) {
240        close(mHeapId);
241        if (mRealHeap) {
242            // by construction we're the last one
243            if (mBase != MAP_FAILED) {
244                sp<IBinder> binder = const_cast<BpMemoryHeap*>(this)->asBinder();
245
246                if (VERBOSE) {
247                    ALOGD("UNMAPPING binder=%p, heap=%p, size=%d, fd=%d",
248                            binder.get(), this, mSize, mHeapId);
249                    CallStack stack;
250                    stack.update();
251                    stack.dump("callstack");
252                }
253
254                munmap(mBase, mSize);
255            }
256        } else {
257            // remove from list only if it was mapped before
258            sp<IBinder> binder = const_cast<BpMemoryHeap*>(this)->asBinder();
259            free_heap(binder);
260        }
261    }
262}
263
264void BpMemoryHeap::assertMapped() const
265{
266    if (mHeapId == -1) {
267        sp<IBinder> binder(const_cast<BpMemoryHeap*>(this)->asBinder());
268        sp<BpMemoryHeap> heap(static_cast<BpMemoryHeap*>(find_heap(binder).get()));
269        heap->assertReallyMapped();
270        if (heap->mBase != MAP_FAILED) {
271            Mutex::Autolock _l(mLock);
272            if (mHeapId == -1) {
273                mBase   = heap->mBase;
274                mSize   = heap->mSize;
275                mOffset = heap->mOffset;
276                android_atomic_write( dup( heap->mHeapId ), &mHeapId );
277            }
278        } else {
279            // something went wrong
280            free_heap(binder);
281        }
282    }
283}
284
285void BpMemoryHeap::assertReallyMapped() const
286{
287    if (mHeapId == -1) {
288
289        // remote call without mLock held, worse case scenario, we end up
290        // calling transact() from multiple threads, but that's not a problem,
291        // only mmap below must be in the critical section.
292
293        Parcel data, reply;
294        data.writeInterfaceToken(IMemoryHeap::getInterfaceDescriptor());
295        status_t err = remote()->transact(HEAP_ID, data, &reply);
296        int parcel_fd = reply.readFileDescriptor();
297        ssize_t size = reply.readInt32();
298        uint32_t flags = reply.readInt32();
299        uint32_t offset = reply.readInt32();
300
301        LOGE_IF(err, "binder=%p transaction failed fd=%d, size=%ld, err=%d (%s)",
302                asBinder().get(), parcel_fd, size, err, strerror(-err));
303
304        int fd = dup( parcel_fd );
305        LOGE_IF(fd==-1, "cannot dup fd=%d, size=%ld, err=%d (%s)",
306                parcel_fd, size, err, strerror(errno));
307
308        int access = PROT_READ;
309        if (!(flags & READ_ONLY)) {
310            access |= PROT_WRITE;
311        }
312
313        Mutex::Autolock _l(mLock);
314        if (mHeapId == -1) {
315            mRealHeap = true;
316            mBase = mmap(0, size, access, MAP_SHARED, fd, offset);
317            if (mBase == MAP_FAILED) {
318                LOGE("cannot map BpMemoryHeap (binder=%p), size=%ld, fd=%d (%s)",
319                        asBinder().get(), size, fd, strerror(errno));
320                close(fd);
321            } else {
322                mSize = size;
323                mFlags = flags;
324                mOffset = offset;
325                android_atomic_write(fd, &mHeapId);
326            }
327        }
328    }
329}
330
331int BpMemoryHeap::getHeapID() const {
332    assertMapped();
333    return mHeapId;
334}
335
336void* BpMemoryHeap::getBase() const {
337    assertMapped();
338    return mBase;
339}
340
341size_t BpMemoryHeap::getSize() const {
342    assertMapped();
343    return mSize;
344}
345
346uint32_t BpMemoryHeap::getFlags() const {
347    assertMapped();
348    return mFlags;
349}
350
351uint32_t BpMemoryHeap::getOffset() const {
352    assertMapped();
353    return mOffset;
354}
355
356// ---------------------------------------------------------------------------
357
358IMPLEMENT_META_INTERFACE(MemoryHeap, "android.utils.IMemoryHeap");
359
360BnMemoryHeap::BnMemoryHeap() {
361}
362
363BnMemoryHeap::~BnMemoryHeap() {
364}
365
366status_t BnMemoryHeap::onTransact(
367        uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
368{
369    switch(code) {
370       case HEAP_ID: {
371            CHECK_INTERFACE(IMemoryHeap, data, reply);
372            reply->writeFileDescriptor(getHeapID());
373            reply->writeInt32(getSize());
374            reply->writeInt32(getFlags());
375            reply->writeInt32(getOffset());
376            return NO_ERROR;
377        } break;
378        default:
379            return BBinder::onTransact(code, data, reply, flags);
380    }
381}
382
383/*****************************************************************************/
384
385HeapCache::HeapCache()
386    : DeathRecipient()
387{
388}
389
390HeapCache::~HeapCache()
391{
392}
393
394void HeapCache::binderDied(const wp<IBinder>& binder)
395{
396    //ALOGD("binderDied binder=%p", binder.unsafe_get());
397    free_heap(binder);
398}
399
400sp<IMemoryHeap> HeapCache::find_heap(const sp<IBinder>& binder)
401{
402    Mutex::Autolock _l(mHeapCacheLock);
403    ssize_t i = mHeapCache.indexOfKey(binder);
404    if (i>=0) {
405        heap_info_t& info = mHeapCache.editValueAt(i);
406        ALOGD_IF(VERBOSE,
407                "found binder=%p, heap=%p, size=%d, fd=%d, count=%d",
408                binder.get(), info.heap.get(),
409                static_cast<BpMemoryHeap*>(info.heap.get())->mSize,
410                static_cast<BpMemoryHeap*>(info.heap.get())->mHeapId,
411                info.count);
412        android_atomic_inc(&info.count);
413        return info.heap;
414    } else {
415        heap_info_t info;
416        info.heap = interface_cast<IMemoryHeap>(binder);
417        info.count = 1;
418        //ALOGD("adding binder=%p, heap=%p, count=%d",
419        //      binder.get(), info.heap.get(), info.count);
420        mHeapCache.add(binder, info);
421        return info.heap;
422    }
423}
424
425void HeapCache::free_heap(const sp<IBinder>& binder)  {
426    free_heap( wp<IBinder>(binder) );
427}
428
429void HeapCache::free_heap(const wp<IBinder>& binder)
430{
431    sp<IMemoryHeap> rel;
432    {
433        Mutex::Autolock _l(mHeapCacheLock);
434        ssize_t i = mHeapCache.indexOfKey(binder);
435        if (i>=0) {
436            heap_info_t& info(mHeapCache.editValueAt(i));
437            int32_t c = android_atomic_dec(&info.count);
438            if (c == 1) {
439                ALOGD_IF(VERBOSE,
440                        "removing binder=%p, heap=%p, size=%d, fd=%d, count=%d",
441                        binder.unsafe_get(), info.heap.get(),
442                        static_cast<BpMemoryHeap*>(info.heap.get())->mSize,
443                        static_cast<BpMemoryHeap*>(info.heap.get())->mHeapId,
444                        info.count);
445                rel = mHeapCache.valueAt(i).heap;
446                mHeapCache.removeItemsAt(i);
447            }
448        } else {
449            LOGE("free_heap binder=%p not found!!!", binder.unsafe_get());
450        }
451    }
452}
453
454sp<IMemoryHeap> HeapCache::get_heap(const sp<IBinder>& binder)
455{
456    sp<IMemoryHeap> realHeap;
457    Mutex::Autolock _l(mHeapCacheLock);
458    ssize_t i = mHeapCache.indexOfKey(binder);
459    if (i>=0)   realHeap = mHeapCache.valueAt(i).heap;
460    else        realHeap = interface_cast<IMemoryHeap>(binder);
461    return realHeap;
462}
463
464void HeapCache::dump_heaps()
465{
466    Mutex::Autolock _l(mHeapCacheLock);
467    int c = mHeapCache.size();
468    for (int i=0 ; i<c ; i++) {
469        const heap_info_t& info = mHeapCache.valueAt(i);
470        BpMemoryHeap const* h(static_cast<BpMemoryHeap const *>(info.heap.get()));
471        ALOGD("hey=%p, heap=%p, count=%d, (fd=%d, base=%p, size=%d)",
472                mHeapCache.keyAt(i).unsafe_get(),
473                info.heap.get(), info.count,
474                h->mHeapId, h->mBase, h->mSize);
475    }
476}
477
478
479// ---------------------------------------------------------------------------
480}; // namespace android
481