arch.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /**
  2. * @file
  3. * Support for different processor and compiler architectures
  4. */
  5. /*
  6. * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
  7. * All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without modification,
  10. * are permitted provided that the following conditions are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright notice,
  13. * this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright notice,
  15. * this list of conditions and the following disclaimer in the documentation
  16. * and/or other materials provided with the distribution.
  17. * 3. The name of the author may not be used to endorse or promote products
  18. * derived from this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  21. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  22. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
  23. * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  24. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
  25. * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  27. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  28. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  29. * OF SUCH DAMAGE.
  30. *
  31. * This file is part of the lwIP TCP/IP stack.
  32. *
  33. * Author: Adam Dunkels <adam@sics.se>
  34. *
  35. */
  36. #ifndef LWIP_HDR_ARCH_H
  37. #define LWIP_HDR_ARCH_H
  38. #ifndef LITTLE_ENDIAN
  39. #define LITTLE_ENDIAN 1234
  40. #endif
  41. #ifndef BIG_ENDIAN
  42. #define BIG_ENDIAN 4321
  43. #endif
  44. #include "arch/cc.h"
  45. /**
  46. * @defgroup compiler_abstraction Compiler/platform abstraction
  47. * @ingroup sys_layer
  48. * All defines related to this section must not be placed in lwipopts.h,
  49. * but in arch/cc.h!
  50. * These options cannot be \#defined in lwipopts.h since they are not options
  51. * of lwIP itself, but options of the lwIP port to your system.
  52. * @{
  53. */
  54. /** Define the byte order of the system.
  55. * Needed for conversion of network data to host byte order.
  56. * Allowed values: LITTLE_ENDIAN and BIG_ENDIAN
  57. */
  58. #ifndef BYTE_ORDER
  59. #define BYTE_ORDER LITTLE_ENDIAN
  60. #endif
  61. /** Define random number generator function of your system */
  62. #ifdef __DOXYGEN__
  63. #define LWIP_RAND() ((u32_t)rand())
  64. #endif
  65. /** Platform specific diagnostic output.\n
  66. * Note the default implementation pulls in printf, which may
  67. * in turn pull in a lot of standard libary code. In resource-constrained
  68. * systems, this should be defined to something less resource-consuming.
  69. */
  70. #ifndef LWIP_PLATFORM_DIAG
  71. #define LWIP_PLATFORM_DIAG(x) do {printf x; printf("\r");} while(0)
  72. #ifdef PRINTF_STDLIB
  73. #include <stdio.h>
  74. #endif
  75. #ifdef PRINTF_CUSTOM
  76. #include "tinystdio.h"
  77. #endif
  78. #include <stdlib.h>
  79. #endif
  80. /** Platform specific assertion handling.\n
  81. * Note the default implementation pulls in printf, fflush and abort, which may
  82. * in turn pull in a lot of standard libary code. In resource-constrained
  83. * systems, this should be defined to something less resource-consuming.
  84. */
  85. #ifndef LWIP_PLATFORM_ASSERT
  86. #define LWIP_PLATFORM_ASSERT(x) do {printf("Assertion \"%s\" failed at line %d in %s\n", \
  87. x, __LINE__, __FILE__); fflush(NULL); abort();} while(0)
  88. #ifdef PRINTF_STDLIB
  89. #include <stdio.h>
  90. #endif
  91. #ifdef PRINTF_CUSTOM
  92. #include "tinystdio.h"
  93. #endif
  94. #include <stdlib.h>
  95. #endif
  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. #if !LWIP_NO_STDDEF_H
  104. #include <stddef.h> /* for size_t */
  105. #endif
  106. /** Define this to 1 in arch/cc.h of your port if your compiler does not provide
  107. * the stdint.h header. You need to typedef the generic types listed in
  108. * lwip/arch.h yourself in this case (u8_t, u16_t...).
  109. */
  110. #ifndef LWIP_NO_STDINT_H
  111. #define LWIP_NO_STDINT_H 0
  112. #endif
  113. /* Define generic types used in lwIP */
  114. #if !LWIP_NO_STDINT_H
  115. #include <stdint.h>
  116. typedef unsigned char u8_t;
  117. typedef signed char s8_t;
  118. typedef unsigned short u16_t;
  119. typedef signed short s16_t;
  120. typedef unsigned int u32_t;
  121. typedef signed int s32_t;
  122. typedef unsigned long mem_ptr_t;
  123. #endif
  124. /** Define this to 1 in arch/cc.h of your port if your compiler does not provide
  125. * the inttypes.h header. You need to define the format strings listed in
  126. * lwip/arch.h yourself in this case (X8_F, U16_F...).
  127. */
  128. #ifndef LWIP_NO_INTTYPES_H
  129. #define LWIP_NO_INTTYPES_H 0
  130. #endif
  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 U16_F
  138. #define U16_F PRIu16
  139. #endif
  140. #ifndef S16_F
  141. #define S16_F PRId16
  142. #endif
  143. #ifndef X16_F
  144. #define X16_F PRIx16
  145. #endif
  146. #ifndef U32_F
  147. #define U32_F PRIu32
  148. #endif
  149. #ifndef S32_F
  150. #define S32_F PRId32
  151. #endif
  152. #ifndef X32_F
  153. #define X32_F PRIx32
  154. #endif
  155. #ifndef SZT_F
  156. #define SZT_F PRIuPTR
  157. #endif
  158. #endif
  159. /** Define this to 1 in arch/cc.h of your port if your compiler does not provide
  160. * the limits.h header. You need to define the type limits yourself in this case
  161. * (e.g. INT_MAX).
  162. */
  163. #ifndef LWIP_NO_LIMITS_H
  164. #define LWIP_NO_LIMITS_H 0
  165. #endif
  166. /* Include limits.h? */
  167. #if !LWIP_NO_LIMITS_H
  168. #include <limits.h>
  169. #endif
  170. /** C++ const_cast<target_type>(val) equivalent to remove constness from a value (GCC -Wcast-qual) */
  171. #ifndef LWIP_CONST_CAST
  172. #define LWIP_CONST_CAST(target_type, val) ((target_type)((ptrdiff_t)val))
  173. #endif
  174. /** Get rid of alignment cast warnings (GCC -Wcast-align) */
  175. #ifndef LWIP_ALIGNMENT_CAST
  176. #define LWIP_ALIGNMENT_CAST(target_type, val) LWIP_CONST_CAST(target_type, val)
  177. #endif
  178. /** Get rid of warnings related to pointer-to-numeric and vice-versa casts,
  179. * e.g. "conversion from 'u8_t' to 'void *' of greater size"
  180. */
  181. #ifndef LWIP_PTR_NUMERIC_CAST
  182. #define LWIP_PTR_NUMERIC_CAST(target_type, val) LWIP_CONST_CAST(target_type, val)
  183. #endif
  184. /** Allocates a memory buffer of specified size that is of sufficient size to align
  185. * its start address using LWIP_MEM_ALIGN.
  186. * You can declare your own version here e.g. to enforce alignment without adding
  187. * trailing padding bytes (see LWIP_MEM_ALIGN_BUFFER) or your own section placement
  188. * requirements.\n
  189. * e.g. if you use gcc and need 32 bit alignment:\n
  190. * \#define LWIP_DECLARE_MEMORY_ALIGNED(variable_name, size) u8_t variable_name[size] \_\_attribute\_\_((aligned(4)))\n
  191. * or more portable:\n
  192. * \#define LWIP_DECLARE_MEMORY_ALIGNED(variable_name, size) u32_t variable_name[(size + sizeof(u32_t) - 1) / sizeof(u32_t)]
  193. */
  194. #ifndef LWIP_DECLARE_MEMORY_ALIGNED
  195. #define LWIP_DECLARE_MEMORY_ALIGNED(variable_name, size) u8_t variable_name[LWIP_MEM_ALIGN_BUFFER(size)]
  196. #endif
  197. /** Calculate memory size for an aligned buffer - returns the next highest
  198. * multiple of MEM_ALIGNMENT (e.g. LWIP_MEM_ALIGN_SIZE(3) and
  199. * LWIP_MEM_ALIGN_SIZE(4) will both yield 4 for MEM_ALIGNMENT == 4).
  200. */
  201. #ifndef LWIP_MEM_ALIGN_SIZE
  202. #define LWIP_MEM_ALIGN_SIZE(size) (((size) + MEM_ALIGNMENT - 1U) & ~(MEM_ALIGNMENT-1U))
  203. #endif
  204. /** Calculate safe memory size for an aligned buffer when using an unaligned
  205. * type as storage. This includes a safety-margin on (MEM_ALIGNMENT - 1) at the
  206. * start (e.g. if buffer is u8_t[] and actual data will be u32_t*)
  207. */
  208. #ifndef LWIP_MEM_ALIGN_BUFFER
  209. #define LWIP_MEM_ALIGN_BUFFER(size) (((size) + MEM_ALIGNMENT - 1U))
  210. #endif
  211. /** Align a memory pointer to the alignment defined by MEM_ALIGNMENT
  212. * so that ADDR % MEM_ALIGNMENT == 0
  213. */
  214. #ifndef LWIP_MEM_ALIGN
  215. #define LWIP_MEM_ALIGN(addr) ((void *)(((mem_ptr_t)(addr) + MEM_ALIGNMENT - 1) & ~(mem_ptr_t)(MEM_ALIGNMENT-1)))
  216. #endif
  217. #ifdef __cplusplus
  218. extern "C" {
  219. #endif
  220. /** Packed structs support.
  221. * Placed BEFORE declaration of a packed struct.\n
  222. * For examples of packed struct declarations, see include/lwip/prot/ subfolder.\n
  223. * A port to GCC/clang is included in lwIP, if you use these compilers there is nothing to do here.
  224. */
  225. #ifndef PACK_STRUCT_BEGIN
  226. #define PACK_STRUCT_BEGIN
  227. #endif /* PACK_STRUCT_BEGIN */
  228. /** Packed structs support.
  229. * Placed AFTER declaration of a packed struct.\n
  230. * For examples of packed struct declarations, see include/lwip/prot/ subfolder.\n
  231. * A port to GCC/clang is included in lwIP, if you use these compilers there is nothing to do here.
  232. */
  233. #ifndef PACK_STRUCT_END
  234. #define PACK_STRUCT_END
  235. #endif /* PACK_STRUCT_END */
  236. /** Packed structs support.
  237. * Placed between end of declaration of a packed struct and trailing semicolon.\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_STRUCT
  242. #if defined(__GNUC__) || defined(__clang__)
  243. #define PACK_STRUCT_STRUCT __attribute__((packed))
  244. #else
  245. #define PACK_STRUCT_STRUCT
  246. #endif
  247. #endif /* PACK_STRUCT_STRUCT */
  248. /** Packed structs support.
  249. * Wraps u32_t and u16_t members.\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_FIELD
  254. #define PACK_STRUCT_FIELD(x) x
  255. #endif /* PACK_STRUCT_FIELD */
  256. /** Packed structs support.
  257. * Wraps u8_t members, where some compilers warn that packing is not necessary.\n
  258. * For examples of packed struct declarations, see include/lwip/prot/ subfolder.\n
  259. * A port to GCC/clang is included in lwIP, if you use these compilers there is nothing to do here.
  260. */
  261. #ifndef PACK_STRUCT_FLD_8
  262. #define PACK_STRUCT_FLD_8(x) PACK_STRUCT_FIELD(x)
  263. #endif /* PACK_STRUCT_FLD_8 */
  264. /** Packed structs support.
  265. * Wraps members that are packed structs themselves, where some compilers warn that packing is not necessary.\n
  266. * For examples of packed struct declarations, see include/lwip/prot/ subfolder.\n
  267. * A port to GCC/clang is included in lwIP, if you use these compilers there is nothing to do here.
  268. */
  269. #ifndef PACK_STRUCT_FLD_S
  270. #define PACK_STRUCT_FLD_S(x) PACK_STRUCT_FIELD(x)
  271. #endif /* PACK_STRUCT_FLD_S */
  272. /** Packed structs support using \#include files before and after struct to be packed.\n
  273. * The file included BEFORE the struct is "arch/bpstruct.h".\n
  274. * The file included AFTER the struct is "arch/epstruct.h".\n
  275. * This can be used to implement struct packing on MS Visual C compilers, see
  276. * the Win32 port in the lwIP contrib repository for reference.
  277. * For examples of packed struct declarations, see include/lwip/prot/ subfolder.\n
  278. * A port to GCC/clang is included in lwIP, if you use these compilers there is nothing to do here.
  279. */
  280. #ifdef __DOXYGEN__
  281. #define PACK_STRUCT_USE_INCLUDES
  282. #endif
  283. /** Eliminates compiler warning about unused arguments (GCC -Wextra -Wunused). */
  284. #ifndef LWIP_UNUSED_ARG
  285. #define LWIP_UNUSED_ARG(x) (void)x
  286. #endif /* LWIP_UNUSED_ARG */
  287. /**
  288. * @}
  289. */
  290. #ifdef __cplusplus
  291. }
  292. #endif
  293. #endif /* LWIP_HDR_ARCH_H */