blob: fa3904d535d1a0e506c5633ee3db5dad3dc2f10a [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>
21#include <sys/types.h>
Brian Carlstromdb4d5402011-08-09 12:18:28 -070022
Brian Carlstrom27ec9612011-09-19 20:20:38 -070023#include "globals.h"
Brian Carlstromdb4d5402011-08-09 12:18:28 -070024
25namespace art {
26
27// Used to keep track of mmap segments.
28class MemMap {
29 public:
30
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070031 // Request an anonymous region of a specified length and a requested base address.
Elliott Hughes6c9c06d2011-11-07 16:43:47 -080032 // Use NULL as the requested base address if you don't care.
33 //
34 // The word "anonymous" in this context means "not backed by a file". The supplied
35 // 'ashmem_name' will be used -- on systems that support it -- to give the mapping
36 // a name.
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070037 //
38 // On success, returns returns a MemMap instance. On failure, returns a NULL;
Elliott Hughes6c9c06d2011-11-07 16:43:47 -080039 static MemMap* Map(const char* ashmem_name, byte* addr, size_t length, int prot);
Brian Carlstromdb4d5402011-08-09 12:18:28 -070040
41 // Map part of a file, taking care of non-page aligned offsets. The
42 // "start" offset is absolute, not relative.
43 //
44 // On success, returns returns a MemMap instance. On failure, returns a NULL;
45 static MemMap* Map(size_t length, int prot, int flags, int fd, off_t start) {
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070046 return Map(NULL, length, prot, flags, fd, start);
47 }
48
49 // Map part of a file, taking care of non-page aligned offsets. The
50 // "start" offset is absolute, not relative. This version allows
51 // requesting a specific address for the base of the mapping.
52 //
53 // On success, returns returns a MemMap instance. On failure, returns a NULL;
Brian Carlstrom27ec9612011-09-19 20:20:38 -070054 static MemMap* Map(byte* addr, size_t length, int prot, int flags, int fd, off_t start);
Brian Carlstromdb4d5402011-08-09 12:18:28 -070055
Brian Carlstrom27ec9612011-09-19 20:20:38 -070056 // Releases the memory mapping
57 ~MemMap();
Brian Carlstromdb4d5402011-08-09 12:18:28 -070058
59 byte* GetAddress() const {
60 return addr_;
61 }
62
63 size_t GetLength() const {
64 return length_;
65 }
66
Brian Carlstromb765be02011-08-17 23:54:10 -070067 byte* GetLimit() const {
68 return addr_ + length_;
69 }
70
Brian Carlstromdb4d5402011-08-09 12:18:28 -070071 private:
Brian Carlstrom27ec9612011-09-19 20:20:38 -070072 MemMap(byte* addr, size_t length, void* base_addr, size_t base_length);
Brian Carlstromdb4d5402011-08-09 12:18:28 -070073
74 byte* addr_; // start of data
75 size_t length_; // length of data
76
77 void* base_addr_; // page-aligned base address
78 size_t base_length_; // length of mapping
79};
80
81} // namespace art
82
83#endif // ART_SRC_MEM_MAP_H_