1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-28 03:17:34 +00:00

AK: Add ALWAYS_INLINE, NEVER_INLINE and FLATTEN macros

It's tedious to write (and look at) [[gnu::always_inline]] etc. :^)
This commit is contained in:
Andreas Kling 2020-04-30 11:43:25 +02:00
parent f1a8fb1e88
commit 888e35f0fe
14 changed files with 63 additions and 56 deletions

View file

@ -56,7 +56,7 @@ public:
const char* characters() const { return m_impl ? m_impl->characters() : nullptr; }
size_t length() const { return m_impl ? m_impl->length() : 0; }
[[gnu::always_inline]] inline u32 hash() const { return m_impl ? m_impl->existing_hash() : 0; }
ALWAYS_INLINE u32 hash() const { return m_impl ? m_impl->existing_hash() : 0; }
StringView view() const;

View file

@ -13,7 +13,7 @@
extern "C" void* mmx_memcpy(void* to, const void* from, size_t);
#endif
[[gnu::always_inline]] inline void fast_u32_copy(u32* dest, const u32* src, size_t count)
ALWAYS_INLINE void fast_u32_copy(u32* dest, const u32* src, size_t count)
{
#if defined(__serenity__) && !defined(KERNEL) && !defined(BOOTSTRAPPER)
if (count >= 256) {
@ -28,7 +28,7 @@ extern "C" void* mmx_memcpy(void* to, const void* from, size_t);
: "memory");
}
[[gnu::always_inline]] inline void fast_u32_fill(u32* dest, u32 value, size_t count)
ALWAYS_INLINE void fast_u32_fill(u32* dest, u32 value, size_t count)
{
asm volatile(
"rep stosl\n"

View file

@ -105,12 +105,12 @@ public:
return *this;
}
[[gnu::always_inline]] inline ~Optional()
ALWAYS_INLINE ~Optional()
{
clear();
}
[[gnu::always_inline]] inline void clear()
ALWAYS_INLINE void clear()
{
if (m_has_value) {
value().~T();
@ -119,17 +119,17 @@ public:
}
SET_TYPESTATE(consumed)
[[gnu::always_inline]] inline bool has_value() const { return m_has_value; }
ALWAYS_INLINE bool has_value() const { return m_has_value; }
CALLABLE_WHEN(consumed)
[[gnu::always_inline]] inline T& value()
ALWAYS_INLINE T& value()
{
ASSERT(m_has_value);
return *reinterpret_cast<T*>(&m_storage);
}
CALLABLE_WHEN(consumed)
[[gnu::always_inline]] inline const T& value() const
ALWAYS_INLINE const T& value() const
{
return value_without_consume_state();
}
@ -144,7 +144,7 @@ public:
return released_value;
}
[[gnu::always_inline]] inline T value_or(const T& fallback) const
ALWAYS_INLINE T value_or(const T& fallback) const
{
if (m_has_value)
return value();
@ -153,7 +153,7 @@ public:
private:
// Call when we don't want to alter the consume state
[[gnu::always_inline]] inline const T& value_without_consume_state() const
ALWAYS_INLINE const T& value_without_consume_state() const
{
ASSERT(m_has_value);
return *reinterpret_cast<const T*>(&m_storage);

View file

@ -36,6 +36,21 @@
#define ARCH(arch) (defined(AK_ARCH_##arch) && AK_ARCH_##arch)
#ifdef ALWAYS_INLINE
#undef ALWAYS_INLINE
#endif
#define ALWAYS_INLINE [[gnu::always_inline]] inline
#ifdef NEVER_INLINE
#undef NEVER_INLINE
#endif
#define NEVER_INLINE [[gnu::noinline]]
#ifdef FLATTEN
#undef FLATTEN
#endif
#define FLATTEN [[gnu::flatten]]
// FIXME: Re-enable this when we can figure out why Clang gets confused about "unknown"
#if 0
# define CONSUMABLE(initial_state) __attribute__((consumable(initial_state)))
@ -70,7 +85,7 @@ inline int open_with_path_length(const char* path, size_t path_length, int optio
#endif
template<typename T>
[[gnu::always_inline]] inline T convert_between_host_and_network(T value)
ALWAYS_INLINE T convert_between_host_and_network(T value)
{
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
if constexpr (sizeof(T) == 8)
@ -86,7 +101,7 @@ template<typename T>
#endif
}
[[gnu::always_inline]] inline int count_trailing_zeroes_32(unsigned int val)
ALWAYS_INLINE int count_trailing_zeroes_32(unsigned int val)
{
#if defined(__GNUC__) || defined(__clang__)
return __builtin_ctz(val);

View file

@ -42,7 +42,7 @@ extern "C" size_t strlen(const char*);
#endif
template<typename PutChFunc, typename T>
[[gnu::always_inline]] inline int print_hex(PutChFunc putch, char*& bufptr, T number, bool upper_case, bool alternate_form, bool left_pad, bool zeroPad, u8 width)
ALWAYS_INLINE int print_hex(PutChFunc putch, char*& bufptr, T number, bool upper_case, bool alternate_form, bool left_pad, bool zeroPad, u8 width)
{
int ret = 0;
@ -96,7 +96,7 @@ template<typename PutChFunc, typename T>
}
template<typename PutChFunc>
[[gnu::always_inline]] inline int print_number(PutChFunc putch, char*& bufptr, u32 number, bool leftPad, bool zeroPad, u32 fieldWidth)
ALWAYS_INLINE int print_number(PutChFunc putch, char*& bufptr, u32 number, bool leftPad, bool zeroPad, u32 fieldWidth)
{
u32 divisor = 1000000000;
char ch;
@ -137,7 +137,7 @@ template<typename PutChFunc>
}
template<typename PutChFunc>
[[gnu::always_inline]] inline int print_u64(PutChFunc putch, char*& bufptr, u64 number, bool leftPad, bool zeroPad, u32 fieldWidth)
ALWAYS_INLINE int print_u64(PutChFunc putch, char*& bufptr, u64 number, bool leftPad, bool zeroPad, u32 fieldWidth)
{
u64 divisor = 10000000000000000000LLU;
char ch;
@ -178,7 +178,7 @@ template<typename PutChFunc>
}
template<typename PutChFunc>
[[gnu::always_inline]] inline int print_double(PutChFunc putch, char*& bufptr, double number, bool leftPad, bool zeroPad, u32 fieldWidth, u32 fraction_length = 6)
ALWAYS_INLINE int print_double(PutChFunc putch, char*& bufptr, double number, bool leftPad, bool zeroPad, u32 fieldWidth, u32 fraction_length = 6)
{
int length = 0;
@ -200,7 +200,7 @@ template<typename PutChFunc>
}
template<typename PutChFunc>
[[gnu::always_inline]] inline int print_i64(PutChFunc putch, char*& bufptr, i64 number, bool leftPad, bool zeroPad, u32 fieldWidth)
ALWAYS_INLINE int print_i64(PutChFunc putch, char*& bufptr, i64 number, bool leftPad, bool zeroPad, u32 fieldWidth)
{
if (number < 0) {
putch(bufptr, '-');
@ -210,7 +210,7 @@ template<typename PutChFunc>
}
template<typename PutChFunc>
[[gnu::always_inline]] inline int print_octal_number(PutChFunc putch, char*& bufptr, u32 number, bool leftPad, bool zeroPad, u32 fieldWidth)
ALWAYS_INLINE int print_octal_number(PutChFunc putch, char*& bufptr, u32 number, bool leftPad, bool zeroPad, u32 fieldWidth)
{
u32 divisor = 134217728;
char ch;
@ -251,7 +251,7 @@ template<typename PutChFunc>
}
template<typename PutChFunc>
[[gnu::always_inline]] inline int print_string(PutChFunc putch, char*& bufptr, const char* str, bool left_pad, size_t field_width, bool dot)
ALWAYS_INLINE int print_string(PutChFunc putch, char*& bufptr, const char* str, bool left_pad, size_t field_width, bool dot)
{
size_t len = strlen(str);
if (!dot && (!field_width || field_width < len))
@ -273,7 +273,7 @@ template<typename PutChFunc>
}
template<typename PutChFunc>
[[gnu::always_inline]] inline int print_signed_number(PutChFunc putch, char*& bufptr, int number, bool leftPad, bool zeroPad, u32 fieldWidth, bool always_sign)
ALWAYS_INLINE int print_signed_number(PutChFunc putch, char*& bufptr, int number, bool leftPad, bool zeroPad, u32 fieldWidth, bool always_sign)
{
if (number < 0) {
putch(bufptr, '-');
@ -285,7 +285,7 @@ template<typename PutChFunc>
}
template<typename PutChFunc>
[[gnu::always_inline]] inline int printf_internal(PutChFunc putch, char* buffer, const char*& fmt, va_list ap)
ALWAYS_INLINE int printf_internal(PutChFunc putch, char* buffer, const char*& fmt, va_list ap)
{
const char* p;

View file

@ -38,20 +38,20 @@ class StringView {
public:
using ConstIterator = const char*;
[[gnu::always_inline]] inline StringView() { }
[[gnu::always_inline]] inline StringView(const char* characters, size_t length)
ALWAYS_INLINE StringView() { }
ALWAYS_INLINE StringView(const char* characters, size_t length)
: m_characters(characters)
, m_length(length)
{
ASSERT(!Checked<uintptr_t>::addition_would_overflow((uintptr_t)characters, length));
}
[[gnu::always_inline]] inline StringView(const unsigned char* characters, size_t length)
ALWAYS_INLINE StringView(const unsigned char* characters, size_t length)
: m_characters((const char*)characters)
, m_length(length)
{
ASSERT(!Checked<uintptr_t>::addition_would_overflow((uintptr_t)characters, length));
}
[[gnu::always_inline]] inline StringView(const char* cstring)
ALWAYS_INLINE StringView(const char* cstring)
: m_characters(cstring)
, m_length(cstring ? __builtin_strlen(cstring) : 0)
{