diff options
| author | 2017-02-14 16:02:32 +0000 | |
|---|---|---|
| committer | 2017-02-14 16:02:33 +0000 | |
| commit | 2a46b682b8fd0276c62e82a56a1f2ac63b210a95 (patch) | |
| tree | fca44701850a648a243d496dc547bab0e3ef5cd0 /runtime/utils.h | |
| parent | 2d98ba68f13dc219c088a12f369c5778bf398f14 (diff) | |
| parent | c069a30d42aefd902c20e8bc09dfad1683f07ded (diff) | |
Merge "ART: invoke-custom support"
Diffstat (limited to 'runtime/utils.h')
| -rw-r--r-- | runtime/utils.h | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/runtime/utils.h b/runtime/utils.h index 67438b5881..96e5bfa8ec 100644 --- a/runtime/utils.h +++ b/runtime/utils.h @@ -301,6 +301,30 @@ constexpr PointerSize ConvertToPointerSize(T any) { } } +// Returns a type cast pointer if object pointed to is within the provided bounds. +// Otherwise returns nullptr. +template <typename T> +inline static T BoundsCheckedCast(const void* pointer, + const void* lower, + const void* upper) { + const uint8_t* bound_begin = static_cast<const uint8_t*>(lower); + const uint8_t* bound_end = static_cast<const uint8_t*>(upper); + DCHECK(bound_begin <= bound_end); + + T result = reinterpret_cast<T>(pointer); + const uint8_t* begin = static_cast<const uint8_t*>(pointer); + const uint8_t* end = begin + sizeof(*result); + if (begin < bound_begin || end > bound_end || begin > end) { + return nullptr; + } + return result; +} + +template <typename T, size_t size> +constexpr size_t ArrayCount(const T (&)[size]) { + return size; +} + } // namespace art #endif // ART_RUNTIME_UTILS_H_ |