Home | History | Annotate | Download | only in runtime
      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_RUNTIME_MEM_MAP_H_
     18 #define ART_RUNTIME_MEM_MAP_H_
     19 
     20 #include <string>
     21 
     22 #include <stddef.h>
     23 #include <sys/mman.h>  // For the PROT_* and MAP_* constants.
     24 #include <sys/types.h>
     25 
     26 #include "globals.h"
     27 
     28 namespace art {
     29 
     30 // Used to keep track of mmap segments.
     31 class MemMap {
     32  public:
     33   // Request an anonymous region of length 'byte_count' and a requested base address.
     34   // Use NULL as the requested base address if you don't care.
     35   //
     36   // The word "anonymous" in this context means "not backed by a file". The supplied
     37   // 'ashmem_name' will be used -- on systems that support it -- to give the mapping
     38   // a name.
     39   //
     40   // On success, returns returns a MemMap instance.  On failure, returns a NULL;
     41   static MemMap* MapAnonymous(const char* ashmem_name, byte* addr, size_t byte_count, int prot);
     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* MapFile(size_t byte_count, int prot, int flags, int fd, off_t start) {
     48     return MapFileAtAddress(NULL, byte_count, prot, flags, fd, start, false);
     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;
     56   static MemMap* MapFileAtAddress(
     57       byte* addr, size_t byte_count, int prot, int flags, int fd, off_t start, bool reuse);
     58 
     59   // Releases the memory mapping
     60   ~MemMap();
     61 
     62   bool Protect(int prot);
     63 
     64   int GetProtect() const {
     65     return prot_;
     66   }
     67 
     68   byte* Begin() const {
     69     return begin_;
     70   }
     71 
     72   size_t Size() const {
     73     return size_;
     74   }
     75 
     76   byte* End() const {
     77     return Begin() + Size();
     78   }
     79 
     80   bool HasAddress(const void* addr) const {
     81     return Begin() <= addr && addr < End();
     82   }
     83 
     84   // Trim by unmapping pages at the end of the map.
     85   void UnMapAtEnd(byte* new_end);
     86 
     87  private:
     88   MemMap(const std::string& name, byte* begin, size_t size, void* base_begin, size_t base_size,
     89          int prot);
     90 
     91   std::string name_;
     92   byte* const begin_;  // Start of data.
     93   size_t size_;  // Length of data.
     94 
     95   void* const base_begin_;  // Page-aligned base address.
     96   const size_t base_size_;  // Length of mapping.
     97   int prot_;  // Protection of the map.
     98 };
     99 
    100 }  // namespace art
    101 
    102 #endif  // ART_RUNTIME_MEM_MAP_H_
    103