MemoryHeapBase.h revision f99c18083e87ccf83430b29b53c7dea52a1675de
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#ifndef ANDROID_MEMORY_HEAP_BASE_H
18#define ANDROID_MEMORY_HEAP_BASE_H
19
20#include <stdlib.h>
21#include <stdint.h>
22
23#include <binder/IMemory.h>
24
25
26namespace android {
27
28// ---------------------------------------------------------------------------
29
30class MemoryHeapBase : public virtual BnMemoryHeap
31{
32public:
33    enum {
34        READ_ONLY = IMemoryHeap::READ_ONLY,
35        MAP_ONCE = IMemoryHeap::MAP_ONCE,
36        // memory won't be mapped locally, but will be mapped in the remote
37        // process.
38        DONT_MAP_LOCALLY = 0x00000100
39    };
40
41    /*
42     * maps the memory referenced by fd. but DOESN'T take ownership
43     * of the filedescriptor (it makes a copy with dup()
44     */
45    MemoryHeapBase(int fd, size_t size, uint32_t flags = 0, uint32_t offset = 0);
46
47    /*
48     * maps memory from the given device
49     */
50    MemoryHeapBase(const char* device, size_t size = 0, uint32_t flags = 0);
51
52    /*
53     * maps memory from ashmem, with the given name for debugging
54     */
55    MemoryHeapBase(size_t size, uint32_t flags = 0, char const* name = NULL);
56
57    virtual ~MemoryHeapBase();
58
59    /* implement IMemoryHeap interface */
60    virtual int         getHeapID() const;
61    virtual void*       getBase() const;
62    virtual size_t      getSize() const;
63    virtual uint32_t    getFlags() const;
64
65    const char*         getDevice() const;
66
67    /* this closes this heap -- use carefully */
68    void dispose();
69
70    /* this is only needed as a workaround, use only if you know
71     * what you are doing */
72    status_t setDevice(const char* device) {
73        if (mDevice == 0)
74            mDevice = device;
75        return mDevice ? NO_ERROR : ALREADY_EXISTS;
76    }
77
78protected:
79            MemoryHeapBase();
80    // init() takes ownership of fd
81    status_t init(int fd, void *base, int size,
82            int flags = 0, const char* device = NULL);
83
84private:
85    status_t mapfd(int fd, size_t size, uint32_t offset = 0);
86
87    int         mFD;
88    size_t      mSize;
89    void*       mBase;
90    uint32_t    mFlags;
91    const char* mDevice;
92    bool        mNeedUnmap;
93};
94
95// ---------------------------------------------------------------------------
96}; // namespace android
97
98#endif // ANDROID_MEMORY_HEAP_BASE_H
99