The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 19 | #include <stdint.h> |
| 20 | #include <stdio.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <fcntl.h> |
| 23 | #include <unistd.h> |
| 24 | |
| 25 | #include <sys/types.h> |
| 26 | #include <sys/mman.h> |
| 27 | |
Mathias Agopian | c5b2c0b | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 28 | #include <binder/IMemory.h> |
Christopher Tate | 94b0d4e | 2016-02-05 19:02:56 -0800 | [diff] [blame^] | 29 | #include <cutils/log.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 30 | #include <utils/KeyedVector.h> |
| 31 | #include <utils/threads.h> |
| 32 | #include <utils/Atomic.h> |
Mathias Agopian | c5b2c0b | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 33 | #include <binder/Parcel.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 34 | #include <utils/CallStack.h> |
| 35 | |
| 36 | #define VERBOSE 0 |
| 37 | |
| 38 | namespace android { |
| 39 | // --------------------------------------------------------------------------- |
| 40 | |
| 41 | class HeapCache : public IBinder::DeathRecipient |
| 42 | { |
| 43 | public: |
| 44 | HeapCache(); |
| 45 | virtual ~HeapCache(); |
Anu Sundararajan | 5728a92 | 2011-06-22 15:58:59 -0500 | [diff] [blame] | 46 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 47 | virtual void binderDied(const wp<IBinder>& who); |
| 48 | |
Anu Sundararajan | 5728a92 | 2011-06-22 15:58:59 -0500 | [diff] [blame] | 49 | sp<IMemoryHeap> find_heap(const sp<IBinder>& binder); |
| 50 | void free_heap(const sp<IBinder>& binder); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 51 | sp<IMemoryHeap> get_heap(const sp<IBinder>& binder); |
| 52 | void dump_heaps(); |
| 53 | |
| 54 | private: |
| 55 | // For IMemory.cpp |
| 56 | struct heap_info_t { |
| 57 | sp<IMemoryHeap> heap; |
| 58 | int32_t count; |
| 59 | }; |
| 60 | |
Anu Sundararajan | 5728a92 | 2011-06-22 15:58:59 -0500 | [diff] [blame] | 61 | void free_heap(const wp<IBinder>& binder); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 62 | |
| 63 | Mutex mHeapCacheLock; |
| 64 | KeyedVector< wp<IBinder>, heap_info_t > mHeapCache; |
| 65 | }; |
| 66 | |
| 67 | static sp<HeapCache> gHeapCache = new HeapCache(); |
| 68 | |
| 69 | /******************************************************************************/ |
| 70 | |
| 71 | enum { |
| 72 | HEAP_ID = IBinder::FIRST_CALL_TRANSACTION |
| 73 | }; |
| 74 | |
| 75 | class BpMemoryHeap : public BpInterface<IMemoryHeap> |
| 76 | { |
| 77 | public: |
| 78 | BpMemoryHeap(const sp<IBinder>& impl); |
| 79 | virtual ~BpMemoryHeap(); |
| 80 | |
| 81 | virtual int getHeapID() const; |
| 82 | virtual void* getBase() const; |
| 83 | virtual size_t getSize() const; |
| 84 | virtual uint32_t getFlags() const; |
Anu Sundararajan | 5728a92 | 2011-06-22 15:58:59 -0500 | [diff] [blame] | 85 | virtual uint32_t getOffset() const; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 86 | |
| 87 | private: |
| 88 | friend class IMemory; |
| 89 | friend class HeapCache; |
Anu Sundararajan | 5728a92 | 2011-06-22 15:58:59 -0500 | [diff] [blame] | 90 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 91 | // for debugging in this module |
| 92 | static inline sp<IMemoryHeap> find_heap(const sp<IBinder>& binder) { |
| 93 | return gHeapCache->find_heap(binder); |
| 94 | } |
| 95 | static inline void free_heap(const sp<IBinder>& binder) { |
| 96 | gHeapCache->free_heap(binder); |
| 97 | } |
| 98 | static inline sp<IMemoryHeap> get_heap(const sp<IBinder>& binder) { |
| 99 | return gHeapCache->get_heap(binder); |
| 100 | } |
| 101 | static inline void dump_heaps() { |
Anu Sundararajan | 5728a92 | 2011-06-22 15:58:59 -0500 | [diff] [blame] | 102 | gHeapCache->dump_heaps(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 103 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 104 | |
| 105 | void assertMapped() const; |
| 106 | void assertReallyMapped() const; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 107 | |
| 108 | mutable volatile int32_t mHeapId; |
| 109 | mutable void* mBase; |
| 110 | mutable size_t mSize; |
| 111 | mutable uint32_t mFlags; |
Anu Sundararajan | 5728a92 | 2011-06-22 15:58:59 -0500 | [diff] [blame] | 112 | mutable uint32_t mOffset; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 113 | mutable bool mRealHeap; |
| 114 | mutable Mutex mLock; |
| 115 | }; |
| 116 | |
| 117 | // ---------------------------------------------------------------------------- |
| 118 | |
| 119 | enum { |
| 120 | GET_MEMORY = IBinder::FIRST_CALL_TRANSACTION |
| 121 | }; |
| 122 | |
| 123 | class BpMemory : public BpInterface<IMemory> |
| 124 | { |
| 125 | public: |
| 126 | BpMemory(const sp<IBinder>& impl); |
| 127 | virtual ~BpMemory(); |
| 128 | virtual sp<IMemoryHeap> getMemory(ssize_t* offset=0, size_t* size=0) const; |
Anu Sundararajan | 5728a92 | 2011-06-22 15:58:59 -0500 | [diff] [blame] | 129 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 130 | private: |
| 131 | mutable sp<IMemoryHeap> mHeap; |
| 132 | mutable ssize_t mOffset; |
| 133 | mutable size_t mSize; |
| 134 | }; |
| 135 | |
| 136 | /******************************************************************************/ |
| 137 | |
| 138 | void* IMemory::fastPointer(const sp<IBinder>& binder, ssize_t offset) const |
| 139 | { |
| 140 | sp<IMemoryHeap> realHeap = BpMemoryHeap::get_heap(binder); |
| 141 | void* const base = realHeap->base(); |
| 142 | if (base == MAP_FAILED) |
| 143 | return 0; |
| 144 | return static_cast<char*>(base) + offset; |
| 145 | } |
| 146 | |
| 147 | void* IMemory::pointer() const { |
| 148 | ssize_t offset; |
| 149 | sp<IMemoryHeap> heap = getMemory(&offset); |
| 150 | void* const base = heap!=0 ? heap->base() : MAP_FAILED; |
| 151 | if (base == MAP_FAILED) |
| 152 | return 0; |
| 153 | return static_cast<char*>(base) + offset; |
| 154 | } |
| 155 | |
| 156 | size_t IMemory::size() const { |
| 157 | size_t size; |
| 158 | getMemory(NULL, &size); |
| 159 | return size; |
| 160 | } |
| 161 | |
| 162 | ssize_t IMemory::offset() const { |
| 163 | ssize_t offset; |
| 164 | getMemory(&offset); |
| 165 | return offset; |
| 166 | } |
| 167 | |
| 168 | /******************************************************************************/ |
| 169 | |
| 170 | BpMemory::BpMemory(const sp<IBinder>& impl) |
| 171 | : BpInterface<IMemory>(impl), mOffset(0), mSize(0) |
| 172 | { |
| 173 | } |
| 174 | |
| 175 | BpMemory::~BpMemory() |
| 176 | { |
| 177 | } |
| 178 | |
| 179 | sp<IMemoryHeap> BpMemory::getMemory(ssize_t* offset, size_t* size) const |
| 180 | { |
| 181 | if (mHeap == 0) { |
| 182 | Parcel data, reply; |
| 183 | data.writeInterfaceToken(IMemory::getInterfaceDescriptor()); |
| 184 | if (remote()->transact(GET_MEMORY, data, &reply) == NO_ERROR) { |
| 185 | sp<IBinder> heap = reply.readStrongBinder(); |
| 186 | ssize_t o = reply.readInt32(); |
| 187 | size_t s = reply.readInt32(); |
| 188 | if (heap != 0) { |
| 189 | mHeap = interface_cast<IMemoryHeap>(heap); |
| 190 | if (mHeap != 0) { |
Christopher Tate | 94b0d4e | 2016-02-05 19:02:56 -0800 | [diff] [blame^] | 191 | size_t heapSize = mHeap->getSize(); |
| 192 | if (s <= heapSize |
| 193 | && o >= 0 |
| 194 | && (static_cast<size_t>(o) <= heapSize - s)) { |
| 195 | mOffset = o; |
| 196 | mSize = s; |
| 197 | } else { |
| 198 | // Hm. |
| 199 | android_errorWriteWithInfoLog(0x534e4554, |
| 200 | "26877992", -1, NULL, 0); |
| 201 | mOffset = 0; |
| 202 | mSize = 0; |
| 203 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 204 | } |
| 205 | } |
| 206 | } |
| 207 | } |
| 208 | if (offset) *offset = mOffset; |
| 209 | if (size) *size = mSize; |
Christopher Tate | 94b0d4e | 2016-02-05 19:02:56 -0800 | [diff] [blame^] | 210 | return (mSize > 0) ? mHeap : 0; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | // --------------------------------------------------------------------------- |
| 214 | |
| 215 | IMPLEMENT_META_INTERFACE(Memory, "android.utils.IMemory"); |
| 216 | |
Mathias Agopian | 83c0446 | 2009-05-22 19:00:22 -0700 | [diff] [blame] | 217 | BnMemory::BnMemory() { |
| 218 | } |
| 219 | |
Anu Sundararajan | 5728a92 | 2011-06-22 15:58:59 -0500 | [diff] [blame] | 220 | BnMemory::~BnMemory() { |
Mathias Agopian | 83c0446 | 2009-05-22 19:00:22 -0700 | [diff] [blame] | 221 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 222 | |
| 223 | status_t BnMemory::onTransact( |
| 224 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) |
| 225 | { |
| 226 | switch(code) { |
| 227 | case GET_MEMORY: { |
| 228 | CHECK_INTERFACE(IMemory, data, reply); |
| 229 | ssize_t offset; |
| 230 | size_t size; |
| 231 | reply->writeStrongBinder( getMemory(&offset, &size)->asBinder() ); |
| 232 | reply->writeInt32(offset); |
| 233 | reply->writeInt32(size); |
| 234 | return NO_ERROR; |
| 235 | } break; |
| 236 | default: |
| 237 | return BBinder::onTransact(code, data, reply, flags); |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | |
| 242 | /******************************************************************************/ |
| 243 | |
| 244 | BpMemoryHeap::BpMemoryHeap(const sp<IBinder>& impl) |
| 245 | : BpInterface<IMemoryHeap>(impl), |
Anu Sundararajan | 5728a92 | 2011-06-22 15:58:59 -0500 | [diff] [blame] | 246 | mHeapId(-1), mBase(MAP_FAILED), mSize(0), mFlags(0), mOffset(0), mRealHeap(false) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 247 | { |
| 248 | } |
| 249 | |
| 250 | BpMemoryHeap::~BpMemoryHeap() { |
| 251 | if (mHeapId != -1) { |
| 252 | close(mHeapId); |
| 253 | if (mRealHeap) { |
| 254 | // by construction we're the last one |
| 255 | if (mBase != MAP_FAILED) { |
| 256 | sp<IBinder> binder = const_cast<BpMemoryHeap*>(this)->asBinder(); |
| 257 | |
| 258 | if (VERBOSE) { |
Steve Block | 9d45368 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 259 | ALOGD("UNMAPPING binder=%p, heap=%p, size=%d, fd=%d", |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 260 | binder.get(), this, mSize, mHeapId); |
Mathias Agopian | cab25d6 | 2013-03-21 17:12:40 -0700 | [diff] [blame] | 261 | CallStack stack(LOG_TAG); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | munmap(mBase, mSize); |
| 265 | } |
| 266 | } else { |
| 267 | // remove from list only if it was mapped before |
| 268 | sp<IBinder> binder = const_cast<BpMemoryHeap*>(this)->asBinder(); |
| 269 | free_heap(binder); |
| 270 | } |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | void BpMemoryHeap::assertMapped() const |
| 275 | { |
| 276 | if (mHeapId == -1) { |
| 277 | sp<IBinder> binder(const_cast<BpMemoryHeap*>(this)->asBinder()); |
| 278 | sp<BpMemoryHeap> heap(static_cast<BpMemoryHeap*>(find_heap(binder).get())); |
| 279 | heap->assertReallyMapped(); |
| 280 | if (heap->mBase != MAP_FAILED) { |
| 281 | Mutex::Autolock _l(mLock); |
| 282 | if (mHeapId == -1) { |
| 283 | mBase = heap->mBase; |
| 284 | mSize = heap->mSize; |
Anu Sundararajan | 5728a92 | 2011-06-22 15:58:59 -0500 | [diff] [blame] | 285 | mOffset = heap->mOffset; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 286 | android_atomic_write( dup( heap->mHeapId ), &mHeapId ); |
| 287 | } |
| 288 | } else { |
| 289 | // something went wrong |
| 290 | free_heap(binder); |
| 291 | } |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | void BpMemoryHeap::assertReallyMapped() const |
| 296 | { |
| 297 | if (mHeapId == -1) { |
| 298 | |
| 299 | // remote call without mLock held, worse case scenario, we end up |
| 300 | // calling transact() from multiple threads, but that's not a problem, |
| 301 | // only mmap below must be in the critical section. |
Anu Sundararajan | 5728a92 | 2011-06-22 15:58:59 -0500 | [diff] [blame] | 302 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 303 | Parcel data, reply; |
| 304 | data.writeInterfaceToken(IMemoryHeap::getInterfaceDescriptor()); |
| 305 | status_t err = remote()->transact(HEAP_ID, data, &reply); |
| 306 | int parcel_fd = reply.readFileDescriptor(); |
| 307 | ssize_t size = reply.readInt32(); |
| 308 | uint32_t flags = reply.readInt32(); |
Anu Sundararajan | 5728a92 | 2011-06-22 15:58:59 -0500 | [diff] [blame] | 309 | uint32_t offset = reply.readInt32(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 310 | |
Elliott Hughes | c47f098 | 2013-03-14 02:56:34 +0000 | [diff] [blame] | 311 | ALOGE_IF(err, "binder=%p transaction failed fd=%d, size=%ld, err=%d (%s)", |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 312 | asBinder().get(), parcel_fd, size, err, strerror(-err)); |
| 313 | |
| 314 | int fd = dup( parcel_fd ); |
Elliott Hughes | c47f098 | 2013-03-14 02:56:34 +0000 | [diff] [blame] | 315 | ALOGE_IF(fd==-1, "cannot dup fd=%d, size=%ld, err=%d (%s)", |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 316 | parcel_fd, size, err, strerror(errno)); |
| 317 | |
| 318 | int access = PROT_READ; |
| 319 | if (!(flags & READ_ONLY)) { |
| 320 | access |= PROT_WRITE; |
| 321 | } |
| 322 | |
| 323 | Mutex::Autolock _l(mLock); |
| 324 | if (mHeapId == -1) { |
| 325 | mRealHeap = true; |
Anu Sundararajan | 5728a92 | 2011-06-22 15:58:59 -0500 | [diff] [blame] | 326 | mBase = mmap(0, size, access, MAP_SHARED, fd, offset); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 327 | if (mBase == MAP_FAILED) { |
Elliott Hughes | c47f098 | 2013-03-14 02:56:34 +0000 | [diff] [blame] | 328 | ALOGE("cannot map BpMemoryHeap (binder=%p), size=%ld, fd=%d (%s)", |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 329 | asBinder().get(), size, fd, strerror(errno)); |
| 330 | close(fd); |
| 331 | } else { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 332 | mSize = size; |
| 333 | mFlags = flags; |
Anu Sundararajan | 5728a92 | 2011-06-22 15:58:59 -0500 | [diff] [blame] | 334 | mOffset = offset; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 335 | android_atomic_write(fd, &mHeapId); |
| 336 | } |
| 337 | } |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | int BpMemoryHeap::getHeapID() const { |
| 342 | assertMapped(); |
| 343 | return mHeapId; |
| 344 | } |
| 345 | |
| 346 | void* BpMemoryHeap::getBase() const { |
| 347 | assertMapped(); |
| 348 | return mBase; |
| 349 | } |
| 350 | |
| 351 | size_t BpMemoryHeap::getSize() const { |
| 352 | assertMapped(); |
| 353 | return mSize; |
| 354 | } |
| 355 | |
| 356 | uint32_t BpMemoryHeap::getFlags() const { |
| 357 | assertMapped(); |
| 358 | return mFlags; |
| 359 | } |
| 360 | |
Anu Sundararajan | 5728a92 | 2011-06-22 15:58:59 -0500 | [diff] [blame] | 361 | uint32_t BpMemoryHeap::getOffset() const { |
| 362 | assertMapped(); |
| 363 | return mOffset; |
| 364 | } |
| 365 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 366 | // --------------------------------------------------------------------------- |
| 367 | |
| 368 | IMPLEMENT_META_INTERFACE(MemoryHeap, "android.utils.IMemoryHeap"); |
| 369 | |
Anu Sundararajan | 5728a92 | 2011-06-22 15:58:59 -0500 | [diff] [blame] | 370 | BnMemoryHeap::BnMemoryHeap() { |
Mathias Agopian | 83c0446 | 2009-05-22 19:00:22 -0700 | [diff] [blame] | 371 | } |
| 372 | |
Anu Sundararajan | 5728a92 | 2011-06-22 15:58:59 -0500 | [diff] [blame] | 373 | BnMemoryHeap::~BnMemoryHeap() { |
Mathias Agopian | 83c0446 | 2009-05-22 19:00:22 -0700 | [diff] [blame] | 374 | } |
| 375 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 376 | status_t BnMemoryHeap::onTransact( |
Mathias Agopian | 83c0446 | 2009-05-22 19:00:22 -0700 | [diff] [blame] | 377 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 378 | { |
| 379 | switch(code) { |
| 380 | case HEAP_ID: { |
| 381 | CHECK_INTERFACE(IMemoryHeap, data, reply); |
| 382 | reply->writeFileDescriptor(getHeapID()); |
| 383 | reply->writeInt32(getSize()); |
| 384 | reply->writeInt32(getFlags()); |
Anu Sundararajan | 5728a92 | 2011-06-22 15:58:59 -0500 | [diff] [blame] | 385 | reply->writeInt32(getOffset()); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 386 | return NO_ERROR; |
| 387 | } break; |
| 388 | default: |
| 389 | return BBinder::onTransact(code, data, reply, flags); |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | /*****************************************************************************/ |
| 394 | |
| 395 | HeapCache::HeapCache() |
| 396 | : DeathRecipient() |
| 397 | { |
| 398 | } |
| 399 | |
| 400 | HeapCache::~HeapCache() |
| 401 | { |
| 402 | } |
| 403 | |
| 404 | void HeapCache::binderDied(const wp<IBinder>& binder) |
| 405 | { |
Steve Block | 9d45368 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 406 | //ALOGD("binderDied binder=%p", binder.unsafe_get()); |
Anu Sundararajan | 5728a92 | 2011-06-22 15:58:59 -0500 | [diff] [blame] | 407 | free_heap(binder); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 408 | } |
| 409 | |
Anu Sundararajan | 5728a92 | 2011-06-22 15:58:59 -0500 | [diff] [blame] | 410 | sp<IMemoryHeap> HeapCache::find_heap(const sp<IBinder>& binder) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 411 | { |
| 412 | Mutex::Autolock _l(mHeapCacheLock); |
| 413 | ssize_t i = mHeapCache.indexOfKey(binder); |
| 414 | if (i>=0) { |
| 415 | heap_info_t& info = mHeapCache.editValueAt(i); |
Steve Block | 9d45368 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 416 | ALOGD_IF(VERBOSE, |
Anu Sundararajan | 5728a92 | 2011-06-22 15:58:59 -0500 | [diff] [blame] | 417 | "found binder=%p, heap=%p, size=%d, fd=%d, count=%d", |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 418 | binder.get(), info.heap.get(), |
| 419 | static_cast<BpMemoryHeap*>(info.heap.get())->mSize, |
| 420 | static_cast<BpMemoryHeap*>(info.heap.get())->mHeapId, |
| 421 | info.count); |
| 422 | android_atomic_inc(&info.count); |
| 423 | return info.heap; |
| 424 | } else { |
| 425 | heap_info_t info; |
| 426 | info.heap = interface_cast<IMemoryHeap>(binder); |
| 427 | info.count = 1; |
Steve Block | 9d45368 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 428 | //ALOGD("adding binder=%p, heap=%p, count=%d", |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 429 | // binder.get(), info.heap.get(), info.count); |
| 430 | mHeapCache.add(binder, info); |
| 431 | return info.heap; |
| 432 | } |
| 433 | } |
| 434 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 435 | void HeapCache::free_heap(const sp<IBinder>& binder) { |
| 436 | free_heap( wp<IBinder>(binder) ); |
| 437 | } |
| 438 | |
Anu Sundararajan | 5728a92 | 2011-06-22 15:58:59 -0500 | [diff] [blame] | 439 | void HeapCache::free_heap(const wp<IBinder>& binder) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 440 | { |
| 441 | sp<IMemoryHeap> rel; |
| 442 | { |
| 443 | Mutex::Autolock _l(mHeapCacheLock); |
| 444 | ssize_t i = mHeapCache.indexOfKey(binder); |
| 445 | if (i>=0) { |
| 446 | heap_info_t& info(mHeapCache.editValueAt(i)); |
| 447 | int32_t c = android_atomic_dec(&info.count); |
| 448 | if (c == 1) { |
Steve Block | 9d45368 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 449 | ALOGD_IF(VERBOSE, |
Anu Sundararajan | 5728a92 | 2011-06-22 15:58:59 -0500 | [diff] [blame] | 450 | "removing binder=%p, heap=%p, size=%d, fd=%d, count=%d", |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 451 | binder.unsafe_get(), info.heap.get(), |
| 452 | static_cast<BpMemoryHeap*>(info.heap.get())->mSize, |
| 453 | static_cast<BpMemoryHeap*>(info.heap.get())->mHeapId, |
| 454 | info.count); |
| 455 | rel = mHeapCache.valueAt(i).heap; |
| 456 | mHeapCache.removeItemsAt(i); |
| 457 | } |
| 458 | } else { |
Steve Block | e6f43dd | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 459 | ALOGE("free_heap binder=%p not found!!!", binder.unsafe_get()); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 460 | } |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | sp<IMemoryHeap> HeapCache::get_heap(const sp<IBinder>& binder) |
| 465 | { |
| 466 | sp<IMemoryHeap> realHeap; |
| 467 | Mutex::Autolock _l(mHeapCacheLock); |
| 468 | ssize_t i = mHeapCache.indexOfKey(binder); |
| 469 | if (i>=0) realHeap = mHeapCache.valueAt(i).heap; |
| 470 | else realHeap = interface_cast<IMemoryHeap>(binder); |
| 471 | return realHeap; |
| 472 | } |
| 473 | |
Anu Sundararajan | 5728a92 | 2011-06-22 15:58:59 -0500 | [diff] [blame] | 474 | void HeapCache::dump_heaps() |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 475 | { |
| 476 | Mutex::Autolock _l(mHeapCacheLock); |
| 477 | int c = mHeapCache.size(); |
| 478 | for (int i=0 ; i<c ; i++) { |
| 479 | const heap_info_t& info = mHeapCache.valueAt(i); |
| 480 | BpMemoryHeap const* h(static_cast<BpMemoryHeap const *>(info.heap.get())); |
Steve Block | 9d45368 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 481 | ALOGD("hey=%p, heap=%p, count=%d, (fd=%d, base=%p, size=%d)", |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 482 | mHeapCache.keyAt(i).unsafe_get(), |
Anu Sundararajan | 5728a92 | 2011-06-22 15:58:59 -0500 | [diff] [blame] | 483 | info.heap.get(), info.count, |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 484 | h->mHeapId, h->mBase, h->mSize); |
| 485 | } |
| 486 | } |
| 487 | |
| 488 | |
| 489 | // --------------------------------------------------------------------------- |
| 490 | }; // namespace android |