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>
23 #include "sanitizer_common/sanitizer_common.h"
25 #include <zircon/types.h>
33 LogWriter(zx_handle_t Vmo) : Vmo(Vmo) {}
35 explicit LogWriter(int Fd) : Fd(Fd) {}
39 // Write a character range into a log.
40 void WriteAll(const char *Begin, const char *End);
44 // Returns a new log instance initialized using the flag-provided values.
45 static LogWriter *Open();
46 // Closes and deallocates the log instance.
47 static void Close(LogWriter *LogWriter);
51 zx_handle_t Vmo = ZX_HANDLE_INVALID;
58 constexpr size_t gcd(size_t a, size_t b) {
59 return (b == 0) ? a : gcd(b, a % b);
62 constexpr size_t lcm(size_t a, size_t b) { return a * b / gcd(a, b); }
64 constexpr size_t nearest_boundary(size_t number, size_t multiple) {
65 return multiple * ((number / multiple) + (number % multiple ? 1 : 0));
68 constexpr size_t next_pow2_helper(size_t num, size_t acc) {
69 return (1u << acc) >= num ? (1u << acc) : next_pow2_helper(num, acc + 1);
72 constexpr size_t next_pow2(size_t number) {
73 return next_pow2_helper(number, 1);
76 template <class T> constexpr T &max(T &A, T &B) { return A > B ? A : B; }
78 template <class T> constexpr T &min(T &A, T &B) { return A <= B ? A : B; }
80 constexpr ptrdiff_t diff(uintptr_t A, uintptr_t B) {
81 return max(A, B) - min(A, B);
86 #endif // XRAY_UTILS_H