OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
| 5 // TODO(vtl): Remove this file. |
| 6 |
5 #ifndef MOJO_PUBLIC_C_SYSTEM_MACROS_H_ | 7 #ifndef MOJO_PUBLIC_C_SYSTEM_MACROS_H_ |
6 #define MOJO_PUBLIC_C_SYSTEM_MACROS_H_ | 8 #define MOJO_PUBLIC_C_SYSTEM_MACROS_H_ |
7 | 9 |
8 #include <stddef.h> | 10 // TODO(vtl): This would be better (and more correct), but it requires fixing |
9 | 11 // lots of public_deps: |
10 // Annotate a variable indicating it's okay if it's unused. | 12 // #include <mojo/macros.h> |
11 // Use like: | 13 #include "mojo/public/c/include/mojo/macros.h" |
12 // int x = ...; | |
13 // MOJO_ALLOW_UNUSED_LOCAL(x); | |
14 #define MOJO_ALLOW_UNUSED_LOCAL(x) false ? (void)x : (void)0 | |
15 | |
16 // Annotate a function indicating that the caller must examine the return value. | |
17 // Use like: | |
18 // int foo() MOJO_WARN_UNUSED_RESULT; | |
19 // Note that it can only be used on the prototype, and not the definition. | |
20 #if defined(__GNUC__) | |
21 #define MOJO_WARN_UNUSED_RESULT __attribute__((warn_unused_result)) | |
22 #else | |
23 #define MOJO_WARN_UNUSED_RESULT | |
24 #endif | |
25 | |
26 // Assert things at compile time. (|msg| should be a valid identifier name.) | |
27 // Use like: | |
28 // MOJO_STATIC_ASSERT(sizeof(Foo) == 12, "Foo has invalid size"); | |
29 #if defined(__cplusplus) | |
30 #define MOJO_STATIC_ASSERT(expr, msg) static_assert(expr, msg) | |
31 #else | |
32 #define MOJO_STATIC_ASSERT(expr, msg) _Static_assert(expr, msg) | |
33 #endif | |
34 | |
35 // Like the C++11 |alignof| operator. | |
36 #if defined(__cplusplus) && __cplusplus >= 201103L | |
37 #define MOJO_ALIGNOF(type) alignof(type) | |
38 #elif defined(__GNUC__) | |
39 #define MOJO_ALIGNOF(type) __alignof__(type) | |
40 #elif defined(_MSC_VER) | |
41 // The use of |sizeof| is to work around a bug in MSVC 2010 (see | |
42 // http://goo.gl/isH0C; supposedly fixed since then). | |
43 #define MOJO_ALIGNOF(type) (sizeof(type) - sizeof(type) + __alignof(type)) | |
44 #else | |
45 #error "Please define MOJO_ALIGNOF() for your compiler." | |
46 #endif | |
47 | |
48 // Specify the alignment of a |struct|, etc. | |
49 // Use like: | |
50 // struct MOJO_ALIGNAS(8) Foo { ... }; | |
51 // Unlike the C++11 |alignas()|, |alignment| must be an integer. It may not be a | |
52 // type, nor can it be an expression like |MOJO_ALIGNOF(type)| (due to the | |
53 // non-C++11 MSVS version). | |
54 #if defined(__cplusplus) && __cplusplus >= 201103L | |
55 #define MOJO_ALIGNAS(alignment) alignas(alignment) | |
56 #elif defined(__GNUC__) | |
57 #define MOJO_ALIGNAS(alignment) __attribute__((aligned(alignment))) | |
58 #elif defined(_MSC_VER) | |
59 #define MOJO_ALIGNAS(alignment) __declspec(align(alignment)) | |
60 #else | |
61 #error "Please define MOJO_ALIGNAS() for your compiler." | |
62 #endif | |
63 | |
64 // Use these to declare functions in C header files so that they'll be usable | |
65 // from C++. | |
66 #if defined(__cplusplus) | |
67 #define MOJO_BEGIN_EXTERN_C extern "C" { | |
68 #define MOJO_END_EXTERN_C } | |
69 #else | |
70 #define MOJO_BEGIN_EXTERN_C | |
71 #define MOJO_END_EXTERN_C | |
72 #endif | |
73 | |
74 // Use |MOJO_RESTRICT| (in C function declarations) in place of C99's | |
75 // |restrict|, to work properly with C++. (It allows certain optimizations, but | |
76 // more importantly it serves a documentary purpose for the caller.) | |
77 // | |
78 // Recommendation: If a function has multiple pointer parameters at least one of | |
79 // which is to non-const, then all the pointer parameters should be declared | |
80 // using |MOJO_RESTRICT| unless aliasing is explicitly allowed. | |
81 #if defined(__GNUC__) || defined(_MSC_VER) | |
82 // Use |__restrict|, since it works both in C++ and when compiling as C90 | |
83 // (|restrict| is only available in C99 and later, and never in C++). | |
84 #define MOJO_RESTRICT __restrict | |
85 #else | |
86 #error "Please define MOJO_RESTRICT for your compiler." | |
87 #endif | |
88 | 14 |
89 #endif // MOJO_PUBLIC_C_SYSTEM_MACROS_H_ | 15 #endif // MOJO_PUBLIC_C_SYSTEM_MACROS_H_ |
OLD | NEW |