Skip to content

Commit

Permalink
graphite2: update gralloc in Main.h
Browse files Browse the repository at this point in the history
- With newer compiler / libraries, there is error G47DF2280: argument
1 value ‘18446744073709551560’ exceeds maximum object size
9223372036854775807 [-Werror=alloc-size-larger-than=]
- Copied in the newer implementation from graphite.git.

Change-Id: I904fff90c7cce9331aeb19c4854b47541c498df0
  • Loading branch information
marksvc committed Jan 3, 2023
1 parent 11884e4 commit a95f116
Showing 1 changed file with 48 additions and 2 deletions.
50 changes: 48 additions & 2 deletions Lib/src/graphite2/src/inc/Main.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,61 @@ class telemetry::category
struct telemetry {};
#endif

// Checked multiplaction to catch overflow or underflow when allocating memory
#if defined(__has_builtin)
#if __has_builtin(__builtin_mul_overflow)
#define HAVE_BUILTIN_OVERFLOW
#endif
#elif defined(__GNUC__) && (__GNUC__ >= 5) && !defined(__INTEL_COMPILER)
#define HAVE_BUILTIN_OVERFLOW
#endif
#if defined(__has_include)
#if __has_include(<intsafe.h>) && !defined(__CYGWIN__)
#define HAVE_INTSAFE_H
#endif
#elif defined(_WIN32)
#define HAVE_INTSAFE_H
#endif

// Need to import intsafe into the top level namespace
#if defined(HAVE_INTSAFE_H)
} // namespace graphite2

#include <intsafe.h>

namespace graphite2 {
#endif

#if defined(HAVE_BUILTIN_OVERFLOW)
inline
bool checked_mul(const size_t a, const size_t b, size_t & t) {
return __builtin_mul_overflow(a, b, &t);
}
#elif defined(HAVE_INTSAFE_H)
inline
bool checked_mul(const size_t a, const size_t b, size_t & t) {
return SizeTMult(a, b, &t) == INTSAFE_E_ARITHMETIC_OVERFLOW;
}
#else
inline
bool checked_mul(const size_t a, const size_t b, size_t & t) {
t = a*b;
return (((a | b) & (~size_t(0) << (sizeof(size_t) << 2))) && (t / a != b));
}
#endif

// typesafe wrapper around malloc for simple types
// use free(pointer) to deallocate

template <typename T> T * gralloc(size_t n)
{
size_t total;
if (checked_mul(n, sizeof(T), total))
return 0;
#ifdef GRAPHITE2_TELEMETRY
telemetry::count_bytes(sizeof(T) * n);
telemetry::count_bytes(total);
#endif
return static_cast<T*>(malloc(sizeof(T) * n));
return static_cast<T*>(malloc(total));
}

template <typename T> T * grzeroalloc(size_t n)
Expand Down

0 comments on commit a95f116

Please sign in to comment.