• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #define PERFETTO_EINTR(x)                                   \
31   ([&] {                                                    \
32     decltype(x) eintr_wrapper_result;                       \
33     do {                                                    \
34       eintr_wrapper_result = (x);                           \
35     } while (eintr_wrapper_result == -1 && errno == EINTR); \
36     return eintr_wrapper_result;                            \
37   }())
38 
39 #if PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
40 // TODO(brucedawson) - create a ::perfetto::base::IOSize to replace this.
41 #if defined(_WIN64)
42 using ssize_t = __int64;
43 #else
44 using ssize_t = long;
45 #endif
46 #endif
47 
48 namespace perfetto {
49 namespace base {
50 
51 #if !PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
52 constexpr uid_t kInvalidUid = static_cast<uid_t>(-1);
53 constexpr pid_t kInvalidPid = static_cast<pid_t>(-1);
54 #endif
55 
56 constexpr size_t kPageSize = 4096;
57 
58 template <typename T>
ArraySize(const T & array)59 constexpr size_t ArraySize(const T& array) {
60   return sizeof(array) / sizeof(array[0]);
61 }
62 
63 // Function object which invokes 'free' on its parameter, which must be
64 // a pointer. Can be used to store malloc-allocated pointers in std::unique_ptr:
65 //
66 // std::unique_ptr<int, base::FreeDeleter> foo_ptr(
67 //     static_cast<int*>(malloc(sizeof(int))));
68 struct FreeDeleter {
operatorFreeDeleter69   inline void operator()(void* ptr) const { free(ptr); }
70 };
71 
72 template <typename T>
AssumeLittleEndian(T value)73 constexpr T AssumeLittleEndian(T value) {
74   static_assert(__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__,
75                 "Unimplemented on big-endian archs");
76   return value;
77 }
78 
79 // Round up |size| to a multiple of |alignment| (must be a power of two).
80 template <size_t alignment>
AlignUp(size_t size)81 constexpr size_t AlignUp(size_t size) {
82   static_assert((alignment & (alignment - 1)) == 0, "alignment must be a pow2");
83   return (size + alignment - 1) & ~(alignment - 1);
84 }
85 
IsAgain(int err)86 inline bool IsAgain(int err) {
87   return err == EAGAIN || err == EWOULDBLOCK;
88 }
89 
90 }  // namespace base
91 }  // namespace perfetto
92 
93 #endif  // INCLUDE_PERFETTO_EXT_BASE_UTILS_H_
94