blob: 368c253c36158b62af2c003bf0a9ba85b421f01a [file] [log] [blame]
Primiano Tuccid7d1be02017-10-30 17:41:34 +00001/*
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
Oystein Eftevaagdd727e42017-12-05 08:49:55 -080017#ifndef PERFETTO_PERFETTO_BASE_UTILS_H_
18#define PERFETTO_PERFETTO_BASE_UTILS_H_
Primiano Tuccid7d1be02017-10-30 17:41:34 +000019
20#include <errno.h>
21#include <stddef.h>
Hector Dearman798bcdf2017-10-31 16:36:46 +000022#include <stdlib.h>
Primiano Tuccid7d1be02017-10-30 17:41:34 +000023
24#define PERFETTO_EINTR(x) \
25 ({ \
26 decltype(x) eintr_wrapper_result; \
27 do { \
28 eintr_wrapper_result = (x); \
29 } while (eintr_wrapper_result == -1 && errno == EINTR); \
30 eintr_wrapper_result; \
31 })
32
33namespace perfetto {
34namespace base {
35
36template <typename T>
37constexpr size_t ArraySize(const T& array) {
38 return sizeof(array) / sizeof(array[0]);
39}
40
41template <typename... T>
42inline void ignore_result(const T&...) {}
43
Hector Dearman798bcdf2017-10-31 16:36:46 +000044// Function object which invokes 'free' on its parameter, which must be
45// a pointer. Can be used to store malloc-allocated pointers in std::unique_ptr:
46//
47// std::unique_ptr<int, base::FreeDeleter> foo_ptr(
48// static_cast<int*>(malloc(sizeof(int))));
49struct FreeDeleter {
Oystein Eftevaagdd727e42017-12-05 08:49:55 -080050 inline void operator()(void* ptr) const { free(ptr); }
Hector Dearman798bcdf2017-10-31 16:36:46 +000051};
52
Primiano Tucci9ebb8822017-11-09 18:36:25 +000053template <typename T>
54constexpr T AssumeLittleEndian(T value) {
55 static_assert(__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__,
56 "Unimplemented on big-endian archs");
57 return value;
58}
59
Primiano Tuccid7d1be02017-10-30 17:41:34 +000060} // namespace base
61} // namespace perfetto
62
Oystein Eftevaagdd727e42017-12-05 08:49:55 -080063#endif // PERFETTO_PERFETTO_BASE_UTILS_H_