blob: 292be2022a569d25dc62be64435b29d867933943 [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
Brian Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_RUNTIME_BASE_MACROS_H_
18#define ART_RUNTIME_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"
Carl Shapiro12eb78e2011-06-24 14:51:06 -070024
Ian Rogers6fac4472014-02-25 17:01:10 -080025#define OVERRIDE override
26#define FINAL final
Ian Rogers6fac4472014-02-25 17:01:10 -080027
Ian Rogers6f3dbba2014-10-14 17:41:57 -070028// Declare a friend relationship in a class with a test. Used rather that FRIEND_TEST to avoid
29// globally importing gtest/gtest.h into the main ART header files.
30#define ART_FRIEND_TEST(test_set_name, individual_test)\
31friend class test_set_name##_##individual_test##_Test
32
Zheng Xuad4450e2015-04-17 18:48:56 +080033// Declare a friend relationship in a class with a typed test.
34#define ART_FRIEND_TYPED_TEST(test_set_name, individual_test)\
35template<typename T> ART_FRIEND_TEST(test_set_name, individual_test)
36
Ian Rogerscf7f1912014-10-22 22:06:39 -070037// A macro to disallow new and delete operators for a class. It goes in the private: declarations.
Vladimir Marko76c92ac2015-09-17 15:39:16 +010038// NOTE: Providing placement new (and matching delete) for constructing container elements.
Ian Rogerscf7f1912014-10-22 22:06:39 -070039#define DISALLOW_ALLOCATION() \
40 public: \
Andreas Gampe65b798e2015-04-06 09:35:22 -070041 NO_RETURN ALWAYS_INLINE void operator delete(void*, size_t) { UNREACHABLE(); } \
Vladimir Marko76c92ac2015-09-17 15:39:16 +010042 ALWAYS_INLINE void* operator new(size_t, void* ptr) noexcept { return ptr; } \
43 ALWAYS_INLINE void operator delete(void*, void*) noexcept { } \
Ian Rogerscf7f1912014-10-22 22:06:39 -070044 private: \
Roland Levillain7cbd27f2016-08-11 23:53:33 +010045 void* operator new(size_t) = delete // NOLINT
Ian Rogerscf7f1912014-10-22 22:06:39 -070046
Roland Levillain7cbd27f2016-08-11 23:53:33 +010047#define SIZEOF_MEMBER(t, f) sizeof((reinterpret_cast<t*>(4096))->f) // NOLINT
Carl Shapiro59e85cd2011-06-21 10:16:23 -070048
Elliott Hughes362f9bc2011-10-17 18:56:41 -070049#define OFFSETOF_MEMBER(t, f) \
Roland Levillain7cbd27f2016-08-11 23:53:33 +010050 (reinterpret_cast<uintptr_t>(&reinterpret_cast<t*>(16)->f) - static_cast<uintptr_t>(16u)) // NOLINT
Carl Shapiro59e85cd2011-06-21 10:16:23 -070051
Vladimir Marko46817b82016-03-29 12:21:58 +010052#define OFFSETOF_MEMBERPTR(t, f) \
Roland Levillain7cbd27f2016-08-11 23:53:33 +010053 (reinterpret_cast<uintptr_t>(&(reinterpret_cast<t*>(16)->*f)) - static_cast<uintptr_t>(16)) // NOLINT
Elliott Hughes93e74e82011-09-13 11:07:03 -070054
Brian Carlstromb1eba212013-07-17 18:07:19 -070055#define PACKED(x) __attribute__ ((__aligned__(x), __packed__))
Elliott Hughes85d15452011-09-16 17:33:01 -070056
Dave Allison70202782013-10-22 17:52:19 -070057// Stringify the argument.
58#define QUOTE(x) #x
59#define STRINGIFY(x) QUOTE(x)
60
Ian Rogerse8ae0dc2013-02-07 10:20:45 -080061#ifndef NDEBUG
Ian Rogers1ffa32f2013-02-05 18:29:08 -080062#define ALWAYS_INLINE
63#else
Ian Rogerse8ae0dc2013-02-07 10:20:45 -080064#define ALWAYS_INLINE __attribute__ ((always_inline))
Ian Rogers1ffa32f2013-02-05 18:29:08 -080065#endif
66
Andreas Gampe9231f4e2016-08-23 17:35:19 -070067// clang doesn't like attributes on lambda functions. It would be nice to say:
68// #define ALWAYS_INLINE_LAMBDA ALWAYS_INLINE
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +010069#define ALWAYS_INLINE_LAMBDA
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +010070
Andreas Gampe86830382014-12-12 21:41:29 -080071#define NO_INLINE __attribute__ ((noinline))
72
Anwar Ghuloum63937db2013-05-24 09:08:32 -070073#if defined (__APPLE__)
Anwar Ghuloum1d9314c2013-05-24 10:44:48 -070074#define HOT_ATTR
Ian Rogers8d31bbd2013-10-13 10:44:14 -070075#define COLD_ATTR
Anwar Ghuloum63937db2013-05-24 09:08:32 -070076#else
Anwar Ghuloum1d9314c2013-05-24 10:44:48 -070077#define HOT_ATTR __attribute__ ((hot))
Ian Rogers8d31bbd2013-10-13 10:44:14 -070078#define COLD_ATTR __attribute__ ((cold))
Anwar Ghuloum63937db2013-05-24 09:08:32 -070079#endif
80
Ian Rogers96faf5b2013-08-09 22:05:32 -070081#define PURE __attribute__ ((__pure__))
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070082
83// Define that a position within code is unreachable, for example:
84// int foo () { LOG(FATAL) << "Don't call me"; UNREACHABLE(); }
85// without the UNREACHABLE a return statement would be necessary.
Ian Rogers07140832014-09-30 15:43:59 -070086#define UNREACHABLE __builtin_unreachable
Elliott Hughesc151f902012-06-21 20:33:21 -070087
Andreas Gampe794ad762015-02-23 08:12:24 -080088// Add the C++11 noreturn attribute.
89#define NO_RETURN [[ noreturn ]] // NOLINT[whitespace/braces] [5]
90
Ian Rogers719d1a32014-03-06 12:13:39 -080091// Annotalysis thread-safety analysis support.
Elliott Hughesf8349362012-06-18 15:00:06 -070092
Andreas Gampe9231f4e2016-08-23 17:35:19 -070093#define ACQUIRED_AFTER(...) __attribute__((acquired_after(__VA_ARGS__)))
94#define ACQUIRED_BEFORE(...) __attribute__((acquired_before(__VA_ARGS__)))
95#define GUARDED_BY(x) __attribute__((guarded_by(x)))
96#define GUARDED_VAR __attribute__((guarded))
97#define LOCK_RETURNED(x) __attribute__((lock_returned(x)))
98#define NO_THREAD_SAFETY_ANALYSIS __attribute__((no_thread_safety_analysis))
Elliott Hughesf8349362012-06-18 15:00:06 -070099#define PT_GUARDED_BY(x)
Ian Rogers719d1a32014-03-06 12:13:39 -0800100// THREAD_ANNOTATION_ATTRIBUTE__(point_to_guarded_by(x))
Andreas Gampe9231f4e2016-08-23 17:35:19 -0700101#define PT_GUARDED_VAR __attribute__((point_to_guarded))
102#define SCOPED_LOCKABLE __attribute__((scoped_lockable))
Elliott Hughesf8349362012-06-18 15:00:06 -0700103
Andreas Gampe9231f4e2016-08-23 17:35:19 -0700104#define EXCLUSIVE_LOCK_FUNCTION(...) __attribute__((exclusive_lock_function(__VA_ARGS__)))
105#define EXCLUSIVE_TRYLOCK_FUNCTION(...) __attribute__((exclusive_trylock_function(__VA_ARGS__)))
106#define SHARED_LOCK_FUNCTION(...) __attribute__((shared_lock_function(__VA_ARGS__)))
107#define SHARED_TRYLOCK_FUNCTION(...) __attribute__((shared_trylock_function(__VA_ARGS__)))
108#define UNLOCK_FUNCTION(...) __attribute__((unlock_function(__VA_ARGS__)))
109#define REQUIRES(...) __attribute__((requires_capability(__VA_ARGS__)))
110#define SHARED_REQUIRES(...) __attribute__((requires_shared_capability(__VA_ARGS__)))
111#define CAPABILITY(...) __attribute__((capability(__VA_ARGS__)))
112#define SHARED_CAPABILITY(...) __attribute__((shared_capability(__VA_ARGS__)))
113#define ASSERT_CAPABILITY(...) __attribute__((assert_capability(__VA_ARGS__)))
114#define ASSERT_SHARED_CAPABILITY(...) __attribute__((assert_shared_capability(__VA_ARGS__)))
115#define RETURN_CAPABILITY(...) __attribute__((lock_returned(__VA_ARGS__)))
116#define TRY_ACQUIRE(...) __attribute__((try_acquire_capability(__VA_ARGS__)))
117#define TRY_ACQUIRE_SHARED(...) __attribute__((try_acquire_shared_capability(__VA_ARGS__)))
118#define ACQUIRE(...) __attribute__((acquire_capability(__VA_ARGS__)))
119#define ACQUIRE_SHARED(...) __attribute__((acquire_shared_capability(__VA_ARGS__)))
120#define RELEASE(...) __attribute__((release_capability(__VA_ARGS__)))
121#define RELEASE_SHARED(...) __attribute__((release_shared_capability(__VA_ARGS__)))
122#define SCOPED_CAPABILITY __attribute__((scoped_lockable))
Elliott Hughesf8349362012-06-18 15:00:06 -0700123
Mathieu Chartier90443472015-07-16 20:32:27 -0700124#define LOCKABLE CAPABILITY("mutex")
125#define SHARED_LOCKABLE SHARED_CAPABILITY("mutex")
126
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700127#endif // ART_RUNTIME_BASE_MACROS_H_