blob: 4c300b47c6931d70f3cb09834c229ea841f0b002 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
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
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080019#include <errno.h>
Mark Salyzyna5e161b2016-09-29 08:08:05 -070020#include <fcntl.h>
21#include <stdint.h>
22#include <stdlib.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080023#include <sys/ioctl.h>
Mark Salyzyna5e161b2016-09-29 08:08:05 -070024#include <sys/stat.h>
25#include <sys/types.h>
26#include <unistd.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080027
Mark Salyzyn7823e122016-09-29 08:08:05 -070028#include <binder/MemoryHeapBase.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080029#include <cutils/ashmem.h>
30#include <cutils/atomic.h>
Mark Salyzyn4dad9ce2016-09-29 08:08:05 -070031#include <log/log.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080032
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080033namespace android {
34
35// ---------------------------------------------------------------------------
36
Anu Sundararajan5728a922011-06-22 15:58:59 -050037MemoryHeapBase::MemoryHeapBase()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080038 : mFD(-1), mSize(0), mBase(MAP_FAILED),
Yi Kong91635562018-06-07 14:38:36 -070039 mDevice(nullptr), mNeedUnmap(false), mOffset(0)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080040{
41}
42
43MemoryHeapBase::MemoryHeapBase(size_t size, uint32_t flags, char const * name)
44 : mFD(-1), mSize(0), mBase(MAP_FAILED), mFlags(flags),
Yi Kong91635562018-06-07 14:38:36 -070045 mDevice(nullptr), mNeedUnmap(false), mOffset(0)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080046{
47 const size_t pagesize = getpagesize();
48 size = ((size + pagesize-1) & ~(pagesize-1));
Yi Kong91635562018-06-07 14:38:36 -070049 int fd = ashmem_create_region(name == nullptr ? "MemoryHeapBase" : name, size);
Steve Blocke6f43dd2012-01-06 19:20:56 +000050 ALOGE_IF(fd<0, "error creating ashmem region: %s", strerror(errno));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080051 if (fd >= 0) {
52 if (mapfd(fd, size) == NO_ERROR) {
53 if (flags & READ_ONLY) {
54 ashmem_set_prot_region(fd, PROT_READ);
55 }
56 }
57 }
58}
59
60MemoryHeapBase::MemoryHeapBase(const char* device, size_t size, uint32_t flags)
61 : mFD(-1), mSize(0), mBase(MAP_FAILED), mFlags(flags),
Yi Kong91635562018-06-07 14:38:36 -070062 mDevice(nullptr), mNeedUnmap(false), mOffset(0)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080063{
Iliyan Malchev0db1a892009-10-29 22:55:00 -070064 int open_flags = O_RDWR;
65 if (flags & NO_CACHING)
66 open_flags |= O_SYNC;
67
68 int fd = open(device, open_flags);
Steve Blocke6f43dd2012-01-06 19:20:56 +000069 ALOGE_IF(fd<0, "error opening %s: %s", device, strerror(errno));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080070 if (fd >= 0) {
71 const size_t pagesize = getpagesize();
72 size = ((size + pagesize-1) & ~(pagesize-1));
73 if (mapfd(fd, size) == NO_ERROR) {
74 mDevice = device;
75 }
76 }
77}
78
Andy Hung5b028522018-10-22 15:29:49 -070079MemoryHeapBase::MemoryHeapBase(int fd, size_t size, uint32_t flags, off_t offset)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080080 : mFD(-1), mSize(0), mBase(MAP_FAILED), mFlags(flags),
Yi Kong91635562018-06-07 14:38:36 -070081 mDevice(nullptr), mNeedUnmap(false), mOffset(0)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080082{
83 const size_t pagesize = getpagesize();
84 size = ((size + pagesize-1) & ~(pagesize-1));
Nick Kralevichec9ec7d2016-12-17 19:47:27 -080085 mapfd(fcntl(fd, F_DUPFD_CLOEXEC, 0), size, offset);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080086}
87
Andy Hung5b028522018-10-22 15:29:49 -070088status_t MemoryHeapBase::init(int fd, void *base, size_t size, int flags, const char* device)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080089{
90 if (mFD != -1) {
91 return INVALID_OPERATION;
92 }
93 mFD = fd;
94 mBase = base;
95 mSize = size;
96 mFlags = flags;
97 mDevice = device;
98 return NO_ERROR;
99}
100
Andy Hung5b028522018-10-22 15:29:49 -0700101status_t MemoryHeapBase::mapfd(int fd, size_t size, off_t offset)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800102{
103 if (size == 0) {
104 // try to figure out the size automatically
Elliott Hughesa5a13a32013-11-21 12:22:39 -0800105 struct stat sb;
Andy Hung5b028522018-10-22 15:29:49 -0700106 if (fstat(fd, &sb) == 0) {
107 size = (size_t)sb.st_size;
108 // sb.st_size is off_t which on ILP32 may be 64 bits while size_t is 32 bits.
109 if ((off_t)size != sb.st_size) {
110 ALOGE("%s: size of file %lld cannot fit in memory",
111 __func__, (long long)sb.st_size);
112 return INVALID_OPERATION;
113 }
114 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800115 // if it didn't work, let mmap() fail.
116 }
117
118 if ((mFlags & DONT_MAP_LOCALLY) == 0) {
Yi Kong91635562018-06-07 14:38:36 -0700119 void* base = (uint8_t*)mmap(nullptr, size,
Benny Wongd4851d72009-08-17 15:28:30 -0500120 PROT_READ|PROT_WRITE, MAP_SHARED, fd, offset);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800121 if (base == MAP_FAILED) {
Andy Hung5b028522018-10-22 15:29:49 -0700122 ALOGE("mmap(fd=%d, size=%zu) failed (%s)",
123 fd, size, strerror(errno));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800124 close(fd);
125 return -errno;
126 }
Andy Hung5b028522018-10-22 15:29:49 -0700127 //ALOGD("mmap(fd=%d, base=%p, size=%zu)", fd, base, size);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800128 mBase = base;
129 mNeedUnmap = true;
130 } else {
Yi Kong91635562018-06-07 14:38:36 -0700131 mBase = nullptr; // not MAP_FAILED
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800132 mNeedUnmap = false;
133 }
134 mFD = fd;
135 mSize = size;
Anu Sundararajan5728a922011-06-22 15:58:59 -0500136 mOffset = offset;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800137 return NO_ERROR;
138}
139
140MemoryHeapBase::~MemoryHeapBase()
141{
142 dispose();
143}
144
145void MemoryHeapBase::dispose()
146{
147 int fd = android_atomic_or(-1, &mFD);
148 if (fd >= 0) {
149 if (mNeedUnmap) {
Andy Hung5b028522018-10-22 15:29:49 -0700150 //ALOGD("munmap(fd=%d, base=%p, size=%zu)", fd, mBase, mSize);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800151 munmap(mBase, mSize);
152 }
Yi Kong91635562018-06-07 14:38:36 -0700153 mBase = nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800154 mSize = 0;
155 close(fd);
156 }
157}
158
159int MemoryHeapBase::getHeapID() const {
160 return mFD;
161}
162
163void* MemoryHeapBase::getBase() const {
164 return mBase;
165}
166
167size_t MemoryHeapBase::getSize() const {
168 return mSize;
169}
170
171uint32_t MemoryHeapBase::getFlags() const {
172 return mFlags;
173}
174
175const char* MemoryHeapBase::getDevice() const {
176 return mDevice;
177}
178
Andy Hung5b028522018-10-22 15:29:49 -0700179off_t MemoryHeapBase::getOffset() const {
Anu Sundararajan5728a922011-06-22 15:58:59 -0500180 return mOffset;
181}
182
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800183// ---------------------------------------------------------------------------
184}; // namespace android