posix/map: start extracting pure mmap class

This commit is contained in:
Danny Robson 2016-10-02 16:18:41 +11:00
parent 152a55ad78
commit 4608c65c84
3 changed files with 196 additions and 0 deletions

View File

@ -292,6 +292,8 @@ POSIX_FILES = \
posix/dir.ipp \
posix/fd.cpp \
posix/fd.hpp \
posix/map.cpp \
posix/map.hpp \
time_posix.cpp

132
posix/map.cpp Normal file
View File

@ -0,0 +1,132 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Copyright 2016 Danny Robson <danny@nerdcruft.net>
*/
#include "map.hpp"
#include "../except.hpp"
using util::posix::map;
///////////////////////////////////////////////////////////////////////////////
map::map (size_t size, int prot, int flags):
m_addr (mmap (nullptr, size, prot, flags, -1, 0)),
m_size (size)
{
if (MAP_FAILED == m_addr)
::util::errno_error::throw_code ();
}
//-----------------------------------------------------------------------------
map::map (size_t size, int prot, int flags, const fd &src, off_t offset):
m_addr (mmap (nullptr, size, prot, flags, src, offset)),
m_size (size)
{
if (MAP_FAILED == m_addr)
::util::errno_error::throw_code ();
}
//-----------------------------------------------------------------------------
map::map (map &&rhs):
m_addr (MAP_FAILED),
m_size (rhs.m_size)
{
std::swap (m_addr, rhs.m_addr);
}
//-----------------------------------------------------------------------------
map::~map ()
{
if (MAP_FAILED == m_addr)
return;
if (munmap (m_addr, m_size))
::util::errno_error::throw_code ();
}
//-----------------------------------------------------------------------------
map&
map::operator= (map &&rhs)
{
std::swap (m_addr, rhs.m_addr);
std::swap (m_size, rhs.m_size);
return *this;
}
///////////////////////////////////////////////////////////////////////////////
uint8_t*
map::begin (void)
{
return static_cast<uint8_t*> (m_addr);
}
//-----------------------------------------------------------------------------
uint8_t*
map::end (void)
{
return static_cast<uint8_t*> (m_addr) + m_size;
}
///////////////////////////////////////////////////////////////////////////////
void*
map::data (void)
{
return m_addr;
}
//-----------------------------------------------------------------------------
const void*
map::data (void) const
{
return m_addr;
}
///////////////////////////////////////////////////////////////////////////////
size_t
map::size (void) const
{
return m_size;
}
//-----------------------------------------------------------------------------
void
map::resize (size_t newsize, resize_t op)
{
int flags = 0;
switch (op) {
case resize_t::MOVE:
flags = flags | MREMAP_MAYMOVE;
break;
case resize_t::NOMOVE:
break;
}
auto res = mremap (m_addr, m_size, newsize, flags);
if (res == MAP_FAILED)
::util::errno_error::throw_code ();
}

62
posix/map.hpp Normal file
View File

@ -0,0 +1,62 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Copyright 2016 Danny Robson <danny@nerdcruft.net>
*/
#ifndef __CRUFT_UTIL_POSIX_HPP
#define __CRUFT_UTIL_POSIX_HPP
#include "./fd.hpp"
#include <sys/mman.h>
#include <cstdint>
namespace util::posix {
class map {
public:
map (const map&) = delete;
map (map&&);
map (size_t size, int prot, int flags);
map (size_t size, int prot, int flags, const fd&, off_t offset = 0);
~map ();
map& operator= (const map&) = delete;
map& operator= (map&&);
uint8_t* begin (void);
uint8_t* end (void);
const uint8_t* begin (void) const;
const uint8_t* end (void) const;
const uint8_t* cbegin (void) const;
const uint8_t* cend (void) const;
void* data (void);
const void* data (void) const;
void sync (void *addr, size_t len, int flags);
size_t size (void) const;
enum class resize_t { MOVE, NOMOVE };
void resize (size_t newsize, resize_t = resize_t::NOMOVE);
private:
void *m_addr;
size_t m_size;
};
}
#endif