Skip to content

Commit

Permalink
Merge pull request #20 from psiha/fix/resizing_anonymous_posix_mapping
Browse files Browse the repository at this point in the history
POSIX: anonymous mapping resizing fixes.
  • Loading branch information
psiha authored Sep 17, 2024
2 parents cf43df9 + eb2587a commit 28fa964
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
8 changes: 7 additions & 1 deletion include/psi/vm/mapping/mapping.posix.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ struct [[ clang::trivial_abi ]] mapping

bool is_file_based() const noexcept { return posix::handle::operator bool(); }

bool is_anonymous() const noexcept { return view_mapping_flags.flags & MAP_ANONYMOUS; }

posix::handle:: reference underlying_file() noexcept { BOOST_ASSERT( is_file_based() ); return *this; }
posix::handle::const_reference underlying_file() const noexcept { BOOST_ASSERT( is_file_based() ); return *this; }

Expand All @@ -74,7 +76,7 @@ struct [[ clang::trivial_abi ]] mapping
}

// override Handle operator bool to allow for anonymous mappings
explicit operator bool() const noexcept { return posix::handle::operator bool() || ( view_mapping_flags.flags & MAP_ANONYMOUS ); }
explicit operator bool() const noexcept { return posix::handle::operator bool() || is_anonymous(); }

flags::viewing view_mapping_flags{};
std ::size_t maximum_size {};
Expand All @@ -84,6 +86,10 @@ struct [[ clang::trivial_abi ]] mapping
err::fallible_result<void, error> set_size( handle:: reference, std::uint64_t desired_size ) noexcept;
std::uint64_t get_size( handle::const_reference ) noexcept;

// (temporary) special overloads to handle anonymous mappings (until a dedicated mapping type sees the light of day)
err::fallible_result<void, error> set_size( mapping &, std::size_t desired_size ) noexcept;
std::size_t get_size( mapping const & ) noexcept;

//------------------------------------------------------------------------------
} // namespace posix
//------------------------------------------------------------------------------
Expand Down
23 changes: 23 additions & 0 deletions src/mappable_objects/file/file.posix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,29 @@ mapping create_mapping
}
return { std::move( file ), view_flags, size };
}

// mapping_posix.cpp (not yet existent file) contents//////////////////////////
err::fallible_result<void, error> set_size( mapping & mapping, std::size_t const desired_size ) noexcept
{
if ( mapping.is_anonymous() )
{
mapping.maximum_size = align_up( desired_size, reserve_granularity );
return err::success;
}
auto result{ set_size( mapping.underlying_file(), desired_size ) };
if ( std::move( result ) )
mapping.maximum_size = align_up( desired_size, reserve_granularity );
return result;
}
std::size_t get_size( mapping const & mapping ) noexcept
{
if ( mapping.is_anonymous() )
return mapping.maximum_size;
// TODO update or verify&return mapping.maximum_size?
return static_cast<std::size_t>( get_size( mapping.underlying_file() ) );
}
///////////////////////////////////////////////////////////////////////////////

#endif // POSIX impl level

//------------------------------------------------------------------------------
Expand Down

0 comments on commit 28fa964

Please sign in to comment.