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