86 static thread_local size_t last_handled_file_id = -1;
93 if (address >= link_file->memory && address < link_file->memory + link_file->length) {
99 if (file ==
nullptr) {
120 if (file->
id == last_handled_file_id) {
128 "Error: Unexpected exception in mapped file which was already remapped with zeros.");
132 last_handled_file_id = file->
id;
136 last_handled_file_id = file->
id;
140 print_error(
"Error: Could not replace mapped file with zeros.");
148using MapViewOfFile3Fn = PVOID(WINAPI *)(HANDLE FileMapping,
153 ULONG AllocationType,
154 ULONG PageProtection,
155 MEM_EXTENDED_PARAMETER *ExtendedParameters,
156 ULONG ParameterCount);
158using VirtualAlloc2Fn = PVOID(WINAPI *)(HANDLE Process,
161 ULONG AllocationType,
162 ULONG PageProtection,
163 MEM_EXTENDED_PARAMETER *ExtendedParameters,
164 ULONG ParameterCount);
169static MapViewOfFile3Fn mmap_MapViewOfFile3 =
nullptr;
171static VirtualAlloc2Fn mmap_VirtualAlloc2 =
nullptr;
177 HANDLE stderr_handle = GetStdHandle(STD_ERROR_HANDLE);
178 WriteFile(stderr_handle, buffer,
length,
nullptr,
nullptr);
183 if (!UnmapViewOfFileEx(file->memory, MEM_PRESERVE_PLACEHOLDER)) {
187 if (!CloseHandle(file->handle)) {
191 ULARGE_INTEGER length_ularge_int;
192 length_ularge_int.QuadPart = file->length;
193 file->handle = CreateFileMapping(INVALID_HANDLE_VALUE,
196 length_ularge_int.HighPart,
197 length_ularge_int.LowPart,
199 if (file->handle ==
nullptr) {
203 void *memory = mmap_MapViewOfFile3(file->handle,
208 MEM_REPLACE_PLACEHOLDER,
212 if (memory ==
nullptr) {
221static LONG page_exception_handler(EXCEPTION_POINTERS *ExceptionInfo)
noexcept
227 if (ExceptionInfo->ExceptionRecord->ExceptionCode == EXCEPTION_IN_PAGE_ERROR ||
228 ExceptionInfo->ExceptionRecord->ExceptionCode == EXCEPTION_ACCESS_VIOLATION)
230 if (ExceptionInfo->ExceptionRecord->NumberParameters >= 2) {
233 if (ExceptionInfo->ExceptionRecord->ExceptionInformation[0] == 1) {
234 return EXCEPTION_CONTINUE_SEARCH;
236 const void *address =
reinterpret_cast<const void *
>(
237 ExceptionInfo->ExceptionRecord->ExceptionInformation[1]);
239 return EXCEPTION_CONTINUE_EXECUTION;
243 return EXCEPTION_CONTINUE_SEARCH;
249 static std::atomic_bool initialized =
false;
257 HMODULE kernelbase = ::LoadLibraryA(
"kernelbase.dll");
259 mmap_MapViewOfFile3 =
reinterpret_cast<MapViewOfFile3Fn
>(
260 ::GetProcAddress(kernelbase,
"MapViewOfFile3"));
261 mmap_VirtualAlloc2 =
reinterpret_cast<VirtualAlloc2Fn
>(
262 ::GetProcAddress(kernelbase,
"VirtualAlloc2"));
264 if (mmap_MapViewOfFile3 && mmap_VirtualAlloc2) {
266 AddVectoredExceptionHandler(FALSE, page_exception_handler);
269 print_error(
"Could not load necessary functions for MMAP error handling.");
280 if (write(STDERR_FILENO, buffer,
length) < 0) {
288 const void *mapped_memory = mmap(
289 file->
memory, file->
length, PROT_READ, MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
290 if (mapped_memory == MAP_FAILED) {
329 static std::atomic_bool initialized =
false;
336 struct sigaction newact = {{
nullptr}}, oldact = {{
nullptr}};
339 newact.sa_flags = SA_SIGINFO;
341 if (sigaction(SIGBUS, &newact, &oldact)) {
371 static std::atomic_size_t id_counter = 0;
373 void *memory, *handle =
nullptr;
380 if (
length ==
size_t(-1)) [[unlikely]] {
391 memory = mmap(
nullptr,
length, PROT_READ, MAP_PRIVATE, fd, 0);
392 if (memory == MAP_FAILED) {
397 void *file_handle = (
void *)_get_osfhandle(fd);
404 if (mmap_MapViewOfFile3 && mmap_VirtualAlloc2) {
405 memory = mmap_VirtualAlloc2(
nullptr,
408 MEM_RESERVE | MEM_RESERVE_PLACEHOLDER,
412 if (memory ==
nullptr) {
416 handle = CreateFileMapping(file_handle,
nullptr, PAGE_READONLY, 0, 0,
nullptr);
417 if (handle ==
nullptr) {
418 VirtualFree(memory, 0, MEM_RELEASE);
422 if (mmap_MapViewOfFile3(handle,
427 MEM_REPLACE_PLACEHOLDER,
432 VirtualFree(memory, 0, MEM_RELEASE);
440 handle = CreateFileMapping(file_handle,
nullptr, PAGE_READONLY, 0, 0,
nullptr);
441 if (handle ==
nullptr) {
445 memory = MapViewOfFile(handle, FILE_MAP_READ, 0, 0, 0);
446 if (memory ==
nullptr) {
455 file->
memory =
static_cast<char *
>(memory);
458 file->
id = id_counter++;
498 munmap(
static_cast<void *
>(file->
memory), file->
length);
500 UnmapViewOfFile(file->
memory);
501 CloseHandle(file->
handle);
File and directory operations.
#define BLI_string_join(...)
Compatibility-like things for windows.
Read Blender memory allocation and freeing.
float length(VecOp< float, D >) RET
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
static bool try_map_zeros(BLI_mmap_file *file)
static bool ensure_mmap_initialized()
static void sigbus_handler(int sig, siginfo_t *siginfo, void *ptr) noexcept
size_t BLI_mmap_get_length(const BLI_mmap_file *file) ATTR_WARN_UNUSED_RESULT
BLI_mmap_file * BLI_mmap_open(int fd) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT
static Vector< BLI_mmap_file * > & open_mmaps_vector()
static bool try_handle_error_for_address(const void *address)
void BLI_mmap_free(BLI_mmap_file *file) ATTR_NONNULL(1)
void * MEM_new_zeroed(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
static struct sigaction next_handler
bool BLI_mmap_any_io_error(const BLI_mmap_file *file) ATTR_WARN_UNUSED_RESULT
bool BLI_mmap_read(BLI_mmap_file *file, void *dest, size_t offset, size_t length) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
static void print_error(const char *message)
void * BLI_mmap_get_pointer(BLI_mmap_file *file) ATTR_WARN_UNUSED_RESULT
size_t BLI_file_descriptor_size(int file) ATTR_WARN_UNUSED_RESULT
static void error_handler_add(BLI_mmap_file *file)
static void error_handler_remove(BLI_mmap_file *file)