1 //===-- xray_utils.h --------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file is a part of XRay, a dynamic runtime instrumentation system.
12 // Some shared utilities for the XRay runtime implementation.
14 //===----------------------------------------------------------------------===//
20 #include <sys/types.h>
25 // Default implementation of the reporting interface for sanitizer errors.
26 void printToStdErr(const char *Buffer);
28 // EINTR-safe write routine, provided a file descriptor and a character range.
29 void retryingWriteAll(int Fd, const char *Begin, const char *End);
31 // Reads a long long value from a provided file.
32 bool readValueFromFile(const char *Filename, long long *Value);
34 // EINTR-safe read routine, providing a file descriptor and a character range.
35 std::pair<ssize_t, bool> retryingReadSome(int Fd, char *Begin, char *End);
37 // EINTR-safe open routine, uses flag-provided values for initialising a log
41 constexpr size_t gcd(size_t a, size_t b) {
42 return (b == 0) ? a : gcd(b, a % b);
45 constexpr size_t lcm(size_t a, size_t b) { return a * b / gcd(a, b); }
47 constexpr size_t nearest_boundary(size_t number, size_t multiple) {
48 return multiple * ((number / multiple) + (number % multiple ? 1 : 0));
51 constexpr size_t next_pow2_helper(size_t num, size_t acc) {
52 return (1u << acc) >= num ? (1u << acc) : next_pow2_helper(num, acc + 1);
55 constexpr size_t next_pow2(size_t number) {
56 return next_pow2_helper(number, 1);
59 template <class T> constexpr T &max(T &A, T &B) { return A > B ? A : B; }
61 template <class T> constexpr T &min(T &A, T &B) { return A <= B ? A : B; }
63 constexpr ptrdiff_t diff(uintptr_t A, uintptr_t B) {
64 return max(A, B) - min(A, B);
69 #endif // XRAY_UTILS_H