1/*
2Copyright (c) 2012, Broadcom Europe Ltd
3All rights reserved.
4
5Redistribution and use in source and binary forms, with or without
6modification, are permitted provided that the following conditions are met:
7 * Redistributions of source code must retain the above copyright
8 notice, this list of conditions and the following disclaimer.
9 * Redistributions in binary form must reproduce the above copyright
10 notice, this list of conditions and the following disclaimer in the
11 documentation and/or other materials provided with the distribution.
12 * Neither the name of the copyright holder nor the
13 names of its contributors may be used to endorse or promote products
14 derived from this software without specific prior written permission.
15
16THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
20DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26*/
27
28/*=============================================================================
29VideoCore OS Abstraction Layer - compiler-specific attributes
30=============================================================================*/
31
32#ifndef VCOS_ATTR_H
33#define VCOS_ATTR_H
34
35/**
36 * Type attribute indicating the enum should be stored in as few bytes as
37 * possible. MetaWare does this by default, so the attribute is useful when
38 * structs need to be portable to GCC too.
39 *
40 * MSVC doesn't support VCOS_ENUM_PACKED, so code that needs to be portable
41 * across all platforms but wants the type-safety and debug-info benefits
42 * of enum types when possible, should do:
43 *
44 * typedef enum VCOS_ENUM_PACKED { a = 0, b = 0xffff } EXAMPLE_T;
45 * struct foo {
46 * int bar;
47 * #if VCOS_HAS_ENUM_PACKED
48 * EXAMPLE_T baz;
49 * #else
50 * uint16_t baz;
51 * #endif
52 * };
53 */
54
55#if defined(__VECTORC__)
56# define VCOS_ENUM_PACKED
57# define VCOS_HAS_ENUM_PACKED 0
58#elif defined(__GNUC__)
59# define VCOS_ENUM_PACKED __attribute__ ((packed))
60# define VCOS_HAS_ENUM_PACKED 1
61#elif defined(__HIGHC__)
62# define VCOS_ENUM_PACKED /* packed enums are default on Metaware */
63# define VCOS_HAS_ENUM_PACKED 1
64#else
65# define VCOS_ENUM_PACKED
66# define VCOS_HAS_ENUM_PACKED 0
67#endif
68
69/** Variable attribute indicating the variable must be emitted even if it appears unused. */
70#if defined(__GNUC__) || defined(__HIGHC__)
71# define VCOS_ATTR_USED __attribute__ ((used))
72#else
73# define VCOS_ATTR_USED
74#endif
75
76/** Variable attribute indicating the compiler should not warn if the variable is unused. */
77#if defined(__GNUC__) || defined(__HIGHC__)
78# define VCOS_ATTR_POSSIBLY_UNUSED __attribute__ ((unused))
79#else
80# define VCOS_ATTR_POSSIBLY_UNUSED
81#endif
82
83/** Variable attribute requiring specific alignment.
84 *
85 * Use as:
86 * int VCOS_ATTR_ALIGNED(256) n;
87 * or:
88 * VCOS_ATTR_ALIGNED(256) int n;
89 * or if you don't want to support MSVC:
90 * int n VCOS_ATTR_ALIGNED(256);
91 */
92#if defined(__GNUC__) || defined(__HIGHC__)
93# define VCOS_ATTR_ALIGNED(n) __attribute__ ((aligned(n)))
94#elif defined(_MSC_VER)
95# define VCOS_ATTR_ALIGNED(n) __declspec(align(n))
96#else
97/* Force a syntax error if this is used when the compiler doesn't support it,
98 * instead of silently misaligning */
99# define VCOS_ATTR_ALIGNED(n) VCOS_ATTR_ALIGNED_NOT_SUPPORTED_ON_THIS_COMPILER
100#endif
101
102/** Variable attribute requiring specific ELF section.
103 *
104 * Use as:
105 * int n VCOS_ATTR_SECTION(".foo") = 1;
106 *
107 * A pointer like &n will have type "VCOS_ATTR_SECTION_QUALIFIER int *".
108 */
109#if defined(__HIGHC__) || defined(__VECTORC__)
110/* hcvc requires 'far' else it'll put small objects in .sdata/.rsdata/.sbss */
111# define VCOS_ATTR_SECTION(s) __attribute__ ((far, section(s)))
112# define VCOS_ATTR_SECTION_QUALIFIER _Far
113#elif defined(__GNUC__)
114# define VCOS_ATTR_SECTION(s) __attribute__ ((section(s)))
115# define VCOS_ATTR_SECTION_QUALIFIER
116#else
117/* Force a syntax error if this is used when the compiler doesn't support it */
118# define VCOS_ATTR_SECTION(s) VCOS_ATTR_SECTION_NOT_SUPPORTED_ON_THIS_COMPILER
119# define VCOS_ATTR_SECTION_QUALIFIER
120#endif
121
122/** Define a function as a weak alias to another function.
123 * @param ret_type Function return type.
124 * @param alias_name Name of the alias.
125 * @param param_list Function parameter list, including the parentheses.
126 * @param target_name Target function (bare function name, not a string).
127 */
128#if defined(__GNUC__) || defined(__HIGHC__)
129 /* N.B. gcc allows __attribute__ after parameter list, but hcvc seems to silently ignore it. */
130# define VCOS_WEAK_ALIAS(ret_type, alias_name, param_list, target_name) \
131 __attribute__ ((weak, alias(#target_name))) ret_type alias_name param_list
132#else
133# define VCOS_WEAK_ALIAS(ret_type, alias, params, target) VCOS_CASSERT(0)
134#endif
135
136/** Define a function as a weak alias to another function, specified as a string.
137 * @param ret_type Function return type.
138 * @param alias_name Name of the alias.
139 * @param param_list Function parameter list, including the parentheses.
140 * @param target_name Target function name as a string.
141 * @note Prefer the use of VCOS_WEAK_ALIAS - it is likely to be more portable.
142 * Only use VCOS_WEAK_ALIAS_STR if you need to do pre-processor mangling of the target
143 * symbol.
144 */
145#if defined(__GNUC__) || defined(__HIGHC__)
146 /* N.B. gcc allows __attribute__ after parameter list, but hcvc seems to silently ignore it. */
147# define VCOS_WEAK_ALIAS_STR(ret_type, alias_name, param_list, target_name) \
148 __attribute__ ((weak, alias(target_name))) ret_type alias_name param_list
149#else
150# define VCOS_WEAK_ALIAS_STR(ret_type, alias, params, target) VCOS_CASSERT(0)
151#endif
152
153#endif /* VCOS_ATTR_H */
154