summaryrefslogtreecommitdiff
path: root/src/mem_map.h
blob: 3c7361320effc08bff170c7de920d47483f70e8a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*
 * Copyright (C) 2008 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef ART_SRC_MEM_MAP_H_
#define ART_SRC_MEM_MAP_H_

#include <stddef.h>
#include <sys/types.h>

#include "globals.h"

namespace art {

// Used to keep track of mmap segments.
class MemMap {
 public:

  // Request an anonymous region of a specified length.
  //
  // On success, returns returns a MemMap instance.  On failure, returns a NULL;
  static MemMap* Map(size_t length, int prot) {
    return Map(NULL, length, prot);
  }

  // Request an anonymous region of a specified length and a requested base address.
  //
  // On success, returns returns a MemMap instance.  On failure, returns a NULL;
  static MemMap* Map(byte* addr, size_t length, int prot);

  // Map part of a file, taking care of non-page aligned offsets.  The
  // "start" offset is absolute, not relative.
  //
  // On success, returns returns a MemMap instance.  On failure, returns a NULL;
  static MemMap* Map(size_t length, int prot, int flags, int fd, off_t start) {
    return Map(NULL, length, prot, flags, fd, start);
  }

  // Map part of a file, taking care of non-page aligned offsets.  The
  // "start" offset is absolute, not relative. This version allows
  // requesting a specific address for the base of the mapping.
  //
  // On success, returns returns a MemMap instance.  On failure, returns a NULL;
  static MemMap* Map(byte* addr, size_t length, int prot, int flags, int fd, off_t start);

  // Releases the memory mapping
  ~MemMap();

  byte* GetAddress() const {
    return addr_;
  }

  size_t GetLength() const {
    return length_;
  }

  byte* GetLimit() const {
    return addr_ + length_;
  }

 private:
  MemMap(byte* addr, size_t length, void* base_addr, size_t base_length);

  byte*  addr_;              // start of data
  size_t length_;            // length of data

  void*  base_addr_;         // page-aligned base address
  size_t base_length_;       // length of mapping
};

}  // namespace art

#endif  // ART_SRC_MEM_MAP_H_