一呼百應, "one call, a hundred responses"
Loading...
Searching...
No Matches
memory.hpp
Go to the documentation of this file.
1#pragma once
2
11#include <type_traits>
12#include <cstring> // strdup
13#include <memory>
14#include <sys/mman.h> // munmap
15#include "attributes/common.hpp"
18
19namespace ein {
20
22template<typename T>
23concept trivially_destructible = std::is_trivially_destructible_v<std::remove_extent_t<T>>;
24
26struct c_munmap {
27 size_t size;
32 void operator()(void * p) const noexcept {
33 if (p != nullptr)
34 munmap(p, size);
35 }
36};
37
39using mmap_ptr = std::unique_ptr<void,c_munmap>;
40
45mmap_ptr make_mmap_ptr(void * p, size_t size) noexcept {
46 return mmap_ptr(p,c_munmap(size));
47}
48
50struct c_free {
52 template <trivially_destructible T>
54 static void operator()(T * p) noexcept {
55 std::free(const_cast<std::remove_const_t<T>*>(p));
56 }
57};
58
60template<trivially_destructible T>
61using unique_c_ptr = std::unique_ptr<T, c_free>;
62
63static_assert(sizeof(char *)== sizeof(unique_c_ptr<char>),"");
64
67
75 ein_noescape char const * str
76) noexcept {
77 return unique_str { strdup(str) };
78}
79
80} // ein
81
82#if defined(EIN_TESTING) || defined(EIN_TESTING_MEMORY)
83TEST_CASE("memory","[memory]") {
84 using namespace ein;
85
86 SECTION("unique_str manages strdup-allocated strings") {
87 const char* original = "Hello, World!";
88 unique_str managed_str = dup(original);
89
90 CHECK(managed_str != nullptr);
91 CHECK(std::strcmp(managed_str.get(), original) == 0);
92 }
93
94 SECTION("unique_c_ptr manages malloc-allocated memory") {
95 constexpr size_t size = 1024;
96 ein::unique_c_ptr<char> managed_ptr(static_cast<char*>(std::malloc(size)));
97 CHECK(managed_ptr);
98 //std::memset(managed_ptr.get(), 0, size);
99 }
100
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);
105
106 mmap_ptr managed_mmap = make_mmap_ptr(ptr, size);
107 CHECK(managed_mmap != nullptr);
108 }
109}
110#endif
a type where the destructor does no work, and therefore it is safe to simply discard.
Definition memory.hpp:23
#define ein_artificial
[[artificial]].
Definition common.hpp:220
#define ein_inline
inline [[always_inline]]
Definition common.hpp:188
#define ein_noescape
portable __attribute__((noescape))
Definition common.hpp:151
#define ein_nodiscard
C++17 [[nodiscard]].
Definition common.hpp:165
#define ein_nonnull(...)
portable [[nonnnull(...)]]
#define ein_pure
[[pure]]
Definition common.hpp:102
#define ein_null_terminated_string_arg(x)
The N th argumnt is a null terminated string.
Definition strings.hpp:37
Definition cpuid.cpp:16
unique_str dup(char const *str) noexcept
duplicate a C string using strdup and manage it as a unique_str
Definition memory.hpp:74
std::unique_ptr< T, c_free > unique_c_ptr
a unique_ptr managed c pointer, deleted by free()
Definition memory.hpp:61
mmap_ptr make_mmap_ptr(void *p, size_t size) noexcept
construct a mmap_ptr using a base pointer and its size for munmap
Definition memory.hpp:45
unique_c_ptr< char const > unique_str
a c string, managed by unique_ptr
Definition memory.hpp:66
std::unique_ptr< void, c_munmap > mmap_ptr
memory mapped data, managed by std::unique_ptr. calls munmap to free.
Definition memory.hpp:39
std::unique_ptr Deleter that calls free()
Definition memory.hpp:50
static void operator()(T *p) noexcept
Definition memory.hpp:54
std::unique_ptr Deleter for memory mapped data
Definition memory.hpp:26
void operator()(void *p) const noexcept
Definition memory.hpp:32
size_t size
Definition memory.hpp:27