The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | |
Mathias Agopian | c5b2c0b | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 23 | #include <binder/IMemory.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 24 | |
| 25 | |
| 26 | namespace android { |
| 27 | |
| 28 | // --------------------------------------------------------------------------- |
| 29 | |
Anu Sundararajan | 5728a92 | 2011-06-22 15:58:59 -0500 | [diff] [blame] | 30 | class MemoryHeapBase : public virtual BnMemoryHeap |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 31 | { |
| 32 | public: |
| 33 | enum { |
| 34 | READ_ONLY = IMemoryHeap::READ_ONLY, |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | // memory won't be mapped locally, but will be mapped in the remote |
| 36 | // process. |
Iliyan Malchev | 0db1a89 | 2009-10-29 22:55:00 -0700 | [diff] [blame] | 37 | DONT_MAP_LOCALLY = 0x00000100, |
| 38 | NO_CACHING = 0x00000200 |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 39 | }; |
| 40 | |
Anu Sundararajan | 5728a92 | 2011-06-22 15:58:59 -0500 | [diff] [blame] | 41 | /* |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 42 | * maps the memory referenced by fd. but DOESN'T take ownership |
| 43 | * of the filedescriptor (it makes a copy with dup() |
| 44 | */ |
Benny Wong | d4851d7 | 2009-08-17 15:28:30 -0500 | [diff] [blame] | 45 | MemoryHeapBase(int fd, size_t size, uint32_t flags = 0, uint32_t offset = 0); |
Anu Sundararajan | 5728a92 | 2011-06-22 15:58:59 -0500 | [diff] [blame] | 46 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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; |
Mathias Agopian | 7b19051 | 2012-09-25 15:28:44 -0700 | [diff] [blame] | 61 | |
| 62 | /* virtual address of the heap. returns MAP_FAILED in case of error */ |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 63 | virtual void* getBase() const; |
Mathias Agopian | 7b19051 | 2012-09-25 15:28:44 -0700 | [diff] [blame] | 64 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 65 | virtual size_t getSize() const; |
| 66 | virtual uint32_t getFlags() const; |
Mathias Agopian | 7b19051 | 2012-09-25 15:28:44 -0700 | [diff] [blame] | 67 | virtual uint32_t getOffset() const; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 68 | |
| 69 | const char* getDevice() const; |
Anu Sundararajan | 5728a92 | 2011-06-22 15:58:59 -0500 | [diff] [blame] | 70 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 71 | /* this closes this heap -- use carefully */ |
| 72 | void dispose(); |
| 73 | |
| 74 | /* this is only needed as a workaround, use only if you know |
| 75 | * what you are doing */ |
| 76 | status_t setDevice(const char* device) { |
| 77 | if (mDevice == 0) |
| 78 | mDevice = device; |
| 79 | return mDevice ? NO_ERROR : ALREADY_EXISTS; |
| 80 | } |
Anu Sundararajan | 5728a92 | 2011-06-22 15:58:59 -0500 | [diff] [blame] | 81 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 82 | protected: |
| 83 | MemoryHeapBase(); |
| 84 | // init() takes ownership of fd |
| 85 | status_t init(int fd, void *base, int size, |
Anu Sundararajan | 5728a92 | 2011-06-22 15:58:59 -0500 | [diff] [blame] | 86 | int flags = 0, const char* device = NULL); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 87 | |
| 88 | private: |
Benny Wong | d4851d7 | 2009-08-17 15:28:30 -0500 | [diff] [blame] | 89 | status_t mapfd(int fd, size_t size, uint32_t offset = 0); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 90 | |
| 91 | int mFD; |
| 92 | size_t mSize; |
| 93 | void* mBase; |
| 94 | uint32_t mFlags; |
| 95 | const char* mDevice; |
| 96 | bool mNeedUnmap; |
Anu Sundararajan | 5728a92 | 2011-06-22 15:58:59 -0500 | [diff] [blame] | 97 | uint32_t mOffset; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 98 | }; |
| 99 | |
| 100 | // --------------------------------------------------------------------------- |
| 101 | }; // namespace android |
| 102 | |
| 103 | #endif // ANDROID_MEMORY_HEAP_BASE_H |