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 | |
Hans Boehm | 4ce4db7 | 2016-07-12 18:05:49 -0700 | [diff] [blame] | 19 | #include <atomic> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 20 | #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 Agopian | c5b2c0b | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 29 | #include <binder/IMemory.h> |
Christopher Tate | 94b0d4e | 2016-02-05 19:02:56 -0800 | [diff] [blame] | 30 | #include <cutils/log.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 31 | #include <utils/KeyedVector.h> |
| 32 | #include <utils/threads.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; |
Hans Boehm | 4ce4db7 | 2016-07-12 18:05:49 -0700 | [diff] [blame] | 59 | // Note that this cannot be meaningfully copied. |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 60 | }; |
| 61 | |
Anu Sundararajan | 5728a92 | 2011-06-22 15:58:59 -0500 | [diff] [blame] | 62 | void free_heap(const wp<IBinder>& binder); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 63 | |
Hans Boehm | 4ce4db7 | 2016-07-12 18:05:49 -0700 | [diff] [blame] | 64 | Mutex mHeapCacheLock; // Protects entire vector below. |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 65 | KeyedVector< wp<IBinder>, heap_info_t > mHeapCache; |
Hans Boehm | 4ce4db7 | 2016-07-12 18:05:49 -0700 | [diff] [blame] | 66 | // We do not use the copy-on-write capabilities of KeyedVector. |
| 67 | // TODO: Reimplemement based on standard C++ container? |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 68 | }; |
| 69 | |
| 70 | static sp<HeapCache> gHeapCache = new HeapCache(); |
| 71 | |
| 72 | /******************************************************************************/ |
| 73 | |
| 74 | enum { |
| 75 | HEAP_ID = IBinder::FIRST_CALL_TRANSACTION |
| 76 | }; |
| 77 | |
| 78 | class BpMemoryHeap : public BpInterface<IMemoryHeap> |
| 79 | { |
| 80 | public: |
Chih-Hung Hsieh | e2347b7 | 2016-04-25 15:41:05 -0700 | [diff] [blame] | 81 | explicit BpMemoryHeap(const sp<IBinder>& impl); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 82 | 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 Sundararajan | 5728a92 | 2011-06-22 15:58:59 -0500 | [diff] [blame] | 88 | virtual uint32_t getOffset() const; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 89 | |
| 90 | private: |
| 91 | friend class IMemory; |
| 92 | friend class HeapCache; |
Anu Sundararajan | 5728a92 | 2011-06-22 15:58:59 -0500 | [diff] [blame] | 93 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 94 | // 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 Sundararajan | 5728a92 | 2011-06-22 15:58:59 -0500 | [diff] [blame] | 105 | gHeapCache->dump_heaps(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 106 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 107 | |
| 108 | void assertMapped() const; |
| 109 | void assertReallyMapped() const; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 110 | |
Hans Boehm | 4ce4db7 | 2016-07-12 18:05:49 -0700 | [diff] [blame] | 111 | mutable std::atomic<int32_t> mHeapId; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 112 | mutable void* mBase; |
| 113 | mutable size_t mSize; |
| 114 | mutable uint32_t mFlags; |
Anu Sundararajan | 5728a92 | 2011-06-22 15:58:59 -0500 | [diff] [blame] | 115 | mutable uint32_t mOffset; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 116 | mutable bool mRealHeap; |
| 117 | mutable Mutex mLock; |
| 118 | }; |
| 119 | |
| 120 | // ---------------------------------------------------------------------------- |
| 121 | |
| 122 | enum { |
| 123 | GET_MEMORY = IBinder::FIRST_CALL_TRANSACTION |
| 124 | }; |
| 125 | |
| 126 | class BpMemory : public BpInterface<IMemory> |
| 127 | { |
| 128 | public: |
Chih-Hung Hsieh | e2347b7 | 2016-04-25 15:41:05 -0700 | [diff] [blame] | 129 | explicit BpMemory(const sp<IBinder>& impl); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 130 | virtual ~BpMemory(); |
| 131 | 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] | 132 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 133 | private: |
| 134 | mutable sp<IMemoryHeap> mHeap; |
| 135 | mutable ssize_t mOffset; |
| 136 | mutable size_t mSize; |
| 137 | }; |
| 138 | |
| 139 | /******************************************************************************/ |
| 140 | |
| 141 | void* 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 | |
| 150 | void* 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 | |
| 159 | size_t IMemory::size() const { |
| 160 | size_t size; |
| 161 | getMemory(NULL, &size); |
| 162 | return size; |
| 163 | } |
| 164 | |
| 165 | ssize_t IMemory::offset() const { |
| 166 | ssize_t offset; |
| 167 | getMemory(&offset); |
| 168 | return offset; |
| 169 | } |
| 170 | |
| 171 | /******************************************************************************/ |
| 172 | |
| 173 | BpMemory::BpMemory(const sp<IBinder>& impl) |
| 174 | : BpInterface<IMemory>(impl), mOffset(0), mSize(0) |
| 175 | { |
| 176 | } |
| 177 | |
| 178 | BpMemory::~BpMemory() |
| 179 | { |
| 180 | } |
| 181 | |
| 182 | sp<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 Tate | 94b0d4e | 2016-02-05 19:02:56 -0800 | [diff] [blame] | 194 | 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 Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 207 | } |
| 208 | } |
| 209 | } |
| 210 | } |
| 211 | if (offset) *offset = mOffset; |
| 212 | if (size) *size = mSize; |
Christopher Tate | 94b0d4e | 2016-02-05 19:02:56 -0800 | [diff] [blame] | 213 | return (mSize > 0) ? mHeap : 0; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | // --------------------------------------------------------------------------- |
| 217 | |
| 218 | IMPLEMENT_META_INTERFACE(Memory, "android.utils.IMemory"); |
| 219 | |
Mathias Agopian | 83c0446 | 2009-05-22 19:00:22 -0700 | [diff] [blame] | 220 | BnMemory::BnMemory() { |
| 221 | } |
| 222 | |
Anu Sundararajan | 5728a92 | 2011-06-22 15:58:59 -0500 | [diff] [blame] | 223 | BnMemory::~BnMemory() { |
Mathias Agopian | 83c0446 | 2009-05-22 19:00:22 -0700 | [diff] [blame] | 224 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 225 | |
| 226 | status_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 Nelissen | 097ca27 | 2014-11-14 08:01:01 -0800 | [diff] [blame] | 234 | reply->writeStrongBinder( IInterface::asBinder(getMemory(&offset, &size)) ); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 235 | 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 | |
| 247 | BpMemoryHeap::BpMemoryHeap(const sp<IBinder>& impl) |
| 248 | : BpInterface<IMemoryHeap>(impl), |
Anu Sundararajan | 5728a92 | 2011-06-22 15:58:59 -0500 | [diff] [blame] | 249 | 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] | 250 | { |
| 251 | } |
| 252 | |
| 253 | BpMemoryHeap::~BpMemoryHeap() { |
Hans Boehm | 4ce4db7 | 2016-07-12 18:05:49 -0700 | [diff] [blame] | 254 | int32_t heapId = mHeapId.load(memory_order_relaxed); |
| 255 | if (heapId != -1) { |
| 256 | close(heapId); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 257 | if (mRealHeap) { |
| 258 | // by construction we're the last one |
| 259 | if (mBase != MAP_FAILED) { |
Marco Nelissen | 097ca27 | 2014-11-14 08:01:01 -0800 | [diff] [blame] | 260 | sp<IBinder> binder = IInterface::asBinder(this); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 261 | |
| 262 | if (VERBOSE) { |
Colin Cross | 6f4f3ab | 2014-02-05 17:42:44 -0800 | [diff] [blame] | 263 | ALOGD("UNMAPPING binder=%p, heap=%p, size=%zu, fd=%d", |
Hans Boehm | 4ce4db7 | 2016-07-12 18:05:49 -0700 | [diff] [blame] | 264 | binder.get(), this, mSize, heapId); |
Mathias Agopian | cab25d6 | 2013-03-21 17:12:40 -0700 | [diff] [blame] | 265 | CallStack stack(LOG_TAG); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | munmap(mBase, mSize); |
| 269 | } |
| 270 | } else { |
| 271 | // remove from list only if it was mapped before |
Marco Nelissen | 097ca27 | 2014-11-14 08:01:01 -0800 | [diff] [blame] | 272 | sp<IBinder> binder = IInterface::asBinder(this); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 273 | free_heap(binder); |
| 274 | } |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | void BpMemoryHeap::assertMapped() const |
| 279 | { |
Hans Boehm | 4ce4db7 | 2016-07-12 18:05:49 -0700 | [diff] [blame] | 280 | int32_t heapId = mHeapId.load(memory_order_acquire); |
| 281 | if (heapId == -1) { |
Marco Nelissen | 097ca27 | 2014-11-14 08:01:01 -0800 | [diff] [blame] | 282 | sp<IBinder> binder(IInterface::asBinder(const_cast<BpMemoryHeap*>(this))); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 283 | 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 Boehm | 4ce4db7 | 2016-07-12 18:05:49 -0700 | [diff] [blame] | 287 | if (mHeapId.load(memory_order_relaxed) == -1) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 288 | mBase = heap->mBase; |
| 289 | mSize = heap->mSize; |
Anu Sundararajan | 5728a92 | 2011-06-22 15:58:59 -0500 | [diff] [blame] | 290 | mOffset = heap->mOffset; |
Hans Boehm | 4ce4db7 | 2016-07-12 18:05:49 -0700 | [diff] [blame] | 291 | 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 Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 295 | } |
| 296 | } else { |
| 297 | // something went wrong |
| 298 | free_heap(binder); |
| 299 | } |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | void BpMemoryHeap::assertReallyMapped() const |
| 304 | { |
Hans Boehm | 4ce4db7 | 2016-07-12 18:05:49 -0700 | [diff] [blame] | 305 | int32_t heapId = mHeapId.load(memory_order_acquire); |
| 306 | if (heapId == -1) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 307 | |
| 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 Sundararajan | 5728a92 | 2011-06-22 15:58:59 -0500 | [diff] [blame] | 311 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 312 | 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 Sundararajan | 5728a92 | 2011-06-22 15:58:59 -0500 | [diff] [blame] | 318 | uint32_t offset = reply.readInt32(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 319 | |
Colin Cross | 6f4f3ab | 2014-02-05 17:42:44 -0800 | [diff] [blame] | 320 | ALOGE_IF(err, "binder=%p transaction failed fd=%d, size=%zd, err=%d (%s)", |
Marco Nelissen | 097ca27 | 2014-11-14 08:01:01 -0800 | [diff] [blame] | 321 | IInterface::asBinder(this).get(), |
| 322 | parcel_fd, size, err, strerror(-err)); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 323 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 324 | Mutex::Autolock _l(mLock); |
Hans Boehm | 4ce4db7 | 2016-07-12 18:05:49 -0700 | [diff] [blame] | 325 | if (mHeapId.load(memory_order_relaxed) == -1) { |
John Eckerdal | 6b0b063 | 2016-04-21 15:04:14 +0200 | [diff] [blame] | 326 | 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 Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 334 | mRealHeap = true; |
Anu Sundararajan | 5728a92 | 2011-06-22 15:58:59 -0500 | [diff] [blame] | 335 | mBase = mmap(0, size, access, MAP_SHARED, fd, offset); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 336 | if (mBase == MAP_FAILED) { |
Colin Cross | 6f4f3ab | 2014-02-05 17:42:44 -0800 | [diff] [blame] | 337 | ALOGE("cannot map BpMemoryHeap (binder=%p), size=%zd, fd=%d (%s)", |
Marco Nelissen | 097ca27 | 2014-11-14 08:01:01 -0800 | [diff] [blame] | 338 | IInterface::asBinder(this).get(), size, fd, strerror(errno)); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 339 | close(fd); |
| 340 | } else { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 341 | mSize = size; |
| 342 | mFlags = flags; |
Anu Sundararajan | 5728a92 | 2011-06-22 15:58:59 -0500 | [diff] [blame] | 343 | mOffset = offset; |
Hans Boehm | 4ce4db7 | 2016-07-12 18:05:49 -0700 | [diff] [blame] | 344 | mHeapId.store(fd, memory_order_release); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 345 | } |
| 346 | } |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | int BpMemoryHeap::getHeapID() const { |
| 351 | assertMapped(); |
Hans Boehm | 4ce4db7 | 2016-07-12 18:05:49 -0700 | [diff] [blame] | 352 | // We either stored mHeapId ourselves, or loaded it with acquire semantics. |
| 353 | return mHeapId.load(memory_order_relaxed); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | void* BpMemoryHeap::getBase() const { |
| 357 | assertMapped(); |
| 358 | return mBase; |
| 359 | } |
| 360 | |
| 361 | size_t BpMemoryHeap::getSize() const { |
| 362 | assertMapped(); |
| 363 | return mSize; |
| 364 | } |
| 365 | |
| 366 | uint32_t BpMemoryHeap::getFlags() const { |
| 367 | assertMapped(); |
| 368 | return mFlags; |
| 369 | } |
| 370 | |
Anu Sundararajan | 5728a92 | 2011-06-22 15:58:59 -0500 | [diff] [blame] | 371 | uint32_t BpMemoryHeap::getOffset() const { |
| 372 | assertMapped(); |
| 373 | return mOffset; |
| 374 | } |
| 375 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 376 | // --------------------------------------------------------------------------- |
| 377 | |
| 378 | IMPLEMENT_META_INTERFACE(MemoryHeap, "android.utils.IMemoryHeap"); |
| 379 | |
Anu Sundararajan | 5728a92 | 2011-06-22 15:58:59 -0500 | [diff] [blame] | 380 | BnMemoryHeap::BnMemoryHeap() { |
Mathias Agopian | 83c0446 | 2009-05-22 19:00:22 -0700 | [diff] [blame] | 381 | } |
| 382 | |
Anu Sundararajan | 5728a92 | 2011-06-22 15:58:59 -0500 | [diff] [blame] | 383 | BnMemoryHeap::~BnMemoryHeap() { |
Mathias Agopian | 83c0446 | 2009-05-22 19:00:22 -0700 | [diff] [blame] | 384 | } |
| 385 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 386 | status_t BnMemoryHeap::onTransact( |
Mathias Agopian | 83c0446 | 2009-05-22 19:00:22 -0700 | [diff] [blame] | 387 | 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] | 388 | { |
| 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 Sundararajan | 5728a92 | 2011-06-22 15:58:59 -0500 | [diff] [blame] | 395 | reply->writeInt32(getOffset()); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 396 | return NO_ERROR; |
| 397 | } break; |
| 398 | default: |
| 399 | return BBinder::onTransact(code, data, reply, flags); |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | /*****************************************************************************/ |
| 404 | |
| 405 | HeapCache::HeapCache() |
| 406 | : DeathRecipient() |
| 407 | { |
| 408 | } |
| 409 | |
| 410 | HeapCache::~HeapCache() |
| 411 | { |
| 412 | } |
| 413 | |
| 414 | void HeapCache::binderDied(const wp<IBinder>& binder) |
| 415 | { |
Steve Block | 9d45368 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 416 | //ALOGD("binderDied binder=%p", binder.unsafe_get()); |
Anu Sundararajan | 5728a92 | 2011-06-22 15:58:59 -0500 | [diff] [blame] | 417 | free_heap(binder); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 418 | } |
| 419 | |
Anu Sundararajan | 5728a92 | 2011-06-22 15:58:59 -0500 | [diff] [blame] | 420 | sp<IMemoryHeap> HeapCache::find_heap(const sp<IBinder>& binder) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 421 | { |
| 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 Block | 9d45368 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 426 | ALOGD_IF(VERBOSE, |
Colin Cross | 6f4f3ab | 2014-02-05 17:42:44 -0800 | [diff] [blame] | 427 | "found binder=%p, heap=%p, size=%zu, fd=%d, count=%d", |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 428 | binder.get(), info.heap.get(), |
| 429 | static_cast<BpMemoryHeap*>(info.heap.get())->mSize, |
Hans Boehm | 4ce4db7 | 2016-07-12 18:05:49 -0700 | [diff] [blame] | 430 | static_cast<BpMemoryHeap*>(info.heap.get()) |
| 431 | ->mHeapId.load(memory_order_relaxed), |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 432 | info.count); |
Hans Boehm | 4ce4db7 | 2016-07-12 18:05:49 -0700 | [diff] [blame] | 433 | ++info.count; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 434 | return info.heap; |
| 435 | } else { |
| 436 | heap_info_t info; |
| 437 | info.heap = interface_cast<IMemoryHeap>(binder); |
| 438 | info.count = 1; |
Steve Block | 9d45368 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 439 | //ALOGD("adding binder=%p, heap=%p, count=%d", |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 440 | // binder.get(), info.heap.get(), info.count); |
| 441 | mHeapCache.add(binder, info); |
| 442 | return info.heap; |
| 443 | } |
| 444 | } |
| 445 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 446 | void HeapCache::free_heap(const sp<IBinder>& binder) { |
| 447 | free_heap( wp<IBinder>(binder) ); |
| 448 | } |
| 449 | |
Anu Sundararajan | 5728a92 | 2011-06-22 15:58:59 -0500 | [diff] [blame] | 450 | void HeapCache::free_heap(const wp<IBinder>& binder) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 451 | { |
| 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 Boehm | 4ce4db7 | 2016-07-12 18:05:49 -0700 | [diff] [blame] | 458 | if (--info.count == 0) { |
Steve Block | 9d45368 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 459 | ALOGD_IF(VERBOSE, |
Colin Cross | 6f4f3ab | 2014-02-05 17:42:44 -0800 | [diff] [blame] | 460 | "removing binder=%p, heap=%p, size=%zu, fd=%d, count=%d", |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 461 | binder.unsafe_get(), info.heap.get(), |
| 462 | static_cast<BpMemoryHeap*>(info.heap.get())->mSize, |
Hans Boehm | 4ce4db7 | 2016-07-12 18:05:49 -0700 | [diff] [blame] | 463 | static_cast<BpMemoryHeap*>(info.heap.get()) |
| 464 | ->mHeapId.load(memory_order_relaxed), |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 465 | info.count); |
| 466 | rel = mHeapCache.valueAt(i).heap; |
| 467 | mHeapCache.removeItemsAt(i); |
| 468 | } |
| 469 | } else { |
Steve Block | e6f43dd | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 470 | 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] | 471 | } |
| 472 | } |
| 473 | } |
| 474 | |
| 475 | sp<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 Sundararajan | 5728a92 | 2011-06-22 15:58:59 -0500 | [diff] [blame] | 485 | void HeapCache::dump_heaps() |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 486 | { |
| 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 Cross | 6f4f3ab | 2014-02-05 17:42:44 -0800 | [diff] [blame] | 492 | ALOGD("hey=%p, heap=%p, count=%d, (fd=%d, base=%p, size=%zu)", |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 493 | mHeapCache.keyAt(i).unsafe_get(), |
Anu Sundararajan | 5728a92 | 2011-06-22 15:58:59 -0500 | [diff] [blame] | 494 | info.heap.get(), info.count, |
Hans Boehm | 4ce4db7 | 2016-07-12 18:05:49 -0700 | [diff] [blame] | 495 | h->mHeapId.load(memory_order_relaxed), h->mBase, h->mSize); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 496 | } |
| 497 | } |
| 498 | |
| 499 | |
| 500 | // --------------------------------------------------------------------------- |
| 501 | }; // namespace android |