blob: eec73cb6990194e7e0a5849d5fc9c8d289c95607 [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
Andreas Gampeaaadff82016-08-29 09:53:48 -070023#include "android-base/macros.h"
Andreas Gamped38374e2016-08-31 13:53:13 -070024#include "android-base/thread_annotations.h"
Carl Shapiro12eb78e2011-06-24 14:51:06 -070025
Ian Rogers6f3dbba2014-10-14 17:41:57 -070026// Declare a friend relationship in a class with a test. Used rather that FRIEND_TEST to avoid
27// globally importing gtest/gtest.h into the main ART header files.
28#define ART_FRIEND_TEST(test_set_name, individual_test)\
29friend class test_set_name##_##individual_test##_Test
30
Zheng Xuad4450e2015-04-17 18:48:56 +080031// Declare a friend relationship in a class with a typed test.
32#define ART_FRIEND_TYPED_TEST(test_set_name, individual_test)\
33template<typename T> ART_FRIEND_TEST(test_set_name, individual_test)
34
Ian Rogerscf7f1912014-10-22 22:06:39 -070035// A macro to disallow new and delete operators for a class. It goes in the private: declarations.
Vladimir Marko76c92ac2015-09-17 15:39:16 +010036// NOTE: Providing placement new (and matching delete) for constructing container elements.
Ian Rogerscf7f1912014-10-22 22:06:39 -070037#define DISALLOW_ALLOCATION() \
38 public: \
Andreas Gampe65b798e2015-04-06 09:35:22 -070039 NO_RETURN ALWAYS_INLINE void operator delete(void*, size_t) { UNREACHABLE(); } \
Vladimir Marko76c92ac2015-09-17 15:39:16 +010040 ALWAYS_INLINE void* operator new(size_t, void* ptr) noexcept { return ptr; } \
41 ALWAYS_INLINE void operator delete(void*, void*) noexcept { } \
Ian Rogerscf7f1912014-10-22 22:06:39 -070042 private: \
Roland Levillain7cbd27f2016-08-11 23:53:33 +010043 void* operator new(size_t) = delete // NOLINT
Ian Rogerscf7f1912014-10-22 22:06:39 -070044
David Srbecky56de89a2018-10-01 15:32:20 +010045// offsetof is not defined by the spec on types with non-standard layout,
46// however it is implemented by compilers in practice.
47// (note that reinterpret_cast is not valid constexpr)
48//
49// Alternative approach would be something like:
50// #define OFFSETOF_HELPER(t, f) \
51// (reinterpret_cast<uintptr_t>(&reinterpret_cast<t*>(16)->f) - static_cast<uintptr_t>(16u))
52// #define OFFSETOF_MEMBER(t, f) \
53// (__builtin_constant_p(OFFSETOF_HELPER(t,f)) ? OFFSETOF_HELPER(t,f) : OFFSETOF_HELPER(t,f))
54#define OFFSETOF_MEMBER(t, f) offsetof(t, f)
Carl Shapiro59e85cd2011-06-21 10:16:23 -070055
Vladimir Marko46817b82016-03-29 12:21:58 +010056#define OFFSETOF_MEMBERPTR(t, f) \
Roland Levillain7cbd27f2016-08-11 23:53:33 +010057 (reinterpret_cast<uintptr_t>(&(reinterpret_cast<t*>(16)->*f)) - static_cast<uintptr_t>(16)) // NOLINT
Elliott Hughes93e74e82011-09-13 11:07:03 -070058
David Srbecky912f36c2018-09-08 12:22:58 +010059#define ALIGNED(x) __attribute__ ((__aligned__(x)))
Brian Carlstromb1eba212013-07-17 18:07:19 -070060#define PACKED(x) __attribute__ ((__aligned__(x), __packed__))
Elliott Hughes85d15452011-09-16 17:33:01 -070061
Dave Allison70202782013-10-22 17:52:19 -070062// Stringify the argument.
63#define QUOTE(x) #x
64#define STRINGIFY(x) QUOTE(x)
65
Mathieu Chartiera7f6b812017-12-11 13:34:29 -080066// Append tokens after evaluating.
67#define APPEND_TOKENS_AFTER_EVAL_2(a, b) a ## b
68#define APPEND_TOKENS_AFTER_EVAL(a, b) APPEND_TOKENS_AFTER_EVAL_2(a, b)
69
Ian Rogerse8ae0dc2013-02-07 10:20:45 -080070#ifndef NDEBUG
Ian Rogers1ffa32f2013-02-05 18:29:08 -080071#define ALWAYS_INLINE
David Srbecky27351be2019-07-12 13:39:34 +010072#define FLATTEN
Ian Rogers1ffa32f2013-02-05 18:29:08 -080073#else
Christian Wailese8efdaa2021-05-26 17:33:54 +000074#define ALWAYS_INLINE __attribute__ ((always_inline))
David Srbecky27351be2019-07-12 13:39:34 +010075#define FLATTEN __attribute__ ((flatten))
Ian Rogers1ffa32f2013-02-05 18:29:08 -080076#endif
77
Andreas Gampe9231f4e2016-08-23 17:35:19 -070078// clang doesn't like attributes on lambda functions. It would be nice to say:
79// #define ALWAYS_INLINE_LAMBDA ALWAYS_INLINE
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +010080#define ALWAYS_INLINE_LAMBDA
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +010081
Andreas Gampe86830382014-12-12 21:41:29 -080082#define NO_INLINE __attribute__ ((noinline))
83
Anwar Ghuloum63937db2013-05-24 09:08:32 -070084#if defined (__APPLE__)
Anwar Ghuloum1d9314c2013-05-24 10:44:48 -070085#define HOT_ATTR
Ian Rogers8d31bbd2013-10-13 10:44:14 -070086#define COLD_ATTR
Anwar Ghuloum63937db2013-05-24 09:08:32 -070087#else
Anwar Ghuloum1d9314c2013-05-24 10:44:48 -070088#define HOT_ATTR __attribute__ ((hot))
Ian Rogers8d31bbd2013-10-13 10:44:14 -070089#define COLD_ATTR __attribute__ ((cold))
Anwar Ghuloum63937db2013-05-24 09:08:32 -070090#endif
91
Ian Rogers96faf5b2013-08-09 22:05:32 -070092#define PURE __attribute__ ((__pure__))
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070093
94// Define that a position within code is unreachable, for example:
95// int foo () { LOG(FATAL) << "Don't call me"; UNREACHABLE(); }
96// without the UNREACHABLE a return statement would be necessary.
Ian Rogers07140832014-09-30 15:43:59 -070097#define UNREACHABLE __builtin_unreachable
Elliott Hughesc151f902012-06-21 20:33:21 -070098
Andreas Gampe794ad762015-02-23 08:12:24 -080099// Add the C++11 noreturn attribute.
100#define NO_RETURN [[ noreturn ]] // NOLINT[whitespace/braces] [5]
101
Andreas Gamped38374e2016-08-31 13:53:13 -0700102// Annotalysis thread-safety analysis support. Things that are not in base.
Elliott Hughesf8349362012-06-18 15:00:06 -0700103
Mathieu Chartier90443472015-07-16 20:32:27 -0700104#define LOCKABLE CAPABILITY("mutex")
105#define SHARED_LOCKABLE SHARED_CAPABILITY("mutex")
106
Vladimir Marko8ffaef92021-05-13 12:51:30 +0000107#define HIDDEN __attribute__((visibility("hidden")))
108#define EXPORT __attribute__((visibility("default")))
109
David Sehr67bf42e2018-02-26 16:43:04 -0800110#endif // ART_LIBARTBASE_BASE_MACROS_H_