diff --git a/.github/workflows/gh-actions.yml b/.github/workflows/gh-actions.yml new file mode 100644 index 0000000..ee9fe18 --- /dev/null +++ b/.github/workflows/gh-actions.yml @@ -0,0 +1,106 @@ +# This starter workflow is for a CMake project running on multiple platforms. There is a different starter workflow if you just want a single platform. +# See: https://github.com/actions/starter-workflows/blob/main/ci/cmake-single-platform.yml +name: Build & Test on Ubuntu, macOS, and Windows + +on: + push: + branches: [ "master" ] + pull_request: + branches: [ "master" ] + +jobs: + build: + runs-on: ${{ matrix.os }} + + strategy: + # Set fail-fast to false to ensure that feedback is delivered for all matrix combinations. Consider changing this to true when your workflow is stable. + fail-fast: false + + # Set up a matrix to run the following 3 configurations: + # 1. + # 2. + # 3. + # + # 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] + include: + - os: windows-latest + c_compiler: clang-cl + cpp_compiler: clang-cl + - os: ubuntu-latest + c_compiler: clang + cpp_compiler: clang++ + - os: macos-latest + c_compiler: clang + cpp_compiler: clang++ + exclude: + - os: windows-latest + c_compiler: clang + - os: ubuntu-latest + c_compiler: clang-cl + - os: macos-latest + c_compiler: clang-cl + + steps: + - uses: actions/checkout@v3 + with: + submodules: true + - uses: actions/setup-java@v3 + with: + java-version: '17' + distribution: 'temurin' + + - name: Install LLVM and Clang on Ubuntu + 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 + shell: sh + + - name: Install LLVM and Clang on MacOS + if: matrix.os == 'macos-latest' + uses: KyleMayes/install-llvm-action@v1.9.0 + 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 + shell: bash + run: | + echo "build-output-dir=${{ github.workspace }}/build" >> "$GITHUB_OUTPUT" + + - name: Configure CMake + # Configure CMake in a 'build' subdirectory. `CMAKE_BUILD_TYPE` is only required if you are using a single-configuration generator such as make. + # See https://cmake.org/cmake/help/latest/variable/CMAKE_BUILD_TYPE.html?highlight=cmake_build_type + run: > + cmake -B ${{ steps.strings.outputs.build-output-dir }} + -DCMAKE_CXX_COMPILER=${{ matrix.cpp_compiler }} + -DCMAKE_C_COMPILER=${{ matrix.c_compiler }} + -DCMAKE_BUILD_TYPE=${{ matrix.build_type }} + -S ${{ github.workspace }} + + - name: Build + # Build your program with the given configuration. Note that --config is needed because the default Windows generator is a multi-config generator (Visual Studio generator). + run: > + cmake --build ${{ steps.strings.outputs.build-output-dir }} --config ${{ matrix.build_type }} && + cmake --build ${{ steps.strings.outputs.build-output-dir }} --config ${{ matrix.build_type }} --target vm_unit_tests + + + - name: Test + working-directory: ${{ steps.strings.outputs.build-output-dir }} + # Execute tests defined by the CMake configuration. Note that --build-config is needed because the default Windows generator is a multi-config generator (Visual Studio generator). + # See https://cmake.org/cmake/help/latest/manual/ctest.1.html for more detail + run: > + cd ${{ steps.strings.outputs.build-output-dir }}/test && + ctest --build-config ${{ matrix.build_type }} diff --git a/.gitignore b/.gitignore index 1377554..53baec5 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,6 @@ *.swp + +out/* +build/* +bin/* +.vs/* diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..fa8d742 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,95 @@ +cmake_minimum_required( VERSION 3.27.0 ) + +project( vm ) + +################### +## Deps +################### + +# Download CPM.cmake + +file( + DOWNLOAD + https://github.com/cpm-cmake/CPM.cmake/releases/download/v0.38.3/CPM.cmake + ${CMAKE_CURRENT_BINARY_DIR}/cmake/CPM.cmake + EXPECTED_HASH SHA256=cc155ce02e7945e7b8967ddfaff0b050e958a723ef7aad3766d368940cb15494 +) +set( CPM_SOURCE_CACHE "${CMAKE_CURRENT_BINARY_DIR}/deps" ) # Using this ensures shallow clones +include( ${CMAKE_CURRENT_BINARY_DIR}/cmake/CPM.cmake ) + + +# Add packages + +set( boost_ver boost-1.84.0 ) +CPMAddPackage( "gh:boostorg/static_assert#${boost_ver}" ) # Boost::core dependency +CPMAddPackage( "gh:boostorg/throw_exception#${boost_ver}" ) # Boost::core dependency +CPMAddPackage( "gh:boostorg/config#${boost_ver}" ) # Boost::core dependency +CPMAddPackage( "gh:boostorg/io#${boost_ver}" ) # Boost::utility dependency +CPMAddPackage( "gh:boostorg/type_traits#${boost_ver}" ) # Boost::utility dependency +CPMAddPackage( "gh:boostorg/predef#${boost_ver}" ) # Boost::winapi dependency +CPMAddPackage( "gh:boostorg/assert#${boost_ver}" ) +CPMAddPackage( "gh:boostorg/core#${boost_ver}" ) +CPMAddPackage( "gh:boostorg/preprocessor#${boost_ver}" ) +CPMAddPackage( "gh:boostorg/winapi#${boost_ver}" ) +CPMAddPackage( "gh:boostorg/utility#${boost_ver}" ) + +CPMAddPackage( "gh:psiha/config_ex#master" ) +CPMAddPackage( "gh:psiha/std_fix#master" ) +CPMAddPackage( "gh:psiha/err#master" ) +CPMAddPackage( "gh:psiha/build#master" ) + +if ( ${CMAKE_SYSTEM_NAME} MATCHES "Linux" ) + set( PSI_USE_LINKER "lld" ) # for thinlto-cache-dir support +endif() +include( ${build_SOURCE_DIR}/build_options.cmake ) + +PSI_add_compile_options( Debug ${PSI_compiler_runtime_sanity_checks} ) +PSI_add_link_options ( Debug ${PSI_linker_runtime_sanity_checks} ) + +PSI_add_compile_options( Release ${PSI_compiler_LTO} ${PSI_compiler_optimize_for_size} ${PSI_compiler_disable_thread_safe_init} ${PSI_compiler_fastmath} ${PSI_compiler_debug_symbols} ) + +if ( WIN32 ) + add_compile_definitions( WIN32_LEAN_AND_MEAN NOMINMAX NOCOMM ) +endif() + +if ( ${CMAKE_SYSTEM_NAME} MATCHES "Linux" ) + PSI_add_link_options( Release -flto ) # lld does not seem to be enough + add_compile_options( -stdlib=libc++ ) + # Needed under WSL for some reason? + PSI_add_link_options( Debug -lc++ -lc++abi -lubsan ) +else() + set( CMAKE_INTERPROCEDURAL_OPTIMIZATION_RELEASE true ) + PSI_add_link_options( Release ${PSI_linker_LTO} ) +endif() + + +################### +## Target(s) +################### + +include( vm.cmake ) + +target_link_libraries( psi_vm PUBLIC + Boost::core + Boost::assert + Boost::preprocessor + Boost::winapi + Boost::utility +) + +target_include_directories( psi_vm PUBLIC + "${build_SOURCE_DIR}/include" + "${config_ex_SOURCE_DIR}/include" + "${err_SOURCE_DIR}/include" +) + +target_include_directories( psi_vm PRIVATE + "${std_fix_SOURCE_DIR}/include" +) + +################### +## Testing +################### + +enable_testing() +add_subdirectory( "${PROJECT_SOURCE_DIR}/test" ) diff --git a/include/psi/vm/align.hpp b/include/psi/vm/align.hpp index c34a346..106e282 100644 --- a/include/psi/vm/align.hpp +++ b/include/psi/vm/align.hpp @@ -24,7 +24,8 @@ namespace align_detail #ifdef __clang__ [[ using gnu: const, always_inline ]] constexpr auto is_aligned( auto const value, auto const alignment ) noexcept { return __builtin_is_aligned( value, alignment ); } #else -[[ using gnu: const, always_inline ]] constexpr auto is_aligned( auto const value, auto const alignment ) noexcept { return value % alignment == 0; } +[[ using gnu: const, always_inline ]] constexpr auto is_aligned( auto const value, auto const alignment ) noexcept { return value % alignment == 0; } +[[ using gnu: const, always_inline ]] constexpr auto is_aligned( auto * const ptr , auto const alignment ) noexcept { return is_aligned( reinterpret_cast( ptr ), alignment ); } #endif [[ using gnu: const, always_inline ]] constexpr auto align_down( auto const value, auto const alignment ) noexcept { diff --git a/include/psi/vm/allocation/allocation.hpp b/include/psi/vm/allocation.hpp similarity index 98% rename from include/psi/vm/allocation/allocation.hpp rename to include/psi/vm/allocation.hpp index 879eee3..d309af7 100644 --- a/include/psi/vm/allocation/allocation.hpp +++ b/include/psi/vm/allocation.hpp @@ -19,7 +19,7 @@ #include #endif // OS -#include "../span.hpp" +#include #include #include @@ -55,7 +55,7 @@ inline std::uint32_t constexpr reserve_granularity{ 64 * 1024 }; #else // POSIX -enum class allocation_type +enum class allocation_type : int { reserve = PROT_NONE, commit = PROT_READ | PROT_WRITE diff --git a/include/psi/vm/amalgamated_lib.cpp b/include/psi/vm/amalgamated_lib.cpp deleted file mode 100644 index ea0100c..0000000 --- a/include/psi/vm/amalgamated_lib.cpp +++ /dev/null @@ -1,47 +0,0 @@ -//////////////////////////////////////////////////////////////////////////////// -/// -/// \file amalgamated_lib.cpp -/// ------------------------- -/// -/// Copyright (c) Domagoj Saric 2011 - 2024. -/// -/// Use, modification and distribution is subject to the -/// Boost Software License, Version 1.0. -/// (See accompanying file LICENSE_1_0.txt or copy at -/// http://www.boost.org/LICENSE_1_0.txt) -/// -/// For more information, see http://www.boost.org -/// -//////////////////////////////////////////////////////////////////////////////// -//------------------------------------------------------------------------------ - -#ifdef PSI_VM_HEADER_ONLY - #error this file is meant for non header only builds -#endif // PSI_VM_HEADER_ONLY - -//...mrmlj...ugh...to be cleaned up -#ifdef _WIN32 -#include "allocation/impl/allocation.win32.cpp" -#include "flags/win32/flags.inl" -#include "flags/win32/mapping.inl" -#include "flags/win32/opening.inl" -#include "mappable_objects/shared_memory/win32/flags.inl" -#else -#include "allocation/impl/allocation.posix.cpp" -#include "flags/posix/flags.inl" -#include "flags/posix/mapping.inl" -#include "flags/posix/opening.inl" -#include "mappable_objects/shared_memory/posix/flags.inl" -#endif - -//..zzz...by utility.inl...#include "mappable_objects/file/file.inl" -#include "mappable_objects/file/utility.inl" - -#include "mapped_view/mapped_view.inl" -//------------------------------------------------------------------------------ -namespace psi::vm -{ - template class basic_mapped_view; - template class basic_mapped_view; -} -//------------------------------------------------------------------------------ \ No newline at end of file diff --git a/include/psi/vm/detail/impl_inline.hpp b/include/psi/vm/detail/impl_inline.hpp deleted file mode 100644 index 62d3382..0000000 --- a/include/psi/vm/detail/impl_inline.hpp +++ /dev/null @@ -1,28 +0,0 @@ -//////////////////////////////////////////////////////////////////////////////// -/// -/// \file impl_inline.hpp -/// --------------------- -/// -/// Copyright (c) Domagoj Saric 2011. - 2024. -/// -/// Use, modification and distribution is subject to the Boost Software License, Version 1.0. -/// (See accompanying file LICENSE_1_0.txt or copy at -/// http://www.boost.org/LICENSE_1_0.txt) -/// -/// For more information, see http://www.boost.org -/// -//////////////////////////////////////////////////////////////////////////////// -//------------------------------------------------------------------------------ -#ifndef impl_inline_hpp__EFACE887_4390_4C0D_898B_1E32DDFD719C -#define impl_inline_hpp__EFACE887_4390_4C0D_898B_1E32DDFD719C -#pragma once -//------------------------------------------------------------------------------ - -#ifdef PSI_VM_HEADER_ONLY -# define PSI_VM_IMPL_INLINE inline -#else -# define PSI_VM_IMPL_INLINE -#endif // PSI_VM_HEADER_ONLY - -//------------------------------------------------------------------------------ -#endif // impl_inline_hpp diff --git a/include/psi/vm/detail/impl_selection.hpp b/include/psi/vm/detail/impl_selection.hpp index 36ca3a5..69f34cd 100644 --- a/include/psi/vm/detail/impl_selection.hpp +++ b/include/psi/vm/detail/impl_selection.hpp @@ -14,40 +14,40 @@ /// //////////////////////////////////////////////////////////////////////////////// //------------------------------------------------------------------------------ -#ifndef impl_selection_hpp__05AF14B5_B23B_4CB8_A253_FD2D07B37ECF -#define impl_selection_hpp__05AF14B5_B23B_4CB8_A253_FD2D07B37ECF #pragma once -//------------------------------------------------------------------------------ -#include -#include + #include // Implementation note: -// "Anti-pattern" forward includes to reduce the verbosity of files that -// include this header. +// Required for PSI_VM_DIR_IMPL_INCLUDE users. // (26.08.2011.) (Domagoj Saric) -#include #include //------------------------------------------------------------------------------ #if !defined( PSI_VM_IMPL ) - #if defined( _WIN32 ) - #define PSI_VM_IMPL() win32 - #elif defined( _WIN32_WINNT ) - #define PSI_VM_IMPL() nt - #elif defined( BOOST_HAS_UNISTD_H ) - #define PSI_VM_IMPL() posix - #define PSI_VM_POSIX_INLINE inline - #else - #define PSI_VM_IMPL() xsi - #endif +# if defined( _WIN32 ) +# define PSI_VM_IMPL() win32 +# elif defined( _WIN32_WINNT ) +# define PSI_VM_IMPL() nt +# elif __has_include( ) +# define PSI_VM_IMPL() posix +# define PSI_VM_POSIX_INLINE inline +# else +# define PSI_VM_IMPL() xsi +# endif #endif // !defined( PSI_VM_IMPL ) #ifndef PSI_VM_POSIX_INLINE - #define PSI_VM_POSIX_INLINE +# define PSI_VM_POSIX_INLINE #endif // PSI_VM_POSIX_INLINE -#define PSI_VM_IMPL_INCLUDE( prefix_path, include ) \ - BOOST_PP_STRINGIZE( prefix_path()PSI_VM_IMPL()include() ) +#define PSI_VM_DIR_IMPL_INCLUDE( include ) \ + BOOST_PP_STRINGIZE( PSI_VM_IMPL()/include() ) + +#define PSI_VM_DIR_IMPL_PREFIXED_INCLUDE( prefix_path, include ) \ + BOOST_PP_STRINGIZE( prefix_path()/PSI_VM_IMPL()/include() ) + +#define PSI_VM_IMPL_INCLUDE( include ) \ + BOOST_PP_STRINGIZE( include.PSI_VM_IMPL().hpp ) //------------------------------------------------------------------------------ namespace psi::vm { @@ -58,4 +58,3 @@ inline namespace PSI_VM_IMPL() {} //------------------------------------------------------------------------------ } // namespace psi::vm //------------------------------------------------------------------------------ -#endif // impl_selection_hpp diff --git a/include/psi/vm/detail/nt.hpp b/include/psi/vm/detail/nt.hpp index 1e4a164..1658194 100644 --- a/include/psi/vm/detail/nt.hpp +++ b/include/psi/vm/detail/nt.hpp @@ -22,10 +22,8 @@ /// http://downloads.securityfocus.com/vulnerabilities/exploits/20940-Ivan.c //////////////////////////////////////////////////////////////////////////////// //------------------------------------------------------------------------------ -#ifndef nt_hpp__532C3d08_E487_4548_B51D_1E64CD74dE9B -#define nt_hpp__532C3d08_E487_4548_B51D_1E64CD74dE9B #pragma once -//------------------------------------------------------------------------------ + #include #include @@ -53,31 +51,24 @@ namespace detail inline HMODULE const ntdll{ ::GetModuleHandleW( L"ntdll.dll" ) }; inline BOOST_ATTRIBUTES( BOOST_COLD, BOOST_RESTRICTED_FUNCTION_L3, BOOST_RESTRICTED_FUNCTION_RETURN ) - void * BOOST_CC_REG get_nt_proc( char const * const proc_name ) noexcept + ::PROC BOOST_CC_REG get_nt_proc( char const * const proc_name ) noexcept { BOOST_ASSERT( ntdll ); - auto const result( ::GetProcAddress( ntdll, proc_name ) ); + auto const result{ ::GetProcAddress( ntdll, proc_name ) }; BOOST_ASSERT( result ); return result; } -#ifdef __clang__ -# pragma clang diagnostic push -# pragma clang diagnostic ignored "-Wmicrosoft-cast" // cast between pointer-to-function and pointer-to-object is a Microsoft extension -#endif - template - Proc * get_nt_proc( char const * const proc_name ) + template + ProcPtr get_nt_proc( char const * const proc_name ) { - return reinterpret_cast( detail::get_nt_proc( proc_name ) ); + return reinterpret_cast( detail::get_nt_proc( proc_name ) ); } -#ifdef __clang__ -# pragma clang diagnostic pop -#endif } // namespace detail using BaseGetNamedObjectDirectory_t = NTSTATUS (WINAPI*)( HANDLE * phDir ); -using NtCreateSection_t = NTSYSAPI NTSTATUS (NTAPI) +using NtCreateSection_t = NTSYSAPI NTSTATUS (NTAPI*) ( OUT PHANDLE SectionHandle, IN ULONG DesiredAccess, @@ -98,7 +89,7 @@ struct SECTION_BASIC_INFORMATION LARGE_INTEGER SectionSize; }; -using NtQuerySection_t = NTSYSAPI NTSTATUS (NTAPI) +using NtQuerySection_t = NTSYSAPI NTSTATUS (NTAPI*) ( IN HANDLE SectionHandle, IN SECTION_INFORMATION_CLASS InformationClass, @@ -108,16 +99,16 @@ using NtQuerySection_t = NTSYSAPI NTSTATUS (NTAPI) ); inline auto const NtQuerySection{ detail::get_nt_proc( "NtQuerySection" ) }; -using NtExtendSection_t = NTSYSAPI NTSTATUS (NTAPI) +using NtExtendSection_t = NTSYSAPI NTSTATUS (NTAPI*) ( IN HANDLE SectionHandle, IN PLARGE_INTEGER NewSectionSize ); inline auto const NtExtendSection{ detail::get_nt_proc( "NtExtendSection" ) }; -using NtAllocateVirtualMemory_t = NTSTATUS (NTAPI)( IN HANDLE ProcessHandle, IN OUT PVOID * BaseAddress, ULONG_PTR ZeroBits, PSIZE_T RegionSize, ULONG allocation_type, ULONG Protect ); -using NtFreeVirtualMemory_t = NTSTATUS (NTAPI)( IN HANDLE ProcessHandle, IN PVOID * BaseAddress, PSIZE_T RegionSize, ULONG FreeType ); -using NtProtectVirtualMemory_t = NTSTATUS (NTAPI)( IN HANDLE ProcessHandle, IN OUT PVOID * BaseAddress, IN OUT PULONG NumberOfBytesToProtect, IN ULONG NewAccessProtection, OUT PULONG OldAccessProtection ); +using NtAllocateVirtualMemory_t = NTSTATUS (NTAPI*)( IN HANDLE ProcessHandle, IN OUT PVOID * BaseAddress, ULONG_PTR ZeroBits, PSIZE_T RegionSize, ULONG allocation_type, ULONG Protect ); +using NtFreeVirtualMemory_t = NTSTATUS (NTAPI*)( IN HANDLE ProcessHandle, IN PVOID * BaseAddress, PSIZE_T RegionSize, ULONG FreeType ); +using NtProtectVirtualMemory_t = NTSTATUS (NTAPI*)( IN HANDLE ProcessHandle, IN OUT PVOID * BaseAddress, IN OUT PULONG NumberOfBytesToProtect, IN ULONG NewAccessProtection, OUT PULONG OldAccessProtection ); inline auto const NtAllocateVirtualMemory{ detail::get_nt_proc( "NtAllocateVirtualMemory" ) }; inline auto const NtFreeVirtualMemory { detail::get_nt_proc( "NtFreeVirtualMemory" ) }; @@ -125,4 +116,3 @@ inline auto const NtFreeVirtualMemory { detail::get_nt_proc -#if defined( BOOST_HAS_UNISTD_H ) - #include -#elif defined( BOOST_MSVC ) - #pragma warning ( disable : 4996 ) // "The POSIX name for this item is deprecated. Instead, use the ISO C++ conformant name." - #include - #include +#if __has_include( ) +# include +# include +# include +#elif defined( _MSC_VER ) +# pragma warning ( disable : 4996 ) // "The POSIX name for this item is deprecated. Instead, use the ISO C++ conformant name." +# include +# include #else - #error no suitable POSIX implementation found -#endif // BOOST_MSVC +# error no suitable POSIX implementation found +#endif // impl -#if defined( BOOST_MSVC ) - #define PSI_VM_POSIX_STANDARD_LINUX_OSX_MSVC( standard, linux, osx, msvc ) msvc +#if defined( _MSC_VER ) +# define PSI_VM_POSIX_STANDARD_LINUX_OSX_MSVC( standard, linux, osx, msvc ) msvc #elif defined( __APPLE__ ) - #define PSI_VM_POSIX_STANDARD_LINUX_OSX_MSVC( standard, linux, osx, msvc ) osx +# define PSI_VM_POSIX_STANDARD_LINUX_OSX_MSVC( standard, linux, osx, msvc ) osx #elif defined( _GNU_SOURCE ) - #define PSI_VM_POSIX_STANDARD_LINUX_OSX_MSVC( standard, linux, osx, msvc ) linux +# define PSI_VM_POSIX_STANDARD_LINUX_OSX_MSVC( standard, linux, osx, msvc ) linux #else - #define PSI_VM_POSIX_STANDARD_LINUX_OSX_MSVC( standard, linux, osx, msvc ) standard +# define PSI_VM_POSIX_STANDARD_LINUX_OSX_MSVC( standard, linux, osx, msvc ) standard #endif // POSIX impl - -#if !defined( __has_include ) -# define __has_include( x ) 0 -#endif // __has_include //------------------------------------------------------------------------------ namespace psi::vm { @@ -55,4 +49,3 @@ inline namespace posix //------------------------------------------------------------------------------ } // namespace psi::vm //------------------------------------------------------------------------------ -#endif // posix_hpp diff --git a/include/psi/vm/detail/win32.hpp b/include/psi/vm/detail/win32.hpp index 9441013..d2cf9b4 100644 --- a/include/psi/vm/detail/win32.hpp +++ b/include/psi/vm/detail/win32.hpp @@ -14,31 +14,13 @@ /// //////////////////////////////////////////////////////////////////////////////// //------------------------------------------------------------------------------ -#ifndef windows_hpp__886EAB51_B4AD_4246_9BE3_D5272EA7D59F -#define windows_hpp__886EAB51_B4AD_4246_9BE3_D5272EA7D59F #pragma once -//------------------------------------------------------------------------------ -#ifndef PSI_VM_HEADER_ONLY - #ifndef WIN32_LEAN_AND_MEAN - #define WIN32_LEAN_AND_MEAN - #endif // WIN32_LEAN_AND_MEAN - #ifndef NOMINMAX - #define NOMINMAX - #endif // NOMINMAX -#endif // PSI_VM_HEADER_ONLY -#include "windows.h" -//------------------------------------------------------------------------------ -namespace psi -{ -//------------------------------------------------------------------------------ -namespace vm -{ -//------------------------------------------------------------------------------ - -//------------------------------------------------------------------------------ -} // namespace vm -//------------------------------------------------------------------------------ -} // namespace psi +#ifndef WIN32_LEAN_AND_MEAN +# define WIN32_LEAN_AND_MEAN +#endif // WIN32_LEAN_AND_MEAN +#ifndef NOMINMAX +# define NOMINMAX +#endif // NOMINMAX +#include //------------------------------------------------------------------------------ -#endif // windows_hpp diff --git a/include/psi/vm/error/error.hpp b/include/psi/vm/error/error.hpp index 1372c92..a794f2d 100644 --- a/include/psi/vm/error/error.hpp +++ b/include/psi/vm/error/error.hpp @@ -3,7 +3,7 @@ /// \file error.hpp /// --------------- /// -/// Copyright (c) Domagoj Saric 2015 - 2018. +/// Copyright (c) Domagoj Saric 2015 - 2024. /// /// Use, modification and distribution is subject to the /// Boost Software License, Version 1.0. @@ -14,13 +14,10 @@ /// //////////////////////////////////////////////////////////////////////////////// //------------------------------------------------------------------------------ -#ifndef error_hpp__6EA873DA_5571_444D_AA8C_AAB9874C529D -#define error_hpp__6EA873DA_5571_444D_AA8C_AAB9874C529D #pragma once -//------------------------------------------------------------------------------ -#include "psi/vm/detail/impl_selection.hpp" -#include PSI_VM_IMPL_INCLUDE( BOOST_PP_EMPTY, BOOST_PP_IDENTITY( /error.hpp ) ) +#include +#include PSI_VM_IMPL_INCLUDE( error ) #include //------------------------------------------------------------------------------ @@ -39,4 +36,3 @@ using fallible_result = err::fallible_result; //------------------------------------------------------------------------------ } // namespace psi //------------------------------------------------------------------------------ -#endif // error_hpp diff --git a/include/psi/vm/mappable_objects/file/file.inl b/include/psi/vm/error/error.nt.hpp similarity index 63% rename from include/psi/vm/mappable_objects/file/file.inl rename to include/psi/vm/error/error.nt.hpp index d0aed12..1b5088a 100644 --- a/include/psi/vm/mappable_objects/file/file.inl +++ b/include/psi/vm/error/error.nt.hpp @@ -1,8 +1,5 @@ //////////////////////////////////////////////////////////////////////////////// /// -/// \file file.inl -/// -------------- -/// /// Copyright (c) Domagoj Saric 2010 - 2024. /// /// Use, modification and distribution is subject to the @@ -14,7 +11,16 @@ /// //////////////////////////////////////////////////////////////////////////////// //------------------------------------------------------------------------------ -#include "../../detail/impl_selection.hpp" +#pragma once + +#include +//------------------------------------------------------------------------------ +namespace psi::vm::nt +{ +//------------------------------------------------------------------------------ + +using error = NTSTATUS; -#include PSI_VM_IMPL_INCLUDE( BOOST_PP_EMPTY, BOOST_PP_IDENTITY( /file.inl ) ) +//------------------------------------------------------------------------------ +} // namespace psi::vm::nt //------------------------------------------------------------------------------ diff --git a/include/psi/vm/error/nt/error.hpp b/include/psi/vm/error/error.posix.hpp similarity index 77% rename from include/psi/vm/error/nt/error.hpp rename to include/psi/vm/error/error.posix.hpp index aa36382..d8c8310 100644 --- a/include/psi/vm/error/nt/error.hpp +++ b/include/psi/vm/error/error.posix.hpp @@ -13,24 +13,22 @@ //------------------------------------------------------------------------------ #pragma once -#include -//------------------------------------------------------------------------------ -namespace psi -{ +#include + +#include //------------------------------------------------------------------------------ -namespace vm +namespace psi::vm { //------------------------------------------------------------------------------ -namespace nt +PSI_VM_POSIX_INLINE +namespace posix { //------------------------------------------------------------------------------ -using error = NTSTATUS; +using error = err::last_errno; //------------------------------------------------------------------------------ -} // namespace nt -//------------------------------------------------------------------------------ -} // namespace vm +} // namespace posix //------------------------------------------------------------------------------ -} // namespace psi +} // namespace psi::vm //------------------------------------------------------------------------------ diff --git a/include/psi/vm/mappable_objects/shared_memory/android/mem.inl b/include/psi/vm/error/error.win32.hpp similarity index 82% rename from include/psi/vm/mappable_objects/shared_memory/android/mem.inl rename to include/psi/vm/error/error.win32.hpp index d92ab93..92b1220 100644 --- a/include/psi/vm/mappable_objects/shared_memory/android/mem.inl +++ b/include/psi/vm/error/error.win32.hpp @@ -1,9 +1,6 @@ //////////////////////////////////////////////////////////////////////////////// /// -/// \file android/mem.inl -/// --------------------- -/// -/// Copyright (c) Domagoj Saric 2024. +/// Copyright (c) Domagoj Saric 2010 - 2024. /// /// Use, modification and distribution is subject to the /// Boost Software License, Version 1.0. @@ -14,17 +11,21 @@ /// //////////////////////////////////////////////////////////////////////////////// //------------------------------------------------------------------------------ -#include +#pragma once + +#include //------------------------------------------------------------------------------ -namespace psi +namespace psi::vm { //------------------------------------------------------------------------------ -namespace vm +inline namespace win32 { //------------------------------------------------------------------------------ +using error = err::last_win32_error; + //------------------------------------------------------------------------------ -} // vm +} // namespace win32 //------------------------------------------------------------------------------ -} // psi +} // namespace psi::vm //------------------------------------------------------------------------------ diff --git a/include/psi/vm/error/posix/error.hpp b/include/psi/vm/error/posix/error.hpp deleted file mode 100644 index fed4bfd..0000000 --- a/include/psi/vm/error/posix/error.hpp +++ /dev/null @@ -1,45 +0,0 @@ -//////////////////////////////////////////////////////////////////////////////// -/// -/// \file posix/error.hpp -/// --------------------- -/// -/// Copyright (c) Domagoj Saric 2010 - 2024. -/// -/// Use, modification and distribution is subject to the -/// Boost Software License, Version 1.0. -/// (See accompanying file LICENSE_1_0.txt or copy at -/// http://www.boost.org/LICENSE_1_0.txt) -/// -/// For more information, see http://www.boost.org -/// -//////////////////////////////////////////////////////////////////////////////// -//------------------------------------------------------------------------------ -#ifndef error_hpp__F043103F_57C5_4EFA_A947_15EE812CF090 -#define error_hpp__F043103F_57C5_4EFA_A947_15EE812CF090 -#pragma once -//------------------------------------------------------------------------------ -#include "psi/vm/detail/impl_selection.hpp" - -#include -//------------------------------------------------------------------------------ -namespace psi -{ -//------------------------------------------------------------------------------ -namespace vm -{ -//------------------------------------------------------------------------------ -PSI_VM_POSIX_INLINE -namespace posix -{ -//------------------------------------------------------------------------------ - -using error = err::last_errno; - -//------------------------------------------------------------------------------ -} // namespace posix -//------------------------------------------------------------------------------ -} // namespace vm -//------------------------------------------------------------------------------ -} // namespace psi -//------------------------------------------------------------------------------ -#endif // errno_hpp diff --git a/include/psi/vm/flags/flags.hpp b/include/psi/vm/flags/flags.hpp index 9b512d5..aeb22c5 100644 --- a/include/psi/vm/flags/flags.hpp +++ b/include/psi/vm/flags/flags.hpp @@ -14,15 +14,9 @@ /// //////////////////////////////////////////////////////////////////////////////// //------------------------------------------------------------------------------ -#ifndef flags_hpp__BFFC0541_21AC_4A80_A9EE_E0450B6D4D8A -#define flags_hpp__BFFC0541_21AC_4A80_A9EE_E0450B6D4D8A #pragma once //------------------------------------------------------------------------------ -//------------------------------------------------------------------------------ -namespace psi -{ -//------------------------------------------------------------------------------ -namespace vm +namespace psi::vm { //------------------------------------------------------------------------------ #ifdef DOXYGEN_ONLY @@ -200,14 +194,10 @@ struct opening } // namespace flags #endif // DOXYGEN_ONLY //------------------------------------------------------------------------------ -} // namespace vm -//------------------------------------------------------------------------------ -} // namespace psi +} // namespace psi::vm //------------------------------------------------------------------------------ #ifndef DOXYGEN_ONLY -#include "psi/vm/detail/impl_selection.hpp" -#include PSI_VM_IMPL_INCLUDE( BOOST_PP_EMPTY, BOOST_PP_IDENTITY( /flags.hpp ) ) +#include +#include PSI_VM_IMPL_INCLUDE( flags ) #endif // DOXYGEN_ONLY - -#endif // flags_hpp diff --git a/include/psi/vm/flags/posix/flags.hpp b/include/psi/vm/flags/flags.posix.hpp similarity index 78% rename from include/psi/vm/flags/posix/flags.hpp rename to include/psi/vm/flags/flags.posix.hpp index 52ffbea..009b446 100644 --- a/include/psi/vm/flags/posix/flags.hpp +++ b/include/psi/vm/flags/flags.posix.hpp @@ -1,8 +1,5 @@ //////////////////////////////////////////////////////////////////////////////// /// -/// \file posix/flags.hpp -/// --------------------- -/// /// Copyright (c) Domagoj Saric 2010 - 2024. /// /// Use, modification and distribution is subject to the @@ -14,17 +11,20 @@ /// //////////////////////////////////////////////////////////////////////////////// //------------------------------------------------------------------------------ -#ifndef flags_hpp__3E991311_2199_4C61_A484_B8b72C528B0F -#define flags_hpp__3E991311_2199_4C61_A484_B8b72C528B0F #pragma once -//------------------------------------------------------------------------------ -#include "psi/vm/detail/impl_selection.hpp" -#include "psi/vm/detail/posix.hpp" -#include "psi/vm/flags/flags.hpp" -#include "fcntl.h" -#include "sys/mman.h" // PROT_* constants -#include "sys/stat.h" // umask +#include +#include + +#include +#if __has_include( ) +#include // PROT_* constants +#define PSI_VM_PROT( combined_value ) static_cast< std::uint32_t >( combined_value ) +#else +#define PSI_VM_PROT( combined_value ) 0 +#endif +#include +#include // umask #include #include @@ -56,17 +56,20 @@ enum struct named_object_construction_policy : flags_t // creation_disposition namespace detail { +#ifdef _MSC_VER + using mode_t = int; +#endif // _MSC_VER enum /*struct*/ rwx_flags : mode_t { - #ifdef _MSC_VER +# ifdef _MSC_VER read = _S_IREAD , write = _S_IWRITE, execute = _S_IEXEC , - #else +# else read = S_IRUSR | S_IRGRP | S_IROTH, write = S_IWUSR | S_IWGRP | S_IWOTH, execute = S_IXUSR | S_IXGRP | S_IXOTH, - #endif // _MSC_VER +# endif // _MSC_VER readwrite = read | write, all = read | write | execute }; // enum /*struct*/ rwx_flags @@ -121,13 +124,14 @@ struct access_privileges /// the same flags for all objects (i.e. like on POSIX systems). /// (08.09.2015.) (Domagoj Saric) // SYSTEM | PROCESS | MAPPING - metaread = 0 << syssh | static_cast< std::uint32_t >( 0 ) << procsh | static_cast< std::uint32_t >( PROT_NONE ) << mapsh, - read = sys_flags::read << syssh | static_cast< std::uint32_t >( O_RDONLY_ ) << procsh | static_cast< std::uint32_t >( PROT_READ ) << mapsh, - write = sys_flags::write << syssh | static_cast< std::uint32_t >( O_WRONLY ) << procsh | static_cast< std::uint32_t >( PROT_WRITE ) << mapsh, - execute = sys_flags::execute << syssh | static_cast< std::uint32_t >( O_EXEC ) << procsh | static_cast< std::uint32_t >( PROT_EXEC ) << mapsh, - readwrite = sys_flags::readwrite << syssh | static_cast< std::uint32_t >( O_RDWR ) << procsh | static_cast< std::uint32_t >( ( PROT_READ | PROT_WRITE ) ) << mapsh, - all = sys_flags::all << syssh | static_cast< std::uint32_t >( O_RDWR ) << procsh | static_cast< std::uint32_t >( ( PROT_READ | PROT_WRITE | PROT_EXEC ) ) << mapsh + metaread = 0 << syssh | static_cast< std::uint32_t >( 0 ) << procsh | PSI_VM_PROT( PROT_NONE ) << mapsh, + read = sys_flags::read << syssh | static_cast< std::uint32_t >( O_RDONLY_ ) << procsh | PSI_VM_PROT( PROT_READ ) << mapsh, + write = sys_flags::write << syssh | static_cast< std::uint32_t >( O_WRONLY ) << procsh | PSI_VM_PROT( PROT_WRITE ) << mapsh, + execute = sys_flags::execute << syssh | static_cast< std::uint32_t >( O_EXEC ) << procsh | PSI_VM_PROT( PROT_EXEC ) << mapsh, + readwrite = sys_flags::readwrite << syssh | static_cast< std::uint32_t >( O_RDWR ) << procsh | PSI_VM_PROT( PROT_READ | PROT_WRITE ) << mapsh, + all = sys_flags::all << syssh | static_cast< std::uint32_t >( O_RDWR ) << procsh | PSI_VM_PROT( PROT_READ | PROT_WRITE | PROT_EXEC ) << mapsh }; +# undef PSI_VM_PROT constexpr static bool unrestricted( flags_t const privileges ) { return ( ( static_cast< std::uint32_t >( privileges ) & all ) == all ); } @@ -135,7 +139,7 @@ struct access_privileges { flags_t /*const*/ privileges; - flags_t BOOST_CC_REG protection() const noexcept { return ( privileges >> mapsh ) & 0xFF; } + flags_t protection() const noexcept { return ( privileges >> mapsh ) & 0xFF; } }; // struct process enum struct child_process : flags_t @@ -179,18 +183,9 @@ struct access_privileges using group = scoped_privileges; using world = scoped_privileges; - flags_t read_umask() - { - // Broken/not thread safe - // http://man7.org/linux/man-pages/man2/umask.2.html @ notes - // https://groups.google.com/forum/#!topic/comp.unix.programmer/v6nv-oP9IJQ - // https://stackoverflow.com/questions/53227072/reading-umask-thread-safe/53288382 - auto const mask( ::umask( 0 ) ); - BOOST_VERIFY( ::umask( mask ) == 0 ); - return static_cast< flags_t >( mask ); - } + static flags_t read_umask() noexcept; - operator flags_t() const noexcept { return flags; } + constexpr operator flags_t() const noexcept { return flags; } static system const process_default; static system const unrestricted ; @@ -200,8 +195,8 @@ struct access_privileges flags_t flags; }; // struct system - flags_t BOOST_CC_REG oflag() const noexcept; - mode_t BOOST_CC_REG pmode() const noexcept { return static_cast< mode_t >( system_access.flags ); } + flags_t oflag() const noexcept; + mode_t pmode() const noexcept { return static_cast< mode_t >( system_access.flags ); } object object_access; child_process child_access ; @@ -223,9 +218,3 @@ constexpr access_privileges::system const access_privileges::system::_644 //------------------------------------------------------------------------------ } // namespace psi //------------------------------------------------------------------------------ - -#ifdef PSI_VM_HEADER_ONLY - #include "flags.inl" -#endif // PSI_VM_HEADER_ONLY - -#endif // opening_hpp diff --git a/include/psi/vm/flags/win32/flags.hpp b/include/psi/vm/flags/flags.win32.hpp similarity index 92% rename from include/psi/vm/flags/win32/flags.hpp rename to include/psi/vm/flags/flags.win32.hpp index e10078f..b476e64 100644 --- a/include/psi/vm/flags/win32/flags.hpp +++ b/include/psi/vm/flags/flags.win32.hpp @@ -1,8 +1,5 @@ //////////////////////////////////////////////////////////////////////////////// /// -/// \file win32/flags.hpp -/// --------------------- -/// /// Copyright (c) Domagoj Saric 2010 - 2024. /// /// Use, modification and distribution is subject to the @@ -14,20 +11,16 @@ /// //////////////////////////////////////////////////////////////////////////////// //------------------------------------------------------------------------------ -#ifndef flags_hpp__903375A7_70FF_43A0_81DF_4EA208D0FBDD -#define flags_hpp__903375A7_70FF_43A0_81DF_4EA208D0FBDD #pragma once -//------------------------------------------------------------------------------ -#include "psi/vm/detail/impl_selection.hpp" -#include "psi/vm/flags/flags.hpp" +#include + +#include #include #include #include #include - -#include //------------------------------------------------------------------------------ namespace psi { @@ -35,7 +28,7 @@ namespace psi namespace vm { //------------------------------------------------------------------------------ -namespace win32 +inline namespace win32 { //------------------------------------------------------------------------------ namespace flags @@ -82,7 +75,7 @@ namespace detail boost::base_from_member, ::SECURITY_DESCRIPTOR { - /*constexpr...boost::base_from_member...*/ + constexpr dynamic_sd() noexcept : boost::base_from_member( std::uint8_t( 0 ) ) {} dynamic_sd( dynamic_sd const & ) = delete; void reset() noexcept { member = 0; } @@ -91,14 +84,7 @@ namespace detail }; // struct dynamic_sd BOOST_ATTRIBUTES( BOOST_EXCEPTIONLESS, BOOST_RESTRICTED_FUNCTION_L3, BOOST_RESTRICTED_FUNCTION_RETURN ) -#ifdef __clang__ - #pragma clang diagnostic push - #pragma clang diagnostic ignored "-Wignored-qualifiers" -#endif - dynamic_sd const * __restrict BOOST_CC_REG make_sd( scope_privileges ); -#ifdef __clang__ - #pragma clang diagnostic pop -#endif + dynamic_sd const * BOOST_CC_REG make_sd( scope_privileges ); } // namespace detail struct access_privileges @@ -243,9 +229,3 @@ struct access_privileges //------------------------------------------------------------------------------ } // namespace psi //------------------------------------------------------------------------------ - -#ifdef PSI_VM_HEADER_ONLY - #include "flags.inl" -#endif // PSI_VM_HEADER_ONLY - -#endif // flags_hpp diff --git a/include/psi/vm/flags/mapping.hpp b/include/psi/vm/flags/mapping.hpp index 3c93c4e..92fd4ff 100644 --- a/include/psi/vm/flags/mapping.hpp +++ b/include/psi/vm/flags/mapping.hpp @@ -14,16 +14,12 @@ /// //////////////////////////////////////////////////////////////////////////////// //------------------------------------------------------------------------------ -#ifndef mapping_hpp__35B462C8_DB74_4F25_A9B7_6CAC69D9753B -#define mapping_hpp__BFFC0541_21AC_4A80_A9EE_E0450B6D4D8A #pragma once + //------------------------------------------------------------------------------ #include "flags.hpp" //------------------------------------------------------------------------------ -namespace psi -{ -//------------------------------------------------------------------------------ -namespace vm +namespace psi::vm { //------------------------------------------------------------------------------ #ifdef DOXYGEN_ONLY @@ -64,14 +60,10 @@ struct viewing; } // namespace flags #endif // DOXYGEN_ONLY //------------------------------------------------------------------------------ -} // namespace vm -//------------------------------------------------------------------------------ -} // namespace psi +} // namespace psi::vm //------------------------------------------------------------------------------ #ifndef DOXYGEN_ONLY -#include "psi/vm/detail/impl_selection.hpp" -#include PSI_VM_IMPL_INCLUDE( BOOST_PP_EMPTY, BOOST_PP_IDENTITY( /mapping.hpp ) ) +#include +#include PSI_VM_IMPL_INCLUDE( mapping ) #endif // DOXYGEN_ONLY - -#endif // opening_hpp diff --git a/include/psi/vm/flags/posix/mapping.hpp b/include/psi/vm/flags/mapping.posix.hpp similarity index 76% rename from include/psi/vm/flags/posix/mapping.hpp rename to include/psi/vm/flags/mapping.posix.hpp index 4136cd7..14f4fc7 100644 --- a/include/psi/vm/flags/posix/mapping.hpp +++ b/include/psi/vm/flags/mapping.posix.hpp @@ -1,8 +1,5 @@ //////////////////////////////////////////////////////////////////////////////// /// -/// \file flags/posix/mapping.hpp -/// ----------------------------- -/// /// Copyright (c) Domagoj Saric 2010 - 2024. /// /// Use, modification and distribution is subject to the @@ -14,20 +11,15 @@ /// //////////////////////////////////////////////////////////////////////////////// //------------------------------------------------------------------------------ -#ifndef mapping_hpp__79CF82B8_F71B_4C75_BE77_98F4FB8A7FFA -#define mapping_hpp__79CF82B8_F71B_4C75_BE77_98F4FB8A7FFA #pragma once -//------------------------------------------------------------------------------ -#include "psi/vm/detail/impl_selection.hpp" -#include "psi/vm/detail/posix.hpp" -#include "psi/vm/flags/flags.hpp" + +#include +#include +#include #include "sys/mman.h" //------------------------------------------------------------------------------ -namespace psi -{ -//------------------------------------------------------------------------------ -namespace vm +namespace psi::vm { //------------------------------------------------------------------------------ PSI_VM_POSIX_INLINE @@ -52,7 +44,7 @@ struct [[ clang::trivial_abi ]] viewing hidden = MAP_PRIVATE }; - static viewing BOOST_CC_REG create + static viewing create ( access_privileges::object, share_mode @@ -81,13 +73,5 @@ using mapping = viewing; //------------------------------------------------------------------------------ } // namespace posix //------------------------------------------------------------------------------ -} // namespace vm -//------------------------------------------------------------------------------ -} // namespace psi +} // namespace psi::vm //------------------------------------------------------------------------------ - -#ifdef PSI_VM_HEADER_ONLY - #include "mapping.inl" -#endif // PSI_VM_HEADER_ONLY - -#endif // mapping.hpp diff --git a/include/psi/vm/flags/win32/mapping.hpp b/include/psi/vm/flags/mapping.win32.hpp similarity index 83% rename from include/psi/vm/flags/win32/mapping.hpp rename to include/psi/vm/flags/mapping.win32.hpp index 0507b0b..d31cbb9 100644 --- a/include/psi/vm/flags/win32/mapping.hpp +++ b/include/psi/vm/flags/mapping.win32.hpp @@ -1,6 +1,6 @@ //////////////////////////////////////////////////////////////////////////////// /// -/// \file flags/win32/mapping.hpp +/// \file flags/mapping.win32.hpp /// ----------------------------- /// /// Copyright (c) Domagoj Saric 2010 - 2018. @@ -14,21 +14,14 @@ /// //////////////////////////////////////////////////////////////////////////////// //------------------------------------------------------------------------------ -#ifndef mapping_hpp__4EF4F246_E244_40F1_A1C0_6D91EF1DA2EC -#define mapping_hpp__4EF4F246_E244_40F1_A1C0_6D91EF1DA2EC #pragma once -//------------------------------------------------------------------------------ -#include "psi/vm/detail/impl_selection.hpp" -#include "psi/vm/flags/win32/flags.hpp" + +#include +#include #include #include - -#ifdef __clang__ -#pragma clang diagnostic push -#pragma clang diagnostic ignored "-Winline-namespace-reopened-noninline" -#endif //------------------------------------------------------------------------------ namespace psi { @@ -36,7 +29,7 @@ namespace psi namespace vm { //------------------------------------------------------------------------------ -namespace win32 +inline namespace win32 { //------------------------------------------------------------------------------ namespace flags @@ -56,8 +49,8 @@ struct viewing hidden = 0x0001 }; - bool is_cow() const; - bool is_hidden() const { return is_cow(); } + bool is_cow () const noexcept; + bool is_hidden() const noexcept { return is_cow(); } static viewing BOOST_CC_REG create ( @@ -116,13 +109,3 @@ struct mapping //------------------------------------------------------------------------------ } // namespace psi //------------------------------------------------------------------------------ - -#ifdef PSI_VM_HEADER_ONLY - #include "mapping.inl" -#endif // PSI_VM_HEADER_ONLY - -#ifdef __clang__ -#pragma clang diagnostic pop -#endif - -#endif // mapping.hpp diff --git a/include/psi/vm/flags/opening.hpp b/include/psi/vm/flags/opening.hpp index 3639ec6..ce9bf90 100644 --- a/include/psi/vm/flags/opening.hpp +++ b/include/psi/vm/flags/opening.hpp @@ -14,10 +14,8 @@ /// //////////////////////////////////////////////////////////////////////////////// //------------------------------------------------------------------------------ -#ifndef opening_hpp__FEEA10FA_EA28_496E_B860_815D484AFB36 -#define opening_hpp__FEEA10FA_EA28_496E_B860_815D484AFB36 #pragma once -//------------------------------------------------------------------------------ + #include "flags.hpp" //------------------------------------------------------------------------------ namespace psi @@ -76,8 +74,6 @@ struct opening //------------------------------------------------------------------------------ #ifndef DOXYGEN_ONLY -#include "psi/vm/detail/impl_selection.hpp" -#include PSI_VM_IMPL_INCLUDE( BOOST_PP_EMPTY, BOOST_PP_IDENTITY( /opening.hpp ) ) +#include +#include PSI_VM_IMPL_INCLUDE( opening ) #endif // DOXYGEN_ONLY - -#endif // opening_hpp diff --git a/include/psi/vm/flags/posix/opening.hpp b/include/psi/vm/flags/opening.posix.hpp similarity index 83% rename from include/psi/vm/flags/posix/opening.hpp rename to include/psi/vm/flags/opening.posix.hpp index 3037dd3..f360ae1 100644 --- a/include/psi/vm/flags/posix/opening.hpp +++ b/include/psi/vm/flags/opening.posix.hpp @@ -1,6 +1,6 @@ //////////////////////////////////////////////////////////////////////////////// /// -/// \file flags/posix/opening.hpp +/// \file flags/opening.posix.hpp /// ----------------------------- /// /// Copyright (c) Domagoj Saric 2010 - 2024. @@ -14,15 +14,11 @@ /// //////////////////////////////////////////////////////////////////////////////// //------------------------------------------------------------------------------ -#ifndef opening_hpp__0F422517_D9AA_4E3F_B3E4_B139021D068E -#define opening_hpp__0F422517_D9AA_4E3F_B3E4_B139021D068E #pragma once -//------------------------------------------------------------------------------ -#include "psi/vm/detail/impl_selection.hpp" -#include "psi/vm/detail/posix.hpp" -#include "psi/vm/flags/posix/flags.hpp" -#include "boost/preprocessor/facilities/is_empty.hpp" +#include +#include +#include #include "fcntl.h" //------------------------------------------------------------------------------ @@ -66,14 +62,14 @@ using system_hints = access_pattern_optimisation_hints; struct opening { - static opening BOOST_CC_REG create + static opening create ( access_privileges, named_object_construction_policy, flags_t combined_system_hints ) noexcept; - static opening BOOST_CC_REG create_for_opening_existing_objects + static opening create_for_opening_existing_objects ( access_privileges::object, access_privileges::child_process, @@ -94,9 +90,3 @@ struct opening //------------------------------------------------------------------------------ } // namespace psi //------------------------------------------------------------------------------ - -#ifdef PSI_VM_HEADER_ONLY - #include "opening.inl" -#endif // PSI_VM_HEADER_ONLY - -#endif // opening_hpp diff --git a/include/psi/vm/flags/win32/opening.hpp b/include/psi/vm/flags/opening.win32.hpp similarity index 80% rename from include/psi/vm/flags/win32/opening.hpp rename to include/psi/vm/flags/opening.win32.hpp index 216ae9a..47de1ec 100644 --- a/include/psi/vm/flags/win32/opening.hpp +++ b/include/psi/vm/flags/opening.win32.hpp @@ -1,8 +1,5 @@ //////////////////////////////////////////////////////////////////////////////// /// -/// \file open_flags.hpp -/// -------------------- -/// /// Copyright (c) Domagoj Saric 2010 - 2024. /// /// Use, modification and distribution is subject to the @@ -14,17 +11,9 @@ /// //////////////////////////////////////////////////////////////////////////////// //------------------------------------------------------------------------------ -#ifndef opening_hpp__E8413961_69B7_4F59_8011_CB65D5EDF6F4 -#define opening_hpp__E8413961_69B7_4F59_8011_CB65D5EDF6F4 #pragma once -//------------------------------------------------------------------------------ -#include "psi/vm/detail/impl_selection.hpp" -#include "psi/vm/flags/win32/flags.hpp" -#ifdef __clang__ -#pragma clang diagnostic push -#pragma clang diagnostic ignored "-Winline-namespace-reopened-noninline" -#endif +#include //------------------------------------------------------------------------------ namespace psi { @@ -32,7 +21,7 @@ namespace psi namespace vm { //------------------------------------------------------------------------------ -namespace win32 +inline namespace win32 { //------------------------------------------------------------------------------ namespace flags @@ -58,7 +47,7 @@ using system_hints = access_pattern_optimisation_hints; struct opening { - static opening BOOST_CC_REG create + static opening create ( access_privileges const & ap, named_object_construction_policy const construction_policy, @@ -90,13 +79,3 @@ struct opening //------------------------------------------------------------------------------ } // namespace psi //------------------------------------------------------------------------------ - -#ifdef PSI_VM_HEADER_ONLY - #include "opening.inl" -#endif // PSI_VM_HEADER_ONLY - -#ifdef __clang__ -#pragma clang diagnostic pop -#endif - -#endif // opening_hpp diff --git a/include/psi/vm/handles/handle.hpp b/include/psi/vm/handles/handle.hpp index 318802b..c53c3a5 100644 --- a/include/psi/vm/handles/handle.hpp +++ b/include/psi/vm/handles/handle.hpp @@ -14,13 +14,11 @@ /// //////////////////////////////////////////////////////////////////////////////// //------------------------------------------------------------------------------ -#ifndef handle_hpp__67A36E06_53FF_4361_9786_9E04A3917CD3 -#define handle_hpp__67A36E06_53FF_4361_9786_9E04A3917CD3 #pragma once -//------------------------------------------------------------------------------ + #include "handle_ref.hpp" -#include "psi/vm/detail/impl_selection.hpp" +#include //------------------------------------------------------------------------------ namespace psi { @@ -54,6 +52,8 @@ class [[ clang::trivial_abi ]] handle_impl using reference = handle_ref; using const_reference = handle_ref; + 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; } @@ -93,5 +93,4 @@ class [[ clang::trivial_abi ]] handle_impl //------------------------------------------------------------------------------ } // namespace psi //------------------------------------------------------------------------------ -#include PSI_VM_IMPL_INCLUDE( BOOST_PP_EMPTY, BOOST_PP_IDENTITY( /handle.hpp ) ) -#endif // handle_hpp +#include PSI_VM_IMPL_INCLUDE( handle ) diff --git a/include/psi/vm/handles/posix/handle.hpp b/include/psi/vm/handles/handle.posix.hpp similarity index 77% rename from include/psi/vm/handles/posix/handle.hpp rename to include/psi/vm/handles/handle.posix.hpp index 3f66751..d44d4e9 100644 --- a/include/psi/vm/handles/posix/handle.hpp +++ b/include/psi/vm/handles/handle.posix.hpp @@ -14,17 +14,12 @@ /// //////////////////////////////////////////////////////////////////////////////// //------------------------------------------------------------------------------ -#ifndef handle_hpp__63113526_C3F1_46DC_850E_D8D8C62031DB -#define handle_hpp__63113526_C3F1_46DC_850E_D8D8C62031DB #pragma once -//------------------------------------------------------------------------------ -#include "../handle.hpp" -#include "../handle_ref.hpp" -#include +#include #include -#include +#include #ifdef _MSC_VER # pragma warning ( disable : 4996 ) // "The POSIX name for this item is deprecated. Instead, use the ISO C++ conformant name." @@ -51,23 +46,23 @@ struct handle_traits { using native_t = int; - static native_t const invalid_value = -1; + static native_t constexpr invalid_value = -1; static BOOST_ATTRIBUTES( BOOST_MINSIZE, BOOST_RESTRICTED_FUNCTION_L1, BOOST_EXCEPTIONLESS ) - void BOOST_CC_REG close( native_t const native_handle ) noexcept + void close( native_t const native_handle ) noexcept { BOOST_VERIFY ( ( ::close( native_handle ) == 0 ) || ( - ( native_handle == -1 ) && - ( errno == EBADF ) + ( native_handle == invalid_value ) && + ( errno == EBADF ) ) ); } static BOOST_ATTRIBUTES( BOOST_MINSIZE, BOOST_RESTRICTED_FUNCTION_L1, BOOST_EXCEPTIONLESS ) - native_t BOOST_CC_REG copy( native_t const native_handle ); //todo + native_t copy( native_t native_handle ); // TODO }; // handle_traits using handle = handle_impl; @@ -79,4 +74,3 @@ using handle = handle_impl; //------------------------------------------------------------------------------ } // namespace psi //------------------------------------------------------------------------------ -#endif // handle_hpp diff --git a/include/psi/vm/handles/win32/handle.hpp b/include/psi/vm/handles/handle.win32.hpp similarity index 74% rename from include/psi/vm/handles/win32/handle.hpp rename to include/psi/vm/handles/handle.win32.hpp index 4e9b7cf..bf72de9 100644 --- a/include/psi/vm/handles/win32/handle.hpp +++ b/include/psi/vm/handles/handle.win32.hpp @@ -1,8 +1,5 @@ //////////////////////////////////////////////////////////////////////////////// /// -/// \file win32/handle.hpp -/// ---------------------- -/// /// Copyright (c) Domagoj Saric 2010 - 2024. /// /// Use, modification and distribution is subject to the @@ -13,24 +10,18 @@ /// For more information, see http://www.boost.org /// //////////////////////////////////////////////////////////////////////////////// -//------------------------------------------------------------------------------ -#ifndef handle_hpp__1CEA6D65_D5C0_474E_833D_2CE927A1C74D -#define handle_hpp__1CEA6D65_D5C0_474E_833D_2CE927A1C74D #pragma once -//------------------------------------------------------------------------------ -#include "../handle.hpp" -#include "../handle_ref.hpp" -#include +#include +#include #include #include -#include "../posix/handle.hpp" +#include "handle.posix.hpp" #include #include - //------------------------------------------------------------------------------ namespace psi { @@ -46,10 +37,10 @@ struct handle_traits { using native_t = boost::winapi::HANDLE_; - inline static native_t const invalid_value = boost::winapi::invalid_handle_value; //...mrmlj...or nullptr eg. for CreateFileMapping + inline static native_t const invalid_value{ boost::winapi::invalid_handle_value }; //...mrmlj...or nullptr eg. for CreateFileMapping static BOOST_ATTRIBUTES( BOOST_MINSIZE, BOOST_RESTRICTED_FUNCTION_L2, BOOST_EXCEPTIONLESS ) - void BOOST_CC_REG close( native_t const native_handle ) + void close( native_t const native_handle ) { BOOST_VERIFY ( @@ -59,7 +50,7 @@ struct handle_traits } static BOOST_ATTRIBUTES( BOOST_MINSIZE, BOOST_RESTRICTED_FUNCTION_L2, BOOST_EXCEPTIONLESS ) - native_t BOOST_CC_REG copy( native_t const native_handle ); //todo + native_t copy( native_t native_handle ); // TODO }; // handle_traits #ifdef BOOST_MSVC @@ -79,4 +70,3 @@ using handle = handle_impl; //------------------------------------------------------------------------------ } // namespace psi //------------------------------------------------------------------------------ -#endif // handle_hpp diff --git a/include/psi/vm/mappable_objects/file/file.hpp b/include/psi/vm/mappable_objects/file/file.hpp index 2dff0aa..f164a01 100644 --- a/include/psi/vm/mappable_objects/file/file.hpp +++ b/include/psi/vm/mappable_objects/file/file.hpp @@ -14,29 +14,15 @@ /// //////////////////////////////////////////////////////////////////////////////// //------------------------------------------------------------------------------ -#ifndef file_hpp__D3705ED0_EC0D_4747_A789_1EE17252B6E2 -#define file_hpp__D3705ED0_EC0D_4747_A789_1EE17252B6E2 #pragma once + +#include +#include PSI_VM_IMPL_INCLUDE( file ) //------------------------------------------------------------------------------ -#include "psi/vm/detail/impl_selection.hpp" -#include "psi/vm/flags/flags.hpp" -#include PSI_VM_IMPL_INCLUDE( BOOST_PP_EMPTY, BOOST_PP_IDENTITY( /file.hpp ) ) -//------------------------------------------------------------------------------ -namespace psi -{ -//------------------------------------------------------------------------------ -namespace vm +namespace psi::vm { //------------------------------------------------------------------------------ //------------------------------------------------------------------------------ -} // namespace vm +} // namespace psi::vm //------------------------------------------------------------------------------ -} // namespace psi -//------------------------------------------------------------------------------ - -#ifdef PSI_VM_HEADER_ONLY - #include "file.inl" -#endif // PSI_VM_HEADER_ONLY - -#endif // file_hpp diff --git a/include/psi/vm/mappable_objects/file/posix/file.hpp b/include/psi/vm/mappable_objects/file/file.posix.hpp similarity index 54% rename from include/psi/vm/mappable_objects/file/posix/file.hpp rename to include/psi/vm/mappable_objects/file/file.posix.hpp index 7d0e313..55c45ce 100644 --- a/include/psi/vm/mappable_objects/file/posix/file.hpp +++ b/include/psi/vm/mappable_objects/file/file.posix.hpp @@ -1,6 +1,6 @@ //////////////////////////////////////////////////////////////////////////////// /// -/// \file mappable_objects/file/posix/file.hpp +/// \file mappable_objects/file/file.posix.hpp /// ------------------------------------------ /// /// Copyright (c) Domagoj Saric 2010 - 2024. @@ -14,21 +14,23 @@ /// //////////////////////////////////////////////////////////////////////////////// //------------------------------------------------------------------------------ -#ifndef file_hpp__1E2F9841_1C6C_40D9_9AA7_BAC0003CD909 -#define file_hpp__1E2F9841_1C6C_40D9_9AA7_BAC0003CD909 #pragma once -//------------------------------------------------------------------------------ -#include "psi/vm/detail/impl_selection.hpp" -#include "psi/vm/detail/posix.hpp" -#include "psi/vm/flags/posix/opening.hpp" -#include "psi/vm/mappable_objects/file/handle.hpp" + +#include +#include +#include +#include +#include + +#if __has_include( ) +#include +#endif + +#include #include //------------------------------------------------------------------------------ -namespace psi -{ -//------------------------------------------------------------------------------ -namespace vm +namespace psi::vm { //------------------------------------------------------------------------------ PSI_VM_POSIX_INLINE @@ -37,29 +39,29 @@ namespace posix //------------------------------------------------------------------------------ template struct is_resizable; -#ifdef BOOST_HAS_UNISTD_H - template <> struct is_resizable : std::true_type {}; +#if __has_include( ) +template <> struct is_resizable : std::true_type {}; #else - template <> struct is_resizable : std::false_type {}; -#endif // BOOST_HAS_UNISTD_H +template <> struct is_resizable : std::false_type {}; +#endif // POSIX impl level -file_handle BOOST_CC_REG create_file( char const * file_name, flags::opening ) noexcept; -#ifdef BOOST_MSVC -file_handle BOOST_CC_REG create_file( wchar_t const * file_name, flags::opening ) noexcept; -#endif // BOOST_MSVC +file_handle create_file( char const * file_name, flags::opening ) noexcept; +#ifdef _MSC_VER +file_handle create_file( wchar_t const * file_name, flags::opening ) noexcept; +#endif // _MSC_VER -bool BOOST_CC_REG delete_file( char const * path ) noexcept; -bool BOOST_CC_REG delete_file( wchar_t const * path ) noexcept; +bool delete_file( char const * path ) noexcept; +bool delete_file( wchar_t const * path ) noexcept; -#ifdef BOOST_HAS_UNISTD_H -err::fallible_result BOOST_CC_REG set_size( file_handle::reference , std::uint64_t desired_size ) noexcept; -#endif // BOOST_HAS_UNISTD_H -std::uint64_t BOOST_CC_REG get_size( file_handle::const_reference ) noexcept; +#if __has_include( ) +err::fallible_result set_size( file_handle::reference , std::uint64_t desired_size ) noexcept; +#endif // POSIX impl level +std::uint64_t get_size( file_handle::const_reference ) noexcept; -#ifdef BOOST_HAS_UNISTD_H +#if __has_include( ) template mapping BOOST_CC_REG create_mapping ( @@ -74,18 +76,10 @@ mapping BOOST_CC_REG create_mapping (void)child_access; //...mrmlj...figure out what to do with this... return { std::forward( file ), flags::viewing::create( object_access, share_mode ), size }; } -#endif // BOOST_HAS_UNISTD_H +#endif // POSIX impl level //------------------------------------------------------------------------------ } // namespace posix //------------------------------------------------------------------------------ -} // namespace vm -//------------------------------------------------------------------------------ -} // namespace psi +} // namespace psi::vm //------------------------------------------------------------------------------ - -#ifdef PSI_VM_HEADER_ONLY -# include "file.inl" -#endif // PSI_VM_HEADER_ONLY - -#endif // file_hpp diff --git a/include/psi/vm/mappable_objects/file/win32/file.hpp b/include/psi/vm/mappable_objects/file/file.win32.hpp similarity index 61% rename from include/psi/vm/mappable_objects/file/win32/file.hpp rename to include/psi/vm/mappable_objects/file/file.win32.hpp index 35afdac..e2a0fb5 100644 --- a/include/psi/vm/mappable_objects/file/win32/file.hpp +++ b/include/psi/vm/mappable_objects/file/file.win32.hpp @@ -1,8 +1,5 @@ //////////////////////////////////////////////////////////////////////////////// /// -/// \file win32/file.hpp -/// -------------------- -/// /// Copyright (c) Domagoj Saric 2010 - 2024. /// /// Use, modification and distribution is subject to the @@ -14,25 +11,20 @@ /// //////////////////////////////////////////////////////////////////////////////// //------------------------------------------------------------------------------ -#ifndef file_hpp__FB482005_18D9_4E3B_9193_A13DBFE88F45 -#define file_hpp__FB482005_18D9_4E3B_9193_A13DBFE88F45 #pragma once -//------------------------------------------------------------------------------ -#include "psi/vm/detail/impl_selection.hpp" -#include "psi/vm/flags/win32/opening.hpp" -#include "psi/vm/mapping/mapping.hpp" -#include "psi/vm/mappable_objects/file/handle.hpp" -#include "psi/vm/error/error.hpp" + +#include +#include +#include +#include +#include #include //------------------------------------------------------------------------------ -namespace psi -{ -//------------------------------------------------------------------------------ -namespace vm +namespace psi::vm { //------------------------------------------------------------------------------ -namespace win32 +inline namespace win32 { //------------------------------------------------------------------------------ @@ -42,12 +34,12 @@ template < > struct is_resizable : std::true_type {}; BOOST_ATTRIBUTES( BOOST_EXCEPTIONLESS, BOOST_RESTRICTED_FUNCTION_L1 ) file_handle BOOST_CC_REG create_file( char const * file_name, flags::opening ) noexcept; file_handle BOOST_CC_REG create_file( wchar_t const * file_name, flags::opening ) noexcept; -bool BOOST_CC_REG delete_file( char const * file_name ) noexcept; -bool BOOST_CC_REG delete_file( wchar_t const * file_name ) noexcept; +bool delete_file( char const * file_name ) noexcept; +bool delete_file( wchar_t const * file_name ) noexcept; -err::fallible_result BOOST_CC_REG set_size( file_handle::reference, std::uint64_t desired_size ) noexcept; -std::uint64_t BOOST_CC_REG get_size( file_handle::reference ) noexcept; +err::fallible_result set_size( file_handle::reference, std::uint64_t desired_size ) noexcept; +std::uint64_t get_size( file_handle::reference ) noexcept; // https://msdn.microsoft.com/en-us/library/ms810613.aspx Managing Memory-Mapped Files @@ -63,13 +55,5 @@ mapping BOOST_CC_REG create_mapping //------------------------------------------------------------------------------ } // namespace win32 //------------------------------------------------------------------------------ -} // namespace vm -//------------------------------------------------------------------------------ -} // namespace psi +} // namespace psi::vm //------------------------------------------------------------------------------ - -#ifdef PSI_VM_HEADER_ONLY - #include "file.inl" -#endif // PSI_VM_HEADER_ONLY - -#endif // file_hpp diff --git a/include/psi/vm/mappable_objects/file/handle.hpp b/include/psi/vm/mappable_objects/file/handle.hpp index 8da7196..1c354de 100644 --- a/include/psi/vm/mappable_objects/file/handle.hpp +++ b/include/psi/vm/mappable_objects/file/handle.hpp @@ -14,11 +14,9 @@ /// //////////////////////////////////////////////////////////////////////////////// //------------------------------------------------------------------------------ -#ifndef handle_hpp__56DDDE10_05C3_4B18_8DC5_89317D689F99 -#define handle_hpp__56DDDE10_05C3_4B18_8DC5_89317D689F99 #pragma once -//------------------------------------------------------------------------------ -#include "psi/vm/handles/handle.hpp" + +#include //------------------------------------------------------------------------------ namespace psi { @@ -27,7 +25,7 @@ namespace vm { //------------------------------------------------------------------------------ -#ifdef BOOST_HAS_UNISTD_H +#if __has_include( ) using file_handle = handle; // "Everything is a file" unofficial *nix philosophy #else struct file_handle : PSI_VM_IMPL()::handle @@ -44,4 +42,3 @@ struct file_handle : PSI_VM_IMPL()::handle //------------------------------------------------------------------------------ } // namespace psi //------------------------------------------------------------------------------ -#endif // handle_hpp diff --git a/include/psi/vm/mappable_objects/file/utility.hpp b/include/psi/vm/mappable_objects/file/utility.hpp index 2917707..7c7575c 100644 --- a/include/psi/vm/mappable_objects/file/utility.hpp +++ b/include/psi/vm/mappable_objects/file/utility.hpp @@ -14,22 +14,17 @@ /// //////////////////////////////////////////////////////////////////////////////// //------------------------------------------------------------------------------ -#ifndef utility_hpp__3713A8AF_A516_4A23_BE6A_2BB79EBF7B5F -#define utility_hpp__3713A8AF_A516_4A23_BE6A_2BB79EBF7B5F #pragma once -//------------------------------------------------------------------------------ -#include "psi/vm/error/error.hpp" -#include "psi/vm/flags/opening.hpp" -#include "psi/vm/mapped_view/mapped_view.hpp" -#include "psi/err/fallible_result.hpp" +#include +#include +#include + +#include #include //------------------------------------------------------------------------------ -namespace psi -{ -//------------------------------------------------------------------------------ -namespace vm +namespace psi::vm { //------------------------------------------------------------------------------ @@ -54,13 +49,5 @@ err::fallible_result map_read_only_file( wchar_t c #endif // _MSC_VER //------------------------------------------------------------------------------ -} // namespace vm +} // namespace psi::vm //------------------------------------------------------------------------------ -} // namespace psi -//------------------------------------------------------------------------------ - -#ifdef PSI_VM_HEADER_ONLY - #include "utility.inl" -#endif // PSI_VM_HEADER_ONLY - -#endif // utility_hpp diff --git a/include/psi/vm/mappable_objects/shared_memory/android/mem.hpp b/include/psi/vm/mappable_objects/shared_memory/android/mem.hpp index 53fa802..b81e04c 100644 --- a/include/psi/vm/mappable_objects/shared_memory/android/mem.hpp +++ b/include/psi/vm/mappable_objects/shared_memory/android/mem.hpp @@ -14,10 +14,8 @@ /// //////////////////////////////////////////////////////////////////////////////// //------------------------------------------------------------------------------ -#ifndef mem_hpp__497A6A4E_4630_4841_BAEE_2A26498ABF6A -#define mem_hpp__497A6A4E_4630_4841_BAEE_2A26498ABF6A #pragma once -//------------------------------------------------------------------------------ + #include "flags.hpp" #include @@ -39,10 +37,7 @@ #include #include //------------------------------------------------------------------------------ -namespace psi -{ -//------------------------------------------------------------------------------ -namespace vm +namespace psi::vm { //------------------------------------------------------------------------------ PSI_VM_POSIX_INLINE @@ -63,12 +58,12 @@ namespace detail /// (03.10.2015.) (Domagoj Saric) static std::string_view constexpr shm_prefix{ ASHMEM_NAME_DEF "/", sizeof( ASHMEM_NAME_DEF "/" ) - 1 }; static std::string_view shm_emulated_path{ "/mnt/sdcard/shm" }; - void BOOST_CC_REG prefix_shm_name( char const * const name, char * const prefixed_name, std::uint8_t const name_length, std::string_view const prefix ) noexcept + void prefix_shm_name( char const * const name, char * const prefixed_name, std::uint8_t const name_length, std::string_view const prefix ) noexcept { std::copy ( prefix.begin(), prefix.end(), prefixed_name ); std::memcpy( prefixed_name + prefix.size(), name, name_length + 1 ); } - void BOOST_CC_REG prefix_shm_name( char const * const name, char * const prefixed_name, std::uint8_t const name_length ) noexcept + void prefix_shm_name( char const * const name, char * const prefixed_name, std::uint8_t const name_length ) noexcept { std::copy ( shm_prefix.begin(), shm_prefix.end(), prefixed_name ); std::memcpy( prefixed_name + shm_prefix.size(), name, name_length + 1 ); @@ -126,7 +121,7 @@ class native_named_memory mflags const flags, std::nothrow_t ) noexcept( true ) - : base_t( detail::shm_open( name, size, flags ), flags, size ) + : base_t{ detail::shm_open( name, size, flags ), flags, size } {} native_named_memory @@ -135,16 +130,16 @@ class native_named_memory std::size_t const size, mflags const flags ) noexcept( false ) - : native_named_memory( name, size, flags, std::nothrow_t() ) + : native_named_memory{ name, size, flags, std::nothrow } { - if ( BOOST_UNLIKELY( !*this ) ) + if ( !*this ) [[ unlikely ]] err::make_and_throw_exception(); } native_named_memory( native_named_memory && other ) noexcept : base_t( std::move( other ) ) {} static - fallible_result BOOST_CC_REG create + fallible_result create ( char const * const name, std::size_t const size, @@ -157,14 +152,14 @@ class native_named_memory std::uint32_t size() const noexcept { auto const result( ::ioctl( mapping::get(), ASHMEM_GET_SIZE, nullptr ) ); - BOOST_ASSERT( result >= 0 ); + BOOST_ASSUME( result >= 0 ); return result; } - fallible_result BOOST_CC_REG resize( std::uint32_t const new_size ) + fallible_result resize( std::uint32_t const new_size ) noexcept { auto const result( ::ioctl( mapping::get(), ASHMEM_SET_SIZE, new_size ) ); - if ( BOOST_UNLIKELY( result < 0 ) ) return error(); + if ( result < 0 ) [[ unlikely ]] return error(); return err::success; } }; // class native_named_memory @@ -184,7 +179,7 @@ class file_backed_named_memory public: static - fallible_result BOOST_CC_REG create + fallible_result create ( char const * const name, std::size_t const size, @@ -219,7 +214,7 @@ class file_backed_named_memory { detail::shm_open( name, size, flags ), flags, size, std::move( file ) }; } - static bool BOOST_CC_REG cleanup( char const * const name ) noexcept + static bool cleanup( char const * const name ) noexcept { auto const length( std::strlen( name ) ); char adjusted_name[ detail::shm_emulated_path.size() + length + 1 ]; @@ -228,10 +223,10 @@ class file_backed_named_memory } static - fallible_result BOOST_CC_REG open( char const * const name, mflags const flags, std::size_t const size ) noexcept; + fallible_result open( char const * const name, mflags const flags, std::size_t const size ) noexcept; - auto BOOST_CC_REG size( ) const noexcept { return get_size( file_ ); } - auto BOOST_CC_REG resize( std::size_t const new_size ) noexcept { return set_size( file_, new_size ); } + auto size( ) const noexcept { return get_size( file_ ); } + auto resize( std::size_t const new_size ) noexcept { return set_size( file_, new_size ); } private: file_backed_named_memory( file_handle::reference const shm, flags::viewing const view_mapping_flags, std::size_t const maximum_size, file_handle && backing_file ) noexcept @@ -255,13 +250,5 @@ namespace detail //------------------------------------------------------------------------------ } // namespace posix //------------------------------------------------------------------------------ -} // namespace vm +} // namespace psi::vm //------------------------------------------------------------------------------ -} // namespace psi -//------------------------------------------------------------------------------ - -#ifdef PSI_VM_HEADER_ONLY -//# include "mem.inl" -#endif // PSI_VM_HEADER_ONLY - -#endif // mem_hpp diff --git a/include/psi/vm/mappable_objects/shared_memory/flags.hpp b/include/psi/vm/mappable_objects/shared_memory/flags.hpp index 0863e95..a8f1303 100644 --- a/include/psi/vm/mappable_objects/shared_memory/flags.hpp +++ b/include/psi/vm/mappable_objects/shared_memory/flags.hpp @@ -14,24 +14,9 @@ /// //////////////////////////////////////////////////////////////////////////////// //------------------------------------------------------------------------------ -#ifndef flags_hpp__D5F08A78_778F_4E71_B0DC_A0880482BFB7 -#define flags_hpp__D5F08A78_778F_4E71_B0DC_A0880482BFB7 #pragma once -//------------------------------------------------------------------------------ -#include "psi/vm/detail/impl_selection.hpp" -#include PSI_VM_IMPL_INCLUDE( BOOST_PP_EMPTY, BOOST_PP_IDENTITY( /flags.hpp ) ) -//------------------------------------------------------------------------------ -namespace psi -{ -//------------------------------------------------------------------------------ -namespace vm -{ -//------------------------------------------------------------------------------ +#include +#include PSI_VM_DIR_IMPL_INCLUDE( BOOST_PP_IDENTITY( flags.hpp ) ) //------------------------------------------------------------------------------ -} // namespace vm -//------------------------------------------------------------------------------ -} // namespace psi -//------------------------------------------------------------------------------ -#endif // flags_hpp diff --git a/include/psi/vm/mappable_objects/shared_memory/mem.hpp b/include/psi/vm/mappable_objects/shared_memory/mem.hpp index a92afe3..7da0ed6 100644 --- a/include/psi/vm/mappable_objects/shared_memory/mem.hpp +++ b/include/psi/vm/mappable_objects/shared_memory/mem.hpp @@ -14,25 +14,17 @@ /// //////////////////////////////////////////////////////////////////////////////// //------------------------------------------------------------------------------ -#ifndef mem_hpp__3BF9463B_9C83_4515_9A4E_53E9C9574517 -#define mem_hpp__3BF9463B_9C83_4515_9A4E_53E9C9574517 #pragma once -//------------------------------------------------------------------------------ + #include "policies.hpp" -#include "psi/vm/detail/impl_selection.hpp" -#include PSI_VM_IMPL_INCLUDE( BOOST_PP_EMPTY, BOOST_PP_IDENTITY( /mem.hpp ) ) +#include +#include PSI_VM_DIR_IMPL_INCLUDE( BOOST_PP_IDENTITY( mem.hpp ) ) //------------------------------------------------------------------------------ -namespace psi -{ -//------------------------------------------------------------------------------ -namespace vm +namespace psi::vm { //------------------------------------------------------------------------------ //------------------------------------------------------------------------------ -} // namespace vm -//------------------------------------------------------------------------------ -} // namespace psi +} // namespace psi::vm //------------------------------------------------------------------------------ -#endif // mem_hpp diff --git a/include/psi/vm/mappable_objects/shared_memory/posix/flags.hpp b/include/psi/vm/mappable_objects/shared_memory/posix/flags.hpp index 194fa32..2569435 100644 --- a/include/psi/vm/mappable_objects/shared_memory/posix/flags.hpp +++ b/include/psi/vm/mappable_objects/shared_memory/posix/flags.hpp @@ -14,18 +14,13 @@ /// //////////////////////////////////////////////////////////////////////////////// //------------------------------------------------------------------------------ -#ifndef flags_hpp__F9CD9C91_1F07_4107_A422_0D814F0FE487 -#define flags_hpp__F9CD9C91_1F07_4107_A422_0D814F0FE487 #pragma once + +#include +#include +#include //------------------------------------------------------------------------------ -#include "psi/vm/detail/impl_selection.hpp" -#include "psi/vm/detail/posix.hpp" -#include "psi/vm/flags/posix/mapping.hpp" -//------------------------------------------------------------------------------ -namespace psi -{ -//------------------------------------------------------------------------------ -namespace vm +namespace psi::vm { //------------------------------------------------------------------------------ PSI_VM_POSIX_INLINE @@ -58,16 +53,16 @@ struct shared_memory value_type value; }; - static shared_memory BOOST_CC_REG create + static shared_memory create ( access_privileges , named_object_construction_policy, system_hints ) noexcept; - operator mapping () const noexcept + operator mapping() const noexcept { - auto flags( mapping::create( ap.object_access, viewing::share_mode::shared ) ); + auto flags{ mapping::create( ap.object_access, viewing::share_mode::shared ) }; flags.flags |= hints.value; return static_cast( flags ); } @@ -82,13 +77,5 @@ struct shared_memory //------------------------------------------------------------------------------ } // namespace posix //------------------------------------------------------------------------------ -} // namespace vm +} // namespace psi::vm //------------------------------------------------------------------------------ -} // namespace psi -//------------------------------------------------------------------------------ - -#ifdef PSI_VM_HEADER_ONLY - #include "flags.inl" -#endif // PSI_VM_HEADER_ONLY - -#endif // flags.hpp diff --git a/include/psi/vm/mappable_objects/shared_memory/posix/mem.hpp b/include/psi/vm/mappable_objects/shared_memory/posix/mem.hpp index 54751ca..7140e56 100644 --- a/include/psi/vm/mappable_objects/shared_memory/posix/mem.hpp +++ b/include/psi/vm/mappable_objects/shared_memory/posix/mem.hpp @@ -14,18 +14,15 @@ /// //////////////////////////////////////////////////////////////////////////////// //------------------------------------------------------------------------------ -#ifndef mem_hpp__448C1250_E77A_4945_87A2_087793290E07 -#define mem_hpp__448C1250_E77A_4945_87A2_087793290E07 #pragma once -//------------------------------------------------------------------------------ -#include "flags.hpp" -#include +#include "flags.hpp" #include -#include #include #include +#include +#include #include #include @@ -46,12 +43,10 @@ #include #include +#include #include //------------------------------------------------------------------------------ -namespace psi -{ -//------------------------------------------------------------------------------ -namespace vm +namespace psi::vm { //------------------------------------------------------------------------------ PSI_VM_POSIX_INLINE @@ -66,7 +61,7 @@ namespace detail { // http://lists.apple.com/archives/darwin-development/2003/Mar/msg00242.html // http://insanecoding.blogspot.hr/2007/11/pathmax-simply-isnt.html - std::size_t constexpr max_shm_name = + inline std::uint32_t constexpr max_shm_name = #if defined( SHM_NAME_MAX ) // OSX SHM_NAME_MAX; #elif defined( PSHMNAMLEN ) // OSX, BSD @@ -75,7 +70,8 @@ namespace detail NAME_MAX; #endif // SHM_NAME_MAX - void BOOST_CC_REG slash_name( char const * const name, char * const slashed_name, std::uint8_t const name_length ) + inline + void preslash_name( char const * const name, char * const slashed_name, std::uint8_t const name_length ) noexcept { slashed_name[ 0 ] = '/'; BOOST_ASSUME( name[ name_length ] == '\0' ); @@ -83,7 +79,7 @@ namespace detail } // FreeBSD extension SHM_ANON http://www.freebsd.org/cgi/man.cgi?query=shm_open - file_handle::reference BOOST_CC_REG shm_open_slashed + file_handle::reference shm_open_slashed ( char const * const slashed_name, std::size_t const size, @@ -98,31 +94,31 @@ namespace detail // strange OSX behaviour (+ O_TRUNC does not seem to work at all -> EINVAL) // http://lists.apple.com/archives/darwin-development/2003/Oct/msg00187.html - auto const oflags ( flags.ap.oflag() | static_cast( flags.nocp ) ); - auto const mode ( flags.ap.pmode() ); - auto file_descriptor( ::shm_open( slashed_name, oflags, mode ) ); - if ( BOOST_LIKELY( file_descriptor != -1 ) ) + auto const oflags { flags.ap.oflag() | static_cast( flags.nocp ) }; + auto const mode { flags.ap.pmode() }; + auto file_descriptor{ ::shm_open( slashed_name, oflags, mode ) }; + if ( file_descriptor != -1 ) [[ likely ]] { - if ( BOOST_UNLIKELY( ::ftruncate( file_descriptor, size ) != 0 ) ) + if ( ::ftruncate( file_descriptor, static_cast( size ) ) != 0 ) [[ unlikely ]] { - BOOST_VERIFY( ::shm_unlink( slashed_name ) == 0 ); - BOOST_VERIFY( ::close ( file_descriptor) == 0 ); - file_descriptor = file_handle::invalid_handle; + BOOST_VERIFY( ::shm_unlink( slashed_name ) == 0 ); + BOOST_VERIFY( ::close ( file_descriptor ) == 0 ); + file_descriptor = file_handle::invalid_value; } } return { file_descriptor }; } - file_handle::reference BOOST_CC_REG shm_open + file_handle::reference shm_open ( char const * const name, std::size_t const size, flags::shared_memory const & flags ) { - auto const length( std::strlen( name ) ); //...todo...constexpr...https://www.daniweb.com/software-development/cpp/code/482276/c-11-compile-time-string-concatenation-with-constexpr + auto const length{ static_cast( std::strlen( name ) ) }; //...todo...constexpr char slashed_name[ 1 + length + 1 ]; - slash_name( name, slashed_name, length ); + preslash_name( name, slashed_name, length ); return shm_open_slashed( slashed_name, size, flags ); } } // namespace detail @@ -149,8 +145,8 @@ class native_named_memory std::size_t const size, mflags const flags, std::nothrow_t - ) noexcept( true ) - : base_t( detail::shm_open( name, size, flags ), flags, size ) + ) noexcept + : base_t{ detail::shm_open( name, size, flags ), flags, size } {} native_named_memory @@ -159,14 +155,14 @@ class native_named_memory std::size_t const size, mflags const flags ) - : base_t( detail::shm_open( name, size, flags ), flags, size ) + : native_named_memory{ name, size, flags, std::nothrow } { - if ( BOOST_UNLIKELY( !*this ) ) + if ( !*this ) [[ unlikely ]] err::make_and_throw_exception(); } static - fallible_result BOOST_CC_REG create + fallible_result create ( char const * const name, std::size_t const size, @@ -176,11 +172,11 @@ class native_named_memory return native_named_memory{ name, size, flags, std::nothrow }; } - static bool BOOST_CC_REG cleanup( char const * const name ) noexcept + static bool cleanup( char const * const name ) noexcept { - auto const length( std::strlen( name ) ); + auto const length{ static_cast( std::strlen( name ) ) }; char slashed_name[ 1 + length + 1 ]; - detail::slash_name( name, slashed_name, length ); + detail::preslash_name( name, slashed_name, length ); auto const result( ::shm_unlink( slashed_name ) ); if ( result != error::no_error ) { @@ -191,8 +187,6 @@ class native_named_memory } auto size() const noexcept { return get_size( *this ); } - -private: }; // class native_named_memory @@ -212,22 +206,22 @@ namespace detail //named_semaphore & operator++() { BOOST_VERIFY( semadd( +1 ) ); return *this; } //named_semaphore & operator--() { BOOST_VERIFY( semadd( -1 ) ); return *this; } - void BOOST_CC_REG remove() noexcept; + void remove() noexcept; - bool BOOST_CC_REG semadd( int value, bool nowait = false ) noexcept; - bool BOOST_CC_REG try_wait() { return semadd( -1, true ); } + bool semadd( short value, bool nowait = false ) noexcept; + bool try_wait() { return semadd( -1, true ); } - std::uint16_t BOOST_CC_REG value() const noexcept; + std::uint16_t value() const noexcept; public: - explicit operator bool() const { return semid_ != -1; } + explicit operator bool() const __restrict noexcept { return semid_ != -1; } private: named_semaphore( int const id ) : semid_( id ) {} - bool BOOST_CC_REG is_initialised() const noexcept; + bool is_initialised() const noexcept; - bool BOOST_CC_REG semop( int opcode, bool nowait = false ) noexcept; + bool semop( short opcode, bool nowait = false ) noexcept; private: int semid_; @@ -251,8 +245,6 @@ namespace detail using mflags = flags::shared_memory; public: - scoped_named_memory() = default; - scoped_named_memory ( char const * const name, @@ -261,9 +253,9 @@ namespace detail std::nothrow_t ) noexcept : - named_memory_guard( name, flags.ap.system_access, flags.nocp ), - shm_name_t ( conditional_make_slashed_name( name ) ), - base_t ( conditional_make_shm_fd ( size, flags ), flags, size ) + named_memory_guard{ name, flags.ap.system_access, flags.nocp }, + shm_name_t { conditional_make_slashed_name( name ) }, + base_t { conditional_make_shm_fd ( size, flags ), flags, size } {} scoped_named_memory @@ -274,7 +266,7 @@ namespace detail ) noexcept( false ) : scoped_named_memory( name, size, flags, std::nothrow_t() ) { - if ( BOOST_UNLIKELY( !*this ) ) + if ( !*this ) [[ unlikely ]] err::make_and_throw_exception(); } @@ -284,7 +276,7 @@ namespace detail scoped_named_memory( scoped_named_memory const & ) = delete; static - fallible_result BOOST_CC_REG create + fallible_result create ( char const * const name, std::size_t const size, @@ -295,9 +287,9 @@ namespace detail } static - fallible_result BOOST_CC_REG open( char const * const name, mflags const flags, std::size_t const size ) noexcept; // todo + fallible_result open( char const * const name, mflags const flags, std::size_t const size ) noexcept; // todo - ~scoped_named_memory() + ~scoped_named_memory() noexcept { if ( static_cast( *this ) ) { @@ -319,22 +311,22 @@ namespace detail private: detail::shm_name_t conditional_make_slashed_name( char const * const name ) const __restrict { - if ( BOOST_UNLIKELY( !static_cast( *this ) ) ) + if ( !named_memory_guard::operator bool() ) [[ unlikely ]] return nullptr; - auto const length( std::strlen( name ) ); - auto const slashed_name( new ( std::nothrow ) char[ 1 + length + 1 ] ); + auto const length{ static_cast( std::strlen( name ) ) }; + auto const slashed_name{ new ( std::nothrow ) char[ 1 + length + 1 ] }; if ( BOOST_LIKELY( slashed_name != nullptr ) ) - detail::slash_name( name, slashed_name, length ); + detail::preslash_name( name, slashed_name, length ); else error::set( ENOMEM ); return detail::shm_name_t( slashed_name ); } - file_handle::reference conditional_make_shm_fd( std::size_t const length, flags::shared_memory const & flags ) const __restrict + file_handle::reference conditional_make_shm_fd( std::size_t const length, flags::shared_memory const & flags ) const noexcept { - auto const & name( static_cast( *this ) ); + auto const name{ shm_name_t::get() }; if ( BOOST_LIKELY( name != nullptr ) ) - return detail::shm_open( name.get(), length, flags ); + return detail::shm_open( name, length, flags ); return { file_handle::traits::invalid_value }; } }; // class scoped_named_memory @@ -353,22 +345,11 @@ namespace detail } // namespace detail #endif // __ANDROID__ -template <> -struct is_resizable : std::true_type {}; - -mapping BOOST_CC_REG create_mapping( handle::reference, mapping ) noexcept; +mapping create_mapping( handle::reference, mapping ) noexcept; //------------------------------------------------------------------------------ } // namespace posix //------------------------------------------------------------------------------ -} // namespace vm -//------------------------------------------------------------------------------ -} // namespace psi +} // namespace psi::vm //------------------------------------------------------------------------------ - -#ifdef PSI_VM_HEADER_ONLY -# include "mem.inl" -#endif // PSI_VM_HEADER_ONLY - -#endif // mem_hpp diff --git a/include/psi/vm/mappable_objects/shared_memory/win32/flags.hpp b/include/psi/vm/mappable_objects/shared_memory/win32/flags.hpp index 48d3337..ed63887 100644 --- a/include/psi/vm/mappable_objects/shared_memory/win32/flags.hpp +++ b/include/psi/vm/mappable_objects/shared_memory/win32/flags.hpp @@ -1,6 +1,6 @@ //////////////////////////////////////////////////////////////////////////////// /// -/// \file shared_memory/win32/flags.hpp +/// \file shared_memory/flags.win32.hpp /// ----------------------------------- /// /// Copyright (c) Domagoj Saric 2010 - 2024. @@ -14,22 +14,17 @@ /// //////////////////////////////////////////////////////////////////////////////// //------------------------------------------------------------------------------ -#ifndef flags_hpp__504C3F9E_97C2_4E8C_82C6_881340C5FBA6 -#define flags_hpp__504C3F9E_97C2_4E8C_82C6_881340C5FBA6 #pragma once -//------------------------------------------------------------------------------ + #include "boost/config.hpp" #include -#include -//------------------------------------------------------------------------------ -namespace psi -{ +#include //------------------------------------------------------------------------------ -namespace vm +namespace psi::vm { //------------------------------------------------------------------------------ -namespace win32 +inline namespace win32 { //------------------------------------------------------------------------------ namespace flags @@ -59,13 +54,5 @@ struct shared_memory : mapping //------------------------------------------------------------------------------ } // namespace win32 //------------------------------------------------------------------------------ -} // namespace vm +} // namespace psi::vm //------------------------------------------------------------------------------ -} // namespace psi -//------------------------------------------------------------------------------ - -#ifdef PSI_VM_HEADER_ONLY - #include "flags.inl" -#endif // PSI_VM_HEADER_ONLY - -#endif // flags.hpp diff --git a/include/psi/vm/mappable_objects/shared_memory/win32/mem.hpp b/include/psi/vm/mappable_objects/shared_memory/win32/mem.hpp index 03c7c30..88838fb 100644 --- a/include/psi/vm/mappable_objects/shared_memory/win32/mem.hpp +++ b/include/psi/vm/mappable_objects/shared_memory/win32/mem.hpp @@ -14,10 +14,8 @@ /// //////////////////////////////////////////////////////////////////////////////// //------------------------------------------------------------------------------ -#ifndef mem_hpp__6DB85D55_CCA0_493D_AB14_78064457885B -#define mem_hpp__6DB85D55_CCA0_493D_AB14_78064457885B #pragma once -//------------------------------------------------------------------------------ + #include "flags.hpp" #include @@ -34,13 +32,10 @@ #include #include //------------------------------------------------------------------------------ -namespace psi -{ -//------------------------------------------------------------------------------ -namespace vm +namespace psi::vm { //------------------------------------------------------------------------------ -namespace win32 +inline namespace win32 { //------------------------------------------------------------------------------ @@ -55,7 +50,7 @@ namespace detail char const * name () const { return &buffer_[ name_offset_ ]; } private: - void BOOST_CC_REG apply( char const * __restrict const name ) + void apply( char const * __restrict const name ) { auto const length( static_cast( ::GetWindowsDirectoryA( &buffer_[ 0 ], static_cast( buffer_.size() ) ) ) ); BOOST_ASSUME( length ); @@ -87,7 +82,7 @@ namespace detail public: static - named_memory_base BOOST_CC_REG create + named_memory_base create ( shm_path const & __restrict name, std::size_t const size, @@ -111,7 +106,7 @@ namespace detail } ) ); - if ( BOOST_UNLIKELY( !file ) ) + if ( !file ) [[ unlikely ]] return {}; auto const preexisting_file( err::last_win32_error::is() ); @@ -136,8 +131,8 @@ namespace detail name.name() ) }; - auto const creation_disposition( flags.creation_disposition ); - auto const preexisting_mapping ( err::last_win32_error::is() ); + auto const creation_disposition{ flags.creation_disposition }; + auto const preexisting_mapping { err::last_win32_error::is() }; BOOST_ASSERT( preexisting_file == preexisting_mapping ); switch ( creation_disposition ) { @@ -391,13 +386,5 @@ namespace detail //------------------------------------------------------------------------------ } // namespace win32 //------------------------------------------------------------------------------ -} // namespace vm +} // namespace psi::vm //------------------------------------------------------------------------------ -} // namespace psi -//------------------------------------------------------------------------------ - -#ifdef PSI_VM_HEADER_ONLY - //#include "mem.inl" -#endif // PSI_VM_HEADER_ONLY - -#endif // mem_hpp diff --git a/include/psi/vm/mapped_view/guarded_operation.hpp b/include/psi/vm/mapped_view/guarded_operation.hpp index 8c877f0..57ceef3 100644 --- a/include/psi/vm/mapped_view/guarded_operation.hpp +++ b/include/psi/vm/mapped_view/guarded_operation.hpp @@ -18,7 +18,7 @@ #define guarded_operation_hpp__B181EBDC_EA6B_451A_90D0_B6E1BE57DCA8 #pragma once //------------------------------------------------------------------------------ -#include "psi/vm/mapped_view/mapped_view.hpp" +#include diff --git a/include/psi/vm/mapped_view/mapped_view.hpp b/include/psi/vm/mapped_view/mapped_view.hpp index 70b4bd6..62986b9 100644 --- a/include/psi/vm/mapped_view/mapped_view.hpp +++ b/include/psi/vm/mapped_view/mapped_view.hpp @@ -13,18 +13,13 @@ /// For more information, see http://www.boost.org /// //////////////////////////////////////////////////////////////////////////////// -//------------------------------------------------------------------------------ -#ifndef mapped_view_hpp__D9C84FF5_E506_4ECB_9778_61E036048D28 -#define mapped_view_hpp__D9C84FF5_E506_4ECB_9778_61E036048D28 #pragma once -//------------------------------------------------------------------------------ -#include "psi/vm/detail/impl_selection.hpp" -#include "psi/vm/error/error.hpp" -#include "psi/vm/mapping/mapping.hpp" -#include "psi/vm/handles/handle.hpp" -#include "psi/vm/mapping/mapping.hpp" -#include "psi/vm/span.hpp" -#include "psi/vm/allocation/allocation.hpp" + +#include +#include +#include +#include +#include #include #include @@ -40,8 +35,6 @@ namespace vm { //------------------------------------------------------------------------------ -inline namespace PSI_VM_IMPL() { struct mapper; } - //////////////////////////////////////////////////////////////////////////////// /// /// \class basic_mapped_view @@ -55,10 +48,9 @@ template class basic_mapped_view : public std::conditional_t { public: - using mapper = PSI_VM_IMPL()::mapper; - using error_t = error; - using mapping_t = mapping; - using span = std::conditional_t; + using error_t = error; + using mapping_t = mapping; + using span = std::conditional_t; using value_type = typename span::value_type; public: @@ -149,9 +141,3 @@ using read_only_mapped_view = basic_mapped_view; //------------------------------------------------------------------------------ } // namespace psi //------------------------------------------------------------------------------ - -#ifdef PSI_VM_HEADER_ONLY - #include "mapped_view.inl" -#endif - -#endif // mapped_view_hpp diff --git a/include/psi/vm/mapped_view/win32/mapped_view.inl b/include/psi/vm/mapped_view/win32/mapped_view.inl deleted file mode 100644 index 0445928..0000000 --- a/include/psi/vm/mapped_view/win32/mapped_view.inl +++ /dev/null @@ -1,96 +0,0 @@ -//////////////////////////////////////////////////////////////////////////////// -/// -/// \file mapped_view.inl -/// --------------------- -/// -/// Copyright (c) Domagoj Saric 2010 - 2024. -/// -/// Use, modification and distribution is subject to the -/// Boost Software License, Version 1.0. -/// (See accompanying file LICENSE_1_0.txt or copy at -/// http://www.boost.org/LICENSE_1_0.txt) -/// -/// For more information, see http://www.boost.org -/// -//////////////////////////////////////////////////////////////////////////////// -//------------------------------------------------------------------------------ -#include "psi/vm/mapped_view/mapped_view.hpp" - -#include "psi/vm/detail/impl_selection.hpp" -#include "psi/vm/detail/win32.hpp" -#include "psi/vm/align.hpp" -//------------------------------------------------------------------------------ -namespace psi::vm::win32 -{ -//------------------------------------------------------------------------------ - -struct mapper -{ - using error_t = error; - using flags_t = flags::mapping; - using mapping_t = mapping; - - static BOOST_ATTRIBUTES( BOOST_MINSIZE, BOOST_EXCEPTIONLESS ) - mapped_span BOOST_CC_REG - map - ( - mapping::handle const source_mapping, - flags ::viewing const flags , - std ::uint64_t const offset , // ERROR_MAPPED_ALIGNMENT - std ::size_t const desired_size - ) noexcept - { - // Implementation note: - // Mapped views hold internal references to the mapping handles so we do - // not need to hold/store them ourselves: - // http://msdn.microsoft.com/en-us/library/aa366537(VS.85).aspx - // (26.03.2010.) (Domagoj Saric) - - ULARGE_INTEGER const win32_offset{ .QuadPart = offset }; - auto const view_start - { - static_cast - ( - ::MapViewOfFile - ( - source_mapping, - flags.map_view_flags, - win32_offset.HighPart, - win32_offset.LowPart, - desired_size - ) - ) - }; - - return - { - view_start, - view_start ? desired_size : 0 - }; - } - - static BOOST_ATTRIBUTES( BOOST_MINSIZE, BOOST_EXCEPTIONLESS, BOOST_RESTRICTED_FUNCTION_L1 ) - void BOOST_CC_REG unmap( mapped_span const view ) - { - BOOST_VERIFY( ::UnmapViewOfFile( view.data() ) || view.empty() ); - } - - static void shrink( mapped_span const view, std::size_t const target_size ) noexcept - { - // TODO OfferVirtualMemory, VirtualUnlock - decommit - ( - align_up ( view.data() + target_size, commit_granularity ), - align_down( view.size() - target_size, commit_granularity ) - ); - } - - static void flush( mapped_span const view ) noexcept - { - BOOST_VERIFY( ::FlushViewOfFile( view.data(), view.size() ) == 0 ); - } -}; // struct mapper - -//------------------------------------------------------------------------------ -} // psi::vm::win32 -//------------------------------------------------------------------------------ diff --git a/include/psi/vm/mapping/mapping.hpp b/include/psi/vm/mapping/mapping.hpp index 1218ed0..2d33de4 100644 --- a/include/psi/vm/mapping/mapping.hpp +++ b/include/psi/vm/mapping/mapping.hpp @@ -13,24 +13,18 @@ /// For more information, see http://www.boost.org /// //////////////////////////////////////////////////////////////////////////////// -//------------------------------------------------------------------------------ -#ifndef mapping_hpp__D42BC724_FD9A_4C7B_B521_CF3C29C948B3 -#define mapping_hpp__D42BC724_FD9A_4C7B_B521_CF3C29C948B3 #pragma once -//------------------------------------------------------------------------------ -#include "psi/vm/detail/impl_selection.hpp" -#include "psi/vm/handles/handle.hpp" -#include PSI_VM_IMPL_INCLUDE( BOOST_PP_EMPTY, BOOST_PP_IDENTITY( /mapping.hpp ) ) +#include +#include + +#include PSI_VM_IMPL_INCLUDE( mapping ) #include #include -#include // merely for the (fwd) decl std::filesystem::path +#include // merely for the (fwd) decl of std::filesystem::path //------------------------------------------------------------------------------ -namespace psi -{ -//------------------------------------------------------------------------------ -namespace vm +namespace psi::vm { //------------------------------------------------------------------------------ @@ -73,8 +67,5 @@ mapping open_mapping( mapping, char const * name ) noexcept; #endif // todo //------------------------------------------------------------------------------ -} // namespace vm -//------------------------------------------------------------------------------ -} // namespace psi +} // namespace psi::vm //------------------------------------------------------------------------------ -#endif // mapping_hpp diff --git a/include/psi/vm/mapping/posix/mapping.hpp b/include/psi/vm/mapping/mapping.posix.hpp similarity index 76% rename from include/psi/vm/mapping/posix/mapping.hpp rename to include/psi/vm/mapping/mapping.posix.hpp index 9de963f..efbb5a9 100644 --- a/include/psi/vm/mapping/posix/mapping.hpp +++ b/include/psi/vm/mapping/mapping.posix.hpp @@ -14,20 +14,13 @@ /// //////////////////////////////////////////////////////////////////////////////// //------------------------------------------------------------------------------ -#ifndef mapping_hpp__99837E03_86B1_42F5_A57D_69A6E828DD08 -#define mapping_hpp__99837E03_86B1_42F5_A57D_69A6E828DD08 #pragma once -//------------------------------------------------------------------------------ -#include "psi/vm/error/error.hpp" -#include "psi/vm/flags/posix/mapping.hpp" -#include "psi/vm/handles/posix/handle.hpp" -#include -//------------------------------------------------------------------------------ -namespace psi -{ +#include +#include +#include //------------------------------------------------------------------------------ -namespace vm +namespace psi::vm { //------------------------------------------------------------------------------ PSI_VM_POSIX_INLINE @@ -79,14 +72,11 @@ struct [[ clang::trivial_abi ]] mapping }; // struct mapping // fwd declarations for file_handle functions which work for mappings as well ('everything is a file') -err::fallible_result BOOST_CC_REG set_size( handle:: reference, std::uint64_t desired_size ) noexcept; -std::uint64_t BOOST_CC_REG get_size( handle::const_reference ) noexcept; +err::fallible_result set_size( handle:: reference, std::uint64_t desired_size ) noexcept; +std::uint64_t get_size( handle::const_reference ) noexcept; //------------------------------------------------------------------------------ } // namespace posix //------------------------------------------------------------------------------ -} // namespace vm -//------------------------------------------------------------------------------ -} // namespace psi +} // namespace psi::vm //------------------------------------------------------------------------------ -#endif // mapping_hpp diff --git a/include/psi/vm/mapping/win32/mapping.hpp b/include/psi/vm/mapping/mapping.win32.hpp similarity index 57% rename from include/psi/vm/mapping/win32/mapping.hpp rename to include/psi/vm/mapping/mapping.win32.hpp index 9f9214f..d523852 100644 --- a/include/psi/vm/mapping/win32/mapping.hpp +++ b/include/psi/vm/mapping/mapping.win32.hpp @@ -14,23 +14,20 @@ /// //////////////////////////////////////////////////////////////////////////////// //------------------------------------------------------------------------------ -#ifndef mapping_hpp__8B2CEDFB_C87C_4AA4_B9D0_8EF0A42825F2 -#define mapping_hpp__8B2CEDFB_C87C_4AA4_B9D0_8EF0A42825F2 #pragma once -//------------------------------------------------------------------------------ -#include "psi/vm/handles/win32/handle.hpp" -#include "psi/vm/flags/win32/mapping.hpp" -#include "psi/vm/error/error.hpp" -#include "psi/vm/error/nt/error.hpp" -#include -//#include //...broken? -#include -#include +#include +#include +#include + +#include #include //------------------------------------------------------------------------------ -namespace psi::vm::win32 +namespace psi::vm +{ +//------------------------------------------------------------------------------ +inline namespace win32 { //------------------------------------------------------------------------------ @@ -66,33 +63,11 @@ struct [[ clang::trivial_abi ]] mapping }; // struct mapping -inline std::uint64_t get_size( mapping::const_handle const mapping_handle ) noexcept -{ - using namespace nt; - SECTION_BASIC_INFORMATION info; - auto const result{ NtQuerySection( mapping_handle.value, SECTION_INFORMATION_CLASS::SectionBasicInformation, &info, sizeof( info ), nullptr ) }; - BOOST_VERIFY( NT_SUCCESS( result ) ); - return info.SectionSize.QuadPart; -} - -inline err::fallible_result set_size( mapping::handle const mapping_handle, std::uint64_t const new_size ) noexcept -{ - using namespace nt; - LARGE_INTEGER ntsz{ .QuadPart = static_cast( new_size ) }; - auto const result{ NtExtendSection( mapping_handle.value, &ntsz ) }; - if ( !NT_SUCCESS( result ) ) [[ unlikely ]] - return result; - - BOOST_ASSERT( ntsz.QuadPart >= static_cast( new_size ) ); - if ( ntsz.QuadPart > static_cast( new_size ) ) - { - BOOST_ASSERT( ntsz.QuadPart == static_cast( get_size( mapping_handle ) ) ); - // NtExtendSection does not seem to support downsizing. TODO workaround - } - return err::success; -} +std::uint64_t get_size( mapping::const_handle ) noexcept; +err::fallible_result set_size( mapping:: handle, std::uint64_t new_size ) noexcept; //------------------------------------------------------------------------------ -} // namespace psi::vm::win32 +} // namespace win32 +//------------------------------------------------------------------------------ +} // namespace psi::vm //------------------------------------------------------------------------------ -#endif // mapping_hpp diff --git a/include/psi/vm/vector.hpp b/include/psi/vm/vector.hpp index 009b67d..104dd62 100644 --- a/include/psi/vm/vector.hpp +++ b/include/psi/vm/vector.hpp @@ -26,6 +26,7 @@ #include #include #include +#include //------------------------------------------------------------------------------ namespace psi { diff --git a/include/psi/vm/allocation/impl/allocation.impl.hpp b/src/allocation/allocation.impl.hpp similarity index 98% rename from include/psi/vm/allocation/impl/allocation.impl.hpp rename to src/allocation/allocation.impl.hpp index c2ed6da..9bffa3c 100644 --- a/include/psi/vm/allocation/impl/allocation.impl.hpp +++ b/src/allocation/allocation.impl.hpp @@ -13,7 +13,7 @@ //------------------------------------------------------------------------------ #pragma once -#include "../allocation.hpp" +#include //------------------------------------------------------------------------------ namespace psi { diff --git a/include/psi/vm/allocation/impl/allocation.posix.cpp b/src/allocation/allocation.posix.cpp similarity index 98% rename from include/psi/vm/allocation/impl/allocation.posix.cpp rename to src/allocation/allocation.posix.cpp index 5392f6e..b8bd27b 100644 --- a/include/psi/vm/allocation/impl/allocation.posix.cpp +++ b/src/allocation/allocation.posix.cpp @@ -12,8 +12,8 @@ //////////////////////////////////////////////////////////////////////////////// //------------------------------------------------------------------------------ #include "allocation.impl.hpp" -#include "psi/vm/align.hpp" -#include "psi/vm/detail/posix.hpp" +#include +#include #include #include diff --git a/include/psi/vm/allocation/impl/allocation.win32.cpp b/src/allocation/allocation.win32.cpp similarity index 96% rename from include/psi/vm/allocation/impl/allocation.win32.cpp rename to src/allocation/allocation.win32.cpp index ff93ce0..e80e235 100644 --- a/include/psi/vm/allocation/impl/allocation.win32.cpp +++ b/src/allocation/allocation.win32.cpp @@ -12,19 +12,15 @@ //////////////////////////////////////////////////////////////////////////////// //------------------------------------------------------------------------------ #include "allocation.impl.hpp" -#include "psi/vm/detail/nt.hpp" +#include +#include #include #include #include // for std::min - -#include -//------------------------------------------------------------------------------ -namespace psi -{ //------------------------------------------------------------------------------ -namespace vm +namespace psi::vm { //------------------------------------------------------------------------------ @@ -188,7 +184,5 @@ bool allocate_fixed( void * const address, std::size_t const size, allocation_ty } //------------------------------------------------------------------------------ -} // namespace vm -//------------------------------------------------------------------------------ -} // namespace psi +} // namespace psi::vm //------------------------------------------------------------------------------ diff --git a/include/psi/vm/allocation/impl/remap.cpp b/src/allocation/remap.cpp similarity index 83% rename from include/psi/vm/allocation/impl/remap.cpp rename to src/allocation/remap.cpp index 9db55a0..d44fb71 100644 --- a/include/psi/vm/allocation/impl/remap.cpp +++ b/src/allocation/remap.cpp @@ -12,19 +12,25 @@ //////////////////////////////////////////////////////////////////////////////// //------------------------------------------------------------------------------ #include "allocation.impl.hpp" +#include #include #include #ifdef __linux__ +#ifndef _GNU_SOURCE # define _GNU_SOURCE +#endif # include +#endif // linux + +#ifndef NDEBUG +#include #endif +#include +#include // memcpy //------------------------------------------------------------------------------ -namespace psi -{ -//------------------------------------------------------------------------------ -namespace vm +namespace psi::vm { //------------------------------------------------------------------------------ @@ -66,9 +72,9 @@ expand_result expand // mremap requires the same protection for the entire range (TODO: add this as a precondition for calling code?) // https://github.com/torvalds/linux/blob/master/mm/mremap.c // TODO: implement 'VirtualQuery4Linux' https://github.com/ouadev/proc_maps_parser - BOOST_VERIFY( ::mprotect( address, current_size, allocation_type::commit ) == 0 ); + BOOST_VERIFY( ::mprotect( address, current_size, static_cast( allocation_type::commit ) ) == 0 ); - auto const actual_address{ ::mremap( address, current_size, required_size_for_end_expansion, realloc_type ) }; // https://laptrinhx.com/linux-mremap-tlb-flush-too-late-1728576753 + auto const actual_address{ ::mremap( address, current_size, required_size_for_end_expansion, static_cast( realloc_type ) ) }; // https://laptrinhx.com/linux-mremap-tlb-flush-too-late-1728576753 if ( actual_address != MAP_FAILED ) [[ likely ]] { auto remap_method{ expand_result::moved }; @@ -77,11 +83,11 @@ expand_result expand BOOST_ASSUME( actual_address == address ); remap_method = expand_result::back_extended; } - return { static_cast< std::byte * >( actual_address ), required_size_for_end_expansion, remap_method }; + return { { static_cast< std::byte * >( actual_address ), required_size_for_end_expansion }, remap_method }; } else { - BOOST_ASSERT_MSG( ( errno == ENOMEM ) && ( realloc_type == reallocation_type::Fixed ), "Unexpected mremap failure" ); + BOOST_ASSERT_MSG( ( errno == ENOMEM ) && ( realloc_type == reallocation_type::fixed ), "Unexpected mremap failure" ); } # endif // linux mremap auto const additional_end_size{ required_size_for_end_expansion - current_size }; @@ -91,7 +97,7 @@ expand_result expand BOOST_ASSERT_MSG( false, "mremap failed but an appending mmap succeeded!?" ); // behaviour investigation # endif BOOST_ASSUME( current_size + additional_end_size == required_size_for_end_expansion ); - return { address, required_size_for_end_expansion, expand_result::back_extended }; + return { { address, required_size_for_end_expansion }, expand_result::method::back_extended }; } } @@ -104,21 +110,21 @@ expand_result expand if ( allocate_fixed( pre_address, additional_front_size, allocation_type::commit ) ) // avoid having a non-committed range before a committed range { BOOST_VERIFY( current_size + additional_front_size == required_size_for_front_expansion ); - return { static_cast< std::byte * >( pre_address ), required_size_for_front_expansion, expand_result::FrontExtended }; + return { { static_cast< std::byte * >( pre_address ), required_size_for_front_expansion }, expand_result::method::front_extended }; } } if ( realloc_type == reallocation_type::moveable ) { // Everything else failed: fallback to the classic allocate new->copy->free old dance. - auto const requested_size{ required_size_for_end_expansion }; //...mrmlj...TODO respect front-expand-only requests + auto requested_size{ required_size_for_end_expansion }; //...mrmlj...TODO respect front-expand-only requests auto const new_location { allocate( requested_size ) }; if ( new_location ) { BOOST_ASSUME( requested_size == required_size_for_end_expansion ); std::memcpy( new_location, address, used_capacity ); free( address, current_size ); - return { static_cast< std::byte * >( new_location ), required_size_for_end_expansion, expand_result::moved }; + return { { static_cast< std::byte * >( new_location ), required_size_for_end_expansion }, expand_result::method::moved }; } } @@ -152,7 +158,5 @@ expand_result expand_front } //------------------------------------------------------------------------------ -} // namespace vm -//------------------------------------------------------------------------------ -} // namespace psi +} // namespace psi::vm //------------------------------------------------------------------------------ diff --git a/include/psi/vm/flags/posix/flags.inl b/src/flags/flags.posix.cpp similarity index 69% rename from include/psi/vm/flags/posix/flags.inl rename to src/flags/flags.posix.cpp index 8490666..b7bcc97 100644 --- a/include/psi/vm/flags/posix/flags.inl +++ b/src/flags/flags.posix.cpp @@ -1,8 +1,5 @@ //////////////////////////////////////////////////////////////////////////////// /// -/// \file flags/posix/flags.inl -/// --------------------------- -/// /// Copyright (c) Domagoj Saric 2010 - 2024. /// /// Use, modification and distribution is subject to the @@ -14,18 +11,11 @@ /// //////////////////////////////////////////////////////////////////////////////// //------------------------------------------------------------------------------ -#ifndef flags_inl__8658DC6F_321D_4ED4_A599_DB5CD7540BC1 -#define flags_inl__8658DC6F_321D_4ED4_A599_DB5CD7540BC1 -#pragma once -//------------------------------------------------------------------------------ -#include "flags.hpp" +#include -#include "psi/vm/detail/impl_inline.hpp" +#include //------------------------------------------------------------------------------ -namespace psi -{ -//------------------------------------------------------------------------------ -namespace vm +namespace psi::vm { //------------------------------------------------------------------------------ inline namespace posix @@ -35,25 +25,24 @@ namespace flags { //------------------------------------------------------------------------------ -PSI_VM_IMPL_INLINE -flags_t BOOST_CC_REG access_privileges::oflag() const noexcept +flags_t access_privileges::oflag() const noexcept { //...zzz...use fadvise... // http://stackoverflow.com/questions/2299402/how-does-one-do-raw-io-on-mac-os-x-ie-equivalent-to-linuxs-o-direct-flag - flags_t result( ( object_access.privileges >> procsh ) & 0xFF ); + flags_t result{ ( object_access.privileges >> procsh ) & 0xFF }; #if O_RDWR != ( O_RDONLY | O_WRONLY ) - auto constexpr o_rdwr( O_RDONLY_ | O_WRONLY ); + auto constexpr o_rdwr{ O_RDONLY_ | O_WRONLY }; if ( ( result & o_rdwr ) == o_rdwr ) result &= ~o_rdwr | O_RDWR; else { /// \note Remove the "Undetectable combined O_RDONLY" workaround flag. /// (09.11.2015.) (Domagoj Saric) - #if !O_RDONLY +# if !O_RDONLY result &= ~O_RDONLY_; - #endif // !O_RDONLY +# endif // !O_RDONLY } #endif // no O_RDWR GNUC extension @@ -62,13 +51,22 @@ flags_t BOOST_CC_REG access_privileges::oflag() const noexcept return result; } + +flags_t access_privileges::system::read_umask() noexcept +{ + // Broken/not thread safe + // http://man7.org/linux/man-pages/man2/umask.2.html @ notes + // https://groups.google.com/forum/#!topic/comp.unix.programmer/v6nv-oP9IJQ + // https://stackoverflow.com/questions/53227072/reading-umask-thread-safe/53288382 + auto const mask{ ::umask( 0 ) }; + BOOST_VERIFY( ::umask( mask ) == 0 ); + return static_cast( mask ); +} + //------------------------------------------------------------------------------ } // flags //------------------------------------------------------------------------------ } // posix //------------------------------------------------------------------------------ -} // vm -//------------------------------------------------------------------------------ -} // psi +} // psi::vm //------------------------------------------------------------------------------ -#endif // flags_inl diff --git a/include/psi/vm/flags/win32/flags.inl b/src/flags/flags.win32.cpp similarity index 77% rename from include/psi/vm/flags/win32/flags.inl rename to src/flags/flags.win32.cpp index 3b17039..cfcfd9b 100644 --- a/include/psi/vm/flags/win32/flags.inl +++ b/src/flags/flags.win32.cpp @@ -1,8 +1,5 @@ //////////////////////////////////////////////////////////////////////////////// /// -/// \file flags/win32/flags.inl -/// --------------------------- -/// /// Copyright (c) Domagoj Saric 2010 - 2024. /// /// Use, modification and distribution is subject to the @@ -14,14 +11,8 @@ /// //////////////////////////////////////////////////////////////////////////////// //------------------------------------------------------------------------------ -#ifndef flags_inl__20EA903E_E0F7_43AA_A0B2_524DDBD3099A -#define flags_inl__20EA903E_E0F7_43AA_A0B2_524DDBD3099A -#pragma once -//------------------------------------------------------------------------------ -#include "psi/vm/detail/win32.hpp" -#include "psi/vm/flags/win32/flags.hpp" - -#include "psi/vm/detail/impl_inline.hpp" +#include +#include #include #include @@ -29,24 +20,16 @@ #include #include //------------------------------------------------------------------------------ -namespace psi +namespace psi::vm { //------------------------------------------------------------------------------ -namespace vm -{ -//------------------------------------------------------------------------------ -namespace win32 +inline namespace win32 { //------------------------------------------------------------------------------ namespace flags { //------------------------------------------------------------------------------ -#ifdef __clang__ -#pragma clang diagnostic push -#pragma clang diagnostic ignored "-Wignored-qualifiers" -#endif - static_assert( (flags_t)named_object_construction_policy::create_new == CREATE_NEW , "" ); static_assert( (flags_t)named_object_construction_policy::create_new_or_truncate_existing == CREATE_ALWAYS , "" ); static_assert( (flags_t)named_object_construction_policy::open_existing == OPEN_EXISTING , "" ); @@ -60,14 +43,7 @@ static_assert( access_privileges::all == ( GENERIC_ALL | FILE_MAP_ALL_AC namespace detail { -#ifdef PSI_VM_HEADER_ONLY - inline -#endif // PSI_VM_HEADER_ONLY -#ifdef __clang__ -#pragma clang diagnostic push -#pragma clang diagnostic ignored "-Wignored-attributes" -#endif - BOOST_ATTRIBUTES( BOOST_DOES_NOT_RETURN, BOOST_RESTRICTED_FUNCTION_L3, BOOST_COLD ) + BOOST_ATTRIBUTES( BOOST_DOES_NOT_RETURN, BOOST_COLD ) void throw_bad_alloc() { #ifdef _MSC_VER @@ -76,9 +52,6 @@ namespace detail #endif // _MSC_VER { BOOST_THROW_EXCEPTION( std::bad_alloc() ); } } -#ifdef __clang__ -#pragma clang diagnostic pop -#endif // https://msdn.microsoft.com/en-us/library/windows/desktop/aa446595(v=vs.85).aspx Creating a Security Descriptor for a New Object in C++ // https://msdn.microsoft.com/en-us/library/windows/desktop/aa379602(v=vs.85).aspx SID strings @@ -89,8 +62,8 @@ namespace detail // https://www.osronline.com/article.cfm?article=56 Keeping Secrets - Windows NT Security (Part I) // http://blogs.technet.com/b/askds/archive/2009/06/01/null-and-empty-dacls.aspx // SECURITY_MAX_SID_SIZE - inline BOOST_ATTRIBUTES( BOOST_EXCEPTIONLESS, BOOST_RESTRICTED_FUNCTION_L1, BOOST_RESTRICTED_FUNCTION_RETURN ) - dynamic_sd const * __restrict make_sd( EXPLICIT_ACCESSW const * __restrict const p_ea_entries, std::uint8_t const number_of_entries ) //KEY_READ + BOOST_ATTRIBUTES( BOOST_EXCEPTIONLESS, BOOST_RESTRICTED_FUNCTION_L1, BOOST_RESTRICTED_FUNCTION_RETURN ) + dynamic_sd const * make_sd( EXPLICIT_ACCESSW const * __restrict const p_ea_entries, std::uint8_t const number_of_entries ) noexcept //KEY_READ { ACL * p_acl; auto const result_code( ::SetEntriesInAclW( number_of_entries, const_cast( p_ea_entries ), nullptr, &p_acl ) ); @@ -100,11 +73,11 @@ namespace detail throw_bad_alloc(); } - SECURITY_DESCRIPTOR sd; static_assert( sizeof( sd ) >= SECURITY_DESCRIPTOR_MIN_LENGTH, "" ); + SECURITY_DESCRIPTOR sd; static_assert( sizeof( sd ) >= SECURITY_DESCRIPTOR_MIN_LENGTH ); BOOST_VERIFY( ::InitializeSecurityDescriptor( &sd, SECURITY_DESCRIPTOR_REVISION ) ); BOOST_VERIFY( ::SetSecurityDescriptorDacl ( &sd, true, p_acl, false ) ); - #if 0 // disabled - get/use all user's groups (as opposed to only the 'primary') approach +# if 0 // disabled - get/use all user's groups (as opposed to only the 'primary') approach HANDLE token; if ( !::OpenThreadToken( ::GetCurrentThread(), TOKEN_QUERY, false, &token ) ) {} @@ -116,7 +89,7 @@ namespace detail static_cast( ::_alloca( required_size ) ), required_size / sizeof( TOKEN_GROUPS ) ) }; BOOST_VERIFY( ::GetTokenInformation( token, TokenGroups, groups.begin(), required_size, &required_size ) ); - #endif // disabled +# endif // disabled //RtlAbsoluteToSelfRelativeSD DWORD length( 0 ); BOOST_VERIFY( ::MakeSelfRelativeSD( &sd, nullptr, &length ) == false && ::GetLastError() == ERROR_INSUFFICIENT_BUFFER ); @@ -131,8 +104,7 @@ namespace detail return p_dsd; } - inline - dynamic_sd const * __restrict make_sd( std::uint32_t const permissions, wchar_t const * __restrict const trustee ) + dynamic_sd const * make_sd( std::uint32_t const permissions, wchar_t const * __restrict const trustee ) noexcept { EXPLICIT_ACCESSW ea; ea.grfAccessPermissions = permissions; @@ -144,11 +116,8 @@ namespace detail return make_sd( &ea, 1 ); } -#ifdef PSI_VM_HEADER_ONLY - inline -#endif // PSI_VM_HEADER_ONLY BOOST_ATTRIBUTES( BOOST_EXCEPTIONLESS, BOOST_RESTRICTED_FUNCTION_L3, BOOST_RESTRICTED_FUNCTION_RETURN ) - dynamic_sd const * __restrict BOOST_CC_REG make_sd( scope_privileges const permissions ) + dynamic_sd const * BOOST_CC_REG make_sd( scope_privileges const permissions ) { EXPLICIT_ACCESSW entries[ permissions.size() ]; std::uint8_t count( 0 ); @@ -184,7 +153,6 @@ namespace detail namespace { - inline #ifdef NDEBUG constexpr #endif // NDEBUG @@ -212,29 +180,22 @@ namespace SECURITY_DESCRIPTOR const all_shall_pass( make_all_shall_pass() ); } // anonymous namespace -inline access_privileges::system const access_privileges::system::process_default = { nullptr , false }; -inline access_privileges::system const access_privileges::system::unrestricted = { &all_shall_pass, false }; -inline access_privileges::system const access_privileges::system::nix_default = { detail::make_sd - ( - access_privileges::system::user ( access_privileges::all ) | - access_privileges::system::group( access_privileges::read ) | - access_privileges::system::world( access_privileges::read ) - ), - true - }; -inline access_privileges::system const access_privileges::system::_644 = { ( nix_default.get_dynamic_sd().add_ref(), nix_default.p_sd ), true }; //access_privileges::system::nix_default; - -#ifdef __clang__ -#pragma clang diagnostic pop -#endif +access_privileges::system const access_privileges::system::process_default = { nullptr , false }; +access_privileges::system const access_privileges::system::unrestricted = { &all_shall_pass, false }; +access_privileges::system const access_privileges::system::nix_default = { detail::make_sd + ( + access_privileges::system::user ( access_privileges::all ) | + access_privileges::system::group( access_privileges::read ) | + access_privileges::system::world( access_privileges::read ) + ), + true + }; +access_privileges::system const access_privileges::system::_644 = { ( nix_default.get_dynamic_sd().add_ref(), nix_default.p_sd ), true }; //access_privileges::system::nix_default; //------------------------------------------------------------------------------ } // flags //------------------------------------------------------------------------------ } // win32 //------------------------------------------------------------------------------ -} // vm -//------------------------------------------------------------------------------ -} // psi +} // psi::vm //------------------------------------------------------------------------------ -#endif // flags_inl diff --git a/include/psi/vm/flags/posix/mapping.inl b/src/flags/mapping.posix.cpp similarity index 82% rename from include/psi/vm/flags/posix/mapping.inl rename to src/flags/mapping.posix.cpp index 91fcf62..5983569 100644 --- a/include/psi/vm/flags/posix/mapping.inl +++ b/src/flags/mapping.posix.cpp @@ -1,8 +1,5 @@ //////////////////////////////////////////////////////////////////////////////// /// -/// \file flags/posix/mapping.inl -/// ----------------------------- -/// /// Copyright (c) Domagoj Saric 2010 - 2024. /// /// Use, modification and distribution is subject to the @@ -14,13 +11,7 @@ /// //////////////////////////////////////////////////////////////////////////////// //------------------------------------------------------------------------------ -#ifndef mapping_inl__79CF82B8_F71B_4C75_BE77_98F4FB8A7FFA -#define mapping_inl__79CF82B8_F71B_4C75_BE77_98F4FB8A7FFA -#pragma once -//------------------------------------------------------------------------------ -#include "mapping.hpp" - -#include "psi/vm/detail/impl_inline.hpp" +#include //------------------------------------------------------------------------------ namespace psi { @@ -35,7 +26,6 @@ namespace flags { //------------------------------------------------------------------------------ -PSI_VM_IMPL_INLINE viewing viewing::create ( access_privileges::object const access_flags, @@ -61,4 +51,3 @@ viewing viewing::create //------------------------------------------------------------------------------ } // psi //------------------------------------------------------------------------------ -#endif // mapping.inl diff --git a/include/psi/vm/flags/win32/mapping.inl b/src/flags/mapping.win32.cpp similarity index 89% rename from include/psi/vm/flags/win32/mapping.inl rename to src/flags/mapping.win32.cpp index 8d95282..eebcc50 100644 --- a/include/psi/vm/flags/win32/mapping.inl +++ b/src/flags/mapping.win32.cpp @@ -1,8 +1,5 @@ //////////////////////////////////////////////////////////////////////////////// /// -/// \file flags/win32/mapping.inl -/// ----------------------------- -/// /// Copyright (c) Domagoj Saric 2010 - 2024. /// /// Use, modification and distribution is subject to the @@ -14,14 +11,9 @@ /// //////////////////////////////////////////////////////////////////////////////// //------------------------------------------------------------------------------ -#ifndef mapping_inl__CD518463_D4CB_4E18_8E35_E0FBBA8CA1D1 -#define mapping_inl__CD518463_D4CB_4E18_8E35_E0FBBA8CA1D1 -#pragma once -//------------------------------------------------------------------------------ -#include "psi/vm/flags/win32/mapping.hpp" +#include -#include "psi/vm/detail/impl_inline.hpp" -#include "psi/vm/detail/win32.hpp" +#include //------------------------------------------------------------------------------ namespace psi { @@ -29,7 +21,7 @@ namespace psi namespace vm { //------------------------------------------------------------------------------ -namespace win32 +inline namespace win32 { //------------------------------------------------------------------------------ namespace flags @@ -43,7 +35,6 @@ static_assert( ( viewing::access_rights::execute & 0xFF ) == FILE_MAP_EX static_assert( (unsigned)viewing::share_mode ::shared == 0 , "Psi.VM internal inconsistency" ); static_assert( (unsigned)viewing::share_mode ::hidden == FILE_MAP_COPY , "Psi.VM internal inconsistency" ); -PSI_VM_IMPL_INLINE viewing viewing::create ( access_privileges::object const object_access, @@ -65,8 +56,7 @@ viewing viewing::create } -PSI_VM_IMPL_INLINE -bool viewing::is_cow() const +bool viewing::is_cow() const noexcept { /// \note Mind the Win32+NativeNT flags mess: FILE_MAP_ALL_ACCESS maps to /// (NativeNT) SECTION_ALL_ACCESS which includes SECTION_QUERY which in @@ -74,14 +64,13 @@ bool viewing::is_cow() const /// MapViewOfFile() documentation, is supposed to be a 'distinct' flag /// WRT the FILE_MAP_ALL_ACCESS flag). /// (05.09.2015.) (Domagoj Saric) - static_assert( FILE_MAP_ALL_ACCESS & FILE_MAP_COPY, "" ); + static_assert( FILE_MAP_ALL_ACCESS & FILE_MAP_COPY ); return ( map_view_flags & FILE_MAP_ALL_ACCESS ) == FILE_MAP_COPY; } namespace detail { - PSI_VM_IMPL_INLINE flags_t BOOST_CC_REG object_access_to_page_access( access_privileges::object const object_access, viewing::share_mode const share_mode ) { // Generate CreateFileMapping flags from access_privileges::object/MapViewOfFile flags @@ -113,7 +102,6 @@ namespace detail } // namespace detail -PSI_VM_IMPL_INLINE mapping mapping::create ( access_privileges const ap, @@ -141,4 +129,3 @@ mapping mapping::create //------------------------------------------------------------------------------ } // psi //------------------------------------------------------------------------------ -#endif // mapping.inl diff --git a/include/psi/vm/flags/posix/opening.inl b/src/flags/opening.posix.cpp similarity index 75% rename from include/psi/vm/flags/posix/opening.inl rename to src/flags/opening.posix.cpp index 50f0fa9..645bbca 100644 --- a/include/psi/vm/flags/posix/opening.inl +++ b/src/flags/opening.posix.cpp @@ -1,8 +1,5 @@ //////////////////////////////////////////////////////////////////////////////// /// -/// \file flags/posix/opening.inl -/// ----------------------------- -/// /// Copyright (c) Domagoj Saric 2010 - 2024. /// /// Use, modification and distribution is subject to the @@ -14,15 +11,8 @@ /// //////////////////////////////////////////////////////////////////////////////// //------------------------------------------------------------------------------ -#ifndef opening_inl__0F422517_D9AA_4E3F_B3E4_B139021D068E -#define opening_inl__0F422517_D9AA_4E3F_B3E4_B139021D068E -#pragma once -//------------------------------------------------------------------------------ -#include "opening.hpp" - -#include "psi/vm/detail/impl_inline.hpp" - -#include +#include +#include //------------------------------------------------------------------------------ namespace psi { @@ -37,22 +27,21 @@ namespace flags { //------------------------------------------------------------------------------ -PSI_VM_IMPL_INLINE -opening BOOST_CC_REG opening::create +opening opening::create ( access_privileges const ap, named_object_construction_policy const construction_policy, flags_t const combined_system_hints ) noexcept { - auto const oflag( ap.oflag() | static_cast( construction_policy ) | combined_system_hints ); - auto const pmode( ap.pmode() ); + auto const oflag{ ap.oflag() | static_cast( construction_policy ) | combined_system_hints }; + auto const pmode{ ap.pmode() }; return { .oflag = oflag, .pmode = static_cast( pmode ) }; } -PSI_VM_IMPL_INLINE -opening BOOST_CC_REG opening::create_for_opening_existing_objects + +opening opening::create_for_opening_existing_objects ( access_privileges::object const object_access, access_privileges::child_process const child_access, @@ -79,4 +68,3 @@ opening BOOST_CC_REG opening::create_for_opening_existing_objects //------------------------------------------------------------------------------ } // psi //------------------------------------------------------------------------------ -#endif // opening_inl diff --git a/include/psi/vm/flags/win32/opening.inl b/src/flags/opening.win32.cpp similarity index 74% rename from include/psi/vm/flags/win32/opening.inl rename to src/flags/opening.win32.cpp index da1060c..3dea082 100644 --- a/include/psi/vm/flags/win32/opening.inl +++ b/src/flags/opening.win32.cpp @@ -1,8 +1,5 @@ //////////////////////////////////////////////////////////////////////////////// /// -/// \file flags/win32/opening.inl -/// ----------------------------- -/// /// Copyright (c) Domagoj Saric 2010 - 2024. /// /// Use, modification and distribution is subject to the @@ -14,27 +11,12 @@ /// //////////////////////////////////////////////////////////////////////////////// //------------------------------------------------------------------------------ -#ifndef opening_inl__77AE8A6F_0E93_433B_A1F2_531BBBB353FC -#define opening_inl__77AE8A6F_0E93_433B_A1F2_531BBBB353FC -#pragma once -//------------------------------------------------------------------------------ -#include "opening.hpp" - -#include "psi/vm/detail/impl_inline.hpp" - -#ifdef __clang__ -#pragma clang diagnostic push -#pragma clang diagnostic ignored "-Winline-namespace-reopened-noninline" -#endif - -//------------------------------------------------------------------------------ -namespace psi -{ +#include //------------------------------------------------------------------------------ -namespace vm +namespace psi::vm { //------------------------------------------------------------------------------ -namespace win32 +inline namespace win32 { //------------------------------------------------------------------------------ namespace flags @@ -46,7 +28,6 @@ static_assert( system_hints::sequential_access == FILE_FLAG_SEQUENTIAL_SCAN static_assert( system_hints::avoid_caching == ( FILE_FLAG_NO_BUFFERING | FILE_FLAG_WRITE_THROUGH ), "" ); static_assert( system_hints::temporary == ( FILE_ATTRIBUTE_TEMPORARY | FILE_FLAG_DELETE_ON_CLOSE ), "" ); -PSI_VM_IMPL_INLINE opening BOOST_CC_REG opening::create_for_opening_existing_objects ( access_privileges::object const object_access, @@ -70,13 +51,5 @@ opening BOOST_CC_REG opening::create_for_opening_existing_objects //------------------------------------------------------------------------------ } // win32 //------------------------------------------------------------------------------ -} // vm -//------------------------------------------------------------------------------ -} // psi +} // psi::vm //------------------------------------------------------------------------------ - -#ifdef __clang__ -#pragma clang diagnostic pop -#endif - -#endif // opening_inl diff --git a/include/psi/vm/mappable_objects/file/posix/file.inl b/src/mappable_objects/file/file.posix.cpp similarity index 62% rename from include/psi/vm/mappable_objects/file/posix/file.inl rename to src/mappable_objects/file/file.posix.cpp index 0114ff1..d75e739 100644 --- a/include/psi/vm/mappable_objects/file/posix/file.inl +++ b/src/mappable_objects/file/file.posix.cpp @@ -14,16 +14,11 @@ /// //////////////////////////////////////////////////////////////////////////////// //------------------------------------------------------------------------------ -#ifndef file_inl__1E2F9841_1C6C_40D9_9AA7_BAC0003CD909 -#define file_inl__1E2F9841_1C6C_40D9_9AA7_BAC0003CD909 -#pragma once -//------------------------------------------------------------------------------ -#include "file.hpp" +#include -#include "psi/vm/detail/impl_inline.hpp" -#include "psi/vm/detail/posix.hpp" -#include "psi/vm/flags/posix/opening.hpp" -#include "psi/vm/mapping/posix/mapping.hpp" +#include +#include +#include #include @@ -33,18 +28,14 @@ #include //------------------------------------------------------------------------------ -namespace psi -{ -//------------------------------------------------------------------------------ -namespace vm +namespace psi::vm { //------------------------------------------------------------------------------ PSI_VM_POSIX_INLINE namespace posix { //------------------------------------------------------------------------------ -PSI_VM_IMPL_INLINE -file_handle BOOST_CC_REG create_file( char const * const file_name, flags::opening const flags ) noexcept +file_handle create_file( char const * const file_name, flags::opening const flags ) noexcept { BOOST_ASSERT( file_name ); // POSIX specific - flags.pmode gets umasked - however this cannot be @@ -56,8 +47,7 @@ file_handle BOOST_CC_REG create_file( char const * const file_name, flags::openi } #ifdef BOOST_MSVC -PSI_VM_IMPL_INLINE -file_handle BOOST_CC_REG create_file( wchar_t const * const file_name, flags::opening const flags ) +file_handle create_file( wchar_t const * const file_name, flags::opening const flags ) { BOOST_ASSERT( file_name ); int const file_handle( ::_wopen( file_name, flags.oflag, flags.pmode ) ); @@ -66,23 +56,22 @@ file_handle BOOST_CC_REG create_file( wchar_t const * const file_name, flags::op #endif // BOOST_MSVC -PSI_VM_IMPL_INLINE bool BOOST_CC_REG delete_file( char const * const file_name ) noexcept { return :: unlink( file_name ) == 0; } +bool delete_file( char const * const file_name ) noexcept { return :: unlink( file_name ) == 0; } #ifdef BOOST_MSVC -PSI_VM_IMPL_INLINE bool BOOST_CC_REG delete_file( wchar_t const * const file_name ) noexcept { return ::_wunlink( file_name ) == 0; } +bool delete_file( wchar_t const * const file_name ) noexcept { return ::_wunlink( file_name ) == 0; } #endif // BOOST_MSVC -#ifdef BOOST_HAS_UNISTD_H -PSI_VM_IMPL_INLINE err::fallible_result BOOST_CC_REG set_size( file_handle::reference const file_handle, std::uint64_t const desired_size ) noexcept +#if __has_include( ) +err::fallible_result set_size( file_handle::reference const file_handle, std::uint64_t const desired_size ) noexcept { if ( ::ftruncate( file_handle, static_cast( desired_size ) ) == 0 ) [[ likely ]] return err::success; return error{}; } -#endif // BOOST_HAS_UNISTD_H +#endif // POSIX impl level -PSI_VM_IMPL_INLINE -std::uint64_t BOOST_CC_REG get_size( file_handle::const_reference const file_handle ) noexcept +std::uint64_t get_size( file_handle::const_reference const file_handle ) noexcept { struct stat file_info{ .st_size = 0 }; // ensure zero is returned for invalid handles (in unchecked/release builds) BOOST_VERIFY( ( ::fstat( file_handle.value, &file_info ) == 0 ) || ( file_handle == handle_traits::invalid_value ) ); @@ -92,8 +81,5 @@ std::uint64_t BOOST_CC_REG get_size( file_handle::const_reference const file_han //------------------------------------------------------------------------------ } // posix //------------------------------------------------------------------------------ -} // vm -//------------------------------------------------------------------------------ -} // psi +} // psi::vm //------------------------------------------------------------------------------ -#endif // file_inl diff --git a/include/psi/vm/mappable_objects/file/win32/file.inl b/src/mappable_objects/file/file.win32.cpp similarity index 87% rename from include/psi/vm/mappable_objects/file/win32/file.inl rename to src/mappable_objects/file/file.win32.cpp index 4e93d0a..07e5709 100644 --- a/include/psi/vm/mappable_objects/file/win32/file.inl +++ b/src/mappable_objects/file/file.win32.cpp @@ -1,8 +1,5 @@ //////////////////////////////////////////////////////////////////////////////// /// -/// \file file.inl -/// -------------- -/// /// Copyright (c) Domagoj Saric 2010 - 2024. /// /// Use, modification and distribution is subject to the @@ -14,27 +11,20 @@ /// //////////////////////////////////////////////////////////////////////////////// //------------------------------------------------------------------------------ -#ifndef file_inl__FB482005_18D9_4E3B_9193_A13DBFE88F45 -#define file_inl__FB482005_18D9_4E3B_9193_A13DBFE88F45 -#pragma once -//------------------------------------------------------------------------------ -#include "file.hpp" +#include -#include "psi/vm/flags/win32/opening.hpp" -#include "psi/vm/detail/impl_inline.hpp" -#include "psi/vm/detail/nt.hpp" -#include "psi/vm/detail/win32.hpp" +#include +#include +#include #include + #include //------------------------------------------------------------------------------ -namespace psi +namespace psi::vm { //------------------------------------------------------------------------------ -namespace vm -{ -//------------------------------------------------------------------------------ -namespace win32 +inline namespace win32 { //------------------------------------------------------------------------------ @@ -67,18 +57,16 @@ namespace detail }; // struct create_file } // namespace detail -PSI_VM_IMPL_INLINE BOOST_ATTRIBUTES( BOOST_EXCEPTIONLESS, BOOST_RESTRICTED_FUNCTION_L1 ) +BOOST_ATTRIBUTES( BOOST_EXCEPTIONLESS, BOOST_RESTRICTED_FUNCTION_L1 ) file_handle BOOST_CC_REG create_file( char const * const file_name, flags::opening const flags ) noexcept { return file_handle{ detail::create_file::do_create( file_name, flags ) }; } -PSI_VM_IMPL_INLINE file_handle BOOST_CC_REG create_file( wchar_t const * const file_name, flags::opening const flags ) noexcept { return file_handle{ detail::create_file::do_create( file_name, flags ) }; } -PSI_VM_IMPL_INLINE bool BOOST_CC_REG delete_file( char const * const file_name ) noexcept { return ::DeleteFileA( file_name ) != false; } -PSI_VM_IMPL_INLINE bool BOOST_CC_REG delete_file( wchar_t const * const file_name ) noexcept { return ::DeleteFileW( file_name ) != false; } +bool delete_file( char const * const file_name ) noexcept { return ::DeleteFileA( file_name ) != false; } +bool delete_file( wchar_t const * const file_name ) noexcept { return ::DeleteFileW( file_name ) != false; } -PSI_VM_IMPL_INLINE -err::fallible_result BOOST_CC_REG set_size( file_handle::reference const file_handle, std::uint64_t const desired_size ) noexcept +err::fallible_result set_size( file_handle::reference const file_handle, std::uint64_t const desired_size ) noexcept { // It is 'OK' to send null/invalid handles to Windows functions (they will // simply fail), this simplifies error handling (it is enough to go through @@ -103,8 +91,7 @@ err::fallible_result BOOST_CC_REG set_size( file_handle::reference } -PSI_VM_IMPL_INLINE -std::uint64_t BOOST_CC_REG get_size( file_handle::reference const file_handle ) noexcept +std::uint64_t get_size( file_handle::reference const file_handle ) noexcept { LARGE_INTEGER file_size; BOOST_VERIFY( ::GetFileSizeEx( file_handle, &file_size ) || ( file_handle == handle_traits::invalid_value ) ); @@ -114,7 +101,7 @@ std::uint64_t BOOST_CC_REG get_size( file_handle::reference const file_handle ) namespace detail { - inline std::uint64_t get_section_size( HANDLE const mapping_handle ) noexcept { return get_size( mapping::const_handle{ mapping_handle } ); } + std::uint64_t get_section_size( HANDLE const mapping_handle ) noexcept { return get_size( mapping::const_handle{ mapping_handle } ); } // https://support.microsoft.com/en-us/kb/125713 Common File Mapping Problems and Platform Differences struct create_mapping_impl @@ -197,14 +184,12 @@ namespace detail }; // struct create_mapping_impl } // namespace detail -PSI_VM_IMPL_INLINE mapping BOOST_CC_REG create_mapping( file_handle::reference const file, flags::mapping const flags, std::uint64_t const maximum_size, char const * const name ) noexcept { return detail::create_mapping_impl::do_map( file, flags, maximum_size, name ); } #if 0 -PSI_VM_IMPL_INLINE mapping BOOST_CC_REG create_mapping( handle::reference const file, flags::mapping const flags ) noexcept { auto const mapping_handle @@ -215,7 +200,6 @@ mapping BOOST_CC_REG create_mapping( handle::reference const file, flags::mappin } #endif -PSI_VM_IMPL_INLINE mapping BOOST_CC_REG create_mapping ( file_handle ::reference const file, @@ -236,8 +220,5 @@ mapping BOOST_CC_REG create_mapping //------------------------------------------------------------------------------ } // win32 //------------------------------------------------------------------------------ -} // vm -//------------------------------------------------------------------------------ -} // psi +} // psi::vm //------------------------------------------------------------------------------ -#endif // file_inl diff --git a/include/psi/vm/mappable_objects/file/utility.inl b/src/mappable_objects/file/utility.cpp similarity index 86% rename from include/psi/vm/mappable_objects/file/utility.inl rename to src/mappable_objects/file/utility.cpp index 8ed44be..074e4d6 100644 --- a/include/psi/vm/mappable_objects/file/utility.inl +++ b/src/mappable_objects/file/utility.cpp @@ -14,22 +14,16 @@ /// //////////////////////////////////////////////////////////////////////////////// //------------------------------------------------------------------------------ -#include "utility.hpp" +#include -#include "file.hpp" - -#include "psi/vm/flags/flags.hpp" -#include "psi/vm/detail/impl_inline.hpp" -#include "psi/vm/detail/impl_selection.hpp" -//------------------------------------------------------------------------------ -namespace psi -{ +#include +#include //------------------------------------------------------------------------------ -namespace vm +namespace psi::vm { //------------------------------------------------------------------------------ -PSI_VM_IMPL_INLINE [[ gnu::const ]] +[[ gnu::const ]] flags::opening create_rw_file_flags() noexcept { using namespace flags; @@ -46,7 +40,7 @@ flags::opening create_rw_file_flags() noexcept ); } -PSI_VM_IMPL_INLINE [[ gnu::const ]] +[[ gnu::const ]] flags::opening create_r_file_flags() noexcept { using namespace flags; @@ -65,8 +59,7 @@ namespace detail0 using opening = flags::opening; using default_file_handle = file_handle ; - PSI_VM_IMPL_INLINE - fallible_result BOOST_CC_REG + fallible_result map_file( default_file_handle && file_handle, std::size_t /*const*/ desired_size ) noexcept { if ( BOOST_UNLIKELY( file_handle.get() == default_file_handle::traits::invalid_value ) ) @@ -112,7 +105,6 @@ namespace detail0 } - PSI_VM_IMPL_INLINE fallible_result map_read_only_file( default_file_handle && file_handle ) noexcept { @@ -143,26 +135,26 @@ namespace detail0 } } // namespace detail0 -PSI_VM_IMPL_INLINE BOOST_ATTRIBUTES( BOOST_MINSIZE, BOOST_EXCEPTIONLESS ) +BOOST_ATTRIBUTES( BOOST_MINSIZE, BOOST_EXCEPTIONLESS ) fallible_result map_file( char const * const file_name, std::size_t const desired_size ) noexcept { return detail0::map_file( create_file( file_name, create_rw_file_flags() ), desired_size ); } -PSI_VM_IMPL_INLINE BOOST_ATTRIBUTES( BOOST_MINSIZE, BOOST_EXCEPTIONLESS ) +BOOST_ATTRIBUTES( BOOST_MINSIZE, BOOST_EXCEPTIONLESS ) fallible_result map_read_only_file( char const * const file_name ) noexcept { return detail0::map_read_only_file( create_file( file_name, create_r_file_flags() ) ); } #ifdef _WIN32 - PSI_VM_IMPL_INLINE BOOST_ATTRIBUTES( BOOST_MINSIZE, BOOST_EXCEPTIONLESS ) + BOOST_ATTRIBUTES( BOOST_MINSIZE, BOOST_EXCEPTIONLESS ) fallible_result map_file( wchar_t const * const file_name, std::size_t const desired_size ) { return detail0::map_file( create_file( file_name, create_rw_file_flags() ), desired_size ); } - PSI_VM_IMPL_INLINE BOOST_ATTRIBUTES( BOOST_MINSIZE, BOOST_EXCEPTIONLESS ) + BOOST_ATTRIBUTES( BOOST_MINSIZE, BOOST_EXCEPTIONLESS ) fallible_result map_read_only_file( wchar_t const * const file_name ) { return detail0::map_read_only_file( create_file( file_name, create_r_file_flags() ) ); @@ -170,11 +162,5 @@ fallible_result map_read_only_file( char const * const fi #endif // _WIN32 //------------------------------------------------------------------------------ -} // vm +} // psi::vm //------------------------------------------------------------------------------ -} // psi -//------------------------------------------------------------------------------ - -#ifndef PSI_VM_HEADER_ONLY - #include "file.inl" -#endif // PSI_VM_HEADER_ONLY diff --git a/include/psi/vm/mappable_objects/shared_memory/posix/flags.inl b/src/mappable_objects/shared_memory/flags.posix.cpp similarity index 83% rename from include/psi/vm/mappable_objects/shared_memory/posix/flags.inl rename to src/mappable_objects/shared_memory/flags.posix.cpp index ebafa0a..8bd83e0 100644 --- a/include/psi/vm/mappable_objects/shared_memory/posix/flags.inl +++ b/src/mappable_objects/shared_memory/flags.posix.cpp @@ -14,12 +14,9 @@ /// //////////////////////////////////////////////////////////////////////////////// //------------------------------------------------------------------------------ -#include "flags.hpp" +#include //------------------------------------------------------------------------------ -namespace psi -{ -//------------------------------------------------------------------------------ -namespace vm +namespace psi::vm { //------------------------------------------------------------------------------ inline namespace posix @@ -29,8 +26,7 @@ namespace flags { //------------------------------------------------------------------------------ -PSI_VM_IMPL_INLINE -shared_memory BOOST_CC_REG shared_memory::create +shared_memory shared_memory::create ( access_privileges const ap, named_object_construction_policy const nocp, @@ -45,7 +41,5 @@ shared_memory BOOST_CC_REG shared_memory::create //------------------------------------------------------------------------------ } // posix //------------------------------------------------------------------------------ -} // vm -//------------------------------------------------------------------------------ -} // psi +} // psi::vm //------------------------------------------------------------------------------ diff --git a/include/psi/vm/mappable_objects/shared_memory/win32/flags.inl b/src/mappable_objects/shared_memory/flags.win32.cpp similarity index 86% rename from include/psi/vm/mappable_objects/shared_memory/win32/flags.inl rename to src/mappable_objects/shared_memory/flags.win32.cpp index 18dc488..8da9e9f 100644 --- a/include/psi/vm/mappable_objects/shared_memory/win32/flags.inl +++ b/src/mappable_objects/shared_memory/flags.win32.cpp @@ -14,17 +14,13 @@ /// //////////////////////////////////////////////////////////////////////////////// //------------------------------------------------------------------------------ -#include "flags.hpp" - -#include "psi/vm/detail/win32.hpp" -//------------------------------------------------------------------------------ -namespace psi -{ +#include +#include //------------------------------------------------------------------------------ -namespace vm +namespace psi::vm { //------------------------------------------------------------------------------ -namespace win32 +inline namespace win32 { //------------------------------------------------------------------------------ namespace flags @@ -34,8 +30,6 @@ namespace flags static_assert( (unsigned)shared_memory::system_hints::default_ == SEC_COMMIT , "Psi.VM internal inconsistency" ); static_assert( (unsigned)shared_memory::system_hints::only_reserve_address_space == SEC_RESERVE, "Psi.VM internal inconsistency" ); - -PSI_VM_IMPL_INLINE shared_memory BOOST_CC_REG shared_memory::create ( access_privileges const ap, @@ -53,7 +47,5 @@ shared_memory BOOST_CC_REG shared_memory::create //------------------------------------------------------------------------------ } // win32 //------------------------------------------------------------------------------ -} // vm -//------------------------------------------------------------------------------ -} // psi +} // psi::vm //------------------------------------------------------------------------------ diff --git a/include/psi/vm/mappable_objects/shared_memory/posix/mem.inl b/src/mappable_objects/shared_memory/mem.posix.cpp similarity index 89% rename from include/psi/vm/mappable_objects/shared_memory/posix/mem.inl rename to src/mappable_objects/shared_memory/mem.posix.cpp index 55bb738..bd0f819 100644 --- a/include/psi/vm/mappable_objects/shared_memory/posix/mem.inl +++ b/src/mappable_objects/shared_memory/mem.posix.cpp @@ -1,8 +1,5 @@ //////////////////////////////////////////////////////////////////////////////// /// -/// \file posix/mem.inl -/// ------------------- -/// /// Copyright (c) Domagoj Saric 2015 - 2024. /// /// Use, modification and distribution is subject to the @@ -14,10 +11,11 @@ /// //////////////////////////////////////////////////////////////////////////////// //------------------------------------------------------------------------------ -#ifndef __ANDROID__ -#include "mem.hpp" - -#include "psi/vm/detail/posix.hpp" +#ifndef __ANDROID__ // TODO rather exclude in CMake +#include +#include +#include +#include // semaphores #include @@ -28,10 +26,7 @@ #include #include //------------------------------------------------------------------------------ -namespace psi -{ -//------------------------------------------------------------------------------ -namespace vm +namespace psi::vm { //------------------------------------------------------------------------------ PSI_VM_POSIX_INLINE @@ -45,7 +40,7 @@ namespace detail // http://stackoverflow.com/questions/13377982/remove-posix-shared-memory-when-not-in-use // http://rhaas.blogspot.hr/2012/06/absurd-shared-memory-limits.html - PSI_VM_IMPL_INLINE BOOST_ATTRIBUTES( BOOST_MINSIZE ) + BOOST_ATTRIBUTES( BOOST_MINSIZE ) named_semaphore::named_semaphore ( char const * const name, @@ -53,11 +48,11 @@ namespace detail flags::named_object_construction_policy const creation_disposition ) noexcept : - semid_( -1 ) + semid_{ -1 } { /// \note No robust mutexes on Android and OSX, boost::hash does not /// give the same value in 32 bit and 64 bit processes (and we want to - /// support mixing those in IPC), and the default prescribed ain't so + /// support mixing those in IPC), and the prescribed default ain't so /// pretty either (http://nikitathespider.com/python/shm/#ftok)...so /// what's a poor programer to do?. /// (10.11.2015.) (Domagoj Saric) @@ -73,10 +68,10 @@ namespace detail char prefixed_name[ sizeof( key_file_prefix ) + length + 1 ]; std::strcpy( prefixed_name, key_file_prefix ); std::strcat( prefixed_name, name ); - auto const key_file( create_file( prefixed_name, flags::opening::create( {{},{},access_privileges}, flags::named_object_construction_policy::open_or_create, 0 ) ) ); + auto const key_file{ create_file( prefixed_name, flags::opening::create( {{},{},access_privileges}, flags::named_object_construction_policy::open_or_create, 0 ) ) }; if ( BOOST_UNLIKELY( !key_file ) ) return; - char seed( 0 ); for ( auto const character : std::string_view{ name } ) seed ^= character; + char seed{ 0 }; for ( auto const character : std::string_view{ name } ) seed ^= character; sem_key = ::ftok( prefixed_name, seed ); if ( BOOST_UNLIKELY( sem_key == -1 ) ) return; @@ -89,7 +84,7 @@ namespace detail /// \note Workaround for SysV semaphores not being automatically /// (and atomically) initialised upon creation (i.e. there is a race /// window between creation and 'initialisation' (implicitly done by the - /// first semop() call) aka "SysV semaphore fatal flaw": for + /// first semop() call) aka the "SysV semaphore fatal flaw": for /// create-or-open calls we have to separate/distinguish between the /// create and open cases (and thus add the EXCL flag for the create /// case/attempt). @@ -142,8 +137,6 @@ namespace detail /// \note Semaphore not yet initialised by its creator - we do not enter /// into a retry loop here as one has to be created by the user anyway /// (09.10.2015.) (Domagoj Saric) - /// \note We do not enter a retry loop here. - /// (09.10.2015.) (Domagoj Saric) //error::set( ENOENT ); //semid_ = -1; remove(); @@ -164,8 +157,8 @@ namespace detail } else { - auto const posix_creation_disposition( static_cast( creation_disposition ) ); - std::uint16_t sysv_creation_disposition( 0 ); + auto const posix_creation_disposition{ static_cast( creation_disposition ) }; + std::uint16_t sysv_creation_disposition{ 0 }; if ( posix_creation_disposition & O_CREAT ) sysv_creation_disposition |= IPC_CREAT; if ( posix_creation_disposition & O_EXCL ) @@ -227,7 +220,7 @@ namespace detail return static_cast( result ); } - bool named_semaphore::semadd( int const value, bool const nowait /*= false*/ ) noexcept + bool named_semaphore::semadd( short const value, bool const nowait /*= false*/ ) noexcept { auto const result( semop( value, nowait ) ); BOOST_ASSERT( result || error::get() == ENOMEM || ( nowait && error::get() == EAGAIN ) ); @@ -235,7 +228,7 @@ namespace detail } BOOST_ATTRIBUTES( BOOST_MINSIZE, BOOST_RESTRICTED_FUNCTION_L2, BOOST_EXCEPTIONLESS, BOOST_WARN_UNUSED_RESULT ) - bool named_semaphore::semop( int const opcode, bool const nowait /*= false*/ ) noexcept + bool named_semaphore::semop( short const opcode, bool const nowait /*= false*/ ) noexcept { // http://linux.die.net/man/2/semop ::sembuf sb; @@ -258,8 +251,6 @@ namespace detail //------------------------------------------------------------------------------ } // posix //------------------------------------------------------------------------------ -} // vm -//------------------------------------------------------------------------------ -} // psi +} // psi::vm //------------------------------------------------------------------------------ #endif // __ANDROID__ diff --git a/include/psi/vm/mapped_view/mapped_view.inl b/src/mapped_view/mapped_view.cpp similarity index 96% rename from include/psi/vm/mapped_view/mapped_view.inl rename to src/mapped_view/mapped_view.cpp index c64f53d..aada868 100644 --- a/include/psi/vm/mapped_view/mapped_view.inl +++ b/src/mapped_view/mapped_view.cpp @@ -1,8 +1,5 @@ //////////////////////////////////////////////////////////////////////////////// /// -/// \file mapped_view.inl -/// --------------------- -/// /// Copyright (c) Domagoj Saric 2010 - 2024. /// /// Use, modification and distribution is subject to the @@ -14,7 +11,8 @@ /// //////////////////////////////////////////////////////////////////////////////// //------------------------------------------------------------------------------ -#include PSI_VM_IMPL_INCLUDE( BOOST_PP_EMPTY, BOOST_PP_IDENTITY( /mapped_view.inl ) ) +#include "mapper.hpp" +#include //------------------------------------------------------------------------------ namespace psi::vm { @@ -126,6 +124,9 @@ basic_mapped_view::expand( std::size_t const target_size, mapping & o return err::success; } // view::expand() +template class basic_mapped_view; +template class basic_mapped_view; + //------------------------------------------------------------------------------ } // namespace psi::vm //------------------------------------------------------------------------------ diff --git a/include/psi/vm/mapped_view/posix/mapped_view.inl b/src/mapped_view/mapped_view.posix.cpp similarity index 50% rename from include/psi/vm/mapped_view/posix/mapped_view.inl rename to src/mapped_view/mapped_view.posix.cpp index 21f5b9b..bf40a92 100644 --- a/include/psi/vm/mapped_view/posix/mapped_view.inl +++ b/src/mapped_view/mapped_view.posix.cpp @@ -1,8 +1,5 @@ //////////////////////////////////////////////////////////////////////////////// /// -/// \file mapped_view.inl -/// --------------------- -/// /// Copyright (c) Domagoj Saric 2010 - 2024. /// /// Use, modification and distribution is subject to the @@ -14,15 +11,15 @@ /// //////////////////////////////////////////////////////////////////////////////// //------------------------------------------------------------------------------ -#include "psi/vm/mapped_view/mapped_view.hpp" +#include "mapper.hpp" -#include "psi/vm/align.hpp" -#include "psi/vm/detail/posix.hpp" -//------------------------------------------------------------------------------ -namespace psi -{ +#include +#include +#include + +#include //------------------------------------------------------------------------------ -namespace vm +namespace psi::vm { //------------------------------------------------------------------------------ inline namespace posix @@ -30,11 +27,6 @@ inline namespace posix //------------------------------------------------------------------------------ namespace { -#if defined( MAP_SHARED_VALIDATE ) && !defined( NDEBUG ) - auto constexpr MAP_SHARED_PSI{ MAP_SHARED_VALIDATE }; -#else - auto constexpr MAP_SHARED_PSI{ MAP_SHARED }; -#endif #if !defined( MAP_UNINITIALIZED ) auto constexpr MAP_UNINITIALIZED{ 0 }; #endif // MAP_UNINITIALIZED @@ -43,7 +35,6 @@ namespace #endif // MAP_ALIGNED_SUPER } // anonymous namespace -PSI_VM_IMPL_INLINE void * mmap( void * const target_address, std::size_t const size, int const protection, int const flags, int const file_handle, std::uint64_t const offset ) noexcept { BOOST_ASSUME( is_aligned( target_address, reserve_granularity ) ); @@ -68,81 +59,73 @@ void * mmap( void * const target_address, std::size_t const size, int const prot return nullptr; } +//------------------------------------------------------------------------------ +} // posix +//------------------------------------------------------------------------------ -struct mapper +BOOST_ATTRIBUTES( BOOST_MINSIZE, BOOST_EXCEPTIONLESS ) +mapped_span BOOST_CC_REG +mapper::map +( + handle::reference const source_mapping, + flags ::viewing const flags , + std ::uint64_t const offset , + std ::size_t const desired_size +) noexcept { - using error_t = error; - using flags_t = flags::mapping; - using mapping_t = mapping; + /// \note mmap() explicitly rejects a zero length/desired_size, IOW + /// unlike with MapViewOfFile() that approach cannot be used to + /// automatically map the entire object - a valid size must be + /// specified. + /// http://man7.org/linux/man-pages/man2/mmap.2.html + /// (30.09.2015.) (Domagoj Saric) - static BOOST_ATTRIBUTES( BOOST_MINSIZE, BOOST_EXCEPTIONLESS ) - mapped_span BOOST_CC_REG - map - ( - handle::reference const source_mapping, - flags ::viewing const flags , - std ::uint64_t const offset , - std ::size_t const desired_size - ) noexcept + auto const view_start { - /// \note mmap() explicitly rejects a zero length/desired_size, IOW - /// unlike with MapViewOfFile() that approach cannot be used to - /// automatically map the entire object - a valid size must be - /// specified. - /// http://man7.org/linux/man-pages/man2/mmap.2.html - /// (30.09.2015.) (Domagoj Saric) - - auto const view_start - { - static_cast + static_cast + ( + posix::mmap ( - posix::mmap - ( - nullptr, - desired_size, - flags.protection, - flags.flags, - source_mapping, - offset - ) + nullptr, + desired_size, + flags.protection, + flags.flags, + source_mapping, + offset ) - }; + ) + }; - return mapped_span{ view_start, view_start ? desired_size : 0 }; - } + return mapped_span{ view_start, view_start ? desired_size : 0 }; +} - static BOOST_ATTRIBUTES( BOOST_MINSIZE, BOOST_EXCEPTIONLESS ) - void BOOST_CC_REG unmap( mapped_span const view ) noexcept - { - [[ maybe_unused ]] auto munmap_result{ ::munmap( view.data(), view.size() ) }; +BOOST_ATTRIBUTES( BOOST_MINSIZE, BOOST_EXCEPTIONLESS ) +void BOOST_CC_REG mapper::unmap( mapped_span const view ) noexcept +{ + [[ maybe_unused ]] auto munmap_result{ ::munmap( view.data(), view.size() ) }; # ifndef __EMSCRIPTEN__ - BOOST_VERIFY - ( - ( munmap_result == 0 ) || - ( view.empty() && !view.data() ) - ); + BOOST_VERIFY + ( + ( munmap_result == 0 ) || + ( view.empty() && !view.data() ) + ); # endif - } +} - static void shrink( mapped_span const view, std::size_t const target_size ) noexcept - { - free - ( - align_up ( view.data() + target_size, commit_granularity ), - align_down( view.size() - target_size, commit_granularity ) - ); - } +void mapper::shrink( mapped_span const view, std::size_t const target_size ) noexcept +{ + free + ( + align_up ( view.data() + target_size, commit_granularity ), + align_down( view.size() - target_size, commit_granularity ) + ); +} - static void flush( mapped_span const view ) noexcept - { - BOOST_VERIFY( ::msync( view.data(), view.size(), MS_ASYNC /*?: | MS_INVALIDATE*/ ) == 0 ); - } -}; // struct mapper +void mapper::flush( mapped_span const view ) noexcept +{ + BOOST_VERIFY( ::msync( view.data(), view.size(), MS_ASYNC /*?: | MS_INVALIDATE*/ ) == 0 ); +} //------------------------------------------------------------------------------ -} // posix -//------------------------------------------------------------------------------ -} // vm -//------------------------------------------------------------------------------ -} // psi +} // psi::vm //------------------------------------------------------------------------------ diff --git a/src/mapped_view/mapped_view.win32.cpp b/src/mapped_view/mapped_view.win32.cpp new file mode 100644 index 0000000..c49b3ac --- /dev/null +++ b/src/mapped_view/mapped_view.win32.cpp @@ -0,0 +1,91 @@ +//////////////////////////////////////////////////////////////////////////////// +/// +/// \file mapped_view.inl +/// --------------------- +/// +/// Copyright (c) Domagoj Saric 2010 - 2024. +/// +/// Use, modification and distribution is subject to the +/// Boost Software License, Version 1.0. +/// (See accompanying file LICENSE_1_0.txt or copy at +/// http://www.boost.org/LICENSE_1_0.txt) +/// +/// For more information, see http://www.boost.org +/// +//////////////////////////////////////////////////////////////////////////////// +//------------------------------------------------------------------------------ +#include "mapper.hpp" + +#include +#include +#include + +#include +//------------------------------------------------------------------------------ +namespace psi::vm +{ +//------------------------------------------------------------------------------ + +BOOST_ATTRIBUTES( BOOST_MINSIZE, BOOST_EXCEPTIONLESS ) +mapped_span BOOST_CC_REG +mapper::map +( + mapping::handle const source_mapping, + flags ::viewing const flags , + std ::uint64_t const offset , // ERROR_MAPPED_ALIGNMENT + std ::size_t const desired_size +) noexcept +{ + // Implementation note: + // Mapped views hold internal references to the mapping handles so we do + // not need to hold/store them ourselves: + // http://msdn.microsoft.com/en-us/library/aa366537(VS.85).aspx + // (26.03.2010.) (Domagoj Saric) + + ULARGE_INTEGER const win32_offset{ .QuadPart = offset }; + auto const view_start + { + static_cast + ( + ::MapViewOfFile + ( + source_mapping, + flags.map_view_flags, + win32_offset.HighPart, + win32_offset.LowPart, + desired_size + ) + ) + }; + + return + { + view_start, + view_start ? desired_size : 0 + }; +} + +BOOST_ATTRIBUTES( BOOST_MINSIZE, BOOST_EXCEPTIONLESS, BOOST_RESTRICTED_FUNCTION_L1 ) +void BOOST_CC_REG mapper::unmap( mapped_span const view ) noexcept +{ + BOOST_VERIFY( ::UnmapViewOfFile( view.data() ) || view.empty() ); +} + +void mapper::shrink( mapped_span const view, std::size_t const target_size ) noexcept +{ + // TODO OfferVirtualMemory, VirtualUnlock + decommit + ( + align_up ( view.data() + target_size, commit_granularity ), + align_down( view.size() - target_size, commit_granularity ) + ); +} + +void mapper::flush( mapped_span const view ) noexcept +{ + BOOST_VERIFY( ::FlushViewOfFile( view.data(), view.size() ) == 0 ); +} + +//------------------------------------------------------------------------------ +} // psi::vm +//------------------------------------------------------------------------------ diff --git a/include/psi/vm/error/win32/error.hpp b/src/mapped_view/mapper.hpp similarity index 52% rename from include/psi/vm/error/win32/error.hpp rename to src/mapped_view/mapper.hpp index 249c26d..c106157 100644 --- a/include/psi/vm/error/win32/error.hpp +++ b/src/mapped_view/mapper.hpp @@ -1,8 +1,5 @@ //////////////////////////////////////////////////////////////////////////////// /// -/// \file win32/error.hpp -/// --------------------- -/// /// Copyright (c) Domagoj Saric 2010 - 2024. /// /// Use, modification and distribution is subject to the @@ -14,29 +11,39 @@ /// //////////////////////////////////////////////////////////////////////////////// //------------------------------------------------------------------------------ -#ifndef error_hpp__C987B8F7_BA90_41F7_B025_98AFE271C6A9 -#define error_hpp__C987B8F7_BA90_41F7_B025_98AFE271C6A9 #pragma once + +#include +#include +#include +#include + +#include //------------------------------------------------------------------------------ -#include -//------------------------------------------------------------------------------ -namespace psi -{ -//------------------------------------------------------------------------------ -namespace vm +namespace psi::vm { //------------------------------------------------------------------------------ -namespace + +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; -using error = err::last_win32_error; + static BOOST_ATTRIBUTES( BOOST_MINSIZE, BOOST_EXCEPTIONLESS, BOOST_RESTRICTED_FUNCTION_L1 ) + void BOOST_CC_REG unmap( mapped_span view ) noexcept; + + static void shrink( mapped_span view, std::size_t target_size ) noexcept; + + static void flush( mapped_span view ) noexcept; +}; // struct mapper //------------------------------------------------------------------------------ -} // namespace -//------------------------------------------------------------------------------ -} // namespace vm -//------------------------------------------------------------------------------ -} // namespace psi +} // namespace psi::vm //------------------------------------------------------------------------------ -#endif // errno_hpp diff --git a/src/mapping/mapping.win32.cpp b/src/mapping/mapping.win32.cpp new file mode 100644 index 0000000..e9aa331 --- /dev/null +++ b/src/mapping/mapping.win32.cpp @@ -0,0 +1,61 @@ +//////////////////////////////////////////////////////////////////////////////// +/// +/// Copyright (c) Domagoj Saric 2010 - 2024. +/// +/// Use, modification and distribution is subject to the +/// Boost Software License, Version 1.0. +/// (See accompanying file LICENSE_1_0.txt or copy at +/// http://www.boost.org/LICENSE_1_0.txt) +/// +/// For more information, see http://www.boost.org +/// +//////////////////////////////////////////////////////////////////////////////// +//------------------------------------------------------------------------------ +#include + +//#include //...broken? +#include +#include + +#include + +#include +//------------------------------------------------------------------------------ +namespace psi::vm +{ +//------------------------------------------------------------------------------ +inline namespace win32 +{ +//------------------------------------------------------------------------------ + +std::uint64_t get_size( mapping::const_handle const mapping_handle ) noexcept +{ + using namespace nt; + SECTION_BASIC_INFORMATION info; + auto const result{ NtQuerySection( mapping_handle.value, SECTION_INFORMATION_CLASS::SectionBasicInformation, &info, sizeof( info ), nullptr ) }; + BOOST_VERIFY( NT_SUCCESS( result ) ); + return info.SectionSize.QuadPart; +} + +err::fallible_result set_size( mapping::handle const mapping_handle, std::uint64_t const new_size ) noexcept +{ + using namespace nt; + LARGE_INTEGER ntsz{ .QuadPart = static_cast( new_size ) }; + auto const result{ NtExtendSection( mapping_handle.value, &ntsz ) }; + if ( !NT_SUCCESS( result ) ) [[ unlikely ]] + return result; + + BOOST_ASSERT( ntsz.QuadPart >= static_cast( new_size ) ); + if ( ntsz.QuadPart > static_cast( new_size ) ) + { + BOOST_ASSERT( ntsz.QuadPart == static_cast( get_size( mapping_handle ) ) ); + // NtExtendSection does not seem to support downsizing. TODO workaround + } + return err::success; +} + +//------------------------------------------------------------------------------ +} // namespace win32 +//------------------------------------------------------------------------------ +} // namespace psi::vm +//------------------------------------------------------------------------------ diff --git a/include/psi/vm/protection.cpp b/src/protection.cpp similarity index 70% rename from include/psi/vm/protection.cpp rename to src/protection.cpp index 38863c7..0213230 100644 --- a/include/psi/vm/protection.cpp +++ b/src/protection.cpp @@ -11,7 +11,7 @@ /// //////////////////////////////////////////////////////////////////////////////// //------------------------------------------------------------------------------ -#include "protection.hpp" +#include #ifdef _WIN32 #include @@ -19,29 +19,26 @@ #include #endif // platform //------------------------------------------------------------------------------ -namespace psi -{ -//------------------------------------------------------------------------------ -namespace vm +namespace psi::vm { //------------------------------------------------------------------------------ enum protection : std::uint32_t { #ifdef _WIN32 - no_access = PAGE_NOACCESS, - read_only = PAGE_READONLY, - read_write = PAGE_READWRITE + access_none = PAGE_NOACCESS, + rd_only = PAGE_READONLY, + rw = PAGE_READWRITE #else // POSIX - no_access = PROT_NONE, - read_only = PROT_READ, - read_write = PROT_READ | PROT_WRITE + access_none = PROT_NONE, + rd_only = PROT_READ, + rw = PROT_READ | PROT_WRITE #endif // platform -}; +}; // enum protection -protection const no_access { protection::no_access }; -protection const read_only { protection::read_only }; -protection const read_write{ protection::read_write }; +protection const no_access { protection::access_none }; +protection const read_only { protection::rd_only }; +protection const read_write{ protection::rw }; err::void_or_error< error > protect( void * const region_begin, std::size_t const region_size, std::underlying_type_t const access_flags ) noexcept { @@ -57,7 +54,5 @@ err::void_or_error< error > protect( void * const region_begin, std::size_t cons } //------------------------------------------------------------------------------ -} // namespace vm -//------------------------------------------------------------------------------ -} // namespace psi +} // namespace psi::vm //------------------------------------------------------------------------------ diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt new file mode 100644 index 0000000..5d21b04 --- /dev/null +++ b/test/CMakeLists.txt @@ -0,0 +1,33 @@ +# psi::vm tests + +# Fix ODR violations and compiler and linker option mismatches: add GTest after everything is already setup. +CPMAddPackage( "gh:google/googletest@1.14.0" ) + +include( GoogleTest ) + +set( vm_test_sources + "${CMAKE_CURRENT_SOURCE_DIR}/vector.cpp" +) + +add_executable( vm_unit_tests EXCLUDE_FROM_ALL ${vm_test_sources} ) +target_link_libraries( vm_unit_tests PRIVATE GTest::gtest GTest::gtest_main psi::vm ) + +set_target_properties( + vm_unit_tests + PROPERTIES + RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/test" + COMMAND vm_unit_tests +) + +gtest_discover_tests( + vm_unit_tests + EXTRA_ARGS + --gtest_color=auto + --gtest_output=xml:${CMAKE_BINARY_DIR}/test/vm_unit_tests.xml + --gtest_catch_exceptions=1 + DISCOVERY_TIMEOUT 120 + PROPERTIES + TIMEOUT 120 +) + +add_test( "vm_unit_tests" vm_unit_tests ) diff --git a/test/vector.cpp b/test/vector.cpp new file mode 100644 index 0000000..cb9ad46 --- /dev/null +++ b/test/vector.cpp @@ -0,0 +1,39 @@ +#include + +#include + +#include +#include +#include +//------------------------------------------------------------------------------ +namespace psi::vm +{ +//------------------------------------------------------------------------------ + +TEST( vector, playground ) +{ + std::filesystem::path const test_vec{ "test_vec" }; + std::filesystem::remove( test_vec ); + { + psi::vm::vector< double, std::uint16_t > vec; + vec.open( test_vec.c_str() ); + EXPECT_EQ( vec.size(), 0 ); + vec.append_range({ 3.14, 0.14, 0.04 }); + EXPECT_EQ( vec.size(), 3 ); + EXPECT_EQ( vec[ 0 ], 3.14 ); + EXPECT_EQ( vec[ 1 ], 0.14 ); + EXPECT_EQ( vec[ 2 ], 0.04 ); + } + { + psi::vm::vector< double, std::uint16_t > vec; + vec.open( test_vec.c_str() ); + EXPECT_EQ( vec.size(), 3 ); + EXPECT_EQ( vec[ 0 ], 3.14 ); + EXPECT_EQ( vec[ 1 ], 0.14 ); + EXPECT_EQ( vec[ 2 ], 0.04 ); + } +} + +//------------------------------------------------------------------------------ +} // namespace psi::vm +//------------------------------------------------------------------------------ diff --git a/vm.cmake b/vm.cmake new file mode 100644 index 0000000..6467b0a --- /dev/null +++ b/vm.cmake @@ -0,0 +1,33 @@ +set( CMAKE_CXX_STANDARD 23 ) + +# TODO finish and replace file globbing +#set( vm_public_headers +# "${CMAKE_CURRENT_SOURCE_DIR}/include/psi/vm/mapped_view/guarded_operation.hpp" +# "${CMAKE_CURRENT_SOURCE_DIR}/include/psi/vm/mapped_view/mapped_view.hpp" +# ... +#) +#set( vm_sources +# ... +# "${CMAKE_CURRENT_SOURCE_DIR}/src/protection.cpp" +#) +file( GLOB_RECURSE vm_public_headers "${CMAKE_CURRENT_SOURCE_DIR}/include/*.hpp" ) +file( GLOB_RECURSE vm_sources "${CMAKE_CURRENT_SOURCE_DIR}/src/*" ) + +source_group( TREE ${CMAKE_CURRENT_SOURCE_DIR}/include/psi/vm FILES ${vm_public_headers} ) +source_group( TREE ${CMAKE_CURRENT_SOURCE_DIR}/src FILES ${vm_sources} ) + +if ( WIN32 ) + set( excluded_impl posix ) +else() + set( excluded_impl win32 ) +endif() +foreach( source ${vm_sources} ) + if ( ${source} MATCHES ${excluded_impl} ) + set_source_files_properties( ${source} PROPERTIES HEADER_FILE_ONLY true ) + endif() +endforeach() + +add_library( psi_vm STATIC ${vm_public_headers} ${vm_sources} ) +add_library( psi::vm ALIAS psi_vm ) +target_include_directories( psi_vm PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include" ) +target_compile_features( psi_vm PUBLIC cxx_std_23 )