]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/compiler-rt/lib/xray/xray_utils.h
Merge clang 7.0.1 and several follow-up changes
[FreeBSD/FreeBSD.git] / contrib / compiler-rt / lib / xray / xray_utils.h
1 //===-- xray_utils.h --------------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file is a part of XRay, a dynamic runtime instrumentation system.
11 //
12 // Some shared utilities for the XRay runtime implementation.
13 //
14 //===----------------------------------------------------------------------===//
15 #ifndef XRAY_UTILS_H
16 #define XRAY_UTILS_H
17
18 #include <cstddef>
19 #include <cstdint>
20 #include <sys/types.h>
21 #include <utility>
22
23 namespace __xray {
24
25 // Default implementation of the reporting interface for sanitizer errors.
26 void printToStdErr(const char *Buffer);
27
28 // EINTR-safe write routine, provided a file descriptor and a character range.
29 void retryingWriteAll(int Fd, const char *Begin, const char *End);
30
31 // Reads a long long value from a provided file.
32 bool readValueFromFile(const char *Filename, long long *Value);
33
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);
36
37 // EINTR-safe open routine, uses flag-provided values for initialising a log
38 // file.
39 int getLogFD();
40
41 constexpr size_t gcd(size_t a, size_t b) {
42   return (b == 0) ? a : gcd(b, a % b);
43 }
44
45 constexpr size_t lcm(size_t a, size_t b) { return a * b / gcd(a, b); }
46
47 constexpr size_t nearest_boundary(size_t number, size_t multiple) {
48   return multiple * ((number / multiple) + (number % multiple ? 1 : 0));
49 }
50
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);
53 }
54
55 constexpr size_t next_pow2(size_t number) {
56   return next_pow2_helper(number, 1);
57 }
58
59 template <class T> constexpr T &max(T &A, T &B) { return A > B ? A : B; }
60
61 template <class T> constexpr T &min(T &A, T &B) { return A <= B ? A : B; }
62
63 constexpr ptrdiff_t diff(uintptr_t A, uintptr_t B) {
64   return max(A, B) - min(A, B);
65 }
66
67 } // namespace __xray
68
69 #endif // XRAY_UTILS_H