xref: /aosp_15_r20/external/libchrome/mojo/public/c/system/macros.h (revision 635a864187cb8b6c713ff48b7e790a6b21769273)
1*635a8641SAndroid Build Coastguard Worker // Copyright 2014 The Chromium Authors. All rights reserved.
2*635a8641SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*635a8641SAndroid Build Coastguard Worker // found in the LICENSE file.
4*635a8641SAndroid Build Coastguard Worker 
5*635a8641SAndroid Build Coastguard Worker #ifndef MOJO_PUBLIC_C_SYSTEM_MACROS_H_
6*635a8641SAndroid Build Coastguard Worker #define MOJO_PUBLIC_C_SYSTEM_MACROS_H_
7*635a8641SAndroid Build Coastguard Worker 
8*635a8641SAndroid Build Coastguard Worker #include <stddef.h>
9*635a8641SAndroid Build Coastguard Worker 
10*635a8641SAndroid Build Coastguard Worker // Assert things at compile time. (|msg| should be a valid identifier name.)
11*635a8641SAndroid Build Coastguard Worker // This macro is currently C++-only, but we want to use it in the C core.h.
12*635a8641SAndroid Build Coastguard Worker // Use like:
13*635a8641SAndroid Build Coastguard Worker //   MOJO_STATIC_ASSERT(sizeof(Foo) == 12, "Foo has invalid size");
14*635a8641SAndroid Build Coastguard Worker #if defined(__cplusplus)
15*635a8641SAndroid Build Coastguard Worker #define MOJO_STATIC_ASSERT(expr, msg) static_assert(expr, msg)
16*635a8641SAndroid Build Coastguard Worker #else
17*635a8641SAndroid Build Coastguard Worker #define MOJO_STATIC_ASSERT(expr, msg)
18*635a8641SAndroid Build Coastguard Worker #endif
19*635a8641SAndroid Build Coastguard Worker 
20*635a8641SAndroid Build Coastguard Worker // Defines a pointer-sized struct field of the given type. This ensures that the
21*635a8641SAndroid Build Coastguard Worker // field has an 8-byte footprint on both 32-bit and 64-bit systems, using an
22*635a8641SAndroid Build Coastguard Worker // anonymous bitfield of either 32 or 0 bits, depending on pointer size. Weird
23*635a8641SAndroid Build Coastguard Worker // formatting here courtesy of clang-format.
24*635a8641SAndroid Build Coastguard Worker #define MOJO_POINTER_FIELD(type, name) \
25*635a8641SAndroid Build Coastguard Worker   type name;                           \
26*635a8641SAndroid Build Coastguard Worker   uint32_t:                            \
27*635a8641SAndroid Build Coastguard Worker   (sizeof(void*) == 4 ? 32 : 0)
28*635a8641SAndroid Build Coastguard Worker 
29*635a8641SAndroid Build Coastguard Worker // Like the C++11 |alignof| operator.
30*635a8641SAndroid Build Coastguard Worker #if __cplusplus >= 201103L
31*635a8641SAndroid Build Coastguard Worker #define MOJO_ALIGNOF(type) alignof(type)
32*635a8641SAndroid Build Coastguard Worker #elif defined(__GNUC__)
33*635a8641SAndroid Build Coastguard Worker #define MOJO_ALIGNOF(type) __alignof__(type)
34*635a8641SAndroid Build Coastguard Worker #elif defined(_MSC_VER)
35*635a8641SAndroid Build Coastguard Worker // The use of |sizeof| is to work around a bug in MSVC 2010 (see
36*635a8641SAndroid Build Coastguard Worker // http://goo.gl/isH0C; supposedly fixed since then).
37*635a8641SAndroid Build Coastguard Worker #define MOJO_ALIGNOF(type) (sizeof(type) - sizeof(type) + __alignof(type))
38*635a8641SAndroid Build Coastguard Worker #else
39*635a8641SAndroid Build Coastguard Worker #error "Please define MOJO_ALIGNOF() for your compiler."
40*635a8641SAndroid Build Coastguard Worker #endif
41*635a8641SAndroid Build Coastguard Worker 
42*635a8641SAndroid Build Coastguard Worker // Specify the alignment of a |struct|, etc.
43*635a8641SAndroid Build Coastguard Worker // Use like:
44*635a8641SAndroid Build Coastguard Worker //   struct MOJO_ALIGNAS(8) Foo { ... };
45*635a8641SAndroid Build Coastguard Worker // Unlike the C++11 |alignas()|, |alignment| must be an integer. It may not be a
46*635a8641SAndroid Build Coastguard Worker // type, nor can it be an expression like |MOJO_ALIGNOF(type)| (due to the
47*635a8641SAndroid Build Coastguard Worker // non-C++11 MSVS version).
48*635a8641SAndroid Build Coastguard Worker #if __cplusplus >= 201103L
49*635a8641SAndroid Build Coastguard Worker #define MOJO_ALIGNAS(alignment) alignas(alignment)
50*635a8641SAndroid Build Coastguard Worker #elif defined(__GNUC__)
51*635a8641SAndroid Build Coastguard Worker #define MOJO_ALIGNAS(alignment) __attribute__((aligned(alignment)))
52*635a8641SAndroid Build Coastguard Worker #elif defined(_MSC_VER)
53*635a8641SAndroid Build Coastguard Worker #define MOJO_ALIGNAS(alignment) __declspec(align(alignment))
54*635a8641SAndroid Build Coastguard Worker #else
55*635a8641SAndroid Build Coastguard Worker #error "Please define MOJO_ALIGNAS() for your compiler."
56*635a8641SAndroid Build Coastguard Worker #endif
57*635a8641SAndroid Build Coastguard Worker 
58*635a8641SAndroid Build Coastguard Worker #endif  // MOJO_PUBLIC_C_SYSTEM_MACROS_H_
59