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> |
| 21 | #include <sys/types.h> |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 22 | |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 23 | #include "globals.h" |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 24 | |
| 25 | namespace art { |
| 26 | |
| 27 | // Used to keep track of mmap segments. |
| 28 | class MemMap { |
| 29 | public: |
| 30 | |
| 31 | // Request an anonymous region of a specified length. |
| 32 | // |
| 33 | // On success, returns returns a MemMap instance. On failure, returns a NULL; |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 34 | static MemMap* Map(size_t length, int prot) { |
| 35 | return Map(NULL, length, prot); |
| 36 | } |
| 37 | |
| 38 | // Request an anonymous region of a specified length and a requested base address. |
| 39 | // |
| 40 | // On success, returns returns a MemMap instance. On failure, returns a NULL; |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 41 | static MemMap* Map(byte* addr, size_t length, int prot); |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 42 | |
| 43 | // Map part of a file, taking care of non-page aligned offsets. The |
| 44 | // "start" offset is absolute, not relative. |
| 45 | // |
| 46 | // On success, returns returns a MemMap instance. On failure, returns a NULL; |
| 47 | static MemMap* Map(size_t length, int prot, int flags, int fd, off_t start) { |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 48 | return Map(NULL, length, prot, flags, fd, start); |
| 49 | } |
| 50 | |
| 51 | // Map part of a file, taking care of non-page aligned offsets. The |
| 52 | // "start" offset is absolute, not relative. This version allows |
| 53 | // requesting a specific address for the base of the mapping. |
| 54 | // |
| 55 | // On success, returns returns a MemMap instance. On failure, returns a NULL; |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 56 | static MemMap* Map(byte* addr, size_t length, int prot, int flags, int fd, off_t start); |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 57 | |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 58 | // Releases the memory mapping |
| 59 | ~MemMap(); |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 60 | |
| 61 | byte* GetAddress() const { |
| 62 | return addr_; |
| 63 | } |
| 64 | |
| 65 | size_t GetLength() const { |
| 66 | return length_; |
| 67 | } |
| 68 | |
Brian Carlstrom | b765be0 | 2011-08-17 23:54:10 -0700 | [diff] [blame] | 69 | byte* GetLimit() const { |
| 70 | return addr_ + length_; |
| 71 | } |
| 72 | |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 73 | private: |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 74 | MemMap(byte* addr, size_t length, void* base_addr, size_t base_length); |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 75 | |
| 76 | byte* addr_; // start of data |
| 77 | size_t length_; // length of data |
| 78 | |
| 79 | void* base_addr_; // page-aligned base address |
| 80 | size_t base_length_; // length of mapping |
| 81 | }; |
| 82 | |
| 83 | } // namespace art |
| 84 | |
| 85 | #endif // ART_SRC_MEM_MAP_H_ |