LibCore: Consistently treat file descriptors as handles on Windows

Also:
 * implement dup and is_socket
 * implement and call init_crt_and_wsa
This commit is contained in:
stasoid 2025-01-07 14:04:16 +05:00 committed by Andrew Kaster
commit b77016cc34
Notes: github-actions[bot] 2025-02-06 02:28:45 +00:00
6 changed files with 120 additions and 49 deletions

View file

@ -1,12 +1,14 @@
/*
* Copyright (c) 2021, Andreas Kling <andreas@ladybird.org>
* Copyright (c) 2024, stasoid <stasoid@yahoo.com>
* Copyright (c) 2024-2025, stasoid <stasoid@yahoo.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibCore/AnonymousBuffer.h>
#include <windows.h>
#include <LibCore/System.h>
#include <AK/Windows.h>
namespace Core {
@ -23,25 +25,25 @@ AnonymousBufferImpl::~AnonymousBufferImpl()
VERIFY(UnmapViewOfFile(m_data));
if (m_fd != -1)
VERIFY(CloseHandle((HANDLE)(intptr_t)m_fd));
MUST(System::close(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);
HANDLE map_handle = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, size >> 32, size & 0xFFFFFFFF, NULL);
if (!map_handle)
return Error::from_windows_error();
return create((int)(intptr_t)map_handle, size);
return create(to_fd(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);
void* ptr = MapViewOfFile(to_handle(fd), FILE_MAP_ALL_ACCESS, 0, 0, size);
if (!ptr)
return Error::from_windows_error();
return adopt_nonnull_ref_or_enomem(new (nothrow) AnonymousBufferImpl(fd, size, ptr));
return adopt_ref(*new AnonymousBufferImpl(fd, size, ptr));
}
ErrorOr<AnonymousBuffer> AnonymousBuffer::create_with_size(size_t size)