MemoryHeapBase.cpp revision 25ba5b6564224dceefa086b5c439ef28dad530ca
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
32#include <utils/MemoryHeapBase.h>
33
34#if HAVE_ANDROID_OS
35#include <linux/android_pmem.h>
36#endif
37
38
39namespace android {
40
41// ---------------------------------------------------------------------------
42
43MemoryHeapBase::MemoryHeapBase()
44    : mFD(-1), mSize(0), mBase(MAP_FAILED),
45      mDevice(NULL), mNeedUnmap(false)
46{
47}
48
49MemoryHeapBase::MemoryHeapBase(size_t size, uint32_t flags, char const * name)
50    : mFD(-1), mSize(0), mBase(MAP_FAILED), mFlags(flags),
51      mDevice(0), mNeedUnmap(false)
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
66MemoryHeapBase::MemoryHeapBase(const char* device, size_t size, uint32_t flags)
67    : mFD(-1), mSize(0), mBase(MAP_FAILED), mFlags(flags),
68      mDevice(0), mNeedUnmap(false)
69{
70    int fd = open(device, O_RDWR);
71    LOGE_IF(fd<0, "error opening %s: %s", device, strerror(errno));
72    if (fd >= 0) {
73        const size_t pagesize = getpagesize();
74        size = ((size + pagesize-1) & ~(pagesize-1));
75        if (mapfd(fd, size) == NO_ERROR) {
76            mDevice = device;
77        }
78    }
79}
80
81MemoryHeapBase::MemoryHeapBase(int fd, size_t size, uint32_t flags)
82    : mFD(-1), mSize(0), mBase(MAP_FAILED), mFlags(flags),
83      mDevice(0), mNeedUnmap(false)
84{
85    const size_t pagesize = getpagesize();
86    size = ((size + pagesize-1) & ~(pagesize-1));
87    mapfd(dup(fd), size);
88}
89
90status_t MemoryHeapBase::init(int fd, void *base, int size, int flags, const char* device)
91{
92    if (mFD != -1) {
93        return INVALID_OPERATION;
94    }
95    mFD = fd;
96    mBase = base;
97    mSize = size;
98    mFlags = flags;
99    mDevice = device;
100    return NO_ERROR;
101}
102
103status_t MemoryHeapBase::mapfd(int fd, size_t size)
104{
105    if (size == 0) {
106        // try to figure out the size automatically
107#if HAVE_ANDROID_OS
108        // first try the PMEM ioctl
109        pmem_region reg;
110        int err = ioctl(fd, PMEM_GET_TOTAL_SIZE, &reg);
111        if (err == 0)
112            size = reg.len;
113#endif
114        if (size == 0) { // try fstat
115            struct stat sb;
116            if (fstat(fd, &sb) == 0)
117                size = sb.st_size;
118        }
119        // if it didn't work, let mmap() fail.
120    }
121
122    if ((mFlags & DONT_MAP_LOCALLY) == 0) {
123        void* base = (uint8_t*)mmap(0, size,
124                PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
125        if (base == MAP_FAILED) {
126            LOGE("mmap(fd=%d, size=%u) failed (%s)",
127                    fd, uint32_t(size), strerror(errno));
128            close(fd);
129            return -errno;
130        }
131        //LOGD("mmap(fd=%d, base=%p, size=%lu)", fd, base, size);
132        mBase = base;
133        mNeedUnmap = true;
134    } else  {
135        mBase = 0; // not MAP_FAILED
136        mNeedUnmap = false;
137    }
138    mFD = fd;
139    mSize = size;
140    return NO_ERROR;
141}
142
143MemoryHeapBase::~MemoryHeapBase()
144{
145    dispose();
146}
147
148void MemoryHeapBase::dispose()
149{
150    int fd = android_atomic_or(-1, &mFD);
151    if (fd >= 0) {
152        if (mNeedUnmap) {
153            //LOGD("munmap(fd=%d, base=%p, size=%lu)", fd, mBase, mSize);
154            munmap(mBase, mSize);
155        }
156        mBase = 0;
157        mSize = 0;
158        close(fd);
159    }
160}
161
162int MemoryHeapBase::getHeapID() const {
163    return mFD;
164}
165
166void* MemoryHeapBase::getBase() const {
167    return mBase;
168}
169
170size_t MemoryHeapBase::getSize() const {
171    return mSize;
172}
173
174uint32_t MemoryHeapBase::getFlags() const {
175    return mFlags;
176}
177
178const char* MemoryHeapBase::getDevice() const {
179    return mDevice;
180}
181
182// ---------------------------------------------------------------------------
183}; // namespace android
184