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