From 088961f77706ace3b21a73c54aa4777913198115 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Domagoj=20=C5=A0ari=C4=87?= Date: Thu, 29 Aug 2024 13:51:24 +0200 Subject: [PATCH] Windows: - fixes and cleanups in anonymous mappings logic - changes to handling the win32 null vs invalid handle 'logic'. --- include/psi/vm/handles/handle.win32.hpp | 6 ++++- include/psi/vm/handles/handle_ref.hpp | 2 +- include/psi/vm/vector.hpp | 2 +- src/mappable_objects/file/file.win32.cpp | 7 ++++-- src/mapped_view/mapped_view.win32.cpp | 28 ++++++++++++++++-------- 5 files changed, 31 insertions(+), 14 deletions(-) diff --git a/include/psi/vm/handles/handle.win32.hpp b/include/psi/vm/handles/handle.win32.hpp index 57adadd..f931f2b 100644 --- a/include/psi/vm/handles/handle.win32.hpp +++ b/include/psi/vm/handles/handle.win32.hpp @@ -34,11 +34,15 @@ 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{ nullptr }; // Win32 is inconsistent: some parts use null while other parts use INVALID_HANDLE_VALUE static BOOST_ATTRIBUTES( BOOST_MINSIZE, BOOST_RESTRICTED_FUNCTION_L1, BOOST_EXCEPTIONLESS ) void close( native_t const native_handle ) { +# if defined( __clang__ ) || defined( __GNUC__ ) + if ( __builtin_constant_p( native_handle ) && ( native_handle == nullptr || native_handle == boost::winapi::INVALID_HANDLE_VALUE_ ) ) + return; +# endif BOOST_VERIFY ( ( boost::winapi::CloseHandle( native_handle ) != false ) || diff --git a/include/psi/vm/handles/handle_ref.hpp b/include/psi/vm/handles/handle_ref.hpp index 4dafe5d..a9e7ec1 100644 --- a/include/psi/vm/handles/handle_ref.hpp +++ b/include/psi/vm/handles/handle_ref.hpp @@ -50,7 +50,7 @@ struct handle_ref [[ gnu::pure ]] constexpr bool operator==( native_handle_t const other ) const noexcept { return value == other; } - native_handle_t const value; + native_handle_t value; }; // struct handle_ref #ifdef _MSC_VER diff --git a/include/psi/vm/vector.hpp b/include/psi/vm/vector.hpp index 2847aa9..c43b2fe 100644 --- a/include/psi/vm/vector.hpp +++ b/include/psi/vm/vector.hpp @@ -230,7 +230,7 @@ class contiguous_container_storage if constexpr ( !headerless ) { auto & stored_size{ this->stored_size() }; - BOOST_ASSERT_MSG( stored_size == 0, "Got garbage in an anonymous mapping!?" ); + BOOST_ASSUME( stored_size == 0 ); // Got garbage in an anonymous mapping!? stored_size = size; } return err::success; diff --git a/src/mappable_objects/file/file.win32.cpp b/src/mappable_objects/file/file.win32.cpp index f54297d..27c7987 100644 --- a/src/mappable_objects/file/file.win32.cpp +++ b/src/mappable_objects/file/file.win32.cpp @@ -126,7 +126,7 @@ namespace detail nullptr, // OBJECT_ATTRIBUTES &maximum_size, flags, - SEC_RESERVE, + SEC_COMMIT, file ) }; @@ -142,8 +142,11 @@ namespace detail } BOOST_ATTRIBUTES( BOOST_EXCEPTIONLESS, BOOST_RESTRICTED_FUNCTION_L1 ) BOOST_FORCEINLINE - mapping BOOST_CC_REG do_map( file_handle::reference const file, flags::mapping const flags, std::uint64_t const maximum_size, auto const * __restrict const name ) noexcept + mapping BOOST_CC_REG do_map( file_handle::reference file, flags::mapping const flags, std::uint64_t const maximum_size, auto const * __restrict const name ) noexcept { + if ( file == file_handle::invalid_value ) + file.value = INVALID_HANDLE_VALUE; // CreateFileMapping wants this instead of null + ::SECURITY_ATTRIBUTES sa; auto const p_security_attributes( flags::detail::make_sa_ptr( sa, flags.system_access.p_sd, reinterpret_cast/*static_cast*/( flags.child_access ) ) ); auto const max_sz ( reinterpret_cast( maximum_size ) ); diff --git a/src/mapped_view/mapped_view.win32.cpp b/src/mapped_view/mapped_view.win32.cpp index 5bbac94..268e6b6 100644 --- a/src/mapped_view/mapped_view.win32.cpp +++ b/src/mapped_view/mapped_view.win32.cpp @@ -57,9 +57,15 @@ map ) ) }; + return + { + view_start, + view_start ? desired_size : 0 + }; #else + // TODO flags parameter LARGE_INTEGER nt_offset{ .QuadPart = static_cast( offset ) }; - auto sz{ desired_size }; + auto sz{ desired_size }; void * view_startv{ nullptr }; auto const success { @@ -77,15 +83,19 @@ map PAGE_READWRITE ) }; - BOOST_ASSERT( sz >= desired_size ); - auto const view_start{ static_cast( view_startv ) }; -#endif - - return + if ( success == nt::STATUS_SUCCESS ) [[ likely ]] { - view_start, - view_start ? desired_size : 0 - }; + BOOST_ASSUME( sz >= desired_size ); + BOOST_ASSUME( view_startv != nullptr ); + return { static_cast( view_startv ), sz }; + } + else + { + BOOST_ASSUME( sz == 0 ); + BOOST_ASSUME( view_startv == nullptr ); + return {}; + } +#endif } namespace