io_posix: add fd constructors to mapped_file

This commit is contained in:
Danny Robson 2016-08-02 18:51:04 +10:00
parent c6f483d077
commit f9fd156df8
2 changed files with 16 additions and 24 deletions

View File

@ -26,16 +26,22 @@
using util::detail::posix::mapped_file; using util::detail::posix::mapped_file;
////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////
mapped_file::mapped_file (const char *_path, int fflags, int mflags): mapped_file::mapped_file (const char *path, int fflags, int mflags):
m_fd (_path, fflags) mapped_file (util::fd (path, fflags), mflags)
{ ; }
//-----------------------------------------------------------------------------
mapped_file::mapped_file (const util::fd &src, int mflags)
{ {
try { struct stat meta;
load_fd (mflags); if (fstat (src, &meta) < 0)
} catch (const errno_error &e) { throw errno_error ();
// ignore zero length mapping error
if (e.code () == EINVAL && m_size == 0) m_size = (size_t)meta.st_size;
return; m_data = (uint8_t *)mmap (NULL, m_size, mflags, MAP_SHARED, src, 0);
} if (m_data == MAP_FAILED)
throw errno_error ();
} }
@ -47,20 +53,6 @@ mapped_file::~mapped_file ()
} }
//----------------------------------------------------------------------------
void
mapped_file::load_fd (int mflags) {
struct stat meta;
if (fstat (m_fd, &meta) < 0)
throw errno_error ();
m_size = (size_t)meta.st_size;
m_data = (uint8_t *)mmap (NULL, m_size, mflags, MAP_SHARED, m_fd, 0);
if (m_data == MAP_FAILED)
throw errno_error ();
}
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------
size_t size_t
mapped_file::size (void) const mapped_file::size (void) const

View File

@ -31,6 +31,7 @@ namespace util {
class mapped_file { class mapped_file {
public: public:
mapped_file (const char *path, int fflags = O_RDONLY | O_BINARY, int mflags = PROT_READ); mapped_file (const char *path, int fflags = O_RDONLY | O_BINARY, int mflags = PROT_READ);
mapped_file (const util::fd&, int mflags = PROT_READ);
mapped_file (const mapped_file&) = delete; mapped_file (const mapped_file&) = delete;
mapped_file& operator= (const mapped_file&) = delete; mapped_file& operator= (const mapped_file&) = delete;
@ -61,7 +62,6 @@ namespace util {
as_view () &; as_view () &;
private: private:
fd m_fd;
uint8_t *m_data; uint8_t *m_data;
size_t m_size; size_t m_size;