![](/media/cache/profiles/debfdc81854eac26ec993b55a659c6e1.jpg.50x50_q85.jpg)
mbed client lightswitch demo
Dependencies: mbed Socket lwip-eth lwip-sys lwip
Fork of mbed-client-classic-example-lwip by
nanostack-libservice/mbed-client-libservice/ns_types.h@11:cada08fc8a70, 2016-06-09 (annotated)
- Committer:
- mbedAustin
- Date:
- Thu Jun 09 17:08:36 2016 +0000
- Revision:
- 11:cada08fc8a70
Commit for public Consumption
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbedAustin | 11:cada08fc8a70 | 1 | /* |
mbedAustin | 11:cada08fc8a70 | 2 | * Copyright (c) 2014-2015 ARM Limited. All rights reserved. |
mbedAustin | 11:cada08fc8a70 | 3 | * SPDX-License-Identifier: Apache-2.0 |
mbedAustin | 11:cada08fc8a70 | 4 | * Licensed under the Apache License, Version 2.0 (the License); you may |
mbedAustin | 11:cada08fc8a70 | 5 | * not use this file except in compliance with the License. |
mbedAustin | 11:cada08fc8a70 | 6 | * You may obtain a copy of the License at |
mbedAustin | 11:cada08fc8a70 | 7 | * |
mbedAustin | 11:cada08fc8a70 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
mbedAustin | 11:cada08fc8a70 | 9 | * |
mbedAustin | 11:cada08fc8a70 | 10 | * Unless required by applicable law or agreed to in writing, software |
mbedAustin | 11:cada08fc8a70 | 11 | * distributed under the License is distributed on an AS IS BASIS, WITHOUT |
mbedAustin | 11:cada08fc8a70 | 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
mbedAustin | 11:cada08fc8a70 | 13 | * See the License for the specific language governing permissions and |
mbedAustin | 11:cada08fc8a70 | 14 | * limitations under the License. |
mbedAustin | 11:cada08fc8a70 | 15 | */ |
mbedAustin | 11:cada08fc8a70 | 16 | /* |
mbedAustin | 11:cada08fc8a70 | 17 | * ns_types.h - Basic compiler and type setup for Nanostack libraries. |
mbedAustin | 11:cada08fc8a70 | 18 | */ |
mbedAustin | 11:cada08fc8a70 | 19 | #ifndef NS_TYPES_H_ |
mbedAustin | 11:cada08fc8a70 | 20 | #define NS_TYPES_H_ |
mbedAustin | 11:cada08fc8a70 | 21 | |
mbedAustin | 11:cada08fc8a70 | 22 | /** \file |
mbedAustin | 11:cada08fc8a70 | 23 | * \brief Basic compiler and type setup |
mbedAustin | 11:cada08fc8a70 | 24 | * |
mbedAustin | 11:cada08fc8a70 | 25 | * We currently assume C99 or later. |
mbedAustin | 11:cada08fc8a70 | 26 | * |
mbedAustin | 11:cada08fc8a70 | 27 | * C99 features being relied on: |
mbedAustin | 11:cada08fc8a70 | 28 | * |
mbedAustin | 11:cada08fc8a70 | 29 | * - <inttypes.h> and <stdbool.h> |
mbedAustin | 11:cada08fc8a70 | 30 | * - inline (with C99 semantics, not C++ as per default GCC); |
mbedAustin | 11:cada08fc8a70 | 31 | * - designated initialisers; |
mbedAustin | 11:cada08fc8a70 | 32 | * - compound literals; |
mbedAustin | 11:cada08fc8a70 | 33 | * - restrict; |
mbedAustin | 11:cada08fc8a70 | 34 | * - [static N] in array parameters; |
mbedAustin | 11:cada08fc8a70 | 35 | * - declarations in for statements; |
mbedAustin | 11:cada08fc8a70 | 36 | * - mixing declarations and statements |
mbedAustin | 11:cada08fc8a70 | 37 | * |
mbedAustin | 11:cada08fc8a70 | 38 | * Compilers should be set to C99 or later mode when building Nanomesh source. |
mbedAustin | 11:cada08fc8a70 | 39 | * For GCC this means "-std=gnu99" (C99 with usual GNU extensions). |
mbedAustin | 11:cada08fc8a70 | 40 | * |
mbedAustin | 11:cada08fc8a70 | 41 | * Also, a little extra care is required for public header files that could be |
mbedAustin | 11:cada08fc8a70 | 42 | * included from C++, especially as C++ lacks some C99 features. |
mbedAustin | 11:cada08fc8a70 | 43 | * |
mbedAustin | 11:cada08fc8a70 | 44 | * (TODO: as this is exposed to API users, do we need a predefine to distinguish |
mbedAustin | 11:cada08fc8a70 | 45 | * internal and external use, for finer control? Not yet, but maybe...) |
mbedAustin | 11:cada08fc8a70 | 46 | */ |
mbedAustin | 11:cada08fc8a70 | 47 | |
mbedAustin | 11:cada08fc8a70 | 48 | /* Make sure <stdint.h> defines its macros if C++ */ |
mbedAustin | 11:cada08fc8a70 | 49 | #ifndef __STDC_LIMIT_MACROS |
mbedAustin | 11:cada08fc8a70 | 50 | #define __STDC_LIMIT_MACROS |
mbedAustin | 11:cada08fc8a70 | 51 | #endif |
mbedAustin | 11:cada08fc8a70 | 52 | #ifndef __STDC_CONSTANT_MACROS |
mbedAustin | 11:cada08fc8a70 | 53 | #define __STDC_CONSTANT_MACROS |
mbedAustin | 11:cada08fc8a70 | 54 | #endif |
mbedAustin | 11:cada08fc8a70 | 55 | |
mbedAustin | 11:cada08fc8a70 | 56 | #include <stddef.h> |
mbedAustin | 11:cada08fc8a70 | 57 | #include <inttypes.h> // includes <stdint.h>; debugf() users need PRIu32 etc |
mbedAustin | 11:cada08fc8a70 | 58 | #include <stdbool.h> |
mbedAustin | 11:cada08fc8a70 | 59 | |
mbedAustin | 11:cada08fc8a70 | 60 | /* |
mbedAustin | 11:cada08fc8a70 | 61 | * Create the optional <stdint.h> 24-bit types if they don't exist (worth trying |
mbedAustin | 11:cada08fc8a70 | 62 | * to use them, as they could exist and be more efficient than 32-bit on 8-bit |
mbedAustin | 11:cada08fc8a70 | 63 | * systems...) |
mbedAustin | 11:cada08fc8a70 | 64 | */ |
mbedAustin | 11:cada08fc8a70 | 65 | #ifndef UINT24_LEAST_MAX |
mbedAustin | 11:cada08fc8a70 | 66 | typedef uint_least32_t uint_least24_t; |
mbedAustin | 11:cada08fc8a70 | 67 | #define UINT_LEAST24_MAX UINT_LEAST32_MAX |
mbedAustin | 11:cada08fc8a70 | 68 | #define UINT24_C(x) UINT32_C(x) |
mbedAustin | 11:cada08fc8a70 | 69 | #define PRIoLEAST24 PRIoLEAST32 |
mbedAustin | 11:cada08fc8a70 | 70 | #define PRIuLEAST24 PRIuLEAST32 |
mbedAustin | 11:cada08fc8a70 | 71 | #define PRIxLEAST24 PRIxLEAST32 |
mbedAustin | 11:cada08fc8a70 | 72 | #define PRIXLEAST24 PRIXLEAST32 |
mbedAustin | 11:cada08fc8a70 | 73 | #endif |
mbedAustin | 11:cada08fc8a70 | 74 | |
mbedAustin | 11:cada08fc8a70 | 75 | #ifndef INT24_LEAST_MAX |
mbedAustin | 11:cada08fc8a70 | 76 | typedef int_least32_t int_least24_t; |
mbedAustin | 11:cada08fc8a70 | 77 | #define INT24_LEAST_MIN INT_LEAST32_MIN |
mbedAustin | 11:cada08fc8a70 | 78 | #define INT24_LEAST_MAX INT_LEAST32_MAX |
mbedAustin | 11:cada08fc8a70 | 79 | #define INT24_C(x) INT32_C(x) |
mbedAustin | 11:cada08fc8a70 | 80 | #define PRIdLEAST24 PRIdLEAST32 |
mbedAustin | 11:cada08fc8a70 | 81 | #define PRIiLEAST24 PRIiLEAST32 |
mbedAustin | 11:cada08fc8a70 | 82 | #endif |
mbedAustin | 11:cada08fc8a70 | 83 | |
mbedAustin | 11:cada08fc8a70 | 84 | #ifndef UINT24_FAST_MAX |
mbedAustin | 11:cada08fc8a70 | 85 | typedef uint_fast32_t uint_fast24_t; |
mbedAustin | 11:cada08fc8a70 | 86 | #define UINT_FAST24_MAX UINT_FAST32_MAX |
mbedAustin | 11:cada08fc8a70 | 87 | #define PRIoFAST24 PRIoFAST32 |
mbedAustin | 11:cada08fc8a70 | 88 | #define PRIuFAST24 PRIuFAST32 |
mbedAustin | 11:cada08fc8a70 | 89 | #define PRIxFAST24 PRIxFAST32 |
mbedAustin | 11:cada08fc8a70 | 90 | #define PRIXFAST24 PRIXFAST32 |
mbedAustin | 11:cada08fc8a70 | 91 | #endif |
mbedAustin | 11:cada08fc8a70 | 92 | |
mbedAustin | 11:cada08fc8a70 | 93 | #ifndef INT24_FAST_MAX |
mbedAustin | 11:cada08fc8a70 | 94 | typedef int_fast32_t int_fast24_t; |
mbedAustin | 11:cada08fc8a70 | 95 | #define INT_FAST24_MIN INT_FAST32_MIN |
mbedAustin | 11:cada08fc8a70 | 96 | #define INT_FAST24_MAX INT_FAST32_MAX |
mbedAustin | 11:cada08fc8a70 | 97 | #define PRIdFAST24 PRIdFAST32 |
mbedAustin | 11:cada08fc8a70 | 98 | #define PRIiFAST24 PRIiFAST32 |
mbedAustin | 11:cada08fc8a70 | 99 | #endif |
mbedAustin | 11:cada08fc8a70 | 100 | |
mbedAustin | 11:cada08fc8a70 | 101 | /* Function attribute - C11 "noreturn" or C++11 "[[noreturn]]" */ |
mbedAustin | 11:cada08fc8a70 | 102 | #ifndef NS_NORETURN |
mbedAustin | 11:cada08fc8a70 | 103 | #if defined __cplusplus && __cplusplus >= 201103L |
mbedAustin | 11:cada08fc8a70 | 104 | #define NS_NORETURN [[noreturn]] |
mbedAustin | 11:cada08fc8a70 | 105 | #elif !defined __cplusplus && __STDC_VERSION__ >= 201112L |
mbedAustin | 11:cada08fc8a70 | 106 | #define NS_NORETURN _Noreturn |
mbedAustin | 11:cada08fc8a70 | 107 | #elif defined __GNUC__ |
mbedAustin | 11:cada08fc8a70 | 108 | #define NS_NORETURN __attribute__((__noreturn__)) |
mbedAustin | 11:cada08fc8a70 | 109 | #elif defined __CC_ARM |
mbedAustin | 11:cada08fc8a70 | 110 | #define NS_NORETURN __declspec(noreturn) |
mbedAustin | 11:cada08fc8a70 | 111 | #elif defined __IAR_SYSTEMS_ICC__ |
mbedAustin | 11:cada08fc8a70 | 112 | #define NS_NORETURN __noreturn |
mbedAustin | 11:cada08fc8a70 | 113 | #else |
mbedAustin | 11:cada08fc8a70 | 114 | #define NS_NORETURN |
mbedAustin | 11:cada08fc8a70 | 115 | #endif |
mbedAustin | 11:cada08fc8a70 | 116 | #endif |
mbedAustin | 11:cada08fc8a70 | 117 | |
mbedAustin | 11:cada08fc8a70 | 118 | /* C11's "alignas" macro, emulated for integer expressions if necessary */ |
mbedAustin | 11:cada08fc8a70 | 119 | #ifndef __alignas_is_defined |
mbedAustin | 11:cada08fc8a70 | 120 | #if __STDC_VERSION__ >= 201112L || (defined __cplusplus && __cplusplus >= 201103L) |
mbedAustin | 11:cada08fc8a70 | 121 | #include <stdalign.h> |
mbedAustin | 11:cada08fc8a70 | 122 | #elif defined __GNUC__ |
mbedAustin | 11:cada08fc8a70 | 123 | #define alignas(n) __attribute__((__aligned__(n))) |
mbedAustin | 11:cada08fc8a70 | 124 | #define __alignas_is_defined 1 |
mbedAustin | 11:cada08fc8a70 | 125 | #elif defined __CC_ARM || defined __TASKING__ |
mbedAustin | 11:cada08fc8a70 | 126 | #define alignas(n) __align(n) |
mbedAustin | 11:cada08fc8a70 | 127 | #define __alignas_is_defined 1 |
mbedAustin | 11:cada08fc8a70 | 128 | #elif defined __IAR_SYSTEMS_ICC__ |
mbedAustin | 11:cada08fc8a70 | 129 | /* Does this really just apply to the next variable? */ |
mbedAustin | 11:cada08fc8a70 | 130 | #define alignas(n) __Alignas(data_alignment=n) |
mbedAustin | 11:cada08fc8a70 | 131 | #define __Alignas(x) _Pragma(#x) |
mbedAustin | 11:cada08fc8a70 | 132 | #define __alignas_is_defined 1 |
mbedAustin | 11:cada08fc8a70 | 133 | #endif |
mbedAustin | 11:cada08fc8a70 | 134 | #endif |
mbedAustin | 11:cada08fc8a70 | 135 | |
mbedAustin | 11:cada08fc8a70 | 136 | /** |
mbedAustin | 11:cada08fc8a70 | 137 | * Marker for functions or objects that may be unused, suppressing warnings. |
mbedAustin | 11:cada08fc8a70 | 138 | * Place after the identifier: |
mbedAustin | 11:cada08fc8a70 | 139 | * ~~~ |
mbedAustin | 11:cada08fc8a70 | 140 | * static int X MAYBE_UNUSED = 3; |
mbedAustin | 11:cada08fc8a70 | 141 | * static int foo(void) MAYBE_UNUSED; |
mbedAustin | 11:cada08fc8a70 | 142 | * ~~~ |
mbedAustin | 11:cada08fc8a70 | 143 | */ |
mbedAustin | 11:cada08fc8a70 | 144 | #if defined __CC_ARM || defined __GNUC__ |
mbedAustin | 11:cada08fc8a70 | 145 | #define MAYBE_UNUSED __attribute__((unused)) |
mbedAustin | 11:cada08fc8a70 | 146 | #else |
mbedAustin | 11:cada08fc8a70 | 147 | #define MAYBE_UNUSED |
mbedAustin | 11:cada08fc8a70 | 148 | #endif |
mbedAustin | 11:cada08fc8a70 | 149 | |
mbedAustin | 11:cada08fc8a70 | 150 | /* |
mbedAustin | 11:cada08fc8a70 | 151 | * C++ (even C++11) doesn't provide restrict: define away or provide |
mbedAustin | 11:cada08fc8a70 | 152 | * alternative. |
mbedAustin | 11:cada08fc8a70 | 153 | */ |
mbedAustin | 11:cada08fc8a70 | 154 | #ifdef __cplusplus |
mbedAustin | 11:cada08fc8a70 | 155 | #ifdef __GNUC__ |
mbedAustin | 11:cada08fc8a70 | 156 | #define restrict __restrict |
mbedAustin | 11:cada08fc8a70 | 157 | #else |
mbedAustin | 11:cada08fc8a70 | 158 | #define restrict |
mbedAustin | 11:cada08fc8a70 | 159 | #endif |
mbedAustin | 11:cada08fc8a70 | 160 | #endif /* __cplusplus */ |
mbedAustin | 11:cada08fc8a70 | 161 | |
mbedAustin | 11:cada08fc8a70 | 162 | |
mbedAustin | 11:cada08fc8a70 | 163 | /** |
mbedAustin | 11:cada08fc8a70 | 164 | * C++ doesn't allow "static" in function parameter types: ie |
mbedAustin | 11:cada08fc8a70 | 165 | * ~~~ |
mbedAustin | 11:cada08fc8a70 | 166 | * entry_t *find_entry(const uint8_t address[static 16]) |
mbedAustin | 11:cada08fc8a70 | 167 | * ~~~ |
mbedAustin | 11:cada08fc8a70 | 168 | * If a header file may be included from C++, use this __static define instead. |
mbedAustin | 11:cada08fc8a70 | 169 | * |
mbedAustin | 11:cada08fc8a70 | 170 | * (Syntax introduced in C99 - `uint8_t address[16]` in a prototype was always |
mbedAustin | 11:cada08fc8a70 | 171 | * equivalent to `uint8_t *address`, but the C99 addition of static tells the |
mbedAustin | 11:cada08fc8a70 | 172 | * compiler that address is never NULL, and always points to at least 16 |
mbedAustin | 11:cada08fc8a70 | 173 | * elements. This adds no new type-checking, but the information could aid |
mbedAustin | 11:cada08fc8a70 | 174 | * compiler optimisation, and it can serve as documentation). |
mbedAustin | 11:cada08fc8a70 | 175 | */ |
mbedAustin | 11:cada08fc8a70 | 176 | #ifdef __cplusplus |
mbedAustin | 11:cada08fc8a70 | 177 | #define __static |
mbedAustin | 11:cada08fc8a70 | 178 | #else |
mbedAustin | 11:cada08fc8a70 | 179 | #define __static static |
mbedAustin | 11:cada08fc8a70 | 180 | #endif |
mbedAustin | 11:cada08fc8a70 | 181 | |
mbedAustin | 11:cada08fc8a70 | 182 | #ifdef __GNUC__ |
mbedAustin | 11:cada08fc8a70 | 183 | #define NS_GCC_VERSION (__GNUC__ * 10000 \ |
mbedAustin | 11:cada08fc8a70 | 184 | + __GNUC_MINOR__ * 100 \ |
mbedAustin | 11:cada08fc8a70 | 185 | + __GNUC_PATCHLEVEL__) |
mbedAustin | 11:cada08fc8a70 | 186 | #endif |
mbedAustin | 11:cada08fc8a70 | 187 | |
mbedAustin | 11:cada08fc8a70 | 188 | /** \brief Compile-time assertion |
mbedAustin | 11:cada08fc8a70 | 189 | * |
mbedAustin | 11:cada08fc8a70 | 190 | * C11 provides _Static_assert, as does GCC even in C99 mode (and |
mbedAustin | 11:cada08fc8a70 | 191 | * as a freestanding implementation, we can't rely on <assert.h> to get |
mbedAustin | 11:cada08fc8a70 | 192 | * the static_assert macro). |
mbedAustin | 11:cada08fc8a70 | 193 | * C++11 provides static_assert as a keyword, as does G++ in C++0x mode. |
mbedAustin | 11:cada08fc8a70 | 194 | * |
mbedAustin | 11:cada08fc8a70 | 195 | * The assertion acts as a declaration that can be placed at file scope, in a |
mbedAustin | 11:cada08fc8a70 | 196 | * code block (except after a label), or as a member of a struct/union. It |
mbedAustin | 11:cada08fc8a70 | 197 | * produces a compiler error if "test" evaluates to 0. |
mbedAustin | 11:cada08fc8a70 | 198 | * |
mbedAustin | 11:cada08fc8a70 | 199 | * Note that this *includes* the required semicolon when defined, else it |
mbedAustin | 11:cada08fc8a70 | 200 | * is totally empty, permitting use in structs. (If the user provided the `;`, |
mbedAustin | 11:cada08fc8a70 | 201 | * it would leave an illegal stray `;` if unavailable). |
mbedAustin | 11:cada08fc8a70 | 202 | */ |
mbedAustin | 11:cada08fc8a70 | 203 | #ifdef __cplusplus |
mbedAustin | 11:cada08fc8a70 | 204 | # if __cplusplus >= 201103L || __cpp_static_assert >= 200410 |
mbedAustin | 11:cada08fc8a70 | 205 | # define NS_STATIC_ASSERT(test, str) static_assert(test, str); |
mbedAustin | 11:cada08fc8a70 | 206 | # elif defined __GXX_EXPERIMENTAL_CXX0X__ && NS_GCC_VERSION >= 40300 |
mbedAustin | 11:cada08fc8a70 | 207 | # define NS_STATIC_ASSERT(test, str) __extension__ static_assert(test, str); |
mbedAustin | 11:cada08fc8a70 | 208 | # else |
mbedAustin | 11:cada08fc8a70 | 209 | # define NS_STATIC_ASSERT(test, str) |
mbedAustin | 11:cada08fc8a70 | 210 | # endif |
mbedAustin | 11:cada08fc8a70 | 211 | #else /* C */ |
mbedAustin | 11:cada08fc8a70 | 212 | # if __STDC_VERSION__ >= 201112L |
mbedAustin | 11:cada08fc8a70 | 213 | # define NS_STATIC_ASSERT(test, str) _Static_assert(test, str); |
mbedAustin | 11:cada08fc8a70 | 214 | # elif defined __GNUC__ && NS_GCC_VERSION >= 40600 && !defined __CC_ARM |
mbedAustin | 11:cada08fc8a70 | 215 | # define NS_STATIC_ASSERT(test, str) __extension__ _Static_assert(test, str); |
mbedAustin | 11:cada08fc8a70 | 216 | # else |
mbedAustin | 11:cada08fc8a70 | 217 | # define NS_STATIC_ASSERT(test, str) |
mbedAustin | 11:cada08fc8a70 | 218 | #endif |
mbedAustin | 11:cada08fc8a70 | 219 | #endif |
mbedAustin | 11:cada08fc8a70 | 220 | |
mbedAustin | 11:cada08fc8a70 | 221 | /** \brief Pragma to suppress warnings about unusual pointer values. |
mbedAustin | 11:cada08fc8a70 | 222 | * |
mbedAustin | 11:cada08fc8a70 | 223 | * Useful if using "poison" values. |
mbedAustin | 11:cada08fc8a70 | 224 | */ |
mbedAustin | 11:cada08fc8a70 | 225 | #ifdef __IAR_SYSTEMS_ICC__ |
mbedAustin | 11:cada08fc8a70 | 226 | #define NS_FUNNY_INTPTR_OK _Pragma("diag_suppress=Pe1053") |
mbedAustin | 11:cada08fc8a70 | 227 | #define NS_FUNNY_INTPTR_RESTORE _Pragma("diag_default=Pe1053") |
mbedAustin | 11:cada08fc8a70 | 228 | #else |
mbedAustin | 11:cada08fc8a70 | 229 | #define NS_FUNNY_INTPTR_OK |
mbedAustin | 11:cada08fc8a70 | 230 | #define NS_FUNNY_INTPTR_RESTORE |
mbedAustin | 11:cada08fc8a70 | 231 | #endif |
mbedAustin | 11:cada08fc8a70 | 232 | |
mbedAustin | 11:cada08fc8a70 | 233 | /** \brief Convert pointer to member to pointer to containing structure */ |
mbedAustin | 11:cada08fc8a70 | 234 | #define NS_CONTAINER_OF(ptr, type, member) \ |
mbedAustin | 11:cada08fc8a70 | 235 | ((type *) ((char *) (ptr) - offsetof(type, member))) |
mbedAustin | 11:cada08fc8a70 | 236 | |
mbedAustin | 11:cada08fc8a70 | 237 | /* |
mbedAustin | 11:cada08fc8a70 | 238 | * Inlining could cause problems when mixing with C++; provide a mechanism to |
mbedAustin | 11:cada08fc8a70 | 239 | * disable it. This could also be turned off for other reasons (although |
mbedAustin | 11:cada08fc8a70 | 240 | * this can usually be done through a compiler flag, eg -O0 on gcc). |
mbedAustin | 11:cada08fc8a70 | 241 | */ |
mbedAustin | 11:cada08fc8a70 | 242 | #ifndef __cplusplus |
mbedAustin | 11:cada08fc8a70 | 243 | #define NS_ALLOW_INLINING |
mbedAustin | 11:cada08fc8a70 | 244 | #endif |
mbedAustin | 11:cada08fc8a70 | 245 | |
mbedAustin | 11:cada08fc8a70 | 246 | /* There is inlining problem in GCC version 4.1.x and we know it works in 4.6.3 */ |
mbedAustin | 11:cada08fc8a70 | 247 | #if defined __GNUC__ && NS_GCC_VERSION < 40600 |
mbedAustin | 11:cada08fc8a70 | 248 | #undef NS_ALLOW_INLINING |
mbedAustin | 11:cada08fc8a70 | 249 | #endif |
mbedAustin | 11:cada08fc8a70 | 250 | |
mbedAustin | 11:cada08fc8a70 | 251 | /** \brief Mark a potentially-inlineable function. |
mbedAustin | 11:cada08fc8a70 | 252 | * |
mbedAustin | 11:cada08fc8a70 | 253 | * We follow C99 semantics, which requires precisely one external definition. |
mbedAustin | 11:cada08fc8a70 | 254 | * To also allow inlining to be totally bypassed under control of |
mbedAustin | 11:cada08fc8a70 | 255 | * NS_ALLOW_INLINING, code can be structured as per the example of ns_list: |
mbedAustin | 11:cada08fc8a70 | 256 | * |
mbedAustin | 11:cada08fc8a70 | 257 | * foo.h |
mbedAustin | 11:cada08fc8a70 | 258 | * ----- |
mbedAustin | 11:cada08fc8a70 | 259 | * ~~~ |
mbedAustin | 11:cada08fc8a70 | 260 | * NS_INLINE int my_func(int); |
mbedAustin | 11:cada08fc8a70 | 261 | * |
mbedAustin | 11:cada08fc8a70 | 262 | * #if defined NS_ALLOW_INLINING || defined FOO_FN |
mbedAustin | 11:cada08fc8a70 | 263 | * #ifndef FOO_FN |
mbedAustin | 11:cada08fc8a70 | 264 | * #define FOO_FN NS_INLINE |
mbedAustin | 11:cada08fc8a70 | 265 | * #endif |
mbedAustin | 11:cada08fc8a70 | 266 | * FOO_FN int my_func(int a) |
mbedAustin | 11:cada08fc8a70 | 267 | * { |
mbedAustin | 11:cada08fc8a70 | 268 | * definition; |
mbedAustin | 11:cada08fc8a70 | 269 | * } |
mbedAustin | 11:cada08fc8a70 | 270 | * #endif |
mbedAustin | 11:cada08fc8a70 | 271 | * ~~~ |
mbedAustin | 11:cada08fc8a70 | 272 | * foo.c |
mbedAustin | 11:cada08fc8a70 | 273 | * ----- |
mbedAustin | 11:cada08fc8a70 | 274 | * ~~~ |
mbedAustin | 11:cada08fc8a70 | 275 | * #define FOO_FN extern |
mbedAustin | 11:cada08fc8a70 | 276 | * #include "foo.h" |
mbedAustin | 11:cada08fc8a70 | 277 | * ~~~ |
mbedAustin | 11:cada08fc8a70 | 278 | * Which generates: |
mbedAustin | 11:cada08fc8a70 | 279 | * ~~~ |
mbedAustin | 11:cada08fc8a70 | 280 | * NS_ALLOW_INLINING set NS_ALLOW_INLINING unset |
mbedAustin | 11:cada08fc8a70 | 281 | * ===================== ======================= |
mbedAustin | 11:cada08fc8a70 | 282 | * Include foo.h Include foo.h |
mbedAustin | 11:cada08fc8a70 | 283 | * ------------- ------------- |
mbedAustin | 11:cada08fc8a70 | 284 | * inline int my_func(int); int my_func(int); |
mbedAustin | 11:cada08fc8a70 | 285 | * |
mbedAustin | 11:cada08fc8a70 | 286 | * // inline definition |
mbedAustin | 11:cada08fc8a70 | 287 | * inline int my_func(int a) |
mbedAustin | 11:cada08fc8a70 | 288 | * { |
mbedAustin | 11:cada08fc8a70 | 289 | * definition; |
mbedAustin | 11:cada08fc8a70 | 290 | * } |
mbedAustin | 11:cada08fc8a70 | 291 | * |
mbedAustin | 11:cada08fc8a70 | 292 | * Compile foo.c Compile foo.c |
mbedAustin | 11:cada08fc8a70 | 293 | * ------------- ------------- |
mbedAustin | 11:cada08fc8a70 | 294 | * (from .h) inline int my_func(int); int my_func(int); |
mbedAustin | 11:cada08fc8a70 | 295 | * |
mbedAustin | 11:cada08fc8a70 | 296 | * // external definition |
mbedAustin | 11:cada08fc8a70 | 297 | * // because of no "inline" // normal external definition |
mbedAustin | 11:cada08fc8a70 | 298 | * extern int my_func(int a) extern int my_func(int a) |
mbedAustin | 11:cada08fc8a70 | 299 | * { { |
mbedAustin | 11:cada08fc8a70 | 300 | * definition; definition; |
mbedAustin | 11:cada08fc8a70 | 301 | * } } |
mbedAustin | 11:cada08fc8a70 | 302 | * ~~~ |
mbedAustin | 11:cada08fc8a70 | 303 | * |
mbedAustin | 11:cada08fc8a70 | 304 | * Note that even with inline keywords, whether the compiler inlines or not is |
mbedAustin | 11:cada08fc8a70 | 305 | * up to it. For example, gcc at "-O0" will not inline at all, and will always |
mbedAustin | 11:cada08fc8a70 | 306 | * call the real functions in foo.o, just as if NS_ALLOW_INLINING was unset. |
mbedAustin | 11:cada08fc8a70 | 307 | * At "-O2", gcc could potentially inline everything, meaning that foo.o is not |
mbedAustin | 11:cada08fc8a70 | 308 | * referenced at all. |
mbedAustin | 11:cada08fc8a70 | 309 | * |
mbedAustin | 11:cada08fc8a70 | 310 | * Alternatively, you could use "static inline", which gives every caller its |
mbedAustin | 11:cada08fc8a70 | 311 | * own internal definition. This is compatible with C++ inlining (which expects |
mbedAustin | 11:cada08fc8a70 | 312 | * the linker to eliminate duplicates), but in C it's less efficient if the code |
mbedAustin | 11:cada08fc8a70 | 313 | * ends up non-inlined, and it's harder to breakpoint. I don't recommend it |
mbedAustin | 11:cada08fc8a70 | 314 | * except for the most trivial functions (which could then probably be macros). |
mbedAustin | 11:cada08fc8a70 | 315 | */ |
mbedAustin | 11:cada08fc8a70 | 316 | #ifdef NS_ALLOW_INLINING |
mbedAustin | 11:cada08fc8a70 | 317 | #define NS_INLINE inline |
mbedAustin | 11:cada08fc8a70 | 318 | #else |
mbedAustin | 11:cada08fc8a70 | 319 | #define NS_INLINE |
mbedAustin | 11:cada08fc8a70 | 320 | #endif |
mbedAustin | 11:cada08fc8a70 | 321 | |
mbedAustin | 11:cada08fc8a70 | 322 | #if defined __SDCC_mcs51 || defined __ICC8051__ || defined __C51__ |
mbedAustin | 11:cada08fc8a70 | 323 | |
mbedAustin | 11:cada08fc8a70 | 324 | /* The 8051 environments: SDCC (historic), IAR (current), Keil (future?) */ |
mbedAustin | 11:cada08fc8a70 | 325 | |
mbedAustin | 11:cada08fc8a70 | 326 | #define NS_LARGE __xdata |
mbedAustin | 11:cada08fc8a70 | 327 | #define NS_LARGE_PTR __xdata |
mbedAustin | 11:cada08fc8a70 | 328 | #ifdef __ICC8051__ |
mbedAustin | 11:cada08fc8a70 | 329 | #define NS_REENTRANT |
mbedAustin | 11:cada08fc8a70 | 330 | #define NS_REENTRANT_PREFIX __idata_reentrant |
mbedAustin | 11:cada08fc8a70 | 331 | #else |
mbedAustin | 11:cada08fc8a70 | 332 | #define NS_REENTRANT __reentrant |
mbedAustin | 11:cada08fc8a70 | 333 | #define NS_REENTRANT_PREFIX |
mbedAustin | 11:cada08fc8a70 | 334 | #endif |
mbedAustin | 11:cada08fc8a70 | 335 | #define NS_NEAR_FUNC __near_func |
mbedAustin | 11:cada08fc8a70 | 336 | |
mbedAustin | 11:cada08fc8a70 | 337 | #else |
mbedAustin | 11:cada08fc8a70 | 338 | |
mbedAustin | 11:cada08fc8a70 | 339 | /* "Normal" systems. Define it all away. */ |
mbedAustin | 11:cada08fc8a70 | 340 | #define NS_LARGE |
mbedAustin | 11:cada08fc8a70 | 341 | #define NS_LARGE_PTR |
mbedAustin | 11:cada08fc8a70 | 342 | #define NS_REENTRANT |
mbedAustin | 11:cada08fc8a70 | 343 | #define NS_REENTRANT_PREFIX |
mbedAustin | 11:cada08fc8a70 | 344 | #define NS_NEAR_FUNC |
mbedAustin | 11:cada08fc8a70 | 345 | |
mbedAustin | 11:cada08fc8a70 | 346 | #endif |
mbedAustin | 11:cada08fc8a70 | 347 | |
mbedAustin | 11:cada08fc8a70 | 348 | /** \brief Scatter-gather descriptor |
mbedAustin | 11:cada08fc8a70 | 349 | * |
mbedAustin | 11:cada08fc8a70 | 350 | * Slightly optimised for small platforms - we assume we won't need any |
mbedAustin | 11:cada08fc8a70 | 351 | * element bigger than 64K. |
mbedAustin | 11:cada08fc8a70 | 352 | */ |
mbedAustin | 11:cada08fc8a70 | 353 | typedef struct ns_iovec { |
mbedAustin | 11:cada08fc8a70 | 354 | void *iov_base; |
mbedAustin | 11:cada08fc8a70 | 355 | uint_fast16_t iov_len; |
mbedAustin | 11:cada08fc8a70 | 356 | } ns_iovec_t; |
mbedAustin | 11:cada08fc8a70 | 357 | |
mbedAustin | 11:cada08fc8a70 | 358 | #endif /* NS_TYPES_H */ |