use [[gnu::...]] attributes

This commit is contained in:
Max Kellermann
2023-03-06 15:57:36 +01:00
parent 3b9aab0684
commit 42f6a0441c
101 changed files with 167 additions and 234 deletions

View File

@@ -4,20 +4,21 @@
#ifndef CLAMP_HPP
#define CLAMP_HPP
#include "Compiler.h"
/**
* Clamps the specified value in a range. Returns #min or #max if the
* value is outside.
*/
template<typename T>
constexpr const T &
Clamp(const T &value, const T &min, const T &max)
Clamp(const T &value, const T &min, const T &max) noexcept
{
return gcc_unlikely(value < min)
? min
: (gcc_unlikely(value > max)
? max : value);
if (value < min) [[unlikely]]
return min;
if (value > max) [[unlikely]]
return max;
return value;
}
#endif

View File

@@ -4,8 +4,6 @@
#ifndef REUSABLE_ARRAY_HXX
#define REUSABLE_ARRAY_HXX
#include "Compiler.h"
#include <cstddef>
#include <utility>
@@ -59,7 +57,7 @@ public:
*/
[[gnu::malloc]] [[gnu::returns_nonnull]]
T *Get(size_t size) {
if (gcc_unlikely(size > capacity)) {
if (size > capacity) [[unlikely]] {
/* too small: grow */
delete[] buffer;

View File

@@ -2,7 +2,6 @@
// Copyright The Music Player Daemon Project
#include "Serial.hxx"
#include "Compiler.h"
#include <atomic>
#include <chrono>
@@ -13,7 +12,7 @@ int
GenerateSerial() noexcept
{
unsigned serial = ++next_serial;
if (gcc_unlikely(serial < 16)) {
if (serial < 16) [[unlikely]] {
/* first-time initialization: seed with a clock value,
which is random enough for our use */

View File

@@ -4,8 +4,6 @@
#ifndef STRING_UTIL_HXX
#define STRING_UTIL_HXX
#include "Compiler.h"
#include <cstddef>
#include <string_view>
@@ -17,7 +15,7 @@
* case-insensitive for ASCII characters
* @return true if found
*/
gcc_pure
[[gnu::pure]]
bool
StringArrayContainsCase(const char *const*haystack,
std::string_view needle) noexcept;

View File

@@ -2,6 +2,7 @@
// author: Max Kellermann <max.kellermann@gmail.com>
#include "TruncateString.hxx"
#include "Compiler.h"
#include <algorithm>

View File

@@ -4,8 +4,6 @@
#ifndef TRUNCATE_STRING_HXX
#define TRUNCATE_STRING_HXX
#include "Compiler.h"
#include <cstddef>
/**
@@ -16,7 +14,7 @@
* terminator)
* @return a pointer to the null terminator
*/
gcc_nonnull_all
[[gnu::nonnull]]
char *
CopyTruncateString(char *dest, const char *src, size_t size) noexcept;

View File

@@ -272,9 +272,9 @@ Latin1ToUTF8(const char *gcc_restrict src, char *gcc_restrict buffer,
char *
UnicodeToUTF8(unsigned ch, char *q) noexcept
{
if (gcc_likely(ch < 0x80)) {
if (ch < 0x80) [[likely]] {
*q++ = (char)ch;
} else if (gcc_likely(ch < 0x800)) {
} else if (ch < 0x800) [[likely]] {
*q++ = MakeLeading1(ch >> 6);
*q++ = MakeContinuation(ch);
} else if (ch < 0x10000) {