1 /** 2 * @file 3 * Support for different processor and compiler architectures 4 */ 5 6 /* 7 * Copyright (c) 2001-2004 Swedish Institute of Computer Science. 8 * All rights reserved. 9 * 10 * Redistribution and use in source and binary forms, with or without modification, 11 * are permitted provided that the following conditions are met: 12 * 13 * 1. Redistributions of source code must retain the above copyright notice, 14 * this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright notice, 16 * this list of conditions and the following disclaimer in the documentation 17 * and/or other materials provided with the distribution. 18 * 3. The name of the author may not be used to endorse or promote products 19 * derived from this software without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 22 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 23 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 24 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 26 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 29 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 30 * OF SUCH DAMAGE. 31 * 32 * This file is part of the lwIP TCP/IP stack. 33 * 34 * Author: Adam Dunkels <adam@sics.se> 35 * 36 */ 37 #ifndef LWIP_HDR_ARCH_H 38 #define LWIP_HDR_ARCH_H 39 40 #ifndef LITTLE_ENDIAN 41 #define LITTLE_ENDIAN 1234 42 #endif 43 44 #ifndef BIG_ENDIAN 45 #define BIG_ENDIAN 4321 46 #endif 47 48 #include "arch/cc.h" 49 50 /** 51 * @defgroup compiler_abstraction Compiler/platform abstraction 52 * @ingroup sys_layer 53 * All defines related to this section must not be placed in lwipopts.h, 54 * but in arch/cc.h! 55 * If the compiler does not provide memset() this file must include a 56 * definition of it, or include a file which defines it. 57 * These options cannot be \#defined in lwipopts.h since they are not options 58 * of lwIP itself, but options of the lwIP port to your system. 59 * @{ 60 */ 61 62 /** Define the byte order of the system. 63 * Needed for conversion of network data to host byte order. 64 * Allowed values: LITTLE_ENDIAN and BIG_ENDIAN 65 */ 66 #ifndef BYTE_ORDER 67 #define BYTE_ORDER LITTLE_ENDIAN 68 #endif 69 70 /** Define random number generator function of your system */ 71 #ifdef __DOXYGEN__ 72 #define LWIP_RAND() ((u32_t)rand()) 73 #endif 74 75 /** Platform specific diagnostic output.<br> 76 * Note the default implementation pulls in printf, which may 77 * in turn pull in a lot of standard library code. In resource-constrained 78 * systems, this should be defined to something less resource-consuming. 79 */ 80 #ifndef LWIP_PLATFORM_DIAG 81 #define LWIP_PLATFORM_DIAG(x) do {printf x;} while(0) 82 #include <stdio.h> 83 #include <stdlib.h> 84 #endif 85 86 /** Platform specific assertion handling.<br> 87 * Note the default implementation pulls in printf, fflush and abort, which may 88 * in turn pull in a lot of standard library code. In resource-constrained 89 * systems, this should be defined to something less resource-consuming. 90 */ 91 #ifndef LWIP_PLATFORM_ASSERT 92 #define LWIP_PLATFORM_ASSERT(x) do {printf("Assertion \"%s\" failed at line %d in %s\n", \ 93 x, __LINE__, __FILE__); fflush(NULL); abort();} while(0) 94 #include <stdio.h> 95 #include <stdlib.h> 96 #endif 97 98 /** Define this to 1 in arch/cc.h of your port if you do not want to 99 * include stddef.h header to get size_t. You need to typedef size_t 100 * by yourself in this case. 101 */ 102 #ifndef LWIP_NO_STDDEF_H 103 #define LWIP_NO_STDDEF_H 0 104 #endif 105 106 #if !LWIP_NO_STDDEF_H 107 #include <stddef.h> /* for size_t */ 108 #endif 109 110 /** Define this to 1 in arch/cc.h of your port if your compiler does not provide 111 * the stdint.h header. You need to typedef the generic types listed in 112 * lwip/arch.h yourself in this case (u8_t, u16_t...). 113 */ 114 #ifndef LWIP_NO_STDINT_H 115 #define LWIP_NO_STDINT_H 0 116 #endif 117 118 /* Define generic types used in lwIP */ 119 #if !LWIP_NO_STDINT_H 120 #include <stdint.h> 121 /* stdint.h is C99 which should also provide support for 64-bit integers */ 122 #if !defined(LWIP_HAVE_INT64) && defined(UINT64_MAX) 123 #define LWIP_HAVE_INT64 1 124 #endif 125 typedef uint8_t u8_t; 126 typedef int8_t s8_t; 127 typedef uint16_t u16_t; 128 typedef int16_t s16_t; 129 typedef uint32_t u32_t; 130 typedef int32_t s32_t; 131 #if LWIP_HAVE_INT64 132 typedef uint64_t u64_t; 133 typedef int64_t s64_t; 134 #endif 135 typedef uintptr_t mem_ptr_t; 136 #endif 137 138 /** Define this to 1 in arch/cc.h of your port if your compiler does not provide 139 * the inttypes.h header. You need to define the format strings listed in 140 * lwip/arch.h yourself in this case (X8_F, U16_F...). 141 */ 142 #ifndef LWIP_NO_INTTYPES_H 143 #define LWIP_NO_INTTYPES_H 0 144 #endif 145 146 /* Define (sn)printf formatters for these lwIP types */ 147 #if !LWIP_NO_INTTYPES_H 148 #include <inttypes.h> 149 #ifndef X8_F 150 #define X8_F "02" PRIx8 151 #endif 152 #ifndef U16_F 153 #define U16_F PRIu16 154 #endif 155 #ifndef S16_F 156 #define S16_F PRId16 157 #endif 158 #ifndef X16_F 159 #define X16_F PRIx16 160 #endif 161 #ifndef U32_F 162 #define U32_F PRIu32 163 #endif 164 #ifndef S32_F 165 #define S32_F PRId32 166 #endif 167 #ifndef X32_F 168 #define X32_F PRIx32 169 #endif 170 #ifndef SZT_F 171 #define SZT_F PRIuPTR 172 #endif 173 #endif 174 175 /** Define this to 1 in arch/cc.h of your port if your compiler does not provide 176 * the limits.h header. You need to define the type limits yourself in this case 177 * (e.g. INT_MAX, SSIZE_MAX). 178 */ 179 #ifndef LWIP_NO_LIMITS_H 180 #define LWIP_NO_LIMITS_H 0 181 #endif 182 183 /* Include limits.h? */ 184 #if !LWIP_NO_LIMITS_H 185 #include <limits.h> 186 #endif 187 188 /* Do we need to define ssize_t? This is a compatibility hack: 189 * Unfortunately, this type seems to be unavailable on some systems (even if 190 * sys/types or unistd.h are available). 191 * Being like that, we define it to 'int' if SSIZE_MAX is not defined. 192 */ 193 #ifdef SSIZE_MAX 194 /* If SSIZE_MAX is defined, unistd.h should provide the type as well */ 195 #ifndef LWIP_NO_UNISTD_H 196 #define LWIP_NO_UNISTD_H 0 197 #endif 198 #if !LWIP_NO_UNISTD_H 199 #include <unistd.h> 200 #endif 201 #else /* SSIZE_MAX */ 202 typedef int ssize_t; 203 #define SSIZE_MAX INT_MAX 204 #endif /* SSIZE_MAX */ 205 206 /* some maximum values needed in lwip code */ 207 #define LWIP_UINT32_MAX 0xffffffff 208 209 /** Define this to 1 in arch/cc.h of your port if your compiler does not provide 210 * the ctype.h header. If ctype.h is available, a few character functions 211 * are mapped to the appropriate functions (lwip_islower, lwip_isdigit...), if 212 * not, a private implementation is provided. 213 */ 214 #ifndef LWIP_NO_CTYPE_H 215 #define LWIP_NO_CTYPE_H 0 216 #endif 217 218 #if LWIP_NO_CTYPE_H 219 #define lwip_in_range(c, lo, up) ((u8_t)(c) >= (lo) && (u8_t)(c) <= (up)) 220 #define lwip_isdigit(c) lwip_in_range((c), '0', '9') 221 #define lwip_isxdigit(c) (lwip_isdigit(c) || lwip_in_range((c), 'a', 'f') || lwip_in_range((c), 'A', 'F')) 222 #define lwip_islower(c) lwip_in_range((c), 'a', 'z') 223 #define lwip_isspace(c) ((c) == ' ' || (c) == '\f' || (c) == '\n' || (c) == '\r' || (c) == '\t' || (c) == '\v') 224 #define lwip_isupper(c) lwip_in_range((c), 'A', 'Z') 225 #define lwip_tolower(c) (lwip_isupper(c) ? (c) - 'A' + 'a' : c) 226 #define lwip_toupper(c) (lwip_islower(c) ? (c) - 'a' + 'A' : c) 227 #else 228 #include <ctype.h> 229 #define lwip_isdigit(c) isdigit((unsigned char)(c)) 230 #define lwip_isxdigit(c) isxdigit((unsigned char)(c)) 231 #define lwip_islower(c) islower((unsigned char)(c)) 232 #define lwip_isspace(c) isspace((unsigned char)(c)) 233 #define lwip_isupper(c) isupper((unsigned char)(c)) 234 #define lwip_tolower(c) tolower((unsigned char)(c)) 235 #define lwip_toupper(c) toupper((unsigned char)(c)) 236 #endif 237 238 /** C++ const_cast<target_type>(val) equivalent to remove constness from a value (GCC -Wcast-qual) */ 239 #ifndef LWIP_CONST_CAST 240 #define LWIP_CONST_CAST(target_type, val) ((target_type)((ptrdiff_t)val)) 241 #endif 242 243 /** Get rid of alignment cast warnings (GCC -Wcast-align) */ 244 #ifndef LWIP_ALIGNMENT_CAST 245 #define LWIP_ALIGNMENT_CAST(target_type, val) LWIP_CONST_CAST(target_type, val) 246 #endif 247 248 /** Get rid of warnings related to pointer-to-numeric and vice-versa casts, 249 * e.g. "conversion from 'u8_t' to 'void *' of greater size" 250 */ 251 #ifndef LWIP_PTR_NUMERIC_CAST 252 #define LWIP_PTR_NUMERIC_CAST(target_type, val) LWIP_CONST_CAST(target_type, val) 253 #endif 254 255 /** Avoid warnings/errors related to implicitly casting away packed attributes by doing a explicit cast */ 256 #ifndef LWIP_PACKED_CAST 257 #define LWIP_PACKED_CAST(target_type, val) LWIP_CONST_CAST(target_type, val) 258 #endif 259 260 /** Allocates a memory buffer of specified size that is of sufficient size to align 261 * its start address using LWIP_MEM_ALIGN. 262 * You can declare your own version here e.g. to enforce alignment without adding 263 * trailing padding bytes (see LWIP_MEM_ALIGN_BUFFER) or your own section placement 264 * requirements.<br> 265 * e.g. if you use gcc and need 32 bit alignment:<br> 266 * \#define LWIP_DECLARE_MEMORY_ALIGNED(variable_name, size) u8_t variable_name[size] \_\_attribute\_\_((aligned(4)))<br> 267 * or more portable:<br> 268 * \#define LWIP_DECLARE_MEMORY_ALIGNED(variable_name, size) u32_t variable_name[(size + sizeof(u32_t) - 1) / sizeof(u32_t)] 269 */ 270 #ifndef LWIP_DECLARE_MEMORY_ALIGNED 271 #define LWIP_DECLARE_MEMORY_ALIGNED(variable_name, size) u8_t variable_name[LWIP_MEM_ALIGN_BUFFER(size)] 272 #endif 273 274 /** Calculate memory size for an aligned buffer - returns the next highest 275 * multiple of MEM_ALIGNMENT (e.g. LWIP_MEM_ALIGN_SIZE(3) and 276 * LWIP_MEM_ALIGN_SIZE(4) will both yield 4 for MEM_ALIGNMENT == 4). 277 */ 278 #ifndef LWIP_MEM_ALIGN_SIZE 279 #define LWIP_MEM_ALIGN_SIZE(size) (((size) + MEM_ALIGNMENT - 1U) & ~(MEM_ALIGNMENT-1U)) 280 #endif 281 282 /** Calculate safe memory size for an aligned buffer when using an unaligned 283 * type as storage. This includes a safety-margin on (MEM_ALIGNMENT - 1) at the 284 * start (e.g. if buffer is u8_t[] and actual data will be u32_t*) 285 */ 286 #ifndef LWIP_MEM_ALIGN_BUFFER 287 #define LWIP_MEM_ALIGN_BUFFER(size) (((size) + MEM_ALIGNMENT - 1U)) 288 #endif 289 290 /** Align a memory pointer to the alignment defined by MEM_ALIGNMENT 291 * so that ADDR % MEM_ALIGNMENT == 0 292 */ 293 #ifndef LWIP_MEM_ALIGN 294 #define LWIP_MEM_ALIGN(addr) ((void *)(((mem_ptr_t)(addr) + MEM_ALIGNMENT - 1) & ~(mem_ptr_t)(MEM_ALIGNMENT-1))) 295 #endif 296 297 #ifdef __cplusplus 298 extern "C" { 299 #endif 300 301 /** Packed structs support. 302 * Placed BEFORE declaration of a packed struct.<br> 303 * For examples of packed struct declarations, see include/lwip/prot/ subfolder.<br> 304 * A port to GCC/clang is included in lwIP, if you use these compilers there is nothing to do here. 305 */ 306 #ifndef PACK_STRUCT_BEGIN 307 #define PACK_STRUCT_BEGIN 308 #endif /* PACK_STRUCT_BEGIN */ 309 310 /** Packed structs support. 311 * Placed AFTER declaration of a packed struct.<br> 312 * For examples of packed struct declarations, see include/lwip/prot/ subfolder.<br> 313 * A port to GCC/clang is included in lwIP, if you use these compilers there is nothing to do here. 314 */ 315 #ifndef PACK_STRUCT_END 316 #define PACK_STRUCT_END 317 #endif /* PACK_STRUCT_END */ 318 319 /** Packed structs support. 320 * Placed between end of declaration of a packed struct and trailing semicolon.<br> 321 * For examples of packed struct declarations, see include/lwip/prot/ subfolder.<br> 322 * A port to GCC/clang is included in lwIP, if you use these compilers there is nothing to do here. 323 */ 324 #ifndef PACK_STRUCT_STRUCT 325 #if defined(__GNUC__) || defined(__clang__) 326 #define PACK_STRUCT_STRUCT __attribute__((packed)) 327 #else 328 #define PACK_STRUCT_STRUCT 329 #endif 330 #endif /* PACK_STRUCT_STRUCT */ 331 332 /** Packed structs support. 333 * Wraps u32_t and u16_t members.<br> 334 * For examples of packed struct declarations, see include/lwip/prot/ subfolder.<br> 335 * A port to GCC/clang is included in lwIP, if you use these compilers there is nothing to do here. 336 */ 337 #ifndef PACK_STRUCT_FIELD 338 #define PACK_STRUCT_FIELD(x) x 339 #endif /* PACK_STRUCT_FIELD */ 340 341 /** Packed structs support. 342 * Wraps u8_t members, where some compilers warn that packing is not necessary.<br> 343 * For examples of packed struct declarations, see include/lwip/prot/ subfolder.<br> 344 * A port to GCC/clang is included in lwIP, if you use these compilers there is nothing to do here. 345 */ 346 #ifndef PACK_STRUCT_FLD_8 347 #define PACK_STRUCT_FLD_8(x) PACK_STRUCT_FIELD(x) 348 #endif /* PACK_STRUCT_FLD_8 */ 349 350 /** Packed structs support. 351 * Wraps members that are packed structs themselves, where some compilers warn that packing is not necessary.<br> 352 * For examples of packed struct declarations, see include/lwip/prot/ subfolder.<br> 353 * A port to GCC/clang is included in lwIP, if you use these compilers there is nothing to do here. 354 */ 355 #ifndef PACK_STRUCT_FLD_S 356 #define PACK_STRUCT_FLD_S(x) PACK_STRUCT_FIELD(x) 357 #endif /* PACK_STRUCT_FLD_S */ 358 359 /** PACK_STRUCT_USE_INCLUDES==1: Packed structs support using \#include files before and after struct to be packed.<br> 360 * The file included BEFORE the struct is "arch/bpstruct.h".<br> 361 * The file included AFTER the struct is "arch/epstruct.h".<br> 362 * This can be used to implement struct packing on MS Visual C compilers, see 363 * the Win32 port in the lwIP/contrib subdir for reference. 364 * For examples of packed struct declarations, see include/lwip/prot/ subfolder.<br> 365 * A port to GCC/clang is included in lwIP, if you use these compilers there is nothing to do here. 366 */ 367 #ifdef __DOXYGEN__ 368 #define PACK_STRUCT_USE_INCLUDES 369 #endif 370 371 /** Eliminates compiler warning about unused arguments (GCC -Wextra -Wunused). */ 372 #ifndef LWIP_UNUSED_ARG 373 #define LWIP_UNUSED_ARG(x) (void)x 374 #endif /* LWIP_UNUSED_ARG */ 375 376 /** LWIP_PROVIDE_ERRNO==1: Let lwIP provide ERRNO values and the 'errno' variable. 377 * If this is disabled, cc.h must either define 'errno', include <errno.h>, 378 * define LWIP_ERRNO_STDINCLUDE to get <errno.h> included or 379 * define LWIP_ERRNO_INCLUDE to <errno.h> or equivalent. 380 */ 381 #if defined __DOXYGEN__ 382 #define LWIP_PROVIDE_ERRNO 383 #endif 384 385 /* Use a special, reproducable version of rand() for fuzz tests? */ 386 #ifdef LWIP_RAND_FOR_FUZZ 387 #ifdef LWIP_RAND 388 #undef LWIP_RAND 389 #endif 390 u32_t lwip_fuzz_rand(void); 391 #define LWIP_RAND() lwip_fuzz_rand() 392 #endif 393 394 /** 395 * @} 396 */ 397 398 #ifdef __cplusplus 399 } 400 #endif 401 402 #endif /* LWIP_HDR_ARCH_H */ 403