Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [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 ART_SRC_MEM_MAP_H_ |
| 18 | #define ART_SRC_MEM_MAP_H_ |
| 19 | |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 20 | #include <stddef.h> |
Elliott Hughes | a168c83 | 2012-06-12 15:34:20 -0700 | [diff] [blame] | 21 | #include <sys/mman.h> // For the PROT_* and MAP_* constants. |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 22 | #include <sys/types.h> |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 23 | |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 24 | #include "globals.h" |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 25 | |
| 26 | namespace art { |
| 27 | |
| 28 | // Used to keep track of mmap segments. |
| 29 | class MemMap { |
| 30 | public: |
Elliott Hughes | ecd3a6f | 2012-06-06 18:16:37 -0700 | [diff] [blame] | 31 | // Request an anonymous region of length 'byte_count' and a requested base address. |
Elliott Hughes | 6c9c06d | 2011-11-07 16:43:47 -0800 | [diff] [blame] | 32 | // Use NULL as the requested base address if you don't care. |
| 33 | // |
| 34 | // The word "anonymous" in this context means "not backed by a file". The supplied |
| 35 | // 'ashmem_name' will be used -- on systems that support it -- to give the mapping |
| 36 | // a name. |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 37 | // |
| 38 | // On success, returns returns a MemMap instance. On failure, returns a NULL; |
Elliott Hughes | ecd3a6f | 2012-06-06 18:16:37 -0700 | [diff] [blame] | 39 | static MemMap* MapAnonymous(const char* ashmem_name, byte* addr, size_t byte_count, int prot); |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 40 | |
| 41 | // Map part of a file, taking care of non-page aligned offsets. The |
| 42 | // "start" offset is absolute, not relative. |
| 43 | // |
| 44 | // On success, returns returns a MemMap instance. On failure, returns a NULL; |
Elliott Hughes | ecd3a6f | 2012-06-06 18:16:37 -0700 | [diff] [blame] | 45 | static MemMap* MapFile(size_t byte_count, int prot, int flags, int fd, off_t start) { |
| 46 | return MapFileAtAddress(NULL, byte_count, prot, flags, fd, start); |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | // Map part of a file, taking care of non-page aligned offsets. The |
| 50 | // "start" offset is absolute, not relative. This version allows |
| 51 | // requesting a specific address for the base of the mapping. |
| 52 | // |
| 53 | // On success, returns returns a MemMap instance. On failure, returns a NULL; |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 54 | static MemMap* MapFileAtAddress( |
Elliott Hughes | ecd3a6f | 2012-06-06 18:16:37 -0700 | [diff] [blame] | 55 | byte* addr, size_t byte_count, int prot, int flags, int fd, off_t start); |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 56 | |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 57 | // Releases the memory mapping |
| 58 | ~MemMap(); |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 59 | |
Logan Chien | d88fa26 | 2012-06-06 15:23:32 +0800 | [diff] [blame] | 60 | bool Protect(int prot); |
| 61 | |
Ian Rogers | 1c849e5 | 2012-06-28 14:00:33 -0700 | [diff] [blame^] | 62 | int GetProtect() const { |
| 63 | return prot_; |
| 64 | } |
| 65 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 66 | byte* Begin() const { |
| 67 | return begin_; |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 68 | } |
| 69 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 70 | size_t Size() const { |
| 71 | return size_; |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 72 | } |
| 73 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 74 | byte* End() const { |
| 75 | return begin_ + size_; |
Brian Carlstrom | b765be0 | 2011-08-17 23:54:10 -0700 | [diff] [blame] | 76 | } |
| 77 | |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 78 | private: |
Ian Rogers | 1c849e5 | 2012-06-28 14:00:33 -0700 | [diff] [blame^] | 79 | MemMap(byte* begin, size_t size, void* base_begin, size_t base_size, int prot); |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 80 | |
Ian Rogers | 1c849e5 | 2012-06-28 14:00:33 -0700 | [diff] [blame^] | 81 | byte* const begin_; // Start of data. |
| 82 | const size_t size_; // Length of data. |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 83 | |
Ian Rogers | 1c849e5 | 2012-06-28 14:00:33 -0700 | [diff] [blame^] | 84 | void* const base_begin_; // Page-aligned base address. |
| 85 | const size_t base_size_; // Length of mapping. |
| 86 | int prot_; // Protection of the map. |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 87 | }; |
| 88 | |
| 89 | } // namespace art |
| 90 | |
| 91 | #endif // ART_SRC_MEM_MAP_H_ |