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 // Like the C++11 |alignof| operator. 21 #if __cplusplus >= 201103L 22 #define MOJO_ALIGNOF(type) alignof(type) 23 #elif defined(__GNUC__) 24 #define MOJO_ALIGNOF(type) __alignof__(type) 25 #elif defined(_MSC_VER) 26 // The use of |sizeof| is to work around a bug in MSVC 2010 (see 27 // http://goo.gl/isH0C; supposedly fixed since then). 28 #define MOJO_ALIGNOF(type) (sizeof(type) - sizeof(type) + __alignof(type)) 29 #else 30 #error "Please define MOJO_ALIGNOF() for your compiler." 31 #endif 32 33 // Specify the alignment of a |struct|, etc. 34 // Use like: 35 // struct MOJO_ALIGNAS(8) Foo { ... }; 36 // Unlike the C++11 |alignas()|, |alignment| must be an integer. It may not be a 37 // type, nor can it be an expression like |MOJO_ALIGNOF(type)| (due to the 38 // non-C++11 MSVS version). 39 #if __cplusplus >= 201103L 40 #define MOJO_ALIGNAS(alignment) alignas(alignment) 41 #elif defined(__GNUC__) 42 #define MOJO_ALIGNAS(alignment) __attribute__((aligned(alignment))) 43 #elif defined(_MSC_VER) 44 #define MOJO_ALIGNAS(alignment) __declspec(align(alignment)) 45 #else 46 #error "Please define MOJO_ALIGNAS() for your compiler." 47 #endif 48 49 #endif // MOJO_PUBLIC_C_SYSTEM_MACROS_H_ 50