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 "MemoryHeapBase" |
| 18 | |
| 19 | #include <stdlib.h> |
| 20 | #include <stdint.h> |
| 21 | #include <unistd.h> |
| 22 | #include <fcntl.h> |
| 23 | #include <errno.h> |
| 24 | #include <sys/types.h> |
| 25 | #include <sys/stat.h> |
| 26 | #include <sys/ioctl.h> |
| 27 | |
| 28 | #include <cutils/log.h> |
| 29 | #include <cutils/ashmem.h> |
| 30 | #include <cutils/atomic.h> |
| 31 | |
Mathias Agopian | c5b2c0b | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 32 | #include <binder/MemoryHeapBase.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 33 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 34 | namespace android { |
| 35 | |
| 36 | // --------------------------------------------------------------------------- |
| 37 | |
Anu Sundararajan | 5728a92 | 2011-06-22 15:58:59 -0500 | [diff] [blame] | 38 | MemoryHeapBase::MemoryHeapBase() |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 39 | : mFD(-1), mSize(0), mBase(MAP_FAILED), |
Anu Sundararajan | 5728a92 | 2011-06-22 15:58:59 -0500 | [diff] [blame] | 40 | mDevice(NULL), mNeedUnmap(false), mOffset(0) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 41 | { |
| 42 | } |
| 43 | |
| 44 | MemoryHeapBase::MemoryHeapBase(size_t size, uint32_t flags, char const * name) |
| 45 | : mFD(-1), mSize(0), mBase(MAP_FAILED), mFlags(flags), |
Anu Sundararajan | 5728a92 | 2011-06-22 15:58:59 -0500 | [diff] [blame] | 46 | mDevice(0), mNeedUnmap(false), mOffset(0) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 47 | { |
| 48 | const size_t pagesize = getpagesize(); |
| 49 | size = ((size + pagesize-1) & ~(pagesize-1)); |
| 50 | int fd = ashmem_create_region(name == NULL ? "MemoryHeapBase" : name, size); |
Steve Block | e6f43dd | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 51 | ALOGE_IF(fd<0, "error creating ashmem region: %s", strerror(errno)); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 52 | if (fd >= 0) { |
| 53 | if (mapfd(fd, size) == NO_ERROR) { |
| 54 | if (flags & READ_ONLY) { |
| 55 | ashmem_set_prot_region(fd, PROT_READ); |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | MemoryHeapBase::MemoryHeapBase(const char* device, size_t size, uint32_t flags) |
| 62 | : mFD(-1), mSize(0), mBase(MAP_FAILED), mFlags(flags), |
Anu Sundararajan | 5728a92 | 2011-06-22 15:58:59 -0500 | [diff] [blame] | 63 | mDevice(0), mNeedUnmap(false), mOffset(0) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 64 | { |
Iliyan Malchev | 0db1a89 | 2009-10-29 22:55:00 -0700 | [diff] [blame] | 65 | int open_flags = O_RDWR; |
| 66 | if (flags & NO_CACHING) |
| 67 | open_flags |= O_SYNC; |
| 68 | |
| 69 | int fd = open(device, open_flags); |
Steve Block | e6f43dd | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 70 | ALOGE_IF(fd<0, "error opening %s: %s", device, strerror(errno)); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 71 | if (fd >= 0) { |
| 72 | const size_t pagesize = getpagesize(); |
| 73 | size = ((size + pagesize-1) & ~(pagesize-1)); |
| 74 | if (mapfd(fd, size) == NO_ERROR) { |
| 75 | mDevice = device; |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | |
Benny Wong | d4851d7 | 2009-08-17 15:28:30 -0500 | [diff] [blame] | 80 | MemoryHeapBase::MemoryHeapBase(int fd, size_t size, uint32_t flags, uint32_t offset) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 81 | : mFD(-1), mSize(0), mBase(MAP_FAILED), mFlags(flags), |
Anu Sundararajan | 5728a92 | 2011-06-22 15:58:59 -0500 | [diff] [blame] | 82 | mDevice(0), mNeedUnmap(false), mOffset(0) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 83 | { |
| 84 | const size_t pagesize = getpagesize(); |
| 85 | size = ((size + pagesize-1) & ~(pagesize-1)); |
Benny Wong | d4851d7 | 2009-08-17 15:28:30 -0500 | [diff] [blame] | 86 | mapfd(dup(fd), size, offset); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | status_t MemoryHeapBase::init(int fd, void *base, int size, int flags, const char* device) |
| 90 | { |
| 91 | if (mFD != -1) { |
| 92 | return INVALID_OPERATION; |
| 93 | } |
| 94 | mFD = fd; |
| 95 | mBase = base; |
| 96 | mSize = size; |
| 97 | mFlags = flags; |
| 98 | mDevice = device; |
| 99 | return NO_ERROR; |
| 100 | } |
| 101 | |
Benny Wong | d4851d7 | 2009-08-17 15:28:30 -0500 | [diff] [blame] | 102 | status_t MemoryHeapBase::mapfd(int fd, size_t size, uint32_t offset) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 103 | { |
| 104 | if (size == 0) { |
| 105 | // try to figure out the size automatically |
Elliott Hughes | a5a13a3 | 2013-11-21 12:22:39 -0800 | [diff] [blame] | 106 | struct stat sb; |
| 107 | if (fstat(fd, &sb) == 0) |
| 108 | size = sb.st_size; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 109 | // if it didn't work, let mmap() fail. |
| 110 | } |
| 111 | |
| 112 | if ((mFlags & DONT_MAP_LOCALLY) == 0) { |
| 113 | void* base = (uint8_t*)mmap(0, size, |
Benny Wong | d4851d7 | 2009-08-17 15:28:30 -0500 | [diff] [blame] | 114 | PROT_READ|PROT_WRITE, MAP_SHARED, fd, offset); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 115 | if (base == MAP_FAILED) { |
Steve Block | e6f43dd | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 116 | ALOGE("mmap(fd=%d, size=%u) failed (%s)", |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 117 | fd, uint32_t(size), strerror(errno)); |
| 118 | close(fd); |
| 119 | return -errno; |
| 120 | } |
Steve Block | 9d45368 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 121 | //ALOGD("mmap(fd=%d, base=%p, size=%lu)", fd, base, size); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 122 | mBase = base; |
| 123 | mNeedUnmap = true; |
| 124 | } else { |
| 125 | mBase = 0; // not MAP_FAILED |
| 126 | mNeedUnmap = false; |
| 127 | } |
| 128 | mFD = fd; |
| 129 | mSize = size; |
Anu Sundararajan | 5728a92 | 2011-06-22 15:58:59 -0500 | [diff] [blame] | 130 | mOffset = offset; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 131 | return NO_ERROR; |
| 132 | } |
| 133 | |
| 134 | MemoryHeapBase::~MemoryHeapBase() |
| 135 | { |
| 136 | dispose(); |
| 137 | } |
| 138 | |
| 139 | void MemoryHeapBase::dispose() |
| 140 | { |
| 141 | int fd = android_atomic_or(-1, &mFD); |
| 142 | if (fd >= 0) { |
| 143 | if (mNeedUnmap) { |
Steve Block | 9d45368 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 144 | //ALOGD("munmap(fd=%d, base=%p, size=%lu)", fd, mBase, mSize); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 145 | munmap(mBase, mSize); |
| 146 | } |
| 147 | mBase = 0; |
| 148 | mSize = 0; |
| 149 | close(fd); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | int MemoryHeapBase::getHeapID() const { |
| 154 | return mFD; |
| 155 | } |
| 156 | |
| 157 | void* MemoryHeapBase::getBase() const { |
| 158 | return mBase; |
| 159 | } |
| 160 | |
| 161 | size_t MemoryHeapBase::getSize() const { |
| 162 | return mSize; |
| 163 | } |
| 164 | |
| 165 | uint32_t MemoryHeapBase::getFlags() const { |
| 166 | return mFlags; |
| 167 | } |
| 168 | |
| 169 | const char* MemoryHeapBase::getDevice() const { |
| 170 | return mDevice; |
| 171 | } |
| 172 | |
Anu Sundararajan | 5728a92 | 2011-06-22 15:58:59 -0500 | [diff] [blame] | 173 | uint32_t MemoryHeapBase::getOffset() const { |
| 174 | return mOffset; |
| 175 | } |
| 176 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 177 | // --------------------------------------------------------------------------- |
| 178 | }; // namespace android |