Skip to content

Commit

Permalink
Merge pull request #9 from psiha/feature/flushing,resizeing_for_win32
Browse files Browse the repository at this point in the history
Feature/flushing,resizeing for win32
  • Loading branch information
psiha authored May 23, 2024
2 parents 71f3528 + ac37a5e commit 187ce42
Show file tree
Hide file tree
Showing 22 changed files with 312 additions and 198 deletions.
43 changes: 23 additions & 20 deletions .github/workflows/gh-actions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ jobs:
# To add more build types (Release, Debug, RelWithDebInfo, etc.) customize the build_type list.
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
build_type: [Release]
c_compiler: [clang, clang-cl]
build_type: [Debug, Release]
c_compiler: [clang, msvc]
include:
- os: windows-latest
c_compiler: clang-cl
cpp_compiler: clang-cl
c_compiler: msvc
cpp_compiler: msvc
- os: ubuntu-latest
c_compiler: clang
cpp_compiler: clang++
Expand All @@ -39,10 +39,14 @@ jobs:
exclude:
- os: windows-latest
c_compiler: clang
- os: windows-latest
build_type: Debug
- os: ubuntu-latest
c_compiler: clang-cl
c_compiler: msvc
- os: macos-latest
c_compiler: msvc
- os: macos-latest
c_compiler: clang-cl
build_type: Debug

steps:
- uses: actions/checkout@v3
Expand All @@ -53,26 +57,25 @@ jobs:
java-version: '17'
distribution: 'temurin'

- name: Install LLVM and Clang on Ubuntu
- name: Setup latest Xcode on MacOS
if: matrix.os == 'macos-latest'
uses: maxim-lobanov/setup-xcode@v1
with:
xcode-version: latest
- name: Install LLVM and Clang on Linux
if: matrix.os == 'ubuntu-latest'
run: |
wget https://apt.llvm.org/llvm.sh
chmod u+x llvm.sh
sudo ./llvm.sh 17 all
echo "PATH=/usr/lib/llvm-17/bin:$PATH" >> $GITHUB_ENV
echo "LLVM_PATH=/usr/lib/llvm-17" >> $GITHUB_ENV
echo "LD_LIBRARY_PATH=/usr/lib/llvm-17/lib" >> $GITHUB_ENV
echo "DYLD_LIBRARY_PATH=/usr/lib/llvm-17/lib" >> $GITHUB_ENV
echo "CC=/usr/lib/llvm-17/bin/clang" >> $GITHUB_ENV
echo "CXX=/usr/lib/llvm-17/bin/clang++" >> $GITHUB_ENV
sudo ./llvm.sh 18 all
echo "PATH=/usr/lib/llvm-18/bin:$PATH" >> $GITHUB_ENV
echo "LLVM_PATH=/usr/lib/llvm-18" >> $GITHUB_ENV
echo "LD_LIBRARY_PATH=/usr/lib/llvm-18/lib" >> $GITHUB_ENV
echo "DYLD_LIBRARY_PATH=/usr/lib/llvm-18/lib" >> $GITHUB_ENV
echo "CC=/usr/lib/llvm-18/bin/clang" >> $GITHUB_ENV
echo "CXX=/usr/lib/llvm-18/bin/clang++" >> $GITHUB_ENV
shell: sh

- name: Install LLVM and Clang on MacOS
if: matrix.os == 'macos-latest'
uses: KyleMayes/[email protected]
with:
version: "15.0.7"

