]> git.proxmox.com Git - ovs.git/blame - lib/compiler.h
Make attribute packed equivalent for MSC compilers.
[ovs.git] / lib / compiler.h
CommitLineData
064af421 1/*
ec68790f 2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013 Nicira, Inc.
064af421 3 *
a14bc59f
BP
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:
064af421 7 *
a14bc59f
BP
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.
064af421
BP
15 */
16
17#ifndef COMPILER_H
18#define COMPILER_H 1
19
2932bd05 20#if __GNUC__ && !__CHECKER__
064af421 21#define NO_RETURN __attribute__((__noreturn__))
67a4917b 22#define OVS_UNUSED __attribute__((__unused__))
064af421
BP
23#define PRINTF_FORMAT(FMT, ARG1) __attribute__((__format__(printf, FMT, ARG1)))
24#define STRFTIME_FORMAT(FMT) __attribute__((__format__(__strftime__, FMT, 0)))
25#define MALLOC_LIKE __attribute__((__malloc__))
26#define ALWAYS_INLINE __attribute__((always_inline))
f85f8ebb 27#define WARN_UNUSED_RESULT __attribute__((__warn_unused_result__))
dfeb9047 28#define SENTINEL(N) __attribute__((sentinel(N)))
4749f73d
BP
29#define OVS_LIKELY(CONDITION) __builtin_expect(!!(CONDITION), 1)
30#define OVS_UNLIKELY(CONDITION) __builtin_expect(!!(CONDITION), 0)
4380e924
BP
31#else
32#define NO_RETURN
33#define OVS_UNUSED
34#define PRINTF_FORMAT(FMT, ARG1)
35#define STRFTIME_FORMAT(FMT)
36#define MALLOC_LIKE
37#define ALWAYS_INLINE
02dd3123 38#define WARN_UNUSED_RESULT
dfeb9047 39#define SENTINEL(N)
4749f73d
BP
40#define OVS_LIKELY(CONDITION) (!!(CONDITION))
41#define OVS_UNLIKELY(CONDITION) (!!(CONDITION))
4380e924 42#endif
064af421 43
ec68790f
BP
44#ifdef __CHECKER__
45/* "sparse" annotations for mutexes and mutex-like constructs.
46 *
47 * In a function prototype, OVS_ACQUIRES(MUTEX) indicates that the function
48 * must be called without MUTEX acquired and that it returns with MUTEX
49 * acquired. OVS_RELEASES(MUTEX) indicates the reverse. OVS_MUST_HOLD
50 * indicates that the function must be called with MUTEX acquired by the
51 * caller and that the function does not release MUTEX.
52 *
53 * In practice, sparse ignores the MUTEX argument. It need not even be a
54 * valid expression. It is meant to indicate to human readers what mutex is
55 * being acquired.
56 *
57 * Since sparse ignores MUTEX, it need not be an actual mutex. It can be
58 * any construct on which paired acquire and release semantics make sense:
59 * read/write locks, temporary memory allocations, whatever.
60 *
61 * OVS_ACQUIRE, OVS_RELEASE, and OVS_HOLDS are suitable for use within macros,
62 * where there is no function prototype to annotate. */
63#define OVS_ACQUIRES(MUTEX) __attribute__((context(MUTEX, 0, 1)))
64#define OVS_RELEASES(MUTEX) __attribute__((context(MUTEX, 1, 0)))
65#define OVS_MUST_HOLD(MUTEX) __attribute__((context(MUTEX, 1, 1)))
66#define OVS_ACQUIRE(MUTEX) __context__(MUTEX, 0, 1)
67#define OVS_RELEASE(MUTEX) __context__(MUTEX, 1, 0)
68#define OVS_HOLDS(MUTEX) __context__(MUTEX, 1, 1)
69#else
70#define OVS_ACQUIRES(MUTEX)
71#define OVS_RELEASES(MUTEX)
72#define OVS_MUST_HOLD(MUTEX)
73#define OVS_ACQUIRE(MUTEX)
74#define OVS_RELEASE(MUTEX)
75#define OVS_HOLDS(MUTEX)
76#endif
77
f25d0cf3
BP
78/* ISO C says that a C implementation may choose any integer type for an enum
79 * that is sufficient to hold all of its values. Common ABIs (such as the
80 * System V ABI used on i386 GNU/Linux) always use a full-sized "int", even
81 * when a smaller type would suffice.
82 *
83 * In GNU C, "enum __attribute__((packed)) name { ... }" defines 'name' as an
84 * enum compatible with a type that is no bigger than necessary. This is the
85 * intended use of OVS_PACKED_ENUM.
86 *
87 * OVS_PACKED_ENUM is intended for use only as a space optimization, since it
88 * only works with GCC. That means that it must not be used in wire protocols
89 * or otherwise exposed outside of a single process. */
90#if __GNUC__ && !__CHECKER__
91#define OVS_PACKED_ENUM __attribute__((__packed__))
92#else
93#define OVS_PACKED_ENUM
94#endif
95
13b6bae6
LS
96#ifndef _MSC_VER
97#define OVS_PACKED(DECL) DECL __attribute__((__packed__))
98#else
99#define OVS_PACKED(DECL) __pragma(pack(push, 1)) DECL __pragma(pack(pop))
100#endif
101
064af421 102#endif /* compiler.h */