39using mmap_ptr = std::unique_ptr<void,c_munmap>;
52 template <trivially_destructible T>
55 std::free(
const_cast<std::remove_const_t<T>*
>(p));
60template<trivially_destructible T>
82#if defined(EIN_TESTING) || defined(EIN_TESTING_MEMORY)
83TEST_CASE(
"memory",
"[memory]") {
86 SECTION(
"unique_str manages strdup-allocated strings") {
87 const char* original =
"Hello, World!";
90 CHECK(managed_str !=
nullptr);
91 CHECK(std::strcmp(managed_str.get(), original) == 0);
94 SECTION(
"unique_c_ptr manages malloc-allocated memory") {
95 constexpr size_t size = 1024;
101 SECTION(
"mmap_ptr manages mmap-allocated memory") {
102 constexpr size_t size = 4096;
103 void* ptr = mmap(
nullptr, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
104 CHECK(ptr != MAP_FAILED);
107 CHECK(managed_mmap !=
nullptr);
a type where the destructor does no work, and therefore it is safe to simply discard.
#define ein_artificial
[[artificial]].
#define ein_inline
inline [[always_inline]]
#define ein_noescape
portable __attribute__((noescape))
#define ein_nodiscard
C++17 [[nodiscard]].
#define ein_nonnull(...)
portable [[nonnnull(...)]]
#define ein_null_terminated_string_arg(x)
The N th argumnt is a null terminated string.
unique_str dup(char const *str) noexcept
duplicate a C string using strdup and manage it as a unique_str
std::unique_ptr< T, c_free > unique_c_ptr
a unique_ptr managed c pointer, deleted by free()
mmap_ptr make_mmap_ptr(void *p, size_t size) noexcept
construct a mmap_ptr using a base pointer and its size for munmap
unique_c_ptr< char const > unique_str
a c string, managed by unique_ptr
std::unique_ptr< void, c_munmap > mmap_ptr
memory mapped data, managed by std::unique_ptr. calls munmap to free.
std::unique_ptr Deleter that calls free()
static void operator()(T *p) noexcept
std::unique_ptr Deleter for memory mapped data
void operator()(void *p) const noexcept