blob: 38613273274b63a9e8c487937d97da26869ec0ba [file] [log] [blame]
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001/*
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 Carlstrom27ec9612011-09-19 20:20:38 -070020#include <stddef.h>
Elliott Hughesa168c832012-06-12 15:34:20 -070021#include <sys/mman.h> // For the PROT_* and MAP_* constants.
Brian Carlstrom27ec9612011-09-19 20:20:38 -070022#include <sys/types.h>
Brian Carlstromdb4d5402011-08-09 12:18:28 -070023
Brian Carlstrom27ec9612011-09-19 20:20:38 -070024#include "globals.h"
Brian Carlstromdb4d5402011-08-09 12:18:28 -070025
26namespace art {
27
28// Used to keep track of mmap segments.
29class MemMap {
30 public:
31
Elliott Hughesecd3a6f2012-06-06 18:16:37 -070032 // Request an anonymous region of length 'byte_count' and a requested base address.
Elliott Hughes6c9c06d2011-11-07 16:43:47 -080033 // Use NULL as the requested base address if you don't care.
34 //
35 // The word "anonymous" in this context means "not backed by a file". The supplied
36 // 'ashmem_name' will be used -- on systems that support it -- to give the mapping
37 // a name.
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070038 //
39 // On success, returns returns a MemMap instance. On failure, returns a NULL;
Elliott Hughesecd3a6f2012-06-06 18:16:37 -070040 static MemMap* MapAnonymous(const char* ashmem_name, byte* addr, size_t byte_count, int prot);
Brian Carlstromdb4d5402011-08-09 12:18:28 -070041
42 // Map part of a file, taking care of non-page aligned offsets. The
43 // "start" offset is absolute, not relative.
44 //
45 // On success, returns returns a MemMap instance. On failure, returns a NULL;
Elliott Hughesecd3a6f2012-06-06 18:16:37 -070046 static MemMap* MapFile(size_t byte_count, int prot, int flags, int fd, off_t start) {
47 return MapFileAtAddress(NULL, byte_count, prot, flags, fd, start);
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070048 }
49
50 // Map part of a file, taking care of non-page aligned offsets. The
51 // "start" offset is absolute, not relative. This version allows
52 // requesting a specific address for the base of the mapping.
53 //
54 // On success, returns returns a MemMap instance. On failure, returns a NULL;
Brian Carlstrom89521892011-12-07 22:05:07 -080055 static MemMap* MapFileAtAddress(
Elliott Hughesecd3a6f2012-06-06 18:16:37 -070056 byte* addr, size_t byte_count, int prot, int flags, int fd, off_t start);
Brian Carlstromdb4d5402011-08-09 12:18:28 -070057
Brian Carlstrom27ec9612011-09-19 20:20:38 -070058 // Releases the memory mapping
59 ~MemMap();
Brian Carlstromdb4d5402011-08-09 12:18:28 -070060
Logan Chiend88fa262012-06-06 15:23:32 +080061 bool Protect(int prot);
62
Ian Rogers30fab402012-01-23 15:43:46 -080063 byte* Begin() const {
64 return begin_;
Brian Carlstromdb4d5402011-08-09 12:18:28 -070065 }
66
Ian Rogers30fab402012-01-23 15:43:46 -080067 size_t Size() const {
68 return size_;
Brian Carlstromdb4d5402011-08-09 12:18:28 -070069 }
70
Ian Rogers30fab402012-01-23 15:43:46 -080071 byte* End() const {
72 return begin_ + size_;
Brian Carlstromb765be02011-08-17 23:54:10 -070073 }
74
Brian Carlstromdb4d5402011-08-09 12:18:28 -070075 private:
Ian Rogers30fab402012-01-23 15:43:46 -080076 MemMap(byte* begin, size_t size, void* base_begin, size_t base_size);
Brian Carlstromdb4d5402011-08-09 12:18:28 -070077
Ian Rogers30fab402012-01-23 15:43:46 -080078 byte* const begin_; // start of data
79 const size_t size_; // length of data
Brian Carlstromdb4d5402011-08-09 12:18:28 -070080
Ian Rogers30fab402012-01-23 15:43:46 -080081 void* const base_begin_; // page-aligned base address
82 const size_t base_size_; // length of mapping
Brian Carlstromdb4d5402011-08-09 12:18:28 -070083};
84
85} // namespace art
86
87#endif // ART_SRC_MEM_MAP_H_