blob: 3b8b8ff89efc3682d9d022da6e5ce02633a55041 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2010 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 */
Carl Shapiro6c21dc12011-06-20 15:20:52 -070016
David Sehr67bf42e2018-02-26 16:43:04 -080017#ifndef ART_LIBARTBASE_BASE_MACROS_H_
18#define ART_LIBARTBASE_BASE_MACROS_H_
Carl Shapiro6c21dc12011-06-20 15:20:52 -070019
Carl Shapiro12eb78e2011-06-24 14:51:06 -070020#include <stddef.h> // for size_t
Mark Salyzyn47a4cc72014-05-22 16:27:06 -070021#include <unistd.h> // for TEMP_FAILURE_RETRY
22
Krzysztof Kosiński737b81f2023-04-30 02:59:54 +000023#include "android-base/format.h"
Andreas Gampeaaadff82016-08-29 09:53:48 -070024#include "android-base/macros.h"
Andreas Gamped38374e2016-08-31 13:53:13 -070025#include "android-base/thread_annotations.h"
Carl Shapiro12eb78e2011-06-24 14:51:06 -070026
Ian Rogers6f3dbba2014-10-14 17:41:57 -070027// Declare a friend relationship in a class with a test. Used rather that FRIEND_TEST to avoid
28// globally importing gtest/gtest.h into the main ART header files.
29#define ART_FRIEND_TEST(test_set_name, individual_test)\
30friend class test_set_name##_##individual_test##_Test
31
Zheng Xuad4450e2015-04-17 18:48:56 +080032// Declare a friend relationship in a class with a typed test.
33#define ART_FRIEND_TYPED_TEST(test_set_name, individual_test)\
34template<typename T> ART_FRIEND_TEST(test_set_name, individual_test)
35
Krzysztof Kosiński737b81f2023-04-30 02:59:54 +000036// Shorthand for formatting with compile time checking of the format string
37#define ART_FORMAT(str, ...) ::fmt::format(FMT_STRING(str), __VA_ARGS__)
38
Ian Rogerscf7f1912014-10-22 22:06:39 -070039// A macro to disallow new and delete operators for a class. It goes in the private: declarations.
Vladimir Marko76c92ac2015-09-17 15:39:16 +010040// NOTE: Providing placement new (and matching delete) for constructing container elements.
Ian Rogerscf7f1912014-10-22 22:06:39 -070041#define DISALLOW_ALLOCATION() \
42 public: \
Andreas Gampe65b798e2015-04-06 09:35:22 -070043 NO_RETURN ALWAYS_INLINE void operator delete(void*, size_t) { UNREACHABLE(); } \
Vladimir Marko76c92ac2015-09-17 15:39:16 +010044 ALWAYS_INLINE void* operator new(size_t, void* ptr) noexcept { return ptr; } \
45 ALWAYS_INLINE void operator delete(void*, void*) noexcept { } \
Ian Rogerscf7f1912014-10-22 22:06:39 -070046 private: \
Roland Levillain7cbd27f2016-08-11 23:53:33 +010047 void* operator new(size_t) = delete // NOLINT
Ian Rogerscf7f1912014-10-22 22:06:39 -070048
David Srbecky56de89a2018-10-01 15:32:20 +010049// offsetof is not defined by the spec on types with non-standard layout,
50// however it is implemented by compilers in practice.
51// (note that reinterpret_cast is not valid constexpr)
52//
53// Alternative approach would be something like:
54// #define OFFSETOF_HELPER(t, f) \
55// (reinterpret_cast<uintptr_t>(&reinterpret_cast<t*>(16)->f) - static_cast<uintptr_t>(16u))
56// #define OFFSETOF_MEMBER(t, f) \
57// (__builtin_constant_p(OFFSETOF_HELPER(t,f)) ? OFFSETOF_HELPER(t,f) : OFFSETOF_HELPER(t,f))
58#define OFFSETOF_MEMBER(t, f) offsetof(t, f)
Carl Shapiro59e85cd2011-06-21 10:16:23 -070059
Vladimir Marko46817b82016-03-29 12:21:58 +010060#define OFFSETOF_MEMBERPTR(t, f) \
Roland Levillain7cbd27f2016-08-11 23:53:33 +010061 (reinterpret_cast<uintptr_t>(&(reinterpret_cast<t*>(16)->*f)) - static_cast<uintptr_t>(16)) // NOLINT
Elliott Hughes93e74e82011-09-13 11:07:03 -070062
David Srbecky912f36c2018-09-08 12:22:58 +010063#define ALIGNED(x) __attribute__ ((__aligned__(x)))
Brian Carlstromb1eba212013-07-17 18:07:19 -070064#define PACKED(x) __attribute__ ((__aligned__(x), __packed__))
Elliott Hughes85d15452011-09-16 17:33:01 -070065
Dave Allison70202782013-10-22 17:52:19 -070066// Stringify the argument.
67#define QUOTE(x) #x
68#define STRINGIFY(x) QUOTE(x)
69
Mathieu Chartiera7f6b812017-12-11 13:34:29 -080070// Append tokens after evaluating.
71#define APPEND_TOKENS_AFTER_EVAL_2(a, b) a ## b
72#define APPEND_TOKENS_AFTER_EVAL(a, b) APPEND_TOKENS_AFTER_EVAL_2(a, b)
73
Ian Rogerse8ae0dc2013-02-07 10:20:45 -080074#ifndef NDEBUG
Ian Rogers1ffa32f2013-02-05 18:29:08 -080075#define ALWAYS_INLINE
David Srbecky27351be2019-07-12 13:39:34 +010076#define FLATTEN
Ian Rogers1ffa32f2013-02-05 18:29:08 -080077#else
Christian Wailese8efdaa2021-05-26 17:33:54 +000078#define ALWAYS_INLINE __attribute__ ((always_inline))
David Srbecky27351be2019-07-12 13:39:34 +010079#define FLATTEN __attribute__ ((flatten))
Ian Rogers1ffa32f2013-02-05 18:29:08 -080080#endif
81
Chris Wailes9f61e0a2021-05-12 17:16:50 -070082#define NO_STACK_PROTECTOR __attribute__ ((no_stack_protector))
83
Andreas Gampe9231f4e2016-08-23 17:35:19 -070084// clang doesn't like attributes on lambda functions. It would be nice to say:
85// #define ALWAYS_INLINE_LAMBDA ALWAYS_INLINE
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +010086#define ALWAYS_INLINE_LAMBDA
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +010087
Andreas Gampe86830382014-12-12 21:41:29 -080088#define NO_INLINE __attribute__ ((noinline))
89
Anwar Ghuloum63937db2013-05-24 09:08:32 -070090#if defined (__APPLE__)
Anwar Ghuloum1d9314c2013-05-24 10:44:48 -070091#define HOT_ATTR
Ian Rogers8d31bbd2013-10-13 10:44:14 -070092#define COLD_ATTR
Anwar Ghuloum63937db2013-05-24 09:08:32 -070093#else
Anwar Ghuloum1d9314c2013-05-24 10:44:48 -070094#define HOT_ATTR __attribute__ ((hot))
Ian Rogers8d31bbd2013-10-13 10:44:14 -070095#define COLD_ATTR __attribute__ ((cold))
Anwar Ghuloum63937db2013-05-24 09:08:32 -070096#endif
97
Ian Rogers96faf5b2013-08-09 22:05:32 -070098#define PURE __attribute__ ((__pure__))
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070099
100// Define that a position within code is unreachable, for example:
101// int foo () { LOG(FATAL) << "Don't call me"; UNREACHABLE(); }
102// without the UNREACHABLE a return statement would be necessary.
Ian Rogers07140832014-09-30 15:43:59 -0700103#define UNREACHABLE __builtin_unreachable
Elliott Hughesc151f902012-06-21 20:33:21 -0700104
Andreas Gampe794ad762015-02-23 08:12:24 -0800105// Add the C++11 noreturn attribute.
106#define NO_RETURN [[ noreturn ]] // NOLINT[whitespace/braces] [5]
107
Andreas Gamped38374e2016-08-31 13:53:13 -0700108// Annotalysis thread-safety analysis support. Things that are not in base.
Elliott Hughesf8349362012-06-18 15:00:06 -0700109
Mathieu Chartier90443472015-07-16 20:32:27 -0700110#define LOCKABLE CAPABILITY("mutex")
111#define SHARED_LOCKABLE SHARED_CAPABILITY("mutex")
112
Dmitrii Ishcheikin5668bf62023-11-15 00:16:51 +0000113// Some of the libs (e.g. libarttest(d)) require more public symbols when built
114// in debug configuration.
115// Using symbol visibility only for release builds allows to reduce the list of
116// exported symbols and eliminates the need to check debug build configurations
117// when changing the exported symbols.
118#ifdef NDEBUG
Vladimir Marko8ffaef92021-05-13 12:51:30 +0000119#define HIDDEN __attribute__((visibility("hidden")))
Dmitrii Ishcheikin8cd02092024-01-30 15:22:45 +0000120#define PROTECTED __attribute__((visibility("protected")))
Vladimir Marko8ffaef92021-05-13 12:51:30 +0000121#define EXPORT __attribute__((visibility("default")))
Dmitrii Ishcheikin5668bf62023-11-15 00:16:51 +0000122#else
123#define HIDDEN
Dmitrii Ishcheikin8cd02092024-01-30 15:22:45 +0000124#define PROTECTED
Dmitrii Ishcheikin5668bf62023-11-15 00:16:51 +0000125#define EXPORT
126#endif
Vladimir Marko8ffaef92021-05-13 12:51:30 +0000127
Dmitrii Ishcheikin8cd02092024-01-30 15:22:45 +0000128// Protected symbols must be declared with "protected" visibility attribute when
129// building the library and "default" visibility when referred to from external
130// libraries/binaries. Otherwise, the external code will expect the symbol to be
131// defined locally and fail to link.
132#ifdef BUILDING_LIBART
133#define LIBART_PROTECTED PROTECTED
134#else
135#define LIBART_PROTECTED EXPORT
136#endif
137
Ruben Ayrapetyanb343f662023-11-20 18:09:32 +0000138// Some global variables shouldn't be visible outside libraries declaring them.
139// The attribute allows hiding them, so preventing direct access.
140#define ALWAYS_HIDDEN __attribute__((visibility("hidden")))
141
David Sehr67bf42e2018-02-26 16:43:04 -0800142#endif // ART_LIBARTBASE_BASE_MACROS_H_