MemoryHeapBase.h revision d4851d74ac9c737acda85f1fdbc028c68f481a36
1edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project/*
2edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project * Copyright (C) 2008 The Android Open Source Project
3edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project *
4edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project * Licensed under the Apache License, Version 2.0 (the "License");
5edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project * you may not use this file except in compliance with the License.
6edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project * You may obtain a copy of the License at
7edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project *
8edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project *      http://www.apache.org/licenses/LICENSE-2.0
9edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project *
10edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project * Unless required by applicable law or agreed to in writing, software
11edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS,
12edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project * See the License for the specific language governing permissions and
14edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project * limitations under the License.
15edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project */
16edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
17edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#ifndef ANDROID_MEMORY_HEAP_BASE_H
18edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#define ANDROID_MEMORY_HEAP_BASE_H
19edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
20edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#include <stdlib.h>
21edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#include <stdint.h>
22edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
23c5b2c0bf8007562536b822eb060fc54a01f8e08bMathias Agopian#include <binder/IMemory.h>
24edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
25edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
26edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Projectnamespace android {
27edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
28edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project// ---------------------------------------------------------------------------
29edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
30edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Projectclass MemoryHeapBase : public virtual BnMemoryHeap
31edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project{
32edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Projectpublic:
33edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    enum {
34edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        READ_ONLY = IMemoryHeap::READ_ONLY,
35edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        MAP_ONCE = IMemoryHeap::MAP_ONCE,
36edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        // memory won't be mapped locally, but will be mapped in the remote
37edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        // process.
38edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        DONT_MAP_LOCALLY = 0x00000100
39edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    };
40edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
41edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    /*
42edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project     * maps the memory referenced by fd. but DOESN'T take ownership
43edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project     * of the filedescriptor (it makes a copy with dup()
44edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project     */
45d4851d74ac9c737acda85f1fdbc028c68f481a36Benny Wong    MemoryHeapBase(int fd, size_t size, uint32_t flags = 0, uint32_t offset = 0);
46edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
47edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    /*
48edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project     * maps memory from the given device
49edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project     */
50edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    MemoryHeapBase(const char* device, size_t size = 0, uint32_t flags = 0);
51edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
52edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    /*
53edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project     * maps memory from ashmem, with the given name for debugging
54edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project     */
55edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    MemoryHeapBase(size_t size, uint32_t flags = 0, char const* name = NULL);
56edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
57edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    virtual ~MemoryHeapBase();
58edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
59edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    /* implement IMemoryHeap interface */
60edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    virtual int         getHeapID() const;
61edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    virtual void*       getBase() const;
62edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    virtual size_t      getSize() const;
63edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    virtual uint32_t    getFlags() const;
64edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
65edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    const char*         getDevice() const;
66edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
67edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    /* this closes this heap -- use carefully */
68edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    void dispose();
69edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
70edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    /* this is only needed as a workaround, use only if you know
71edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project     * what you are doing */
72edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    status_t setDevice(const char* device) {
73edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        if (mDevice == 0)
74edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project            mDevice = device;
75edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        return mDevice ? NO_ERROR : ALREADY_EXISTS;
76edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    }
77edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
78edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Projectprotected:
79edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project            MemoryHeapBase();
80edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    // init() takes ownership of fd
81edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    status_t init(int fd, void *base, int size,
82edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project            int flags = 0, const char* device = NULL);
83edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
84edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Projectprivate:
85d4851d74ac9c737acda85f1fdbc028c68f481a36Benny Wong    status_t mapfd(int fd, size_t size, uint32_t offset = 0);
86edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
87edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    int         mFD;
88edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    size_t      mSize;
89edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    void*       mBase;
90edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    uint32_t    mFlags;
91edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    const char* mDevice;
92edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    bool        mNeedUnmap;
93edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project};
94edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
95edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project// ---------------------------------------------------------------------------
96edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}; // namespace android
97edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
98edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#endif // ANDROID_MEMORY_HEAP_BASE_H
99