Primiano Tucci | d7d1be0 | 2017-10-30 17:41:34 +0000 | [diff] [blame] | 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 | |
Oystein Eftevaag | dd727e4 | 2017-12-05 08:49:55 -0800 | [diff] [blame^] | 17 | #ifndef PERFETTO_PERFETTO_BASE_UTILS_H_ |
| 18 | #define PERFETTO_PERFETTO_BASE_UTILS_H_ |
Primiano Tucci | d7d1be0 | 2017-10-30 17:41:34 +0000 | [diff] [blame] | 19 | |
| 20 | #include <errno.h> |
| 21 | #include <stddef.h> |
Hector Dearman | 798bcdf | 2017-10-31 16:36:46 +0000 | [diff] [blame] | 22 | #include <stdlib.h> |
Primiano Tucci | d7d1be0 | 2017-10-30 17:41:34 +0000 | [diff] [blame] | 23 | |
| 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 | |
| 33 | namespace perfetto { |
| 34 | namespace base { |
| 35 | |
| 36 | template <typename T> |
| 37 | constexpr size_t ArraySize(const T& array) { |
| 38 | return sizeof(array) / sizeof(array[0]); |
| 39 | } |
| 40 | |
| 41 | template <typename... T> |
| 42 | inline void ignore_result(const T&...) {} |
| 43 | |
Hector Dearman | 798bcdf | 2017-10-31 16:36:46 +0000 | [diff] [blame] | 44 | // 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)))); |
| 49 | struct FreeDeleter { |
Oystein Eftevaag | dd727e4 | 2017-12-05 08:49:55 -0800 | [diff] [blame^] | 50 | inline void operator()(void* ptr) const { free(ptr); } |
Hector Dearman | 798bcdf | 2017-10-31 16:36:46 +0000 | [diff] [blame] | 51 | }; |
| 52 | |
Primiano Tucci | 9ebb882 | 2017-11-09 18:36:25 +0000 | [diff] [blame] | 53 | template <typename T> |
| 54 | constexpr T AssumeLittleEndian(T value) { |
| 55 | static_assert(__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__, |
| 56 | "Unimplemented on big-endian archs"); |
| 57 | return value; |
| 58 | } |
| 59 | |
Primiano Tucci | d7d1be0 | 2017-10-30 17:41:34 +0000 | [diff] [blame] | 60 | } // namespace base |
| 61 | } // namespace perfetto |
| 62 | |
Oystein Eftevaag | dd727e4 | 2017-12-05 08:49:55 -0800 | [diff] [blame^] | 63 | #endif // PERFETTO_PERFETTO_BASE_UTILS_H_ |