-
Notifications
You must be signed in to change notification settings - Fork 209
Expand file tree
/
Copy pathvirtual_memory.cpp
More file actions
257 lines (218 loc) · 8.19 KB
/
virtual_memory.cpp
File metadata and controls
257 lines (218 loc) · 8.19 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
// Copyright (C) 2015-2025 Jonathan Müller and foonathan/memory contributors
// SPDX-License-Identifier: Zlib
#include "virtual_memory.hpp"
#include "detail/debug_helpers.hpp"
#include "error.hpp"
#include "memory_arena.hpp"
using namespace foonathan::memory;
void detail::virtual_memory_allocator_leak_handler::operator()(std::ptrdiff_t amount)
{
detail::debug_handle_memory_leak({FOONATHAN_MEMORY_LOG_PREFIX "::virtual_memory_allocator",
nullptr},
amount);
}
#if defined(_WIN32)
#include <windows.h>
namespace
{
std::size_t get_page_size() noexcept
{
static_assert(sizeof(std::size_t) >= sizeof(DWORD), "possible loss of data");
SYSTEM_INFO info;
GetSystemInfo(&info);
return std::size_t(info.dwPageSize);
}
} // namespace
std::size_t foonathan::memory::get_virtual_memory_page_size() noexcept
{
static const std::size_t page_size = get_page_size();
return page_size;
}
#if defined(FOONATHAN_MEMORY_ALLOW_VIRTUAL_MEMORY_PAGE_SIZE)
const std::size_t foonathan::memory::virtual_memory_page_size =
foonathan::memory::get_virtual_memory_page_size();
#endif
void* foonathan::memory::virtual_memory_reserve(std::size_t no_pages) noexcept
{
auto pages =
#if (_MSC_VER <= 1900) || WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
VirtualAlloc(nullptr, no_pages * get_virtual_memory_page_size(), MEM_RESERVE,
PAGE_READWRITE);
#else
VirtualAllocFromApp(nullptr, no_pages * get_virtual_memory_page_size(), MEM_RESERVE,
PAGE_READWRITE);
#endif
return pages;
}
void foonathan::memory::virtual_memory_release(void* pages, std::size_t) noexcept
{
auto result = VirtualFree(pages, 0u, MEM_RELEASE);
FOONATHAN_MEMORY_ASSERT_MSG(result, "cannot release pages");
(void)result;
}
void* foonathan::memory::virtual_memory_commit(void* memory, std::size_t no_pages) noexcept
{
auto region =
#if (_MSC_VER <= 1900) || WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
VirtualAlloc(memory, no_pages * get_virtual_memory_page_size(), MEM_COMMIT, PAGE_READWRITE);
#else
VirtualAllocFromApp(memory, no_pages * get_virtual_memory_page_size(), MEM_COMMIT,
PAGE_READWRITE);
#endif
if (!region)
return nullptr;
FOONATHAN_MEMORY_ASSERT(region == memory);
return region;
}
void foonathan::memory::virtual_memory_decommit(void* memory, std::size_t no_pages) noexcept
{
auto result = VirtualFree(memory, no_pages * get_virtual_memory_page_size(), MEM_DECOMMIT);
FOONATHAN_MEMORY_ASSERT_MSG(result, "cannot decommit memory");
(void)result;
}
#elif defined(__unix__) || defined(__APPLE__) || defined(__VXWORKS__) \
|| defined(__QNXNTO__) // POSIX systems
#include <sys/mman.h>
#include <unistd.h>
std::size_t foonathan::memory::get_virtual_memory_page_size() noexcept
{
#if defined(PAGESIZE)
return PAGESIZE;
#elif defined(PAGE_SIZE)
return PAGE_SIZE;
#else
static const std::size_t page_size = static_cast<std::size_t>(sysconf(_SC_PAGESIZE));
return page_size;
#endif
}
#if defined(FOONATHAN_MEMORY_ALLOW_VIRTUAL_MEMORY_PAGE_SIZE)
const std::size_t foonathan::memory::virtual_memory_page_size =
foonathan::memory::get_virtual_memory_page_size();
#endif
#ifndef MAP_ANONYMOUS
#define MAP_ANONYMOUS MAP_ANON
#endif
void* foonathan::memory::virtual_memory_reserve(std::size_t no_pages) noexcept
{
auto pages = mmap(nullptr, no_pages * get_virtual_memory_page_size(), PROT_NONE,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
return pages == MAP_FAILED ? nullptr : pages;
}
void foonathan::memory::virtual_memory_release(void* pages, std::size_t no_pages) noexcept
{
auto result = munmap(pages, no_pages * get_virtual_memory_page_size());
FOONATHAN_MEMORY_ASSERT_MSG(result == 0, "cannot release pages");
(void)result;
}
void* foonathan::memory::virtual_memory_commit(void* memory, std::size_t no_pages) noexcept
{
auto size = no_pages * get_virtual_memory_page_size();
auto result = mprotect(memory, size, PROT_WRITE | PROT_READ);
if (result != 0)
return nullptr;
// advise that the memory will be needed
#if defined(MADV_WILLNEED)
madvise(memory, size, MADV_WILLNEED);
#elif defined(POSIX_MADV_WILLNEED)
posix_madvise(memory, size, POSIX_MADV_WILLNEED);
#endif
return memory;
}
void foonathan::memory::virtual_memory_decommit(void* memory, std::size_t no_pages) noexcept
{
auto size = no_pages * get_virtual_memory_page_size();
// advise that the memory won't be needed anymore
#if defined(MADV_FREE)
madvise(memory, size, MADV_FREE);
#elif defined(MADV_DONTNEED)
madvise(memory, size, MADV_DONTNEED);
#elif defined(POSIX_MADV_DONTNEED)
posix_madvise(memory, size, POSIX_MADV_DONTNEED);
#endif
auto result = mprotect(memory, size, PROT_NONE);
FOONATHAN_MEMORY_ASSERT_MSG(result == 0, "cannot decommit memory");
(void)result;
}
#else
#warning "virtual memory functions not available on your platform, define your own"
#endif
namespace
{
std::size_t calc_no_pages(std::size_t size) noexcept
{
auto const page_size = get_virtual_memory_page_size();
auto div = size / page_size;
auto rest = size % page_size;
return div + (rest != 0u) + (detail::debug_fence_size ? 2u : 1u);
}
} // namespace
void* virtual_memory_allocator::allocate_node(std::size_t size, std::size_t)
{
auto no_pages = calc_no_pages(size);
auto pages = virtual_memory_reserve(no_pages);
if (!pages || !virtual_memory_commit(pages, no_pages))
FOONATHAN_THROW(
out_of_memory({FOONATHAN_MEMORY_LOG_PREFIX "::virtual_memory_allocator", nullptr},
no_pages * get_virtual_memory_page_size()));
on_allocate(size);
return detail::debug_fill_new(pages, size, get_virtual_memory_page_size());
}
void virtual_memory_allocator::deallocate_node(void* node, std::size_t size, std::size_t) noexcept
{
auto pages = detail::debug_fill_free(node, size, get_virtual_memory_page_size());
on_deallocate(size);
auto no_pages = calc_no_pages(size);
virtual_memory_decommit(pages, no_pages);
virtual_memory_release(pages, no_pages);
}
std::size_t virtual_memory_allocator::max_node_size() const noexcept
{
return std::size_t(-1);
}
std::size_t virtual_memory_allocator::max_alignment() const noexcept
{
return get_virtual_memory_page_size();
}
#if FOONATHAN_MEMORY_EXTERN_TEMPLATE
template class foonathan::memory::allocator_traits<virtual_memory_allocator>;
#endif
virtual_block_allocator::virtual_block_allocator(std::size_t block_size, std::size_t no_blocks)
: block_size_(block_size)
{
auto const page_size = get_virtual_memory_page_size();
FOONATHAN_MEMORY_ASSERT(block_size % page_size == 0u);
FOONATHAN_MEMORY_ASSERT(no_blocks > 0);
auto total_size = block_size_ * no_blocks;
auto no_pages = total_size / page_size;
cur_ = static_cast<char*>(virtual_memory_reserve(no_pages));
if (!cur_)
FOONATHAN_THROW(out_of_memory(info(), total_size));
end_ = cur_ + total_size;
}
virtual_block_allocator::~virtual_block_allocator() noexcept
{
virtual_memory_release(cur_,
static_cast<std::size_t>(end_ - cur_) / get_virtual_memory_page_size());
}
memory_block virtual_block_allocator::allocate_block()
{
if (std::size_t(end_ - cur_) < block_size_)
FOONATHAN_THROW(out_of_fixed_memory(info(), block_size_));
auto mem = virtual_memory_commit(cur_, block_size_ / get_virtual_memory_page_size());
if (!mem)
FOONATHAN_THROW(out_of_fixed_memory(info(), block_size_));
cur_ += block_size_;
return {mem, block_size_};
}
void virtual_block_allocator::deallocate_block(memory_block block) noexcept
{
detail::debug_check_pointer([&]
{ return static_cast<char*>(block.memory) == cur_ - block_size_; },
info(), block.memory);
cur_ -= block_size_;
virtual_memory_decommit(cur_, block_size_ / get_virtual_memory_page_size());
}
allocator_info virtual_block_allocator::info() noexcept
{
return {FOONATHAN_MEMORY_LOG_PREFIX "::virtual_block_allocator", this};
}