mirror of
https://github.com/vimpunk/mio.git
synced 2026-02-16 23:29:55 +08:00
Create invalid_handle value and update unmapped shared_mmap handle methods to return it
This commit is contained in:
parent
2dbf5d3050
commit
5ac44ecb00
@ -42,11 +42,13 @@ namespace detail {
|
|||||||
enum { map_entire_file = 0 };
|
enum { map_entire_file = 0 };
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
using file_handle_type = HANDLE;
|
using file_handle_type = HANDLE;
|
||||||
#else
|
#else
|
||||||
using file_handle_type = int;
|
using file_handle_type = int;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
constexpr static file_handle_type invalid_handle = INVALID_HANDLE_VALUE;
|
||||||
|
|
||||||
template<typename ByteT> struct basic_mmap
|
template<typename ByteT> struct basic_mmap
|
||||||
{
|
{
|
||||||
using value_type = ByteT;
|
using value_type = ByteT;
|
||||||
|
|||||||
@ -32,6 +32,10 @@ namespace mio {
|
|||||||
// `map`, in which case a memory mapping of the entire file is created.
|
// `map`, in which case a memory mapping of the entire file is created.
|
||||||
using detail::map_entire_file;
|
using detail::map_entire_file;
|
||||||
|
|
||||||
|
// This value represents an invalid file handle type. This can be used to
|
||||||
|
// determine whether `basic_mmap::file_handle` is valid, for example.
|
||||||
|
using detail::invalid_handle;
|
||||||
|
|
||||||
template<
|
template<
|
||||||
access_mode AccessMode,
|
access_mode AccessMode,
|
||||||
typename ByteT
|
typename ByteT
|
||||||
|
|||||||
@ -120,8 +120,15 @@ public:
|
|||||||
* however, a mapped region of a file gets its own handle, which is returned by
|
* however, a mapped region of a file gets its own handle, which is returned by
|
||||||
* 'mapping_handle'.
|
* 'mapping_handle'.
|
||||||
*/
|
*/
|
||||||
handle_type file_handle() const noexcept { return pimpl_->file_handle(); }
|
handle_type file_handle() const noexcept
|
||||||
handle_type mapping_handle() const noexcept { return pimpl_->mapping_handle(); }
|
{
|
||||||
|
return pimpl_ ? pimpl_->file_handle() : invalid_handle;
|
||||||
|
}
|
||||||
|
|
||||||
|
handle_type mapping_handle() const noexcept
|
||||||
|
{
|
||||||
|
return pimpl_ ? pimpl_->mapping_handle() : invalid_handle;
|
||||||
|
}
|
||||||
|
|
||||||
/** Returns whether a valid memory mapping has been created. */
|
/** Returns whether a valid memory mapping has been created. */
|
||||||
bool is_open() const noexcept { return pimpl_ && pimpl_->is_open(); }
|
bool is_open() const noexcept { return pimpl_ && pimpl_->is_open(); }
|
||||||
@ -142,7 +149,9 @@ public:
|
|||||||
size_type size() const noexcept { return pimpl_ ? pimpl_->length() : 0; }
|
size_type size() const noexcept { return pimpl_ ? pimpl_->length() : 0; }
|
||||||
size_type length() const noexcept { return pimpl_ ? pimpl_->length() : 0; }
|
size_type length() const noexcept { return pimpl_ ? pimpl_->length() : 0; }
|
||||||
size_type mapped_length() const noexcept
|
size_type mapped_length() const noexcept
|
||||||
{ return pimpl_ ? pimpl_->mapped_length() : 0; }
|
{
|
||||||
|
return pimpl_ ? pimpl_->mapped_length() : 0;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the offset, relative to the file's start, at which the mapping was
|
* Returns the offset, relative to the file's start, at which the mapping was
|
||||||
|
|||||||
@ -89,8 +89,7 @@ int main()
|
|||||||
CHECK_INVALID_MMAP(m);
|
CHECK_INVALID_MMAP(m);
|
||||||
|
|
||||||
// Invalid handle?
|
// Invalid handle?
|
||||||
m = mio::make_mmap_source(
|
m = mio::make_mmap_source(mio::invalid_handle, 0, 0, error);
|
||||||
INVALID_HANDLE_VALUE/*Psst... This is an implementation detail!*/, 0, 0, error);
|
|
||||||
CHECK_INVALID_MMAP(m);
|
CHECK_INVALID_MMAP(m);
|
||||||
|
|
||||||
// Invalid offset?
|
// Invalid offset?
|
||||||
@ -102,6 +101,9 @@ int main()
|
|||||||
// Make sure custom types compile.
|
// Make sure custom types compile.
|
||||||
mio::ummap_source _1;
|
mio::ummap_source _1;
|
||||||
mio::shared_ummap_source _2;
|
mio::shared_ummap_source _2;
|
||||||
|
// Make sure shared_mmap mapping compiles as all testing was done on
|
||||||
|
// normal mmaps.
|
||||||
|
mio::shared_mmap_source _3(path, 0, mio::map_entire_file);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::printf("all tests passed!\n");
|
std::printf("all tests passed!\n");
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user