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 | |
| 20 | #include <sys/mman.h> |
| 21 | |
| 22 | #include "utils.h" |
| 23 | |
| 24 | namespace art { |
| 25 | |
| 26 | // Used to keep track of mmap segments. |
| 27 | class MemMap { |
| 28 | public: |
| 29 | |
| 30 | // Request an anonymous region of a specified length. |
| 31 | // |
| 32 | // On success, returns returns a MemMap instance. On failure, returns a NULL; |
| 33 | static MemMap* Map(size_t length, int prot, int flags) { |
| 34 | size_t page_aligned_size = RoundUp(length, kPageSize); |
| 35 | byte* addr = reinterpret_cast<byte*>(mmap(NULL, |
| 36 | page_aligned_size, |
| 37 | prot, |
| 38 | MAP_ANONYMOUS | flags, |
| 39 | -1, |
| 40 | 0)); |
| 41 | if (addr == MAP_FAILED) { |
| 42 | return NULL; |
| 43 | } |
| 44 | return new MemMap(addr, length, addr, page_aligned_size); |
| 45 | } |
| 46 | |
| 47 | // Map part of a file, taking care of non-page aligned offsets. The |
| 48 | // "start" offset is absolute, not relative. |
| 49 | // |
| 50 | // On success, returns returns a MemMap instance. On failure, returns a NULL; |
| 51 | static MemMap* Map(size_t length, int prot, int flags, int fd, off_t start) { |
| 52 | // adjust to be page-aligned |
| 53 | int page_offset = start % kPageSize; |
| 54 | off_t page_aligned_offset = start - page_offset; |
| 55 | size_t page_aligned_size = length + page_offset; |
| 56 | byte* addr = reinterpret_cast<byte*>(mmap(NULL, |
| 57 | page_aligned_size, |
| 58 | prot, |
| 59 | MAP_FILE | flags, |
| 60 | fd, |
| 61 | page_aligned_offset)); |
| 62 | if (addr == MAP_FAILED) { |
| 63 | return NULL; |
| 64 | } |
| 65 | return new MemMap(addr+page_offset, length, addr, page_aligned_size); |
| 66 | } |
| 67 | |
| 68 | ~MemMap() { |
| 69 | Unmap(); |
Brian Carlstrom | 4e777d4 | 2011-08-15 13:53:52 -0700 | [diff] [blame^] | 70 | } |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 71 | |
| 72 | // Release a memory mapping, returning true on success or it was previously unmapped. |
| 73 | bool Unmap() { |
| 74 | if (base_addr_ == NULL && base_length_ == 0) { |
| 75 | return true; |
| 76 | } |
| 77 | int result = munmap(base_addr_, base_length_); |
| 78 | base_addr_ = NULL; |
| 79 | base_length_ = 0; |
| 80 | if (result == -1) { |
| 81 | return false; |
| 82 | } |
| 83 | return true; |
| 84 | } |
| 85 | |
| 86 | byte* GetAddress() const { |
| 87 | return addr_; |
| 88 | } |
| 89 | |
| 90 | size_t GetLength() const { |
| 91 | return length_; |
| 92 | } |
| 93 | |
| 94 | private: |
| 95 | MemMap(byte* addr, size_t length, void* base_addr, size_t base_length) |
| 96 | : addr_(addr), length_(length), base_addr_(base_addr), base_length_(base_length) { |
| 97 | CHECK(addr_ != NULL); |
| 98 | CHECK(length_ != 0); |
| 99 | CHECK(base_addr_ != NULL); |
| 100 | CHECK(base_length_ != 0); |
| 101 | }; |
| 102 | |
| 103 | byte* addr_; // start of data |
| 104 | size_t length_; // length of data |
| 105 | |
| 106 | void* base_addr_; // page-aligned base address |
| 107 | size_t base_length_; // length of mapping |
| 108 | }; |
| 109 | |
| 110 | } // namespace art |
| 111 | |
| 112 | #endif // ART_SRC_MEM_MAP_H_ |