-
Notifications
You must be signed in to change notification settings - Fork 544
/
Copy pathhelpers.hh
100 lines (81 loc) · 3.67 KB
/
helpers.hh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#ifndef __HELPERS_HH
#define __HELPERS_HH
#include <cstdlib>
#include <source_location>
#include <string>
#include <string_view>
#include <java-interop-util.h>
#include "platform-compat.hh"
using namespace std::string_view_literals;
namespace xamarin::android
{
namespace detail {
template<typename T>
concept TPointer = requires { std::is_pointer_v<T>; };
}
class [[gnu::visibility("hidden")]] Helpers
{
public:
template<typename Ret, typename P1, typename P2>
force_inline static Ret add_with_overflow_check (P1 a, P2 b, std::source_location sloc = std::source_location::current ()) noexcept
{
constexpr bool DoNotLogLocation = false;
Ret ret;
if (__builtin_add_overflow (a, b, &ret)) [[unlikely]] {
// It will leak memory, but it's fine, we're exiting the app anyway
char *message = nullptr;
int n = asprintf (&message, "Integer overflow on addition at %s:%u", sloc.file_name (), sloc.line ());
abort_application (n == -1 ? "Integer overflow on addition" : message, DoNotLogLocation, sloc);
}
return ret;
}
template<typename Ret, typename P1, typename P2>
force_inline static Ret multiply_with_overflow_check (P1 a, P2 b, std::source_location sloc = std::source_location::current ()) noexcept
{
constexpr bool DoNotLogLocation = false;
Ret ret;
if (__builtin_mul_overflow (a, b, &ret)) [[unlikely]] {
// It will leak memory, but it's fine, we're exiting the app anyway
char *message = nullptr;
int n = asprintf (&message, "Integer overflow on multiplication at %s:%u", sloc.file_name (), sloc.line ());
abort_application (n == -1 ? "Integer overflow on multiplication" : message, DoNotLogLocation, sloc);
}
return ret;
}
[[noreturn]] static void abort_application (LogCategories category, const char *message, bool log_location = true, std::source_location sloc = std::source_location::current ()) noexcept;
[[noreturn]] static void abort_application (LogCategories category, std::string const& message, bool log_location = true, std::source_location sloc = std::source_location::current ()) noexcept
{
abort_application (category, message.c_str (), log_location, sloc);
}
[[noreturn]] static void abort_application (LogCategories category, std::string_view const& message, bool log_location = true, std::source_location sloc = std::source_location::current ()) noexcept
{
abort_application (category, message.data (), log_location, sloc);
}
[[noreturn]] static void abort_application (const char *message, bool log_location = true, std::source_location sloc = std::source_location::current ()) noexcept
{
abort_application (LOG_DEFAULT, message, log_location, sloc);
}
[[noreturn]] static void abort_application (std::string const& message, bool log_location = true, std::source_location sloc = std::source_location::current ()) noexcept
{
abort_application (LOG_DEFAULT, message.c_str (), log_location, sloc);
}
[[noreturn]] static void abort_application (std::string_view const& message, bool log_location = true, std::source_location sloc = std::source_location::current ()) noexcept
{
abort_application (LOG_DEFAULT, message.data (), log_location, sloc);
}
};
template<detail::TPointer TRet = void*, detail::TPointer TPtr> [[gnu::always_inline]]
static inline constexpr auto pointer_add (TPtr ptr, size_t offset) noexcept -> TRet
{
return reinterpret_cast<TRet>(reinterpret_cast<uintptr_t>(ptr) + offset);
}
[[gnu::always_inline]]
static inline constexpr auto optional_string (const char* s, const char *replacement = nullptr) noexcept -> const char*
{
if (s != nullptr) [[likely]] {
return s;
}
return replacement == nullptr ? "<null>" : replacement;
}
}
#endif // __HELPERS_HH