1//===-- mem_map.cpp ---------------------------------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "mem_map.h"
10
11#include "common.h"
12
13namespace scudo {
14
15bool MemMapDefault::mapImpl(uptr Addr, uptr Size, const char *Name,
16 uptr Flags) {
17 void *MappedAddr =
18 ::scudo::map(Addr: reinterpret_cast<void *>(Addr), Size, Name, Flags, Data: &Data);
19 if (MappedAddr == nullptr)
20 return false;
21 Base = reinterpret_cast<uptr>(MappedAddr);
22 MappedBase = Base;
23 Capacity = Size;
24 return true;
25}
26
27void MemMapDefault::unmapImpl(uptr Addr, uptr Size) {
28 if (Size == Capacity) {
29 Base = MappedBase = Capacity = 0;
30 } else {
31 if (Base == Addr) {
32 Base = Addr + Size;
33 MappedBase = MappedBase == 0 ? Base : Max(A: MappedBase, B: Base);
34 }
35 Capacity -= Size;
36 }
37
38 ::scudo::unmap(Addr: reinterpret_cast<void *>(Addr), Size, UNMAP_ALL, Data: &Data);
39}
40
41bool MemMapDefault::remapImpl(uptr Addr, uptr Size, const char *Name,
42 uptr Flags) {
43 void *RemappedPtr =
44 ::scudo::map(Addr: reinterpret_cast<void *>(Addr), Size, Name, Flags, Data: &Data);
45 const uptr RemappedAddr = reinterpret_cast<uptr>(RemappedPtr);
46 MappedBase = MappedBase == 0 ? RemappedAddr : Min(A: MappedBase, B: RemappedAddr);
47 return RemappedAddr == Addr;
48}
49
50void MemMapDefault::releaseAndZeroPagesToOSImpl(uptr From, uptr Size) {
51 DCHECK_NE(MappedBase, 0U);
52 DCHECK_GE(From, MappedBase);
53 return ::scudo::releasePagesToOS(BaseAddress: MappedBase, Offset: From - MappedBase, Size, Data: &Data);
54}
55
56void MemMapDefault::setMemoryPermissionImpl(uptr Addr, uptr Size, uptr Flags) {
57 return ::scudo::setMemoryPermission(Addr, Size, Flags);
58}
59
60void ReservedMemoryDefault::releaseImpl() {
61 ::scudo::unmap(Addr: reinterpret_cast<void *>(Base), Size: Capacity, UNMAP_ALL, Data: &Data);
62}
63
64bool ReservedMemoryDefault::createImpl(uptr Addr, uptr Size, const char *Name,
65 uptr Flags) {
66 void *Reserved = ::scudo::map(Addr: reinterpret_cast<void *>(Addr), Size, Name,
67 Flags: Flags | MAP_NOACCESS, Data: &Data);
68 if (Reserved == nullptr)
69 return false;
70
71 Base = reinterpret_cast<uptr>(Reserved);
72 Capacity = Size;
73
74 return true;
75}
76
77ReservedMemoryDefault::MemMapT ReservedMemoryDefault::dispatchImpl(uptr Addr,
78 uptr Size) {
79 ReservedMemoryDefault::MemMapT NewMap(Addr, Size);
80 NewMap.setMapPlatformData(Data);
81 return NewMap;
82}
83
84} // namespace scudo
85

source code of compiler-rt/lib/scudo/standalone/mem_map.cpp