1*8f0ba417SAndroid Build Coastguard Worker /* 2*8f0ba417SAndroid Build Coastguard Worker * Copyright (C) 2018 The Android Open Source Project 3*8f0ba417SAndroid Build Coastguard Worker * 4*8f0ba417SAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License"); 5*8f0ba417SAndroid Build Coastguard Worker * you may not use this file except in compliance with the License. 6*8f0ba417SAndroid Build Coastguard Worker * You may obtain a copy of the License at 7*8f0ba417SAndroid Build Coastguard Worker * 8*8f0ba417SAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0 9*8f0ba417SAndroid Build Coastguard Worker * 10*8f0ba417SAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software 11*8f0ba417SAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS, 12*8f0ba417SAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13*8f0ba417SAndroid Build Coastguard Worker * See the License for the specific language governing permissions and 14*8f0ba417SAndroid Build Coastguard Worker * limitations under the License. 15*8f0ba417SAndroid Build Coastguard Worker */ 16*8f0ba417SAndroid Build Coastguard Worker 17*8f0ba417SAndroid Build Coastguard Worker #pragma once 18*8f0ba417SAndroid Build Coastguard Worker 19*8f0ba417SAndroid Build Coastguard Worker #include <sys/types.h> 20*8f0ba417SAndroid Build Coastguard Worker 21*8f0ba417SAndroid Build Coastguard Worker #include <memory> 22*8f0ba417SAndroid Build Coastguard Worker 23*8f0ba417SAndroid Build Coastguard Worker #include "android-base/macros.h" 24*8f0ba417SAndroid Build Coastguard Worker #include "android-base/off64_t.h" 25*8f0ba417SAndroid Build Coastguard Worker #include "android-base/unique_fd.h" 26*8f0ba417SAndroid Build Coastguard Worker 27*8f0ba417SAndroid Build Coastguard Worker #if defined(_WIN32) 28*8f0ba417SAndroid Build Coastguard Worker #include <windows.h> 29*8f0ba417SAndroid Build Coastguard Worker #define PROT_READ 1 30*8f0ba417SAndroid Build Coastguard Worker #define PROT_WRITE 2 31*8f0ba417SAndroid Build Coastguard Worker using os_handle = HANDLE; 32*8f0ba417SAndroid Build Coastguard Worker #else 33*8f0ba417SAndroid Build Coastguard Worker #include <sys/mman.h> 34*8f0ba417SAndroid Build Coastguard Worker using os_handle = int; 35*8f0ba417SAndroid Build Coastguard Worker #endif 36*8f0ba417SAndroid Build Coastguard Worker 37*8f0ba417SAndroid Build Coastguard Worker namespace android { 38*8f0ba417SAndroid Build Coastguard Worker namespace base { 39*8f0ba417SAndroid Build Coastguard Worker 40*8f0ba417SAndroid Build Coastguard Worker /** 41*8f0ba417SAndroid Build Coastguard Worker * A region of a file mapped into memory (for grepping: also known as MmapFile or file mapping). 42*8f0ba417SAndroid Build Coastguard Worker */ 43*8f0ba417SAndroid Build Coastguard Worker class MappedFile { 44*8f0ba417SAndroid Build Coastguard Worker public: 45*8f0ba417SAndroid Build Coastguard Worker /** 46*8f0ba417SAndroid Build Coastguard Worker * Creates a new mapping of the file pointed to by `fd`. Unlike the underlying OS primitives, 47*8f0ba417SAndroid Build Coastguard Worker * `offset` does not need to be page-aligned. If `PROT_WRITE` is set in `prot`, the mapping 48*8f0ba417SAndroid Build Coastguard Worker * will be writable, otherwise it will be read-only. Mappings are always `MAP_SHARED`. 49*8f0ba417SAndroid Build Coastguard Worker */ 50*8f0ba417SAndroid Build Coastguard Worker static std::unique_ptr<MappedFile> FromFd(borrowed_fd fd, off64_t offset, size_t length, 51*8f0ba417SAndroid Build Coastguard Worker int prot); 52*8f0ba417SAndroid Build Coastguard Worker 53*8f0ba417SAndroid Build Coastguard Worker /** 54*8f0ba417SAndroid Build Coastguard Worker * Same thing, but using the raw OS file handle instead of a CRT wrapper. 55*8f0ba417SAndroid Build Coastguard Worker */ 56*8f0ba417SAndroid Build Coastguard Worker static std::unique_ptr<MappedFile> FromOsHandle(os_handle h, off64_t offset, size_t length, 57*8f0ba417SAndroid Build Coastguard Worker int prot); 58*8f0ba417SAndroid Build Coastguard Worker 59*8f0ba417SAndroid Build Coastguard Worker /** 60*8f0ba417SAndroid Build Coastguard Worker * Removes the mapping. 61*8f0ba417SAndroid Build Coastguard Worker */ 62*8f0ba417SAndroid Build Coastguard Worker ~MappedFile(); 63*8f0ba417SAndroid Build Coastguard Worker 64*8f0ba417SAndroid Build Coastguard Worker /** 65*8f0ba417SAndroid Build Coastguard Worker * Not copyable but movable. 66*8f0ba417SAndroid Build Coastguard Worker */ 67*8f0ba417SAndroid Build Coastguard Worker MappedFile(MappedFile&& other); 68*8f0ba417SAndroid Build Coastguard Worker MappedFile& operator=(MappedFile&& other); 69*8f0ba417SAndroid Build Coastguard Worker data()70*8f0ba417SAndroid Build Coastguard Worker char* data() const { return base_ + offset_; } size()71*8f0ba417SAndroid Build Coastguard Worker size_t size() const { return size_; } 72*8f0ba417SAndroid Build Coastguard Worker 73*8f0ba417SAndroid Build Coastguard Worker private: 74*8f0ba417SAndroid Build Coastguard Worker DISALLOW_IMPLICIT_CONSTRUCTORS(MappedFile); 75*8f0ba417SAndroid Build Coastguard Worker 76*8f0ba417SAndroid Build Coastguard Worker void Close(); 77*8f0ba417SAndroid Build Coastguard Worker 78*8f0ba417SAndroid Build Coastguard Worker char* base_; 79*8f0ba417SAndroid Build Coastguard Worker size_t size_; 80*8f0ba417SAndroid Build Coastguard Worker 81*8f0ba417SAndroid Build Coastguard Worker size_t offset_; 82*8f0ba417SAndroid Build Coastguard Worker 83*8f0ba417SAndroid Build Coastguard Worker #if defined(_WIN32) MappedFile(char * base,size_t size,size_t offset,HANDLE handle)84*8f0ba417SAndroid Build Coastguard Worker MappedFile(char* base, size_t size, size_t offset, HANDLE handle) 85*8f0ba417SAndroid Build Coastguard Worker : base_(base), size_(size), offset_(offset), handle_(handle) {} 86*8f0ba417SAndroid Build Coastguard Worker HANDLE handle_; 87*8f0ba417SAndroid Build Coastguard Worker #else MappedFile(char * base,size_t size,size_t offset)88*8f0ba417SAndroid Build Coastguard Worker MappedFile(char* base, size_t size, size_t offset) : base_(base), size_(size), offset_(offset) {} 89*8f0ba417SAndroid Build Coastguard Worker #endif 90*8f0ba417SAndroid Build Coastguard Worker }; 91*8f0ba417SAndroid Build Coastguard Worker 92*8f0ba417SAndroid Build Coastguard Worker } // namespace base 93*8f0ba417SAndroid Build Coastguard Worker } // namespace android 94