1 /*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #ifndef INCLUDE_PERFETTO_EXT_BASE_UTILS_H_
18 #define INCLUDE_PERFETTO_EXT_BASE_UTILS_H_
19
20 #include "perfetto/base/build_config.h"
21 #include "perfetto/base/compiler.h"
22
23 #include <errno.h>
24 #include <stddef.h>
25 #include <stdlib.h>
26 #if !PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
27 #include <sys/types.h>
28 #endif
29
30 #if PERFETTO_BUILDFLAG(PERFETTO_OS_LINUX) || \
31 PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID)
32 #include <unistd.h> // For getpagesize().
33 #elif PERFETTO_BUILDFLAG(PERFETTO_OS_APPLE)
34 #include <mach/vm_page_size.h>
35 #endif
36
37 #include <atomic>
38
39 #define PERFETTO_EINTR(x) \
40 ([&] { \
41 decltype(x) eintr_wrapper_result; \
42 do { \
43 eintr_wrapper_result = (x); \
44 } while (eintr_wrapper_result == -1 && errno == EINTR); \
45 return eintr_wrapper_result; \
46 }())
47
48 #if PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
49 // TODO(brucedawson) - create a ::perfetto::base::IOSize to replace this.
50 #if defined(_WIN64)
51 using ssize_t = __int64;
52 #else
53 using ssize_t = long;
54 #endif
55 #endif
56
57 namespace perfetto {
58 namespace base {
59
60 #if !PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
61 constexpr uid_t kInvalidUid = static_cast<uid_t>(-1);
62 constexpr pid_t kInvalidPid = static_cast<pid_t>(-1);
63 #endif
64
65 // Do not add new usages of kPageSize, consider using GetSysPageSize() below.
66 // TODO(primiano): over time the semantic of kPageSize became too ambiguous.
67 // Strictly speaking, this constant is incorrect on some new devices where the
68 // page size can be 16K (e.g., crbug.com/1116576). Unfortunately too much code
69 // ended up depending on kPageSize for purposes that are not strictly related
70 // with the kernel's mm subsystem.
71 constexpr size_t kPageSize = 4096;
72
73 // Returns the system's page size. Use this when dealing with mmap, madvise and
74 // similar mm-related syscalls.
GetSysPageSize()75 inline uint32_t GetSysPageSize() {
76 ignore_result(kPageSize); // Just to keep the amalgamated build happy.
77 #if PERFETTO_BUILDFLAG(PERFETTO_OS_LINUX) || \
78 PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID)
79 static std::atomic<uint32_t> page_size{0};
80 // This function might be called in hot paths. Avoid calling getpagesize() all
81 // the times, in many implementations getpagesize() calls sysconf() which is
82 // not cheap.
83 uint32_t cached_value = page_size.load(std::memory_order_relaxed);
84 if (PERFETTO_UNLIKELY(cached_value == 0)) {
85 cached_value = static_cast<uint32_t>(getpagesize());
86 page_size.store(cached_value, std::memory_order_relaxed);
87 }
88 return cached_value;
89 #elif PERFETTO_BUILDFLAG(PERFETTO_OS_APPLE)
90 return static_cast<uint32_t>(vm_page_size);
91 #else
92 return 4096;
93 #endif
94 }
95
96 template <typename T>
ArraySize(const T & array)97 constexpr size_t ArraySize(const T& array) {
98 return sizeof(array) / sizeof(array[0]);
99 }
100
101 // Function object which invokes 'free' on its parameter, which must be
102 // a pointer. Can be used to store malloc-allocated pointers in std::unique_ptr:
103 //
104 // std::unique_ptr<int, base::FreeDeleter> foo_ptr(
105 // static_cast<int*>(malloc(sizeof(int))));
106 struct FreeDeleter {
operatorFreeDeleter107 inline void operator()(void* ptr) const { free(ptr); }
108 };
109
110 template <typename T>
AssumeLittleEndian(T value)111 constexpr T AssumeLittleEndian(T value) {
112 static_assert(__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__,
113 "Unimplemented on big-endian archs");
114 return value;
115 }
116
117 // Round up |size| to a multiple of |alignment| (must be a power of two).
118 template <size_t alignment>
AlignUp(size_t size)119 constexpr size_t AlignUp(size_t size) {
120 static_assert((alignment & (alignment - 1)) == 0, "alignment must be a pow2");
121 return (size + alignment - 1) & ~(alignment - 1);
122 }
123
IsAgain(int err)124 inline bool IsAgain(int err) {
125 return err == EAGAIN || err == EWOULDBLOCK;
126 }
127
128 } // namespace base
129 } // namespace perfetto
130
131 #endif // INCLUDE_PERFETTO_EXT_BASE_UTILS_H_
132