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, Universitaetstrasse 6, CH-8092 Zurich. Attn: Systems Group.
14 */
15
16#ifndef LIBBARRELFISH_STATIC_ASSERT_H
17#define LIBBARRELFISH_STATIC_ASSERT_H
18
19#include <sys/cdefs.h>
20
21__BEGIN_DECLS
22
23/*
24 * Variant based on Padraig Brady's implementation
25 * http://www.pixelbeat.org/programming/gcc/static_assert.html
26 */
27
28#define ASSERT_CONCAT_(a, b) a##b
29#define ASSERT_CONCAT(a, b) ASSERT_CONCAT_(a, b)
30
31#ifdef __COUNTER__
32  /* Microsoft compiler. */
33  #define STATIC_ASSERT(e,m) \
34    enum { ASSERT_CONCAT(static_assert_, __COUNTER__) = 1/(!!(e)) }
35#else
36  /* This can't be used twice on the same line so ensure if using in headers
37   * that the headers are not included twice (by wrapping in #ifndef...#endif)
38   * Note it doesn't cause an issue when used on same line of separate modules
39   * compiled with gcc -combine -fwhole-program.  */
40  #define STATIC_ASSERT(e,m) \
41    enum { ASSERT_CONCAT(assert_line_, __LINE__) = 1/(!!(e)) }
42#endif
43
44#define STATIC_ASSERT_SIZEOF(tname,n)                           \
45    STATIC_ASSERT(sizeof(tname) == n,                           \
46                  ASSERT_CONCAT("Size mismatch:", tname)        \
47                 )
48
49#define sa_offsetof(x,y) ((size_t)(((void*)&(((x*)0)->y)) - (void*)(x*)0))
50
51#define STATIC_ASSERT_OFFSETOF(tname, field, offset)            \
52    STATIC_ASSERT(sa_offsetof(tname, field) == offset,          \
53                  ASSERT_CONCAT("Offset mismatch:", field)      \
54                 )
55
56
57__END_DECLS
58
59#endif // LIBBARRELFISH_STATIC_ASSERT_H
60
61