native 0.0.1
Vectors, masks and wide register packs for C++26
Loading...
Searching...
No Matches
native.memory.ccm
1module;
2// SPDX-FileCopyrightText: 2024 Edward Kmett <ekmett@gmail.com>
3// SPDX-License-Identifier: BSD-2-Clause OR Apache-2.0
4#include <type_traits>
5#include <cstdlib>
6#include <cstring>
7#include <memory>
8#include "native/attributes.h"
9
10#if defined(__unix__) || defined(__APPLE__)
11#include <sys/mman.h>
12#endif
13
14export module native.memory;
15
21export namespace native {
22
25 template<typename T>
26 concept trivially_destructible = std::is_trivially_destructible_v<std::remove_extent_t<T>>;
27
30 struct c_free {
34 template <trivially_destructible T>
36 static void operator()(T * p) noexcept {
37 std::free(const_cast<std::remove_const_t<T>*>(p));
38 }
39 };
40
43 template<trivially_destructible T>
44 using unique_c_ptr = std::unique_ptr<T, c_free>;
45
46 static_assert(sizeof(char *)== sizeof(unique_c_ptr<char>),"");
47
51
61 native_noescape char const * str
62 ) noexcept {
63 auto const size = std::strlen(str) + 1;
64 auto * copy = static_cast<char *>(std::malloc(size));
65 if (copy == nullptr) return {};
66 std::memcpy(copy, str, size);
67 return unique_str{copy};
68 }
69
70} // native
71
72#if defined(__unix__) || defined(__APPLE__)
73export namespace native {
74
77 struct c_munmap {
78 std::size_t size;
84 void operator()(void * p) const noexcept {
85 if (p != nullptr)
86 munmap(p, size);
87 }
88 };
89
92 using mmap_ptr = std::unique_ptr<void,c_munmap>;
93
100 mmap_ptr make_mmap_ptr(void * p, std::size_t size) noexcept {
101 return mmap_ptr(p,c_munmap(size));
102 }
103
104} // native
105#endif
Compiler attributes for host code, with shader-safe shared modifiers.
#define native_artificial
[[artificial]].
Definition attributes.h:245
#define native_inline
inline [[always_inline]]
Definition attributes.h:212
#define native_noescape
portable __attribute__((noescape))
Definition attributes.h:175
#define native_nodiscard
C++17 [[nodiscard]].
Definition attributes.h:189
std::unique_ptr< T, c_free > unique_c_ptr
unique_c_ptr< char const > unique_str
unique_str dup(char const *str) noexcept
#define native_nonnull(...)
portable [[nonnnull(...)]]
#define native_null_terminated_string_arg(x)
The N th argumnt is a null terminated string.
Architecture-tagged vectors, register packs and supporting value types. Native arithmetic follows its...
static void operator()(T *p) noexcept