LibCore: Port AnonymousBuffer to Windows

This commit is contained in:
stasoid 2024-10-27 18:13:42 +05:00 committed by Andrew Kaster
parent 457cdd0cc3
commit e6749eb6b7
Notes: github-actions[bot] 2024-11-09 19:41:30 +00:00
4 changed files with 71 additions and 7 deletions

View file

@ -43,11 +43,6 @@ ErrorOr<AnonymousBuffer> AnonymousBuffer::create_from_anon_fd(int fd, size_t siz
return AnonymousBuffer(move(impl)); return AnonymousBuffer(move(impl));
} }
AnonymousBuffer::AnonymousBuffer(NonnullRefPtr<AnonymousBufferImpl> impl)
: m_impl(move(impl))
{
}
AnonymousBufferImpl::AnonymousBufferImpl(int fd, size_t size, void* data) AnonymousBufferImpl::AnonymousBufferImpl(int fd, size_t size, void* data)
: m_fd(fd) : m_fd(fd)
, m_size(size) , m_size(size)

View file

@ -18,6 +18,7 @@ namespace Core {
class AnonymousBufferImpl final : public RefCounted<AnonymousBufferImpl> { class AnonymousBufferImpl final : public RefCounted<AnonymousBufferImpl> {
public: public:
static ErrorOr<NonnullRefPtr<AnonymousBufferImpl>> create(size_t);
static ErrorOr<NonnullRefPtr<AnonymousBufferImpl>> create(int fd, size_t); static ErrorOr<NonnullRefPtr<AnonymousBufferImpl>> create(int fd, size_t);
~AnonymousBufferImpl(); ~AnonymousBufferImpl();
@ -65,7 +66,10 @@ public:
} }
private: private:
explicit AnonymousBuffer(NonnullRefPtr<AnonymousBufferImpl>); explicit AnonymousBuffer(NonnullRefPtr<AnonymousBufferImpl> impl)
: m_impl(move(impl))
{
}
RefPtr<AnonymousBufferImpl> m_impl; RefPtr<AnonymousBufferImpl> m_impl;
}; };

View file

@ -0,0 +1,59 @@
/*
* 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, HIWORD(size), LOWORD(size), 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));
}
}

View file

@ -29,7 +29,6 @@ if (LAGOM_TOOLS_ONLY)
endif() endif()
set(SOURCES set(SOURCES
AnonymousBuffer.cpp
Command.cpp Command.cpp
ConfigFile.cpp ConfigFile.cpp
DateTime.cpp DateTime.cpp
@ -54,6 +53,13 @@ set(SOURCES
Timer.cpp Timer.cpp
UDPServer.cpp UDPServer.cpp
) )
if (WIN32)
list(APPEND SOURCES AnonymousBufferWindows.cpp)
else()
list(APPEND SOURCES AnonymousBuffer.cpp)
endif()
if (NOT WIN32 AND NOT EMSCRIPTEN) if (NOT WIN32 AND NOT EMSCRIPTEN)
list(APPEND SOURCES LocalServer.cpp) list(APPEND SOURCES LocalServer.cpp)
endif() endif()