blob: d793c240f95f414a42a260f101dc115945ce6bf4 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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
Mathias Agopian07952722009-05-19 19:08:10 -070023#include <binder/IMemory.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024
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.
Iliyan Malchevbaef6142009-10-29 22:55:00 -070038 DONT_MAP_LOCALLY = 0x00000100,
39 NO_CACHING = 0x00000200
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040 };
41
42 /*
43 * maps the memory referenced by fd. but DOESN'T take ownership
44 * of the filedescriptor (it makes a copy with dup()
45 */
Benny Wongf99c1802009-08-17 15:28:30 -050046 MemoryHeapBase(int fd, size_t size, uint32_t flags = 0, uint32_t offset = 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047
48 /*
49 * maps memory from the given device
50 */
51 MemoryHeapBase(const char* device, size_t size = 0, uint32_t flags = 0);
52
53 /*
54 * maps memory from ashmem, with the given name for debugging
55 */
56 MemoryHeapBase(size_t size, uint32_t flags = 0, char const* name = NULL);
57
58 virtual ~MemoryHeapBase();
59
60 /* implement IMemoryHeap interface */
61 virtual int getHeapID() const;
62 virtual void* getBase() const;
63 virtual size_t getSize() const;
64 virtual uint32_t getFlags() const;
65
66 const char* getDevice() const;
67
68 /* this closes this heap -- use carefully */
69 void dispose();
70
71 /* this is only needed as a workaround, use only if you know
72 * what you are doing */
73 status_t setDevice(const char* device) {
74 if (mDevice == 0)
75 mDevice = device;
76 return mDevice ? NO_ERROR : ALREADY_EXISTS;
77 }
78
79protected:
80 MemoryHeapBase();
81 // init() takes ownership of fd
82 status_t init(int fd, void *base, int size,
83 int flags = 0, const char* device = NULL);
84
85private:
Benny Wongf99c1802009-08-17 15:28:30 -050086 status_t mapfd(int fd, size_t size, uint32_t offset = 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087
88 int mFD;
89 size_t mSize;
90 void* mBase;
91 uint32_t mFlags;
92 const char* mDevice;
93 bool mNeedUnmap;
94};
95
96// ---------------------------------------------------------------------------
97}; // namespace android
98
99#endif // ANDROID_MEMORY_HEAP_BASE_H