summaryrefslogtreecommitdiff
path: root/lib/xray/xray_utils.h
blob: 60438973fbd05e7efc60a9e7d53adc04c8be1081 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
//===-- xray_utils.h --------------------------------------------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file is a part of XRay, a dynamic runtime instrumentation system.
//
// Some shared utilities for the XRay runtime implementation.
//
//===----------------------------------------------------------------------===//
#ifndef XRAY_UTILS_H
#define XRAY_UTILS_H

#include <cstddef>
#include <cstdint>
#include <sys/types.h>
#include <utility>

#include "sanitizer_common/sanitizer_common.h"
#if SANITIZER_FUCHSIA
#include <zircon/types.h>
#endif

namespace __xray {

class LogWriter {
public:
#if SANITIZER_FUCHSIA
 LogWriter(zx_handle_t Vmo) : Vmo(Vmo) {}
#else
  explicit LogWriter(int Fd) : Fd(Fd) {}
#endif
 ~LogWriter();

 // Write a character range into a log.
 void WriteAll(const char *Begin, const char *End);

 void Flush();

 // Returns a new log instance initialized using the flag-provided values.
 static LogWriter *Open();
 // Closes and deallocates the log instance.
 static void Close(LogWriter *LogWriter);

private:
#if SANITIZER_FUCHSIA
 zx_handle_t Vmo = ZX_HANDLE_INVALID;
 uint64_t Offset = 0;
#else
 int Fd = -1;
#endif
};

constexpr size_t gcd(size_t a, size_t b) {
  return (b == 0) ? a : gcd(b, a % b);
}

constexpr size_t lcm(size_t a, size_t b) { return a * b / gcd(a, b); }

constexpr size_t nearest_boundary(size_t number, size_t multiple) {
  return multiple * ((number / multiple) + (number % multiple ? 1 : 0));
}

constexpr size_t next_pow2_helper(size_t num, size_t acc) {
  return (1u << acc) >= num ? (1u << acc) : next_pow2_helper(num, acc + 1);
}

constexpr size_t next_pow2(size_t number) {
  return next_pow2_helper(number, 1);
}

template <class T> constexpr T &max(T &A, T &B) { return A > B ? A : B; }

template <class T> constexpr T &min(T &A, T &B) { return A <= B ? A : B; }

constexpr ptrdiff_t diff(uintptr_t A, uintptr_t B) {
  return max(A, B) - min(A, B);
}

} // namespace __xray

#endif // XRAY_UTILS_H