1 //===-- xray_utils.h --------------------------------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file is a part of XRay, a dynamic runtime instrumentation system.
11 // Some shared utilities for the XRay runtime implementation.
13 //===----------------------------------------------------------------------===//
19 #include <sys/types.h>
22 #include "sanitizer_common/sanitizer_common.h"
24 #include <zircon/types.h>
32 LogWriter(zx_handle_t Vmo
) : Vmo(Vmo
) {}
34 explicit LogWriter(int Fd
) : Fd(Fd
) {}
38 // Write a character range into a log.
39 void WriteAll(const char *Begin
, const char *End
);
43 // Returns a new log instance initialized using the flag-provided values.
44 static LogWriter
*Open();
45 // Closes and deallocates the log instance.
46 static void Close(LogWriter
*LogWriter
);
50 zx_handle_t Vmo
= ZX_HANDLE_INVALID
;
57 constexpr size_t gcd(size_t a
, size_t b
) {
58 return (b
== 0) ? a
: gcd(b
, a
% b
);
61 constexpr size_t lcm(size_t a
, size_t b
) { return a
* b
/ gcd(a
, b
); }
63 constexpr size_t nearest_boundary(size_t number
, size_t multiple
) {
64 return multiple
* ((number
/ multiple
) + (number
% multiple
? 1 : 0));
67 constexpr size_t next_pow2_helper(size_t num
, size_t acc
) {
68 return (1u << acc
) >= num
? (1u << acc
) : next_pow2_helper(num
, acc
+ 1);
71 constexpr size_t next_pow2(size_t number
) {
72 return next_pow2_helper(number
, 1);
75 template <class T
> constexpr T
&max(T
&A
, T
&B
) { return A
> B
? A
: B
; }
77 template <class T
> constexpr T
&min(T
&A
, T
&B
) { return A
<= B
? A
: B
; }
79 constexpr ptrdiff_t diff(uintptr_t A
, uintptr_t B
) {
80 return max(A
, B
) - min(A
, B
);
85 #endif // XRAY_UTILS_H