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