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.\n
76  * Note the default implementation pulls in printf, which may
77  * in turn pull in a lot of standard libary 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.\n
87  * Note the default implementation pulls in printf, fflush and abort, which may
88  * in turn pull in a lot of standard libary 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 "sys/types.h"
200 #else
201 typedef int ssize_t;
202 #endif
203 #else /* SSIZE_MAX */
204 typedef int ssize_t;
205 #define SSIZE_MAX INT_MAX
206 #endif /* SSIZE_MAX */
207 
208 /* some maximum values needed in lwip code */
209 #define LWIP_UINT32_MAX 0xffffffff
210 
211 /** Define this to 1 in arch/cc.h of your port if your compiler does not provide
212  * the ctype.h header. If ctype.h is available, a few character functions
213  * are mapped to the appropriate functions (lwip_islower, lwip_isdigit...), if
214  * not, a private implementation is provided.
215  */
216 #ifndef LWIP_NO_CTYPE_H
217 #define LWIP_NO_CTYPE_H 0
218 #endif
219 
220 #if LWIP_NO_CTYPE_H
221 #define lwip_in_range(c, lo, up)  ((u8_t)(c) >= (lo) && (u8_t)(c) <= (up))
222 #define lwip_isdigit(c)           lwip_in_range((c), '0', '9')
223 #define lwip_isxdigit(c)          (lwip_isdigit(c) || lwip_in_range((c), 'a', 'f') || lwip_in_range((c), 'A', 'F'))
224 #define lwip_islower(c)           lwip_in_range((c), 'a', 'z')
225 #define lwip_isspace(c)           ((c) == ' ' || (c) == '\f' || (c) == '\n' || (c) == '\r' || (c) == '\t' || (c) == '\v')
226 #define lwip_isupper(c)           lwip_in_range((c), 'A', 'Z')
227 #define lwip_tolower(c)           (lwip_isupper(c) ? (c) - 'A' + 'a' : c)
228 #define lwip_toupper(c)           (lwip_islower(c) ? (c) - 'a' + 'A' : c)
229 #else
230 #include <ctype.h>
231 #define lwip_isdigit(c)           isdigit((unsigned char)(c))
232 #define lwip_isxdigit(c)          isxdigit((unsigned char)(c))
233 #define lwip_islower(c)           islower((unsigned char)(c))
234 #define lwip_isspace(c)           isspace((unsigned char)(c))
235 #define lwip_isupper(c)           isupper((unsigned char)(c))
236 #define lwip_tolower(c)           tolower((unsigned char)(c))
237 #define lwip_toupper(c)           toupper((unsigned char)(c))
238 #endif
239 
240 /** C++ const_cast<target_type>(val) equivalent to remove constness from a value (GCC -Wcast-qual) */
241 #ifndef LWIP_CONST_CAST
242 #define LWIP_CONST_CAST(target_type, val) ((target_type)((ptrdiff_t)val))
243 #endif
244 
245 /** Get rid of alignment cast warnings (GCC -Wcast-align) */
246 #ifndef LWIP_ALIGNMENT_CAST
247 #define LWIP_ALIGNMENT_CAST(target_type, val) LWIP_CONST_CAST(target_type, val)
248 #endif
249 
250 /** Get rid of warnings related to pointer-to-numeric and vice-versa casts,
251  * e.g. "conversion from 'u8_t' to 'void *' of greater size"
252  */
253 #ifndef LWIP_PTR_NUMERIC_CAST
254 #define LWIP_PTR_NUMERIC_CAST(target_type, val) LWIP_CONST_CAST(target_type, val)
255 #endif
256 
257 /** Avoid warnings/errors related to implicitly casting away packed attributes by doing a explicit cast */
258 #ifndef LWIP_PACKED_CAST
259 #define LWIP_PACKED_CAST(target_type, val) LWIP_CONST_CAST(target_type, val)
260 #endif
261 
262 /** Allocates a memory buffer of specified size that is of sufficient size to align
263  * its start address using LWIP_MEM_ALIGN.
264  * You can declare your own version here e.g. to enforce alignment without adding
265  * trailing padding bytes (see LWIP_MEM_ALIGN_BUFFER) or your own section placement
266  * requirements.\n
267  * e.g. if you use gcc and need 32 bit alignment:\n
268  * \#define LWIP_DECLARE_MEMORY_ALIGNED(variable_name, size) u8_t variable_name[size] \_\_attribute\_\_((aligned(4)))\n
269  * or more portable:\n
270  * \#define LWIP_DECLARE_MEMORY_ALIGNED(variable_name, size) u32_t variable_name[(size + sizeof(u32_t) - 1) / sizeof(u32_t)]
271  */
272 #ifndef LWIP_DECLARE_MEMORY_ALIGNED
273 #define LWIP_DECLARE_MEMORY_ALIGNED(variable_name, size) u8_t variable_name[LWIP_MEM_ALIGN_BUFFER(size)]
274 #endif
275 
276 /** Calculate memory size for an aligned buffer - returns the next highest
277  * multiple of MEM_ALIGNMENT (e.g. LWIP_MEM_ALIGN_SIZE(3) and
278  * LWIP_MEM_ALIGN_SIZE(4) will both yield 4 for MEM_ALIGNMENT == 4).
279  */
280 #ifndef LWIP_MEM_ALIGN_SIZE
281 #define LWIP_MEM_ALIGN_SIZE(size) (((size) + MEM_ALIGNMENT - 1U) & ~(MEM_ALIGNMENT-1U))
282 #endif
283 
284 /** Calculate safe memory size for an aligned buffer when using an unaligned
285  * type as storage. This includes a safety-margin on (MEM_ALIGNMENT - 1) at the
286  * start (e.g. if buffer is u8_t[] and actual data will be u32_t*)
287  */
288 #ifndef LWIP_MEM_ALIGN_BUFFER
289 #define LWIP_MEM_ALIGN_BUFFER(size) (((size) + MEM_ALIGNMENT - 1U))
290 #endif
291 
292 /** Align a memory pointer to the alignment defined by MEM_ALIGNMENT
293  * so that ADDR % MEM_ALIGNMENT == 0
294  */
295 #ifndef LWIP_MEM_ALIGN
296 #define LWIP_MEM_ALIGN(addr) ((void *)(((mem_ptr_t)(addr) + MEM_ALIGNMENT - 1) & ~(mem_ptr_t)(MEM_ALIGNMENT-1)))
297 #endif
298 
299 #ifdef __cplusplus
300 extern "C" {
301 #endif
302 
303 /** Packed structs support.
304   * Placed BEFORE declaration of a packed struct.\n
305   * For examples of packed struct declarations, see include/lwip/prot/ subfolder.\n
306   * A port to GCC/clang is included in lwIP, if you use these compilers there is nothing to do here.
307   */
308 #ifndef PACK_STRUCT_BEGIN
309 #define PACK_STRUCT_BEGIN
310 #endif /* PACK_STRUCT_BEGIN */
311 
312 /** Packed structs support.
313   * Placed AFTER declaration of a packed struct.\n
314   * For examples of packed struct declarations, see include/lwip/prot/ subfolder.\n
315   * A port to GCC/clang is included in lwIP, if you use these compilers there is nothing to do here.
316   */
317 #ifndef PACK_STRUCT_END
318 #define PACK_STRUCT_END
319 #endif /* PACK_STRUCT_END */
320 
321 /** Packed structs support.
322   * Placed between end of declaration of a packed struct and trailing semicolon.\n
323   * For examples of packed struct declarations, see include/lwip/prot/ subfolder.\n
324   * A port to GCC/clang is included in lwIP, if you use these compilers there is nothing to do here.
325   */
326 #ifndef PACK_STRUCT_STRUCT
327 #if defined(__GNUC__) || defined(__clang__)
328 #define PACK_STRUCT_STRUCT __attribute__((packed))
329 #else
330 #define PACK_STRUCT_STRUCT
331 #endif
332 #endif /* PACK_STRUCT_STRUCT */
333 
334 /** Packed structs support.
335   * Wraps u32_t and u16_t members.\n
336   * For examples of packed struct declarations, see include/lwip/prot/ subfolder.\n
337   * A port to GCC/clang is included in lwIP, if you use these compilers there is nothing to do here.
338   */
339 #ifndef PACK_STRUCT_FIELD
340 #define PACK_STRUCT_FIELD(x) x
341 #endif /* PACK_STRUCT_FIELD */
342 
343 /** Packed structs support.
344   * Wraps u8_t members, where some compilers warn that packing is not necessary.\n
345   * For examples of packed struct declarations, see include/lwip/prot/ subfolder.\n
346   * A port to GCC/clang is included in lwIP, if you use these compilers there is nothing to do here.
347   */
348 #ifndef PACK_STRUCT_FLD_8
349 #define PACK_STRUCT_FLD_8(x) PACK_STRUCT_FIELD(x)
350 #endif /* PACK_STRUCT_FLD_8 */
351 
352 /** Packed structs support.
353   * Wraps members that are packed structs themselves, where some compilers warn that packing is not necessary.\n
354   * For examples of packed struct declarations, see include/lwip/prot/ subfolder.\n
355   * A port to GCC/clang is included in lwIP, if you use these compilers there is nothing to do here.
356   */
357 #ifndef PACK_STRUCT_FLD_S
358 #define PACK_STRUCT_FLD_S(x) PACK_STRUCT_FIELD(x)
359 #endif /* PACK_STRUCT_FLD_S */
360 
361 /** PACK_STRUCT_USE_INCLUDES==1: Packed structs support using \#include files before and after struct to be packed.\n
362  * The file included BEFORE the struct is "arch/bpstruct.h".\n
363  * The file included AFTER the struct is "arch/epstruct.h".\n
364  * This can be used to implement struct packing on MS Visual C compilers, see
365  * the Win32 port in the lwIP contrib repository for reference.
366  * For examples of packed struct declarations, see include/lwip/prot/ subfolder.\n
367  * A port to GCC/clang is included in lwIP, if you use these compilers there is nothing to do here.
368  */
369 #ifdef __DOXYGEN__
370 #define PACK_STRUCT_USE_INCLUDES
371 #endif
372 
373 /** Eliminates compiler warning about unused arguments (GCC -Wextra -Wunused). */
374 #ifndef LWIP_UNUSED_ARG
375 #define LWIP_UNUSED_ARG(x) (void)x
376 #endif /* LWIP_UNUSED_ARG */
377 
378 /** LWIP_PROVIDE_ERRNO==1: Let lwIP provide ERRNO values and the 'errno' variable.
379  * If this is disabled, cc.h must either define 'errno', include <errno.h>,
380  * define LWIP_ERRNO_STDINCLUDE to get <errno.h> included or
381  * define LWIP_ERRNO_INCLUDE to <errno.h> or equivalent.
382  */
383 #if defined __DOXYGEN__
384 #define LWIP_PROVIDE_ERRNO
385 #endif
386 
387 /**
388  * @}
389  */
390 
391 #ifdef __cplusplus
392 }
393 #endif
394 
395 #endif /* LWIP_HDR_ARCH_H */
396