xref: /freebsd/sys/compat/linuxkpi/common/include/linux/compiler.h (revision d448578b445da95806ef9af996a0db9754daadeb)
1 /*-
2  * Copyright (c) 2010 Isilon Systems, Inc.
3  * Copyright (c) 2010 iX Systems, Inc.
4  * Copyright (c) 2010 Panasas, Inc.
5  * Copyright (c) 2013-2016 Mellanox Technologies, Ltd.
6  * Copyright (c) 2015 François Tigeot
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice unmodified, this list of conditions, and the following
14  *    disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 #ifndef	_LINUXKPI_LINUX_COMPILER_H_
31 #define	_LINUXKPI_LINUX_COMPILER_H_
32 
33 #include <sys/cdefs.h>
34 #include <sys/endian.h>
35 
36 #include <compat/linuxkpi/common/include/linux/compiler_types.h>
37 
38 #define	__bitwise
39 #define __devinitdata
40 #define __init
41 #define	__initconst
42 #define	__devinit
43 #define	__devexit
44 #define __exit
45 #define	____cacheline_aligned		__aligned(CACHE_LINE_SIZE)
46 #define	____cacheline_aligned_in_smp	__aligned(CACHE_LINE_SIZE)
47 
48 #if __has_attribute(__counted_by__)
49 #define	__counted_by(_x)		__attribute__((__counted_by__(_x)))
50 #else
51 #define	__counted_by(_x)
52 #endif
53 #if BYTE_ORDER == LITTLE_ENDIAN
54 #define	__counted_by_le(_x)		__counted_by(_x)
55 #define	__counted_by_be(_x)
56 #else
57 #define	__counted_by_le(_x)
58 #define	__counted_by_be(_x)		__counted_by(_x)
59 #endif
60 
61 #define	likely(x)			__builtin_expect(!!(x), 1)
62 #define	unlikely(x)			__builtin_expect(!!(x), 0)
63 #define typeof(x)			__typeof(x)
64 
65 #define	uninitialized_var(x)		x = x
66 
67 #define	barrier()			__asm__ __volatile__("": : :"memory")
68 
69 #define	lower_32_bits(n)		((u32)(n))
70 #define	upper_32_bits(n)		((u32)(((n) >> 16) >> 16))
71 
72 #define	WRITE_ONCE(x,v) do {		\
73 	barrier();			\
74 	(*(volatile __typeof(x) *)(uintptr_t)&(x)) = (v); \
75 	barrier();			\
76 } while (0)
77 
78 #define	READ_ONCE(x) ({			\
79 	__typeof(x) __var = ({		\
80 		barrier();		\
81 		(*(const volatile __typeof(x) *)&(x)); \
82 	});				\
83 	barrier();			\
84 	__var;				\
85 })
86 
87 #define	lockless_dereference(p) READ_ONCE(p)
88 
89 #define	_AT(T,X)	((T)(X))
90 #define	__must_be_array(a)	__same_type(a, &(a)[0])
91 
92 #define	sizeof_field(_s, _m)	sizeof(((_s *)0)->_m)
93 
94 #define is_signed_type(t)	((t)-1 < (t)1)
95 #define is_unsigned_type(t)	((t)-1 > (t)1)
96 
97 #if __has_builtin(__builtin_dynamic_object_size)
98 #define	__struct_size(_s)	__builtin_dynamic_object_size(_s, 0)
99 #else
100 #define	__struct_size(_s)	__builtin_object_size(_s, 0)
101 #endif
102 
103 #endif	/* _LINUXKPI_LINUX_COMPILER_H_ */
104