mmap atomic
parent
97990401e2
commit
a0a855463d
|
@ -1,6 +1,6 @@
|
||||||
cmake_minimum_required(VERSION 3.20)
|
cmake_minimum_required(VERSION 3.20)
|
||||||
include(cmake/color.cmake)
|
include(cmake/color.cmake)
|
||||||
set(BLT_VERSION 0.18.35)
|
set(BLT_VERSION 0.18.36)
|
||||||
set(BLT_TEST_VERSION 0.0.1)
|
set(BLT_TEST_VERSION 0.0.1)
|
||||||
|
|
||||||
set(BLT_TARGET BLT)
|
set(BLT_TARGET BLT)
|
||||||
|
|
|
@ -25,19 +25,12 @@
|
||||||
#include <blt/std/utility.h>
|
#include <blt/std/utility.h>
|
||||||
#include <blt/std/types.h>
|
#include <blt/std/types.h>
|
||||||
#include <blt/std/assert.h>
|
#include <blt/std/assert.h>
|
||||||
// TODO: remove
|
#include <blt/std/mmap.h>
|
||||||
//#include <blt/std/hashmap.h>
|
|
||||||
#include <blt/compatibility.h>
|
#include <blt/compatibility.h>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
#include "logging.h"
|
#include "logging.h"
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
|
|
||||||
#ifdef __unix__
|
|
||||||
|
|
||||||
#include <sys/mman.h>
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
||||||
namespace blt
|
namespace blt
|
||||||
{
|
{
|
||||||
|
|
||||||
|
@ -543,71 +536,6 @@ namespace blt
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// size of 2mb in bytes
|
|
||||||
inline constexpr blt::size_t BLT_2MB_SIZE = 4096 * 512;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Logging function used for handling mmap errors. call after a failed mmap call.
|
|
||||||
* @param LOG_FUNC function to log with, must be a BLT_*_STREAM
|
|
||||||
*/
|
|
||||||
template<typename LOG_FUNC>
|
|
||||||
static void handle_mmap_error(LOG_FUNC func = BLT_ERROR_STREAM)
|
|
||||||
{
|
|
||||||
#define BLT_WRITE(arg) func << arg << '\n';
|
|
||||||
switch (errno)
|
|
||||||
{
|
|
||||||
case EACCES:
|
|
||||||
BLT_WRITE("fd not set to open!");
|
|
||||||
break;
|
|
||||||
case EAGAIN:
|
|
||||||
BLT_WRITE("The file has been locked, or too much memory has been locked");
|
|
||||||
break;
|
|
||||||
case EBADF:
|
|
||||||
BLT_WRITE("fd is not a valid file descriptor");
|
|
||||||
break;
|
|
||||||
case EEXIST:
|
|
||||||
BLT_WRITE("MAP_FIXED_NOREPLACE was specified in flags, and the range covered "
|
|
||||||
"by addr and length clashes with an existing mapping.");
|
|
||||||
break;
|
|
||||||
case EINVAL:
|
|
||||||
BLT_WRITE("We don't like addr, length, or offset (e.g., they are too large, or not aligned on a page boundary).");
|
|
||||||
BLT_WRITE("Or length was 0");
|
|
||||||
BLT_WRITE("Or flags contained none of MAP_PRIVATE, MAP_SHARED, or MAP_SHARED_VALIDATE.");
|
|
||||||
break;
|
|
||||||
case ENFILE:
|
|
||||||
BLT_WRITE("The system-wide limit on the total number of open files has been reached.");
|
|
||||||
break;
|
|
||||||
case ENODEV:
|
|
||||||
BLT_WRITE("The underlying filesystem of the specified file does not support memory mapping.");
|
|
||||||
break;
|
|
||||||
case ENOMEM:
|
|
||||||
BLT_WRITE("No memory is available.");
|
|
||||||
BLT_WRITE("Or The process's maximum number of mappings would have been exceeded. "
|
|
||||||
"This error can also occur for munmap(), when unmapping a region in the middle of an existing mapping, "
|
|
||||||
"since this results in two smaller mappings on either side of the region being unmapped.");
|
|
||||||
BLT_WRITE("Or The process's RLIMIT_DATA limit, described in getrlimit(2), would have been exceeded.");
|
|
||||||
BLT_WRITE("Or We don't like addr, because it exceeds the virtual address space of the CPU.");
|
|
||||||
break;
|
|
||||||
case EOVERFLOW:
|
|
||||||
BLT_WRITE("On 32-bit architecture together with the large file extension (i.e., using 64-bit off_t): "
|
|
||||||
"the number of pages used for length plus number of "
|
|
||||||
"pages used for offset would overflow unsigned long (32 bits).");
|
|
||||||
break;
|
|
||||||
case EPERM:
|
|
||||||
BLT_WRITE("The prot argument asks for PROT_EXEC but the mapped area "
|
|
||||||
"belongs to a file on a filesystem that was mounted no-exec.");
|
|
||||||
BLT_WRITE("Or The operation_t was prevented by a file seal");
|
|
||||||
BLT_WRITE("Or The MAP_HUGETLB flag was specified, but the caller "
|
|
||||||
"was not privileged (did not have the CAP_IPC_LOCK capability) "
|
|
||||||
"and is not a member of the sysctl_hugetlb_shm_group group; "
|
|
||||||
"see the description of /proc/sys/vm/sysctl_hugetlb_shm_group");
|
|
||||||
break;
|
|
||||||
case ETXTBSY:
|
|
||||||
BLT_WRITE("MAP_DENYWRITE was set but the object specified by fd is open for writing.");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename T, bool WARN_ON_FAIL = false>
|
template<typename T, bool WARN_ON_FAIL = false>
|
||||||
static inline T* allocate_huge_page(blt::size_t BLOCK_SIZE, blt::size_t HUGE_PAGE_SIZE = BLT_2MB_SIZE)
|
static inline T* allocate_huge_page(blt::size_t BLOCK_SIZE, blt::size_t HUGE_PAGE_SIZE = BLT_2MB_SIZE)
|
||||||
{
|
{
|
||||||
|
|
|
@ -0,0 +1,43 @@
|
||||||
|
#pragma once
|
||||||
|
/*
|
||||||
|
* Copyright (C) 2024 Brett Terpstra
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef BLT_ATOMIC_ALLOCATOR_H
|
||||||
|
#define BLT_ATOMIC_ALLOCATOR_H
|
||||||
|
|
||||||
|
#include <optional>
|
||||||
|
#include <limits>
|
||||||
|
#include <vector>
|
||||||
|
#include <blt/std/ranges.h>
|
||||||
|
#include <blt/std/utility.h>
|
||||||
|
#include <blt/std/types.h>
|
||||||
|
#include <blt/std/assert.h>
|
||||||
|
#include <blt/std/mmap.h>
|
||||||
|
#include <blt/compatibility.h>
|
||||||
|
#include <stdexcept>
|
||||||
|
#include "logging.h"
|
||||||
|
#include <cstdlib>
|
||||||
|
|
||||||
|
|
||||||
|
namespace blt
|
||||||
|
{
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif //BLT_ATOMIC_ALLOCATOR_H
|
|
@ -0,0 +1,51 @@
|
||||||
|
#pragma once
|
||||||
|
/*
|
||||||
|
* Copyright (C) 2024 Brett Terpstra
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef BLT_MMAP_H
|
||||||
|
#define BLT_MMAP_H
|
||||||
|
|
||||||
|
#include <blt/std/logging.h>
|
||||||
|
#include <blt/std/types.h>
|
||||||
|
|
||||||
|
#ifdef __unix__
|
||||||
|
|
||||||
|
#include <sys/mman.h>
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// size of 2mb in bytes
|
||||||
|
inline constexpr blt::size_t BLT_2MB_SIZE = 4096 * 512;
|
||||||
|
|
||||||
|
namespace blt
|
||||||
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Logging function used for handling mmap errors. call after a failed mmap call.
|
||||||
|
*/
|
||||||
|
void handle_mmap_error(blt::logging::logger func);
|
||||||
|
|
||||||
|
inline static constexpr blt::size_t align_to_size(blt::size_t in, blt::size_t align)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void* allocate_2mb_huge_pages(blt::size_t bytes);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif //BLT_MMAP_H
|
|
@ -1 +1 @@
|
||||||
Subproject commit d88c5e15079047777b418132ece5879e7c9aaa2b
|
Subproject commit 8a889d3699b3c09ade435641fb034427f3fd12b6
|
|
@ -0,0 +1,81 @@
|
||||||
|
/*
|
||||||
|
* <Short Description>
|
||||||
|
* Copyright (C) 2024 Brett Terpstra
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
#include <blt/std/mmap.h>
|
||||||
|
|
||||||
|
namespace blt
|
||||||
|
{
|
||||||
|
|
||||||
|
void handle_mmap_error(blt::logging::logger func)
|
||||||
|
{
|
||||||
|
{
|
||||||
|
#define BLT_WRITE(arg) func << arg << '\n';
|
||||||
|
switch (errno)
|
||||||
|
{
|
||||||
|
case EACCES:
|
||||||
|
BLT_WRITE("fd not set to open!");
|
||||||
|
break;
|
||||||
|
case EAGAIN:
|
||||||
|
BLT_WRITE("The file has been locked, or too much memory has been locked");
|
||||||
|
break;
|
||||||
|
case EBADF:
|
||||||
|
BLT_WRITE("fd is not a valid file descriptor");
|
||||||
|
break;
|
||||||
|
case EEXIST:
|
||||||
|
BLT_WRITE("MAP_FIXED_NOREPLACE was specified in flags, and the range covered "
|
||||||
|
"by addr and length clashes with an existing mapping.");
|
||||||
|
break;
|
||||||
|
case EINVAL:
|
||||||
|
BLT_WRITE("We don't like addr, length, or offset (e.g., they are too large, or not aligned on a page boundary).");
|
||||||
|
BLT_WRITE("Or length was 0");
|
||||||
|
BLT_WRITE("Or flags contained none of MAP_PRIVATE, MAP_SHARED, or MAP_SHARED_VALIDATE.");
|
||||||
|
break;
|
||||||
|
case ENFILE:
|
||||||
|
BLT_WRITE("The system-wide limit on the total number of open files has been reached.");
|
||||||
|
break;
|
||||||
|
case ENODEV:
|
||||||
|
BLT_WRITE("The underlying filesystem of the specified file does not support memory mapping.");
|
||||||
|
break;
|
||||||
|
case ENOMEM:
|
||||||
|
BLT_WRITE("No memory is available.");
|
||||||
|
BLT_WRITE("Or The process's maximum number of mappings would have been exceeded. "
|
||||||
|
"This error can also occur for munmap(), when unmapping a region in the middle of an existing mapping, "
|
||||||
|
"since this results in two smaller mappings on either side of the region being unmapped.");
|
||||||
|
BLT_WRITE("Or The process's RLIMIT_DATA limit, described in getrlimit(2), would have been exceeded.");
|
||||||
|
BLT_WRITE("Or We don't like addr, because it exceeds the virtual address space of the CPU.");
|
||||||
|
break;
|
||||||
|
case EOVERFLOW:
|
||||||
|
BLT_WRITE("On 32-bit architecture together with the large file extension (i.e., using 64-bit off_t): "
|
||||||
|
"the number of pages used for length plus number of "
|
||||||
|
"pages used for offset would overflow unsigned long (32 bits).");
|
||||||
|
break;
|
||||||
|
case EPERM:
|
||||||
|
BLT_WRITE("The prot argument asks for PROT_EXEC but the mapped area "
|
||||||
|
"belongs to a file on a filesystem that was mounted no-exec.");
|
||||||
|
BLT_WRITE("Or The operation_t was prevented by a file seal");
|
||||||
|
BLT_WRITE("Or The MAP_HUGETLB flag was specified, but the caller "
|
||||||
|
"was not privileged (did not have the CAP_IPC_LOCK capability) "
|
||||||
|
"and is not a member of the sysctl_hugetlb_shm_group group; "
|
||||||
|
"see the description of /proc/sys/vm/sysctl_hugetlb_shm_group");
|
||||||
|
break;
|
||||||
|
case ETXTBSY:
|
||||||
|
BLT_WRITE("MAP_DENYWRITE was set but the object specified by fd is open for writing.");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue