blob: ff89738789af31424544b298a4d7edfa80638e08 [file] [log] [blame]
The Android Open Source Projectd245d1d2008-10-21 07:00:00 -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#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 <utils/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 };
37
38 /*
39 * maps the memory referenced by fd. but DOESN'T take ownership
40 * of the filedescriptor (it makes a copy with dup()
41 */
42 MemoryHeapBase(int fd, size_t size, uint32_t flags = 0);
43
44 /*
45 * maps memory from the given device
46 */
47 MemoryHeapBase(const char* device, size_t size = 0, uint32_t flags = 0);
48
49 /*
50 * maps memory from ashmem, with the given name for debugging
51 */
52 MemoryHeapBase(size_t size, uint32_t flags = 0, char const* name = NULL);
53
54 virtual ~MemoryHeapBase();
55
56 /* implement IMemoryHeap interface */
57 virtual int getHeapID() const;
58 virtual void* getBase() const;
59 virtual size_t getSize() const;
60 virtual uint32_t getFlags() const;
61
62 const char* getDevice() const;
63
64 /* this closes this heap -- use carefully */
65 void dispose();
66
67 /* this is only needed as a workaround, use only if you know
68 * what you are doing */
69 status_t setDevice(const char* device) {
70 if (mDevice == 0)
71 mDevice = device;
72 return mDevice ? NO_ERROR : ALREADY_EXISTS;
73 }
74
75protected:
76 MemoryHeapBase();
77 // init() takes ownership of fd
78 status_t init(int fd, void *base, int size,
79 int flags = 0, const char* device = NULL);
80
81private:
82 status_t mapfd(int fd, size_t size);
83
84 int mFD;
85 size_t mSize;
86 void* mBase;
87 uint32_t mFlags;
88 const char* mDevice;
89 bool mNeedUnmap;
90};
91
92// ---------------------------------------------------------------------------
93}; // namespace android
94
95#endif // ANDROID_MEMORY_HEAP_BASE_H