1254225Speter/**
2254225Speter * @file
3254225Speter * Support for different processor and compiler architectures
4254225Speter */
5254225Speter
6254225Speter/*
7254225Speter * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
8254225Speter * All rights reserved.
9254225Speter *
10254225Speter * Redistribution and use in source and binary forms, with or without modification,
11254225Speter * are permitted provided that the following conditions are met:
12254225Speter *
13254225Speter * 1. Redistributions of source code must retain the above copyright notice,
14254225Speter *    this list of conditions and the following disclaimer.
15254225Speter * 2. Redistributions in binary form must reproduce the above copyright notice,
16254225Speter *    this list of conditions and the following disclaimer in the documentation
17254225Speter *    and/or other materials provided with the distribution.
18254225Speter * 3. The name of the author may not be used to endorse or promote products
19254225Speter *    derived from this software without specific prior written permission.
20254225Speter *
21254225Speter * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
22254225Speter * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23254225Speter * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
24254225Speter * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25254225Speter * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
26254225Speter * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27254225Speter * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28254225Speter * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
29254225Speter * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
30254225Speter * OF SUCH DAMAGE.
31254225Speter *
32254225Speter * This file is part of the lwIP TCP/IP stack.
33254225Speter *
34254225Speter * Author: Adam Dunkels <adam@sics.se>
35254225Speter *
36254225Speter */
37254225Speter#ifndef LWIP_HDR_ARCH_H
38254225Speter#define LWIP_HDR_ARCH_H
39254225Speter
40254225Speter// #ifndef LITTLE_ENDIAN
41254225Speter// #define LITTLE_ENDIAN 1234
42254225Speter// #endif
43254225Speter//
44254225Speter// #ifndef BIG_ENDIAN
45254225Speter// #define BIG_ENDIAN 4321
46254225Speter// #endif
47254225Speter
48254225Speter#include "arch/cc.h"
49254225Speter
50254225Speter/**
51254225Speter * @defgroup compiler_abstraction Compiler/platform abstraction
52254225Speter * @ingroup sys_layer
53254225Speter * All defines related to this section must not be placed in lwipopts.h,
54254225Speter * but in arch/cc.h!
55254225Speter * These options cannot be \#defined in lwipopts.h since they are not options
56254225Speter * of lwIP itself, but options of the lwIP port to your system.
57254225Speter * @{
58254225Speter */
59254225Speter
60254225Speter/** Define the byte order of the system.
61254225Speter * Needed for conversion of network data to host byte order.
62254225Speter * Allowed values: LITTLE_ENDIAN and BIG_ENDIAN
63254225Speter */
64254225Speter#ifndef BYTE_ORDER
65254225Speter#define BYTE_ORDER LITTLE_ENDIAN
66254225Speter#endif
67254225Speter
68254225Speter/** Define random number generator function of your system */
69254225Speter#ifndef __DOXYGEN__
70254225Speter#define LWIP_RAND() ((u32_t)rand())
71254225Speter#endif
72254225Speter
73254225Speter/** Platform specific diagnostic output.\n
74254225Speter * Note the default implementation pulls in printf, which may
75254225Speter * in turn pull in a lot of standard libary code. In resource-constrained
76254225Speter * systems, this should be defined to something less resource-consuming.
77254225Speter */
78254225Speter#ifndef LWIP_PLATFORM_DIAG
79254225Speter#define LWIP_PLATFORM_DIAG(x) do {printf x;} while(0)
80254225Speter#include <stdio.h>
81254225Speter#include <stdlib.h>
82254225Speter#endif
83254225Speter
84254225Speter/** Platform specific assertion handling.\n
85254225Speter * Note the default implementation pulls in printf, fflush and abort, which may
86254225Speter * in turn pull in a lot of standard libary code. In resource-constrained
87254225Speter * systems, this should be defined to something less resource-consuming.
88254225Speter */
89254225Speter#ifndef LWIP_PLATFORM_ASSERT
90254225Speter#define LWIP_PLATFORM_ASSERT(x) do {printf("Assertion \"%s\" failed at line %d in %s\n", \
91254225Speter                                     x, __LINE__, __FILE__); fflush(NULL); abort();} while(0)
92254225Speter#include <stdio.h>
93254225Speter#include <stdlib.h>
94254225Speter#endif
95254225Speter
96254225Speter/** Define this to 1 in arch/cc.h of your port if you do not want to
97254225Speter * include stddef.h header to get size_t. You need to typedef size_t
98254225Speter * by yourself in this case.
99254225Speter */
100254225Speter#ifndef LWIP_NO_STDDEF_H
101254225Speter#define LWIP_NO_STDDEF_H 0
102254225Speter#endif
103254225Speter
104254225Speter#if !LWIP_NO_STDDEF_H
105254225Speter#include <stddef.h> /* for size_t */
106254225Speter#endif
107254225Speter
108254225Speter/** Define this to 1 in arch/cc.h of your port if your compiler does not provide
109254225Speter * the stdint.h header. You need to typedef the generic types listed in
110254225Speter * lwip/arch.h yourself in this case (u8_t, u16_t...).
111254225Speter */
112254225Speter#ifndef LWIP_NO_STDINT_H
113254225Speter#define LWIP_NO_STDINT_H 0
114254225Speter#endif
115254225Speter
116254225Speter/* Define generic types used in lwIP */
117254225Speter#if !LWIP_NO_STDINT_H
118254225Speter#include <stdint.h>
119254225Spetertypedef uint8_t   u8_t;
120254225Spetertypedef int8_t    s8_t;
121254225Spetertypedef uint16_t  u16_t;
122254225Spetertypedef int16_t   s16_t;
123254225Spetertypedef uint32_t  u32_t;
124254225Spetertypedef int32_t   s32_t;
125254225Spetertypedef uintptr_t mem_ptr_t;
126254225Speter#endif
127254225Speter
128254225Speter/** Define this to 1 in arch/cc.h of your port if your compiler does not provide
129254225Speter * the inttypes.h header. You need to define the format strings listed in
130254225Speter * lwip/arch.h yourself in this case (X8_F, U16_F...).
131254225Speter */
132254225Speter#ifndef LWIP_NO_INTTYPES_H
133254225Speter#define LWIP_NO_INTTYPES_H 0
134254225Speter#endif
135254225Speter
136254225Speter/* Define (sn)printf formatters for these lwIP types */
137254225Speter#if !LWIP_NO_INTTYPES_H
138254225Speter#include <inttypes.h>
139254225Speter#ifndef X8_F
140254225Speter#define X8_F  "02" PRIx8
141254225Speter#endif
142254225Speter#ifndef U16_F
143254225Speter#define U16_F PRIu16
144254225Speter#endif
145254225Speter#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#endif
208
209/** Calculate memory size for an aligned buffer - returns the next highest
210 * multiple of MEM_ALIGNMENT (e.g. LWIP_MEM_ALIGN_SIZE(3) and
211 * LWIP_MEM_ALIGN_SIZE(4) will both yield 4 for MEM_ALIGNMENT == 4).
212 */
213#ifndef LWIP_MEM_ALIGN_SIZE
214#define LWIP_MEM_ALIGN_SIZE(size) (((size) + MEM_ALIGNMENT - 1U) & ~(MEM_ALIGNMENT-1U))
215#endif
216
217/** Calculate safe memory size for an aligned buffer when using an unaligned
218 * type as storage. This includes a safety-margin on (MEM_ALIGNMENT - 1) at the
219 * start (e.g. if buffer is u8_t[] and actual data will be u32_t*)
220 */
221#ifndef LWIP_MEM_ALIGN_BUFFER
222#define LWIP_MEM_ALIGN_BUFFER(size) (((size) + MEM_ALIGNMENT - 1U))
223#endif
224
225/** Align a memory pointer to the alignment defined by MEM_ALIGNMENT
226 * so that ADDR % MEM_ALIGNMENT == 0
227 */
228#ifndef LWIP_MEM_ALIGN
229#define LWIP_MEM_ALIGN(addr) ((void *)(((mem_ptr_t)(addr) + MEM_ALIGNMENT - 1) & ~(mem_ptr_t)(MEM_ALIGNMENT-1)))
230#endif
231
232#ifdef __cplusplus
233extern "C" {
234#endif
235
236/** Packed structs support.
237  * Placed BEFORE declaration of a packed struct.\n
238  * For examples of packed struct declarations, see include/lwip/prot/ subfolder.\n
239  * A port to GCC/clang is included in lwIP, if you use these compilers there is nothing to do here.
240  */
241#ifndef PACK_STRUCT_BEGIN
242#define PACK_STRUCT_BEGIN
243#endif /* PACK_STRUCT_BEGIN */
244
245/** Packed structs support.
246  * Placed AFTER declaration of a packed struct.\n
247  * For examples of packed struct declarations, see include/lwip/prot/ subfolder.\n
248  * A port to GCC/clang is included in lwIP, if you use these compilers there is nothing to do here.
249  */
250#ifndef PACK_STRUCT_END
251#define PACK_STRUCT_END
252#endif /* PACK_STRUCT_END */
253
254/** Packed structs support.
255  * Placed between end of declaration of a packed struct and trailing semicolon.\n
256  * For examples of packed struct declarations, see include/lwip/prot/ subfolder.\n
257  * A port to GCC/clang is included in lwIP, if you use these compilers there is nothing to do here.
258  */
259#ifndef PACK_STRUCT_STRUCT
260#if defined(__GNUC__) || defined(__clang__)
261#define PACK_STRUCT_STRUCT __attribute__((packed))
262#else
263#define PACK_STRUCT_STRUCT
264#endif
265#endif /* PACK_STRUCT_STRUCT */
266
267/** Packed structs support.
268  * Wraps u32_t and u16_t members.\n
269  * For examples of packed struct declarations, see include/lwip/prot/ subfolder.\n
270  * A port to GCC/clang is included in lwIP, if you use these compilers there is nothing to do here.
271  */
272#ifndef PACK_STRUCT_FIELD
273#define PACK_STRUCT_FIELD(x) x
274#endif /* PACK_STRUCT_FIELD */
275
276/** Packed structs support.
277  * Wraps u8_t members, where some compilers warn that packing is not necessary.\n
278  * For examples of packed struct declarations, see include/lwip/prot/ subfolder.\n
279  * A port to GCC/clang is included in lwIP, if you use these compilers there is nothing to do here.
280  */
281#ifndef PACK_STRUCT_FLD_8
282#define PACK_STRUCT_FLD_8(x) PACK_STRUCT_FIELD(x)
283#endif /* PACK_STRUCT_FLD_8 */
284
285/** Packed structs support.
286  * Wraps members that are packed structs themselves, where some compilers warn that packing is not necessary.\n
287  * For examples of packed struct declarations, see include/lwip/prot/ subfolder.\n
288  * A port to GCC/clang is included in lwIP, if you use these compilers there is nothing to do here.
289  */
290#ifndef PACK_STRUCT_FLD_S
291#define PACK_STRUCT_FLD_S(x) PACK_STRUCT_FIELD(x)
292#endif /* PACK_STRUCT_FLD_S */
293
294/** Packed structs support using \#include files before and after struct to be packed.\n
295 * The file included BEFORE the struct is "arch/bpstruct.h".\n
296 * The file included AFTER the struct is "arch/epstruct.h".\n
297 * This can be used to implement struct packing on MS Visual C compilers, see
298 * the Win32 port in the lwIP contrib repository for reference.
299 * For examples of packed struct declarations, see include/lwip/prot/ subfolder.\n
300 * A port to GCC/clang is included in lwIP, if you use these compilers there is nothing to do here.
301 */
302#ifdef __DOXYGEN__
303#define PACK_STRUCT_USE_INCLUDES
304#endif
305
306/** Eliminates compiler warning about unused arguments (GCC -Wextra -Wunused). */
307#ifndef LWIP_UNUSED_ARG
308#define LWIP_UNUSED_ARG(x) (void)x
309#endif /* LWIP_UNUSED_ARG */
310
311/**
312 * @}
313 */
314
315#ifdef __cplusplus
316}
317#endif
318
319#endif /* LWIP_HDR_ARCH_H */
320