1/**
2 * \file
3 * \brief Top-level header for convenient inclusion of standard
4 * libbarrelfish headers.
5 */
6
7/*
8 * Copyright (c) 2007, 2008, 2009, ETH Zurich.
9 * All rights reserved.
10 *
11 * This file is distributed under the terms in the attached LICENSE file.
12 * If you do not find this file, copies can be found by writing to:
13 * ETH Zurich D-INFK, Haldeneggsteig 4, CH-8092 Zurich. Attn: Systems Group.
14 */
15
16#ifndef LIBBARRELFISH_STATIC_ASSERT_H
17#define LIBBARRELFISH_STATIC_ASSERT_H
18
19/*
20 * Variant based on Padraig Brady's implementation
21 * http://www.pixelbeat.org/programming/gcc/static_assert.html
22 */
23
24#define ASSERT_CONCAT_(a, b) a##b
25#define ASSERT_CONCAT(a, b) ASSERT_CONCAT_(a, b)
26
27#ifdef __COUNTER__
28  /* Microsoft compiler. */
29  #define STATIC_ASSERT(e,m) \
30    enum { ASSERT_CONCAT(static_assert_, __COUNTER__) = 1/(!!(e)) }
31#else
32  /* This can't be used twice on the same line so ensure if using in headers
33   * that the headers are not included twice (by wrapping in #ifndef...#endif)
34   * Note it doesn't cause an issue when used on same line of separate modules
35   * compiled with gcc -combine -fwhole-program.  */
36  #define STATIC_ASSERT(e,m) \
37    enum { ASSERT_CONCAT(assert_line_, __LINE__) = 1/(!!(e)) }
38#endif
39
40#define STATIC_ASSERT_SIZEOF(tname,n)                           \
41    STATIC_ASSERT(sizeof(tname) == n,                           \
42                  ASSERT_CONCAT("Size mismatch:", tname)        \
43                 )
44
45#define sa_offsetof(x,y) ((size_t)(((void*)&(((x*)0)->y)) - (void*)(x*)0))
46
47#define STATIC_ASSERT_OFFSETOF(tname, field, offset)            \
48    STATIC_ASSERT(sa_offsetof(tname, field) == offset,          \
49                  ASSERT_CONCAT("Offset mismatch:", field)      \
50                 )
51
52#endif // LIBBARRELFISH_STATIC_ASSERT_H
53
54