arch.h 11 KB

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