Define `static_assert()` when not defined by kernel
[lttng-modules.git] / include / wrapper / compiler.h
CommitLineData
b7cdc182 1/* SPDX-License-Identifier: (GPL-2.0-only or LGPL-2.1-only)
9f36eaed 2 *
40d6e366
MD
3 * wrapper/compiler.h
4 *
5 * Copyright (C) 2013 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
40d6e366
MD
6 */
7
9f36eaed
MJ
8#ifndef _LTTNG_WRAPPER_COMPILER_H
9#define _LTTNG_WRAPPER_COMPILER_H
10
40d6e366 11#include <linux/compiler.h>
5f4c791e 12#include <lttng/kernel-version.h>
40d6e366
MD
13
14/*
15 * Don't allow compiling with buggy compiler.
16 */
17
cf262f57
MD
18#ifdef GCC_VERSION
19
40d6e366
MD
20/*
21 * http://gcc.gnu.org/bugzilla/show_bug.cgi?id=58854
22 */
cf262f57
MD
23# ifdef __ARMEL__
24# if GCC_VERSION >= 40800 && GCC_VERSION <= 40802
25# error Your gcc version produces clobbered frame accesses
26# endif
40d6e366 27# endif
be06402d
MD
28
29/*
30 * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=63293
31 */
32# ifdef __aarch64__
33# if GCC_VERSION < 50100
34# error Your gcc version performs unsafe access to deallocated stack
35# endif
36# endif
37
40d6e366
MD
38#endif
39
a8f2d0c7
MJ
40/*
41 * READ/WRITE_ONCE were introduced in kernel 3.19 and ACCESS_ONCE
42 * was removed in 4.15. Prefer READ/WRITE but fallback to ACCESS
43 * when they are not available.
44 */
45#ifndef READ_ONCE
46# define READ_ONCE(x) ACCESS_ONCE(x)
47#endif
48
49#ifndef WRITE_ONCE
50# define WRITE_ONCE(x, val) ({ ACCESS_ONCE(x) = val; })
51#endif
52
585e5dcc
MJ
53/*
54 * In v4.15 a smp read barrier was added to READ_ONCE to replace
55 * lockless_dereference(), replicate this behavior on prior kernels
56 * and remove calls to smp_read_barrier_depends which was dropped
57 * in v5.9.
58 */
5f4c791e 59#if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,15,0))
585e5dcc
MJ
60#define LTTNG_READ_ONCE(x) READ_ONCE(x)
61#else
62#define LTTNG_READ_ONCE(x) \
63({ \
64 typeof(x) __val = READ_ONCE(x); \
65 smp_read_barrier_depends(); \
66 __val; \
67})
68#endif
69
0a25550c
MD
70#define __LTTNG_COMPOUND_LITERAL(type, ...) (type[]) { __VA_ARGS__ }
71
77d6fd12
FD
72/*
73 * The static_assert macro was defined by the kernel in v5.1.
74 * If the macro is not defined, we define it.
75 * (kernel source: include/linux/build_bug.h)
76 */
77#ifndef static_assert
78
79# define static_assert(expr, ...) __static_assert(expr, ##__VA_ARGS__, #expr)
80# define __static_assert(expr, msg, ...) _Static_assert(expr, msg)
81
82#endif
83
40d6e366 84#endif /* _LTTNG_WRAPPER_COMPILER_H */
This page took 0.047005 seconds and 4 git commands to generate.