From e6749eb6b723142760939d8cf063478020c8cc71 Mon Sep 17 00:00:00 2001 From: stasoid Date: Sun, 27 Oct 2024 18:13:42 +0500 Subject: [PATCH] LibCore: Port AnonymousBuffer to Windows --- .../Libraries/LibCore/AnonymousBuffer.cpp | 5 -- Userland/Libraries/LibCore/AnonymousBuffer.h | 6 +- .../LibCore/AnonymousBufferWindows.cpp | 59 +++++++++++++++++++ Userland/Libraries/LibCore/CMakeLists.txt | 8 ++- 4 files changed, 71 insertions(+), 7 deletions(-) create mode 100644 Userland/Libraries/LibCore/AnonymousBufferWindows.cpp diff --git a/Userland/Libraries/LibCore/AnonymousBuffer.cpp b/Userland/Libraries/LibCore/AnonymousBuffer.cpp index 4f7906e4a6e..55004c7868f 100644 --- a/Userland/Libraries/LibCore/AnonymousBuffer.cpp +++ b/Userland/Libraries/LibCore/AnonymousBuffer.cpp @@ -43,11 +43,6 @@ ErrorOr AnonymousBuffer::create_from_anon_fd(int fd, size_t siz return AnonymousBuffer(move(impl)); } -AnonymousBuffer::AnonymousBuffer(NonnullRefPtr impl) - : m_impl(move(impl)) -{ -} - AnonymousBufferImpl::AnonymousBufferImpl(int fd, size_t size, void* data) : m_fd(fd) , m_size(size) diff --git a/Userland/Libraries/LibCore/AnonymousBuffer.h b/Userland/Libraries/LibCore/AnonymousBuffer.h index aa428bbd1c2..f82e5837c6b 100644 --- a/Userland/Libraries/LibCore/AnonymousBuffer.h +++ b/Userland/Libraries/LibCore/AnonymousBuffer.h @@ -18,6 +18,7 @@ namespace Core { class AnonymousBufferImpl final : public RefCounted { public: + static ErrorOr> create(size_t); static ErrorOr> create(int fd, size_t); ~AnonymousBufferImpl(); @@ -65,7 +66,10 @@ public: } private: - explicit AnonymousBuffer(NonnullRefPtr); + explicit AnonymousBuffer(NonnullRefPtr impl) + : m_impl(move(impl)) + { + } RefPtr m_impl; }; diff --git a/Userland/Libraries/LibCore/AnonymousBufferWindows.cpp b/Userland/Libraries/LibCore/AnonymousBufferWindows.cpp new file mode 100644 index 00000000000..91baddd0859 --- /dev/null +++ b/Userland/Libraries/LibCore/AnonymousBufferWindows.cpp @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2021, Andreas Kling + * Copyright (c) 2024, stasoid + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include +#include + +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> AnonymousBufferImpl::create(size_t size) +{ + HANDLE map_handle = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, HIWORD(size), LOWORD(size), NULL); + if (!map_handle) + return Error::from_windows_error(GetLastError()); + + return create((int)(intptr_t)map_handle, size); +} + +ErrorOr> 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::create_with_size(size_t size) +{ + auto impl = TRY(AnonymousBufferImpl::create(size)); + return AnonymousBuffer(move(impl)); +} + +ErrorOr AnonymousBuffer::create_from_anon_fd(int fd, size_t size) +{ + auto impl = TRY(AnonymousBufferImpl::create(fd, size)); + return AnonymousBuffer(move(impl)); +} + +} diff --git a/Userland/Libraries/LibCore/CMakeLists.txt b/Userland/Libraries/LibCore/CMakeLists.txt index e5967c1f4f4..38a7cb24e25 100644 --- a/Userland/Libraries/LibCore/CMakeLists.txt +++ b/Userland/Libraries/LibCore/CMakeLists.txt @@ -29,7 +29,6 @@ if (LAGOM_TOOLS_ONLY) endif() set(SOURCES - AnonymousBuffer.cpp Command.cpp ConfigFile.cpp DateTime.cpp @@ -54,6 +53,13 @@ set(SOURCES Timer.cpp UDPServer.cpp ) + +if (WIN32) + list(APPEND SOURCES AnonymousBufferWindows.cpp) +else() + list(APPEND SOURCES AnonymousBuffer.cpp) +endif() + if (NOT WIN32 AND NOT EMSCRIPTEN) list(APPEND SOURCES LocalServer.cpp) endif()