ladybird/Libraries/LibCore/AnonymousBufferWindows.cpp
stasoid dabf3da7e5
Some checks are pending
CI / Lagom (false, FUZZ, ubuntu-24.04, Linux, Clang) (push) Waiting to run
CI / Lagom (false, NO_FUZZ, macos-15, macOS, Clang) (push) Waiting to run
CI / Lagom (false, NO_FUZZ, ubuntu-24.04, Linux, GNU) (push) Waiting to run
CI / Lagom (true, NO_FUZZ, ubuntu-24.04, Linux, Clang) (push) Waiting to run
Package the js repl as a binary artifact / build-and-package (macos-14, macOS, macOS-universal2) (push) Waiting to run
Package the js repl as a binary artifact / build-and-package (ubuntu-24.04, Linux, Linux-x86_64) (push) Waiting to run
Run test262 and test-wasm / run_and_update_results (push) Waiting to run
Lint Code / lint (push) Waiting to run
Push notes / build (push) Waiting to run
LibCore: Fix bug in CreateFileMapping call
size >> 31 >> 1 is used instead of size >> 32 to support 32-bit Windows
(size_t is 32 bit there, and you cannot shift 32-bit value by 32 bits
on x86)
This is equivalent to sizeof(size) == 4 ? 0 : size >> 32
2024-11-19 22:07:01 -07:00

59 lines
1.6 KiB
C++

/*
* Copyright (c) 2021, Andreas Kling <andreas@ladybird.org>
* Copyright (c) 2024, stasoid <stasoid@yahoo.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibCore/AnonymousBuffer.h>
#include <windows.h>
namespace Core {
AnonymousBufferImpl::AnonymousBufferImpl(int fd, size_t size, void* data)
: m_fd(fd)
, m_size(size)
, m_data(data)
{
}
AnonymousBufferImpl::~AnonymousBufferImpl()
{
if (m_data)
VERIFY(UnmapViewOfFile(m_data));
if (m_fd != -1)
VERIFY(CloseHandle((HANDLE)(intptr_t)m_fd));
}
ErrorOr<NonnullRefPtr<AnonymousBufferImpl>> AnonymousBufferImpl::create(size_t size)
{
HANDLE map_handle = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, size >> 31 >> 1, size & 0xFFFFFFFF, NULL);
if (!map_handle)
return Error::from_windows_error(GetLastError());
return create((int)(intptr_t)map_handle, size);
}
ErrorOr<NonnullRefPtr<AnonymousBufferImpl>> AnonymousBufferImpl::create(int fd, size_t size)
{
void* ptr = MapViewOfFile((HANDLE)(intptr_t)fd, FILE_MAP_ALL_ACCESS, 0, 0, size);
if (!ptr)
return Error::from_windows_error(GetLastError());
return adopt_nonnull_ref_or_enomem(new (nothrow) AnonymousBufferImpl(fd, size, ptr));
}
ErrorOr<AnonymousBuffer> AnonymousBuffer::create_with_size(size_t size)
{
auto impl = TRY(AnonymousBufferImpl::create(size));
return AnonymousBuffer(move(impl));
}
ErrorOr<AnonymousBuffer> AnonymousBuffer::create_from_anon_fd(int fd, size_t size)
{
auto impl = TRY(AnonymousBufferImpl::create(fd, size));
return AnonymousBuffer(move(impl));
}
}