xref: /nrf52832-nimble/rt-thread/components/net/lwip-2.0.2/src/include/lwip/arch.h (revision 104654410c56c573564690304ae786df310c91fc)
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 <[email protected]>
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  * These options cannot be \#defined in lwipopts.h since they are not options
56  * of lwIP itself, but options of the lwIP port to your system.
57  * @{
58  */
59 
60 /** Define the byte order of the system.
61  * Needed for conversion of network data to host byte order.
62  * Allowed values: LITTLE_ENDIAN and BIG_ENDIAN
63  */
64 #ifndef BYTE_ORDER
65 #define BYTE_ORDER LITTLE_ENDIAN
66 #endif
67 
68 /** Define random number generator function of your system */
69 #ifdef __DOXYGEN__
70 #define LWIP_RAND() ((u32_t)rand())
71 #endif
72 
73 /** Platform specific diagnostic output.\n
74  * Note the default implementation pulls in printf, which may
75  * in turn pull in a lot of standard libary code. In resource-constrained
76  * systems, this should be defined to something less resource-consuming.
77  */
78 #ifndef LWIP_PLATFORM_DIAG
79 #define LWIP_PLATFORM_DIAG(x) do {printf x;} while(0)
80 #include <stdio.h>
81 #include <stdlib.h>
82 #endif
83 
84 /** Platform specific assertion handling.\n
85  * Note the default implementation pulls in printf, fflush and abort, which may
86  * in turn pull in a lot of standard libary code. In resource-constrained
87  * systems, this should be defined to something less resource-consuming.
88  */
89 #ifndef LWIP_PLATFORM_ASSERT
90 #define LWIP_PLATFORM_ASSERT(x) do {printf("Assertion \"%s\" failed at line %d in %s\n", \
91                                      x, __LINE__, __FILE__); fflush(NULL); abort();} while(0)
92 #include <stdio.h>
93 #include <stdlib.h>
94 #endif
95 
96 /** Define this to 1 in arch/cc.h of your port if you do not want to
97  * include stddef.h header to get size_t. You need to typedef size_t
98  * by yourself in this case.
99  */
100 #ifndef LWIP_NO_STDDEF_H
101 #define LWIP_NO_STDDEF_H 0
102 #endif
103 
104 #if !LWIP_NO_STDDEF_H
105 #include <stddef.h> /* for size_t */
106 #endif
107 
108 /** Define this to 1 in arch/cc.h of your port if your compiler does not provide
109  * the stdint.h header. You need to typedef the generic types listed in
110  * lwip/arch.h yourself in this case (u8_t, u16_t...).
111  */
112 #ifndef LWIP_NO_STDINT_H
113 #define LWIP_NO_STDINT_H 0
114 #endif
115 
116 /* Define generic types used in lwIP */
117 #if !LWIP_NO_STDINT_H
118 #include <stdint.h>
119 typedef uint8_t   u8_t;
120 typedef int8_t    s8_t;
121 typedef uint16_t  u16_t;
122 typedef int16_t   s16_t;
123 typedef uint32_t  u32_t;
124 typedef int32_t   s32_t;
125 typedef uintptr_t mem_ptr_t;
126 #endif
127 
128 /** Define this to 1 in arch/cc.h of your port if your compiler does not provide
129  * the inttypes.h header. You need to define the format strings listed in
130  * lwip/arch.h yourself in this case (X8_F, U16_F...).
131  */
132 #ifndef LWIP_NO_INTTYPES_H
133 #define LWIP_NO_INTTYPES_H 0
134 #endif
135 
136 /* Define (sn)printf formatters for these lwIP types */
137 #if !LWIP_NO_INTTYPES_H
138 #include <inttypes.h>
139 #ifndef X8_F
140 #define X8_F  "02" PRIx8
141 #endif
142 #ifndef U16_F
143 #define U16_F PRIu16
144 #endif
145 #ifndef S16_F
146 #define S16_F PRId16
147 #endif
148 #ifndef X16_F
149 #define X16_F PRIx16
150 #endif
151 #ifndef U32_F
152 #define U32_F PRIu32
153 #endif
154 #ifndef S32_F
155 #define S32_F PRId32
156 #endif
157 #ifndef X32_F
158 #define X32_F PRIx32
159 #endif
160 #ifndef SZT_F
161 #define SZT_F PRIuPTR
162 #endif
163 #endif
164 
165 /** Define this to 1 in arch/cc.h of your port if your compiler does not provide
166  * the limits.h header. You need to define the type limits yourself in this case
167  * (e.g. INT_MAX).
168  */
169 #ifndef LWIP_NO_LIMITS_H
170 #define LWIP_NO_LIMITS_H 0
171 #endif
172 
173 /* Include limits.h? */
174 #if !LWIP_NO_LIMITS_H
175 #include <limits.h>
176 #endif
177 
178 /** C++ const_cast<target_type>(val) equivalent to remove constness from a value (GCC -Wcast-qual) */
179 #ifndef LWIP_CONST_CAST
180 #define LWIP_CONST_CAST(target_type, val) ((target_type)((ptrdiff_t)val))
181 #endif
182 
183 /** Get rid of alignment cast warnings (GCC -Wcast-align) */
184 #ifndef LWIP_ALIGNMENT_CAST
185 #define LWIP_ALIGNMENT_CAST(target_type, val) LWIP_CONST_CAST(target_type, val)
186 #endif
187 
188 /** Get rid of warnings related to pointer-to-numeric and vice-versa casts,
189  * e.g. "conversion from 'u8_t' to 'void *' of greater size"
190  */
191 #ifndef LWIP_PTR_NUMERIC_CAST
192 #define LWIP_PTR_NUMERIC_CAST(target_type, val) LWIP_CONST_CAST(target_type, val)
193 #endif
194 
195 /** Allocates a memory buffer of specified size that is of sufficient size to align
196  * its start address using LWIP_MEM_ALIGN.
197  * You can declare your own version here e.g. to enforce alignment without adding
198  * trailing padding bytes (see LWIP_MEM_ALIGN_BUFFER) or your own section placement
199  * requirements.\n
200  * e.g. if you use gcc and need 32 bit alignment:\n
201  * \#define LWIP_DECLARE_MEMORY_ALIGNED(variable_name, size) u8_t variable_name[size] \_\_attribute\_\_((aligned(4)))\n
202  * or more portable:\n
203  * \#define LWIP_DECLARE_MEMORY_ALIGNED(variable_name, size) u32_t variable_name[(size + sizeof(u32_t) - 1) / sizeof(u32_t)]
204  */
205 #ifndef LWIP_DECLARE_MEMORY_ALIGNED
206 #define LWIP_DECLARE_MEMORY_ALIGNED(variable_name, size) u8_t variable_name[LWIP_MEM_ALIGN_BUFFER(size)]
207 
208 #endif
209 
210 /** Calculate memory size for an aligned buffer - returns the next highest
211  * multiple of MEM_ALIGNMENT (e.g. LWIP_MEM_ALIGN_SIZE(3) and
212  * LWIP_MEM_ALIGN_SIZE(4) will both yield 4 for MEM_ALIGNMENT == 4).
213  */
214 #ifndef LWIP_MEM_ALIGN_SIZE
215 #define LWIP_MEM_ALIGN_SIZE(size) (((size) + MEM_ALIGNMENT - 1U) & ~(MEM_ALIGNMENT-1U))
216 #endif
217 
218 /** Calculate safe memory size for an aligned buffer when using an unaligned
219  * type as storage. This includes a safety-margin on (MEM_ALIGNMENT - 1) at the
220  * start (e.g. if buffer is u8_t[] and actual data will be u32_t*)
221  */
222 #ifndef LWIP_MEM_ALIGN_BUFFER
223 #define LWIP_MEM_ALIGN_BUFFER(size) (((size) + MEM_ALIGNMENT - 1U))
224 #endif
225 
226 /** Align a memory pointer to the alignment defined by MEM_ALIGNMENT
227  * so that ADDR % MEM_ALIGNMENT == 0
228  */
229 #ifndef LWIP_MEM_ALIGN
230 #define LWIP_MEM_ALIGN(addr) ((void *)(((mem_ptr_t)(addr) + MEM_ALIGNMENT - 1) & ~(mem_ptr_t)(MEM_ALIGNMENT-1)))
231 #endif
232 
233 #ifdef __cplusplus
234 extern "C" {
235 #endif
236 
237 /** Packed structs support.
238   * Placed BEFORE declaration of a packed struct.\n
239   * For examples of packed struct declarations, see include/lwip/prot/ subfolder.\n
240   * A port to GCC/clang is included in lwIP, if you use these compilers there is nothing to do here.
241   */
242 #ifndef PACK_STRUCT_BEGIN
243 #define PACK_STRUCT_BEGIN
244 #endif /* PACK_STRUCT_BEGIN */
245 
246 /** Packed structs support.
247   * Placed AFTER declaration of a packed struct.\n
248   * For examples of packed struct declarations, see include/lwip/prot/ subfolder.\n
249   * A port to GCC/clang is included in lwIP, if you use these compilers there is nothing to do here.
250   */
251 #ifndef PACK_STRUCT_END
252 #define PACK_STRUCT_END
253 #endif /* PACK_STRUCT_END */
254 
255 /** Packed structs support.
256   * Placed between end of declaration of a packed struct and trailing semicolon.\n
257   * For examples of packed struct declarations, see include/lwip/prot/ subfolder.\n
258   * A port to GCC/clang is included in lwIP, if you use these compilers there is nothing to do here.
259   */
260 #ifndef PACK_STRUCT_STRUCT
261 #if defined(__GNUC__) || defined(__clang__)
262 #define PACK_STRUCT_STRUCT __attribute__((packed))
263 #else
264 #define PACK_STRUCT_STRUCT
265 #endif
266 #endif /* PACK_STRUCT_STRUCT */
267 
268 /** Packed structs support.
269   * Wraps u32_t and u16_t members.\n
270   * For examples of packed struct declarations, see include/lwip/prot/ subfolder.\n
271   * A port to GCC/clang is included in lwIP, if you use these compilers there is nothing to do here.
272   */
273 #ifndef PACK_STRUCT_FIELD
274 #define PACK_STRUCT_FIELD(x) x
275 #endif /* PACK_STRUCT_FIELD */
276 
277 /** Packed structs support.
278   * Wraps u8_t members, where some compilers warn that packing is not necessary.\n
279   * For examples of packed struct declarations, see include/lwip/prot/ subfolder.\n
280   * A port to GCC/clang is included in lwIP, if you use these compilers there is nothing to do here.
281   */
282 #ifndef PACK_STRUCT_FLD_8
283 #define PACK_STRUCT_FLD_8(x) PACK_STRUCT_FIELD(x)
284 #endif /* PACK_STRUCT_FLD_8 */
285 
286 /** Packed structs support.
287   * Wraps members that are packed structs themselves, where some compilers warn that packing is not necessary.\n
288   * For examples of packed struct declarations, see include/lwip/prot/ subfolder.\n
289   * A port to GCC/clang is included in lwIP, if you use these compilers there is nothing to do here.
290   */
291 #ifndef PACK_STRUCT_FLD_S
292 #define PACK_STRUCT_FLD_S(x) PACK_STRUCT_FIELD(x)
293 #endif /* PACK_STRUCT_FLD_S */
294 
295 /** Packed structs support using \#include files before and after struct to be packed.\n
296  * The file included BEFORE the struct is "arch/bpstruct.h".\n
297  * The file included AFTER the struct is "arch/epstruct.h".\n
298  * This can be used to implement struct packing on MS Visual C compilers, see
299  * the Win32 port in the lwIP contrib repository for reference.
300  * For examples of packed struct declarations, see include/lwip/prot/ subfolder.\n
301  * A port to GCC/clang is included in lwIP, if you use these compilers there is nothing to do here.
302  */
303 #ifdef __DOXYGEN__
304 #define PACK_STRUCT_USE_INCLUDES
305 #endif
306 
307 /** Eliminates compiler warning about unused arguments (GCC -Wextra -Wunused). */
308 #ifndef LWIP_UNUSED_ARG
309 #define LWIP_UNUSED_ARG(x) (void)x
310 #endif /* LWIP_UNUSED_ARG */
311 
312 /**
313  * @}
314  */
315 
316 #ifdef __cplusplus
317 }
318 #endif
319 
320 #endif /* LWIP_HDR_ARCH_H */
321