- name: Set reusable strings
# Turn repeated input strings (such as the build output directory) into step outputs. These step outputs can be used throughout the workflow file.
id: strings
Expand Down
18 changes: 5 additions & 13 deletions include/psi/vm/allocation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,7 @@
#include <span>
#include <type_traits>
//------------------------------------------------------------------------------
namespace psi
{
//------------------------------------------------------------------------------
namespace vm
namespace psi::vm
{
//------------------------------------------------------------------------------

Expand All @@ -39,7 +36,7 @@ namespace vm
# pragma warning( disable : 5030 ) // Unrecognized attribute
#endif // _MSC_VER

#if defined( _WIN32 )
#if defined( _WIN32 ) //////////////////////////////////////////////////////////

enum class allocation_type : std::uint32_t
{
Expand All @@ -53,7 +50,7 @@ inline std::uint16_t constexpr page_size { 4 * 1024 };
inline std::uint16_t constexpr commit_granularity { 4 * 1024 };
inline std::uint32_t constexpr reserve_granularity{ 64 * 1024 };

#else // POSIX
#else // POSIX /////////////////////////////////////////////////////////////////

enum class allocation_type : int
{
Expand Down Expand Up @@ -86,7 +83,7 @@ static_assert( page_size == PAGE_SIZE );
inline std::uint16_t constexpr commit_granularity{ 4 * 1024 };
inline std::uint16_t constexpr reserve_granularity{ 4 * 1024 };

#endif // platform
#endif // platform /////////////////////////////////////////////////////////////

[[ gnu::assume_aligned( reserve_granularity ), gnu::malloc, nodiscard ]] void * allocate( std::size_t & size ) noexcept;
[[ gnu::assume_aligned( reserve_granularity ), gnu::malloc, nodiscard ]] void * reserve ( std::size_t & size ) noexcept;
Expand All @@ -101,9 +98,6 @@ inline std::uint16_t constexpr reserve_granularity{ 4 * 1024 };
[[ nodiscard ]] inline bool commit ( mapped_span const span ) noexcept { return commit ( span.data(), span.size() ); }
inline void decommit( mapped_span const span ) noexcept { return decommit( span.data(), span.size() ); }

// TODO madvise&co
// https://learn.microsoft.com/en-us/windows/win32/api/memoryapi/nf-memoryapi-prefetchvirtualmemory

struct [[ nodiscard ]] expand_result
{
mapped_span new_span;
Expand Down Expand Up @@ -150,7 +144,5 @@ expand_result expand
#endif // _MSC_VER

//------------------------------------------------------------------------------
} // namespace vm
//------------------------------------------------------------------------------
} // namespace psi
} // namespace psi::vm
//------------------------------------------------------------------------------
4 changes: 4 additions & 0 deletions include/psi/vm/detail/nt.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,17 @@ NTSTATUS constexpr STATUS_SUCCESS{ 0 };
auto constexpr STATUS_CONFLICTING_ADDRESSES{ NTSTATUS( 0xC0000018 ) };
#endif

inline auto const current_process{ reinterpret_cast<HANDLE>( std::intptr_t{ -1 } ) };

namespace detail
{
inline HMODULE const ntdll{ ::GetModuleHandleW( L"ntdll.dll" ) };

inline BOOST_ATTRIBUTES( BOOST_COLD, BOOST_RESTRICTED_FUNCTION_L3, BOOST_RESTRICTED_FUNCTION_RETURN )
::PROC BOOST_CC_REG get_nt_proc( char const * const proc_name ) noexcept
{
BOOST_ASSERT( current_process == ::GetCurrentProcess() ); // for lack of a better place for this sanity check

BOOST_ASSERT( ntdll );
auto const result{ ::GetProcAddress( ntdll, proc_name ) };
BOOST_ASSERT( result );
Expand Down
13 changes: 4 additions & 9 deletions include/psi/vm/flags/mapping.win32.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/// \file flags/mapping.win32.hpp
/// -----------------------------
///
/// Copyright (c) Domagoj Saric 2010 - 2018.
/// Copyright (c) Domagoj Saric 2010 - 2024.
///
/// Use, modification and distribution is subject to the
/// Boost Software License, Version 1.0.
Expand All @@ -23,10 +23,7 @@

#include <cstdint>
//------------------------------------------------------------------------------
namespace psi
{
//------------------------------------------------------------------------------
namespace vm
namespace psi::vm
{
//------------------------------------------------------------------------------
inline namespace win32
Expand All @@ -43,7 +40,7 @@ struct [[ clang::trivial_abi ]] viewing
{
using access_rights = access_privileges;

enum struct share_mode
enum struct share_mode : std::uint8_t
{
shared = 0,
hidden = 0x0001
Expand Down Expand Up @@ -105,7 +102,5 @@ struct mapping
//------------------------------------------------------------------------------
} // namespace win32
//------------------------------------------------------------------------------
} // namespace vm
//------------------------------------------------------------------------------
} // namespace psi
} // namespace psi::vm
//------------------------------------------------------------------------------
32 changes: 24 additions & 8 deletions include/psi/vm/handles/handle.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,31 +54,47 @@ class [[ clang::trivial_abi ]] handle_impl

inline static auto const invalid_value{ traits::invalid_value };

constexpr handle_impl( ) noexcept : handle_{ traits::invalid_value } { }
explicit constexpr handle_impl( native_handle_t const native_handle ) noexcept : handle_{ native_handle } { }
constexpr handle_impl( handle_impl && other ) noexcept : handle_{ other.handle_ } { other.handle_ = traits::invalid_value; }
~handle_impl( ) noexcept { traits::close( handle_ ); }
constexpr handle_impl( ) noexcept : handle_{ invalid_value } { }
explicit constexpr handle_impl( native_handle_t const native_handle ) noexcept : handle_{ native_handle } { }
constexpr handle_impl( handle_impl && other ) noexcept : handle_{ other.handle_ } { other.handle_ = invalid_value; }
~handle_impl( ) noexcept { traits::close( handle_ ); }

handle_impl & operator=( handle_impl && __restrict other ) noexcept
{
close();
this->handle_ = other.handle_;
other.handle_ = traits::invalid_value;
other.handle_ = invalid_value;
return *this;
}

void close() noexcept { traits::close( release() ); }
void reset( native_handle_t const new_handle ) noexcept
{
auto const old_handle{ handle_ };
handle_ = new_handle;
if constexpr ( requires{ __builtin_constant_p( handle_ ); } ) //...mrmlj...TODO deduplicate
if ( __builtin_constant_p( handle_ ) && handle_ == invalid_value )
return;
traits::close( old_handle );
}

void close() noexcept
{
if constexpr ( requires{ __builtin_constant_p( handle_ ); } )
if ( __builtin_constant_p( handle_ ) && handle_ == invalid_value )
return;
traits::close( release() );
}

native_handle_t release() noexcept
{
auto const result{ handle_ };
handle_ = traits::invalid_value;
handle_ = invalid_value;
return result;
}

native_handle_t get() const noexcept { return handle_; }

explicit operator bool() const noexcept { return handle_ != traits::invalid_value; }
explicit operator bool() const noexcept { return handle_ != invalid_value; }

private:
native_handle_t handle_;
Expand Down
9 changes: 2 additions & 7 deletions include/psi/vm/handles/handle.posix.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,7 @@

#include <cerrno>
//------------------------------------------------------------------------------
namespace psi
{
//------------------------------------------------------------------------------
namespace vm
namespace psi::vm
{
//------------------------------------------------------------------------------
PSI_VM_POSIX_INLINE
Expand Down Expand Up @@ -70,7 +67,5 @@ using handle = handle_impl<handle_traits>;
//------------------------------------------------------------------------------
} // namespace posix
//------------------------------------------------------------------------------
} // namespace vm
//------------------------------------------------------------------------------
} // namespace psi
} // namespace psi::vm
//------------------------------------------------------------------------------
9 changes: 2 additions & 7 deletions include/psi/vm/handles/handle.win32.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,7 @@

#include <cstdint>
//------------------------------------------------------------------------------
namespace psi
{
//------------------------------------------------------------------------------
namespace vm
namespace psi::vm
{
//------------------------------------------------------------------------------
inline namespace win32
Expand Down Expand Up @@ -66,7 +63,5 @@ using handle = handle_impl<handle_traits>;
//------------------------------------------------------------------------------
} // namespace win32
//------------------------------------------------------------------------------
} // namespace vm
//------------------------------------------------------------------------------
} // namespace psi
} // namespace psi::vm
//------------------------------------------------------------------------------
2 changes: 1 addition & 1 deletion include/psi/vm/mappable_objects/file/file.win32.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ std::uint64_t get_size( file_handle::reference

mapping BOOST_CC_REG create_mapping
(
file_handle::reference,
file_handle &&,
flags::access_privileges::object,
flags::access_privileges::child_process,
flags::mapping ::share_mode,
Expand Down
11 changes: 2 additions & 9 deletions include/psi/vm/mapped_view/mapped_view.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,7 @@
#include <cstdint>
#include <span>
//------------------------------------------------------------------------------
namespace psi
{
//------------------------------------------------------------------------------
namespace vm
namespace psi::vm
{
//------------------------------------------------------------------------------

Expand Down Expand Up @@ -77,8 +74,6 @@ class basic_mapped_view : public std::conditional_t<read_only, read_only_mapped_

fallible_result<void> expand( std::size_t target_size, mapping & ) noexcept;

void flush() const noexcept requires( !read_only );

explicit operator bool() const noexcept { return !this->empty(); }

void unmap() noexcept
Expand Down Expand Up @@ -137,7 +132,5 @@ using mapped_view = basic_mapped_view<false>;
using read_only_mapped_view = basic_mapped_view<true >;

//------------------------------------------------------------------------------
} // namespace vm
//------------------------------------------------------------------------------
} // namespace psi
} // namespace vm::psi
//------------------------------------------------------------------------------
38 changes: 17 additions & 21 deletions src/mapped_view/mapper.hpp → include/psi/vm/mapped_view/ops.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,36 +13,32 @@
//------------------------------------------------------------------------------
#pragma once

#include <psi/vm/flags/mapping.hpp>
#include <psi/vm/handles/handle.hpp>
#include <psi/vm/mapping/mapping.hpp>
#include <psi/vm/mappable_objects/file/handle.hpp>
#include <psi/vm/span.hpp>

#include <boost/config_ex.hpp>
//------------------------------------------------------------------------------
namespace psi::vm
{
//------------------------------------------------------------------------------

struct mapper
{
static BOOST_ATTRIBUTES( BOOST_MINSIZE, BOOST_EXCEPTIONLESS )
mapped_span BOOST_CC_REG
map
(
mapping::handle source_mapping,
flags ::viewing flags ,
std ::uint64_t offset ,
std ::size_t desired_size
) noexcept;
void flush_blocking( mapped_span range, file_handle::reference source_file ) noexcept;
#ifndef _WIN32
// No way to perform this on Windows through only the view itself
// https://learn.microsoft.com/en-us/windows/win32/api/memoryapi/nf-memoryapi-flushviewoffile
void flush_blocking( mapped_span range ) noexcept;
#endif
void flush_async ( mapped_span range ) noexcept;

// these below out to go/get special versions in allocation.hpp
void discard( mapped_span range ) noexcept;

static BOOST_ATTRIBUTES( BOOST_MINSIZE, BOOST_EXCEPTIONLESS, BOOST_RESTRICTED_FUNCTION_L1 )
void BOOST_CC_REG unmap( mapped_span view ) noexcept;
// TODO prefetch/WILL_NEED
// https://learn.microsoft.com/en-us/windows/win32/api/memoryapi/nf-memoryapi-prefetchvirtualmemory

static void shrink( mapped_span view, std::size_t target_size ) noexcept;

static void flush( mapped_span view ) noexcept;
}; // struct mapper
#ifndef _WIN32
// utility verbosity reducing wrapper
void madvise( mapped_span range, int advice ) noexcept;
#endif

//------------------------------------------------------------------------------
} // namespace psi::vm
Expand Down
Loading

0 comments on commit 187ce42

Please sign in to comment.