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 #pragma once
18 
19 #include <stdlib.h>
20 #include <stdint.h>
21 
22 #include <binder/IMemory.h>
23 
24 
25 namespace android {
26 
27 // ---------------------------------------------------------------------------
28 
29 class MemoryHeapBase : public virtual BnMemoryHeap
30 {
31 public:
32     enum {
33         READ_ONLY = IMemoryHeap::READ_ONLY,
34         // memory won't be mapped locally, but will be mapped in the remote
35         // process.
36         DONT_MAP_LOCALLY = 0x00000100,
37         NO_CACHING = 0x00000200
38     };
39 
40     /*
41      * maps the memory referenced by fd. but DOESN'T take ownership
42      * of the filedescriptor (it makes a copy with dup()
43      */
44     MemoryHeapBase(int fd, size_t size, uint32_t flags = 0, off_t offset = 0);
45 
46     /*
47      * maps memory from the given device
48      */
49     explicit MemoryHeapBase(const char* device, size_t size = 0, uint32_t flags = 0);
50 
51     /*
52      * maps memory from ashmem, with the given name for debugging
53      * if the READ_ONLY flag is set, the memory will be writeable by the calling process,
54      * but not by others. this is NOT the case with the other ctors.
55      */
56     explicit MemoryHeapBase(size_t size, uint32_t flags = 0, char const* name = nullptr);
57 
58     virtual ~MemoryHeapBase();
59 
60     /* implement IMemoryHeap interface */
61     int         getHeapID() const override;
62 
63     /* virtual address of the heap. returns MAP_FAILED in case of error */
64     void*       getBase() const override;
65 
66     size_t      getSize() const override;
67     uint32_t    getFlags() const override;
68     off_t       getOffset() const override;
69 
70     const char*         getDevice() const;
71 
72     /* this closes this heap -- use carefully */
73     void dispose();
74 
75 protected:
76             MemoryHeapBase();
77     // init() takes ownership of fd
78     status_t init(int fd, void *base, size_t size,
79             int flags = 0, const char* device = nullptr);
80 
81 private:
82     status_t mapfd(int fd, bool writeableByCaller, size_t size, off_t offset = 0);
83 
84     int         mFD;
85     size_t      mSize;
86     void*       mBase;
87     uint32_t    mFlags;
88     const char* mDevice;
89     bool        mNeedUnmap;
90     off_t       mOffset;
91 };
92 
93 // ---------------------------------------------------------------------------
94 } // namespace android
95