Apollo 10.0
自动驾驶开放平台
macros.h
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2018 The Apollo Authors. All Rights Reserved.
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 CYBER_BASE_MACROS_H_
18#define CYBER_BASE_MACROS_H_
19
20#include <cstdlib>
21#include <new>
22
23#define eprosima eprosima_wrap
24
25#if __GNUC__ >= 3
26#define cyber_likely(x) (__builtin_expect((x), 1))
27#define cyber_unlikely(x) (__builtin_expect((x), 0))
28#else
29#define cyber_likely(x) (x)
30#define cyber_unlikely(x) (x)
31#endif
32
33#define CACHELINE_SIZE 64
34
35#define DEFINE_TYPE_TRAIT(name, func) \
36 template <typename T> \
37 struct name { \
38 template <typename Class> \
39 static constexpr bool Test(decltype(&Class::func)*) { \
40 return true; \
41 } \
42 template <typename> \
43 static constexpr bool Test(...) { \
44 return false; \
45 } \
46 \
47 static constexpr bool value = Test<T>(nullptr); \
48 }; \
49 \
50 template <typename T> \
51 constexpr bool name<T>::value;
52
53inline void cpu_relax() {
54#if defined(__aarch64__)
55 asm volatile("yield" ::: "memory");
56#else
57 asm volatile("rep; nop" ::: "memory");
58#endif
59}
60
61inline void* CheckedMalloc(size_t size) {
62 void* ptr = std::malloc(size);
63 if (!ptr) {
64 throw std::bad_alloc();
65 }
66 return ptr;
67}
68
69inline void* CheckedCalloc(size_t num, size_t size) {
70 void* ptr = std::calloc(num, size);
71 if (!ptr) {
72 throw std::bad_alloc();
73 }
74 return ptr;
75}
76
77#endif // CYBER_BASE_MACROS_H_
void * CheckedMalloc(size_t size)
Definition macros.h:61
void cpu_relax()
Definition macros.h:53
void * CheckedCalloc(size_t num, size_t size)
Definition macros.h:69