blob: 6cd7d60253ba29473e3db4d8eae0daf982b0f631 [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"
Andreas Gamped38374e2016-08-31 13:53:13 -070024#include "android-base/thread_annotations.h"
Carl Shapiro12eb78e2011-06-24 14:51:06 -070025
Ian Rogers6fac4472014-02-25 17:01:10 -080026#define OVERRIDE override
27#define FINAL final
Ian Rogers6fac4472014-02-25 17:01:10 -080028
Ian Rogers6f3dbba2014-10-14 17:41:57 -070029// Declare a friend relationship in a class with a test. Used rather that FRIEND_TEST to avoid
30// globally importing gtest/gtest.h into the main ART header files.
31#define ART_FRIEND_TEST(test_set_name, individual_test)\
32friend class test_set_name##_##individual_test##_Test
33
Zheng Xuad4450e2015-04-17 18:48:56 +080034// Declare a friend relationship in a class with a typed test.
35#define ART_FRIEND_TYPED_TEST(test_set_name, individual_test)\
36template<typename T> ART_FRIEND_TEST(test_set_name, individual_test)
37
Ian Rogerscf7f1912014-10-22 22:06:39 -070038// A macro to disallow new and delete operators for a class. It goes in the private: declarations.
Vladimir Marko76c92ac2015-09-17 15:39:16 +010039// NOTE: Providing placement new (and matching delete) for constructing container elements.
Ian Rogerscf7f1912014-10-22 22:06:39 -070040#define DISALLOW_ALLOCATION() \
41 public: \
Andreas Gampe65b798e2015-04-06 09:35:22 -070042 NO_RETURN ALWAYS_INLINE void operator delete(void*, size_t) { UNREACHABLE(); } \
Vladimir Marko76c92ac2015-09-17 15:39:16 +010043 ALWAYS_INLINE void* operator new(size_t, void* ptr) noexcept { return ptr; } \
44 ALWAYS_INLINE void operator delete(void*, void*) noexcept { } \
Ian Rogerscf7f1912014-10-22 22:06:39 -070045 private: \
Roland Levillain7cbd27f2016-08-11 23:53:33 +010046 void* operator new(size_t) = delete // NOLINT
Ian Rogerscf7f1912014-10-22 22:06:39 -070047
Roland Levillain7cbd27f2016-08-11 23:53:33 +010048#define SIZEOF_MEMBER(t, f) sizeof((reinterpret_cast<t*>(4096))->f) // NOLINT
Carl Shapiro59e85cd2011-06-21 10:16:23 -070049
Elliott Hughes362f9bc2011-10-17 18:56:41 -070050#define OFFSETOF_MEMBER(t, f) \
Roland Levillain7cbd27f2016-08-11 23:53:33 +010051 (reinterpret_cast<uintptr_t>(&reinterpret_cast<t*>(16)->f) - static_cast<uintptr_t>(16u)) // NOLINT
Carl Shapiro59e85cd2011-06-21 10:16:23 -070052
Vladimir Marko46817b82016-03-29 12:21:58 +010053#define OFFSETOF_MEMBERPTR(t, f) \
Roland Levillain7cbd27f2016-08-11 23:53:33 +010054 (reinterpret_cast<uintptr_t>(&(reinterpret_cast<t*>(16)->*f)) - static_cast<uintptr_t>(16)) // NOLINT
Elliott Hughes93e74e82011-09-13 11:07:03 -070055
Brian Carlstromb1eba212013-07-17 18:07:19 -070056#define PACKED(x) __attribute__ ((__aligned__(x), __packed__))
Elliott Hughes85d15452011-09-16 17:33:01 -070057
Dave Allison70202782013-10-22 17:52:19 -070058// Stringify the argument.
59#define QUOTE(x) #x
60#define STRINGIFY(x) QUOTE(x)
61
Ian Rogerse8ae0dc2013-02-07 10:20:45 -080062#ifndef NDEBUG
Ian Rogers1ffa32f2013-02-05 18:29:08 -080063#define ALWAYS_INLINE
64#else
Ian Rogerse8ae0dc2013-02-07 10:20:45 -080065#define ALWAYS_INLINE __attribute__ ((always_inline))
Ian Rogers1ffa32f2013-02-05 18:29:08 -080066#endif
67
Andreas Gampe9231f4e2016-08-23 17:35:19 -070068// clang doesn't like attributes on lambda functions. It would be nice to say:
69// #define ALWAYS_INLINE_LAMBDA ALWAYS_INLINE
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +010070#define ALWAYS_INLINE_LAMBDA
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +010071
Andreas Gampe86830382014-12-12 21:41:29 -080072#define NO_INLINE __attribute__ ((noinline))
73
Anwar Ghuloum63937db2013-05-24 09:08:32 -070074#if defined (__APPLE__)
Anwar Ghuloum1d9314c2013-05-24 10:44:48 -070075#define HOT_ATTR
Ian Rogers8d31bbd2013-10-13 10:44:14 -070076#define COLD_ATTR
Anwar Ghuloum63937db2013-05-24 09:08:32 -070077#else
Anwar Ghuloum1d9314c2013-05-24 10:44:48 -070078#define HOT_ATTR __attribute__ ((hot))
Ian Rogers8d31bbd2013-10-13 10:44:14 -070079#define COLD_ATTR __attribute__ ((cold))
Anwar Ghuloum63937db2013-05-24 09:08:32 -070080#endif
81
Ian Rogers96faf5b2013-08-09 22:05:32 -070082#define PURE __attribute__ ((__pure__))
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070083
84// Define that a position within code is unreachable, for example:
85// int foo () { LOG(FATAL) << "Don't call me"; UNREACHABLE(); }
86// without the UNREACHABLE a return statement would be necessary.
Ian Rogers07140832014-09-30 15:43:59 -070087#define UNREACHABLE __builtin_unreachable
Elliott Hughesc151f902012-06-21 20:33:21 -070088
Andreas Gampe794ad762015-02-23 08:12:24 -080089// Add the C++11 noreturn attribute.
90#define NO_RETURN [[ noreturn ]] // NOLINT[whitespace/braces] [5]
91
Andreas Gamped38374e2016-08-31 13:53:13 -070092// Annotalysis thread-safety analysis support. Things that are not in base.
Elliott Hughesf8349362012-06-18 15:00:06 -070093
Mathieu Chartier90443472015-07-16 20:32:27 -070094#define LOCKABLE CAPABILITY("mutex")
95#define SHARED_LOCKABLE SHARED_CAPABILITY("mutex")
96
Brian Carlstromfc0e3212013-07-17 14:40:12 -070097#endif // ART_RUNTIME_BASE_MACROS_H_