Skip to content

Commit

Permalink
Windows:
Browse files Browse the repository at this point in the history
 - fixes and cleanups in anonymous mappings logic
 - changes to handling the win32 null vs invalid handle 'logic'.
  • Loading branch information
psiha committed Aug 29, 2024
1 parent 36ad9cc commit 088961f
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 14 deletions.
6 changes: 5 additions & 1 deletion include/psi/vm/handles/handle.win32.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) ||
Expand Down
2 changes: 1 addition & 1 deletion include/psi/vm/handles/handle_ref.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion include/psi/vm/vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
7 changes: 5 additions & 2 deletions src/mappable_objects/file/file.win32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ namespace detail
nullptr, // OBJECT_ATTRIBUTES
&maximum_size,
flags,
SEC_RESERVE,
SEC_COMMIT,
file
)
};
Expand All @@ -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<bool const &>/*static_cast<bool>*/( flags.child_access ) ) );
auto const max_sz ( reinterpret_cast<ULARGE_INTEGER const &>( maximum_size ) );
Expand Down
28 changes: 19 additions & 9 deletions src/mapped_view/mapped_view.win32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,15 @@ map
)
)
};
return
{
view_start,
view_start ? desired_size : 0
};
#else
// TODO flags parameter
LARGE_INTEGER nt_offset{ .QuadPart = static_cast<LONGLONG>( offset ) };
auto sz{ desired_size };
auto sz{ desired_size };
void * view_startv{ nullptr };
auto const success
{
Expand All @@ -77,15 +83,19 @@ map
PAGE_READWRITE
)
};
BOOST_ASSERT( sz >= desired_size );
auto const view_start{ static_cast<std::byte *>( 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<std::byte *>( view_startv ), sz };
}
else
{
BOOST_ASSUME( sz == 0 );
BOOST_ASSUME( view_startv == nullptr );
return {};
}
#endif
}

namespace
Expand Down

0 comments on commit 088961f

Please sign in to comment.