11#ifndef __HELPERS_HH
22#define __HELPERS_HH
33
4- #include < cstdint>
54#include < cstdlib>
65#include < source_location>
76
109
1110namespace xamarin ::android
1211{
13- // #define ADD_WITH_OVERFLOW_CHECK(__ret_type__, __a__, __b__) xamarin::android::Helpers::add_with_overflow_check<__ret_type__>(__FILE__, __LINE__, (__a__), (__b__))
14- // #define MULTIPLY_WITH_OVERFLOW_CHECK(__ret_type__, __a__, __b__) xamarin::android::Helpers::multiply_with_overflow_check<__ret_type__>(__FILE__, __LINE__, (__a__), (__b__))
15-
1612 class [[gnu::visibility(" hidden" )]] Helpers
1713 {
1814 public:
@@ -24,32 +20,21 @@ namespace xamarin::android
2420
2521 if (__builtin_add_overflow (a, b, &ret)) [[unlikely]] {
2622 log_fatal (LOG_DEFAULT, " Integer overflow on addition at %s:%u" , sloc.file_name (), sloc.line ());
27- abort_application (DoNotLogLocation);
23+ abort_application (DoNotLogLocation, sloc );
2824 }
2925
3026 return ret;
3127 }
3228
33- // Can't use templates as above with add_with_oveflow because of a bug in the clang compiler
34- // shipped with the NDK:
35- //
36- // https://github.com/android-ndk/ndk/issues/294
37- // https://github.com/android-ndk/ndk/issues/295
38- // https://bugs.llvm.org/show_bug.cgi?id=16404
39- //
40- // Using templated parameter types for `a` and `b` would make clang generate that tries to
41- // use 128-bit integers and thus output code that calls `__muloti4` and so linking would
42- // fail
43- //
44- template <typename Ret>
45- force_inline static Ret multiply_with_overflow_check (size_t a, size_t b, std::source_location sloc = std::source_location::current ()) noexcept
29+ template <typename Ret, typename P1, typename P2>
30+ force_inline static Ret multiply_with_overflow_check (P1 a, P2 b, std::source_location sloc = std::source_location::current ()) noexcept
4631 {
4732 constexpr bool DoNotLogLocation = false ;
4833 Ret ret;
4934
5035 if (__builtin_mul_overflow (a, b, &ret)) [[unlikely]] {
5136 log_fatal (LOG_DEFAULT, " Integer overflow on multiplication at %s:%u" , sloc.file_name (), sloc.line ());
52- abort_application (DoNotLogLocation);
37+ abort_application (DoNotLogLocation, sloc );
5338 }
5439
5540 return ret;
0 commit comments