cleanup: reduce code bloat

This commit is contained in:
Eugene Kosov 2021-12-03 17:21:59 +06:00
parent fa1325512b
commit 0064316f19
2 changed files with 60 additions and 61 deletions

View file

@ -346,26 +346,6 @@ or the MySQL version that created the redo log file. */
header */
#define LOG_FILE_HDR_SIZE (4 * OS_FILE_LOG_BLOCK_SIZE)
/** Memory mapped file */
class mapped_file_t
{
public:
mapped_file_t()= default;
mapped_file_t(const mapped_file_t &)= delete;
mapped_file_t &operator=(const mapped_file_t &)= delete;
mapped_file_t(mapped_file_t &&)= delete;
mapped_file_t &operator=(mapped_file_t &&)= delete;
~mapped_file_t() noexcept;
dberr_t map(const char *path, bool read_only= false,
bool nvme= false) noexcept;
dberr_t unmap() noexcept;
byte *data() noexcept { return m_area.data(); }
private:
span<byte> m_area;
};
/** Abstraction for reading, writing and flushing file cache to disk */
class file_io
{

View file

@ -221,47 +221,6 @@ void log_t::create()
(aligned_malloc(OS_FILE_LOG_BLOCK_SIZE, OS_FILE_LOG_BLOCK_SIZE));
}
mapped_file_t::~mapped_file_t() noexcept
{
if (!m_area.empty())
unmap();
}
dberr_t mapped_file_t::map(const char *path, bool read_only,
bool nvme) noexcept
{
auto fd= mysql_file_open(innodb_log_file_key, path,
read_only ? O_RDONLY : O_RDWR, MYF(MY_WME));
if (fd == -1)
return DB_ERROR;
const auto file_size= os_file_get_size(path).m_total_size;
const int nvme_flag= nvme ? MAP_SYNC : 0;
void *ptr= my_mmap(0, static_cast<size_t>(file_size),
read_only ? PROT_READ : PROT_READ | PROT_WRITE,
MAP_SHARED_VALIDATE | nvme_flag, fd, 0);
mysql_file_close(fd, MYF(MY_WME));
if (ptr == MAP_FAILED)
return DB_ERROR;
m_area= {static_cast<byte *>(ptr),
static_cast<span<byte>::size_type>(file_size)};
return DB_SUCCESS;
}
dberr_t mapped_file_t::unmap() noexcept
{
ut_ad(!m_area.empty());
if (my_munmap(m_area.data(), m_area.size()))
return DB_ERROR;
m_area= {};
return DB_SUCCESS;
}
file_os_io::file_os_io(file_os_io &&rhs) : m_fd(rhs.m_fd)
{
rhs.m_fd= OS_FILE_CLOSED;
@ -331,6 +290,66 @@ dberr_t file_os_io::flush() noexcept
#include <libpmem.h>
/** Memory mapped file */
class mapped_file_t
{
public:
mapped_file_t()= default;
mapped_file_t(const mapped_file_t &)= delete;
mapped_file_t &operator=(const mapped_file_t &)= delete;
mapped_file_t(mapped_file_t &&)= delete;
mapped_file_t &operator=(mapped_file_t &&)= delete;
~mapped_file_t() noexcept;
dberr_t map(const char *path, bool read_only= false,
bool nvme= false) noexcept;
dberr_t unmap() noexcept;
byte *data() noexcept { return m_area.data(); }
private:
span<byte> m_area;
};
mapped_file_t::~mapped_file_t() noexcept
{
if (!m_area.empty())
unmap();
}
dberr_t mapped_file_t::map(const char *path, bool read_only,
bool nvme) noexcept
{
auto fd= mysql_file_open(innodb_log_file_key, path,
read_only ? O_RDONLY : O_RDWR, MYF(MY_WME));
if (fd == -1)
return DB_ERROR;
const auto file_size= size_t{os_file_get_size(path).m_total_size};
const int nvme_flag= nvme ? MAP_SYNC : 0;
void *ptr=
my_mmap(0, file_size, read_only ? PROT_READ : PROT_READ | PROT_WRITE,
MAP_SHARED_VALIDATE | nvme_flag, fd, 0);
mysql_file_close(fd, MYF(MY_WME));
if (ptr == MAP_FAILED)
return DB_ERROR;
m_area= {static_cast<byte *>(ptr), file_size};
return DB_SUCCESS;
}
dberr_t mapped_file_t::unmap() noexcept
{
ut_ad(!m_area.empty());
if (my_munmap(m_area.data(), m_area.size()))
return DB_ERROR;
m_area= {};
return DB_SUCCESS;
}
static bool is_pmem(const char *path) noexcept
{
mapped_file_t mf;