io: allow mutable memory for mapped_file

This commit is contained in:
Danny Robson 2014-07-07 15:19:01 +10:00
parent 5c862400a4
commit f46c090c72
2 changed files with 16 additions and 18 deletions

25
io.cpp
View File

@ -14,7 +14,7 @@
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with libgim. If not, see <http://www.gnu.org/licenses/>. * along with libgim. If not, see <http://www.gnu.org/licenses/>.
* *
* Copyright 2010-2012 Danny Robson <danny@nerdcruft.net> * Copyright 2010-2014 Danny Robson <danny@nerdcruft.net>
*/ */
#include "io.hpp" #include "io.hpp"
@ -182,18 +182,8 @@ util::set_cwd (const boost::filesystem::path &path) {
#include <sys/mman.h> #include <sys/mman.h>
mapped_file::mapped_file (const char *_path):
m_fd (open (_path, O_RDONLY))
{ load_fd (); }
mapped_file::mapped_file (const std::string &_path):
m_fd (open (_path.c_str (), O_RDONLY))
{ load_fd (); }
mapped_file::mapped_file (const boost::filesystem::path &_path): mapped_file::mapped_file (const boost::filesystem::path &_path):
m_fd (open (_path.native ().c_str (), O_RDONLY)) m_fd (open (_path.native ().c_str (), O_RDWR))
{ load_fd (); } { load_fd (); }
@ -210,7 +200,7 @@ mapped_file::load_fd (void) {
throw errno_error (); throw errno_error ();
m_size = (size_t)meta.st_size; m_size = (size_t)meta.st_size;
m_data = (uint8_t *)mmap (NULL, m_size, PROT_READ, MAP_PRIVATE, m_fd, 0); m_data = (uint8_t *)mmap (NULL, m_size, PROT_READ | PROT_WRITE, MAP_SHARED, m_fd, 0);
if (m_data == MAP_FAILED) if (m_data == MAP_FAILED)
throw errno_error (); throw errno_error ();
} }
@ -225,6 +215,15 @@ mapped_file::size (void) const {
} }
uint8_t*
mapped_file::data (void) {
CHECK (m_size > 0);
CHECK (m_data != NULL);
return m_data;
}
const uint8_t* const uint8_t*
mapped_file::data (void) const { mapped_file::data (void) const {
CHECK (m_size > 0); CHECK (m_size > 0);

9
io.hpp
View File

@ -14,7 +14,7 @@
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with libgim. If not, see <http://www.gnu.org/licenses/>. * along with libgim. If not, see <http://www.gnu.org/licenses/>.
* *
* Copyright 2010-2012 Danny Robson <danny@nerdcruft.net> * Copyright 2010-2014 Danny Robson <danny@nerdcruft.net>
*/ */
#ifndef __UTIL_IO_HPP #ifndef __UTIL_IO_HPP
@ -114,16 +114,15 @@ namespace util {
void load_fd (void); void load_fd (void);
public: public:
mapped_file (const char *path);
mapped_file (const std::string &path);
mapped_file (const boost::filesystem::path &path); mapped_file (const boost::filesystem::path &path);
mapped_file (const mapped_file &rhs); mapped_file (const mapped_file&) = delete;
mapped_file& operator =(const mapped_file &rhs); mapped_file& operator= (const mapped_file&) = delete;
~mapped_file (); ~mapped_file ();
const uint8_t* data (void) const; const uint8_t* data (void) const;
uint8_t* data (void);
size_t size (void) const; size_t size (void) const;
}; };
#endif #endif