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