blob: 43a01e472cd6e51f0e9f39fa5d77094cdc345fda [file] [log] [blame]
Mathias Agopian7922fa22009-05-18 15:08:03 -07001/*
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 Agopian16475702009-05-19 19:08:10 -070032#include <binder/MemoryHeapBase.h>
Mathias Agopian7922fa22009-05-18 15:08:03 -070033
Mathias Agopian7922fa22009-05-18 15:08:03 -070034namespace android {
35
36// ---------------------------------------------------------------------------
37
Anu Sundararajan2498ce02011-06-22 15:58:59 -050038MemoryHeapBase::MemoryHeapBase()
Mathias Agopian7922fa22009-05-18 15:08:03 -070039 : mFD(-1), mSize(0), mBase(MAP_FAILED),
Anu Sundararajan2498ce02011-06-22 15:58:59 -050040 mDevice(NULL), mNeedUnmap(false), mOffset(0)
Mathias Agopian7922fa22009-05-18 15:08:03 -070041{
42}
43
44MemoryHeapBase::MemoryHeapBase(size_t size, uint32_t flags, char const * name)
45 : mFD(-1), mSize(0), mBase(MAP_FAILED), mFlags(flags),
Anu Sundararajan2498ce02011-06-22 15:58:59 -050046 mDevice(0), mNeedUnmap(false), mOffset(0)
Mathias Agopian7922fa22009-05-18 15:08:03 -070047{
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 Blockeafc6212012-01-06 19:20:56 +000051 ALOGE_IF(fd<0, "error creating ashmem region: %s", strerror(errno));
Mathias Agopian7922fa22009-05-18 15:08:03 -070052 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
61MemoryHeapBase::MemoryHeapBase(const char* device, size_t size, uint32_t flags)
62 : mFD(-1), mSize(0), mBase(MAP_FAILED), mFlags(flags),
Anu Sundararajan2498ce02011-06-22 15:58:59 -050063 mDevice(0), mNeedUnmap(false), mOffset(0)
Mathias Agopian7922fa22009-05-18 15:08:03 -070064{
Iliyan Malchev9e534012009-10-29 22:55:00 -070065 int open_flags = O_RDWR;
66 if (flags & NO_CACHING)
67 open_flags |= O_SYNC;
68
69 int fd = open(device, open_flags);
Steve Blockeafc6212012-01-06 19:20:56 +000070 ALOGE_IF(fd<0, "error opening %s: %s", device, strerror(errno));
Mathias Agopian7922fa22009-05-18 15:08:03 -070071 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 Wongb69dbf22009-08-17 15:28:30 -050080MemoryHeapBase::MemoryHeapBase(int fd, size_t size, uint32_t flags, uint32_t offset)
Mathias Agopian7922fa22009-05-18 15:08:03 -070081 : mFD(-1), mSize(0), mBase(MAP_FAILED), mFlags(flags),
Anu Sundararajan2498ce02011-06-22 15:58:59 -050082 mDevice(0), mNeedUnmap(false), mOffset(0)
Mathias Agopian7922fa22009-05-18 15:08:03 -070083{
84 const size_t pagesize = getpagesize();
85 size = ((size + pagesize-1) & ~(pagesize-1));
Benny Wongb69dbf22009-08-17 15:28:30 -050086 mapfd(dup(fd), size, offset);
Mathias Agopian7922fa22009-05-18 15:08:03 -070087}
88
89status_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 Wongb69dbf22009-08-17 15:28:30 -0500102status_t MemoryHeapBase::mapfd(int fd, size_t size, uint32_t offset)
Mathias Agopian7922fa22009-05-18 15:08:03 -0700103{
104 if (size == 0) {
105 // try to figure out the size automatically
Elliott Hughesd79b9942013-11-21 12:22:39 -0800106 struct stat sb;
107 if (fstat(fd, &sb) == 0)
108 size = sb.st_size;
Mathias Agopian7922fa22009-05-18 15:08:03 -0700109 // 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 Wongb69dbf22009-08-17 15:28:30 -0500114 PROT_READ|PROT_WRITE, MAP_SHARED, fd, offset);
Mathias Agopian7922fa22009-05-18 15:08:03 -0700115 if (base == MAP_FAILED) {
Steve Blockeafc6212012-01-06 19:20:56 +0000116 ALOGE("mmap(fd=%d, size=%u) failed (%s)",
Mathias Agopian7922fa22009-05-18 15:08:03 -0700117 fd, uint32_t(size), strerror(errno));
118 close(fd);
119 return -errno;
120 }
Steve Block52aa6df2011-12-20 16:23:08 +0000121 //ALOGD("mmap(fd=%d, base=%p, size=%lu)", fd, base, size);
Mathias Agopian7922fa22009-05-18 15:08:03 -0700122 mBase = base;
123 mNeedUnmap = true;
124 } else {
125 mBase = 0; // not MAP_FAILED
126 mNeedUnmap = false;
127 }
128 mFD = fd;
129 mSize = size;
Anu Sundararajan2498ce02011-06-22 15:58:59 -0500130 mOffset = offset;
Mathias Agopian7922fa22009-05-18 15:08:03 -0700131 return NO_ERROR;
132}
133
134MemoryHeapBase::~MemoryHeapBase()
135{
136 dispose();
137}
138
139void MemoryHeapBase::dispose()
140{
141 int fd = android_atomic_or(-1, &mFD);
142 if (fd >= 0) {
143 if (mNeedUnmap) {
Steve Block52aa6df2011-12-20 16:23:08 +0000144 //ALOGD("munmap(fd=%d, base=%p, size=%lu)", fd, mBase, mSize);
Mathias Agopian7922fa22009-05-18 15:08:03 -0700145 munmap(mBase, mSize);
146 }
147 mBase = 0;
148 mSize = 0;
149 close(fd);
150 }
151}
152
153int MemoryHeapBase::getHeapID() const {
154 return mFD;
155}
156
157void* MemoryHeapBase::getBase() const {
158 return mBase;
159}
160
161size_t MemoryHeapBase::getSize() const {
162 return mSize;
163}
164
165uint32_t MemoryHeapBase::getFlags() const {
166 return mFlags;
167}
168
169const char* MemoryHeapBase::getDevice() const {
170 return mDevice;
171}
172
Anu Sundararajan2498ce02011-06-22 15:58:59 -0500173uint32_t MemoryHeapBase::getOffset() const {
174 return mOffset;
175}
176
Mathias Agopian7922fa22009-05-18 15:08:03 -0700177// ---------------------------------------------------------------------------
178}; // namespace android