def.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without modification,
  6. * are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
  19. * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  20. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
  21. * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  22. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  23. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  24. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  25. * OF SUCH DAMAGE.
  26. *
  27. * This file is part of the lwIP TCP/IP stack.
  28. *
  29. * Author: Adam Dunkels <adam@sics.se>
  30. *
  31. */
  32. #ifndef __LWIP_DEF_H__
  33. #define __LWIP_DEF_H__
  34. /* arch.h might define NULL already */
  35. #include "lwip/arch.h"
  36. #include "lwip/opt.h"
  37. #ifdef __cplusplus
  38. extern "C" {
  39. #endif
  40. #define LWIP_MAX(x , y) (((x) > (y)) ? (x) : (y))
  41. #define LWIP_MIN(x , y) (((x) < (y)) ? (x) : (y))
  42. #ifndef NULL
  43. #define NULL ((void *)0)
  44. #endif
  45. /** Get the absolute difference between 2 u32_t values (correcting overflows)
  46. * 'a' is expected to be 'higher' (without overflow) than 'b'. */
  47. #define LWIP_U32_DIFF(a, b) (((a) >= (b)) ? ((a) - (b)) : (((a) + ((b) ^ 0xFFFFFFFF) + 1)))
  48. /* Endianess-optimized shifting of two u8_t to create one u16_t */
  49. #if BYTE_ORDER == LITTLE_ENDIAN
  50. #define LWIP_MAKE_U16(a, b) ((a << 8) | b)
  51. #else
  52. #define LWIP_MAKE_U16(a, b) ((b << 8) | a)
  53. #endif
  54. #ifndef LWIP_PLATFORM_BYTESWAP
  55. #define LWIP_PLATFORM_BYTESWAP 0
  56. #endif
  57. #ifndef LWIP_PREFIX_BYTEORDER_FUNCS
  58. /* workaround for naming collisions on some platforms */
  59. #ifdef htons
  60. #undef htons
  61. #endif /* htons */
  62. #ifdef htonl
  63. #undef htonl
  64. #endif /* htonl */
  65. #ifdef ntohs
  66. #undef ntohs
  67. #endif /* ntohs */
  68. #ifdef ntohl
  69. #undef ntohl
  70. #endif /* ntohl */
  71. #define htons(x) lwip_htons(x)
  72. #define ntohs(x) lwip_ntohs(x)
  73. #define htonl(x) lwip_htonl(x)
  74. #define ntohl(x) lwip_ntohl(x)
  75. #endif /* LWIP_PREFIX_BYTEORDER_FUNCS */
  76. #if BYTE_ORDER == BIG_ENDIAN
  77. #define lwip_htons(x) (x)
  78. #define lwip_ntohs(x) (x)
  79. #define lwip_htonl(x) (x)
  80. #define lwip_ntohl(x) (x)
  81. #define PP_HTONS(x) (x)
  82. #define PP_NTOHS(x) (x)
  83. #define PP_HTONL(x) (x)
  84. #define PP_NTOHL(x) (x)
  85. #else /* BYTE_ORDER != BIG_ENDIAN */
  86. #if LWIP_PLATFORM_BYTESWAP
  87. #define lwip_htons(x) LWIP_PLATFORM_HTONS(x)
  88. #define lwip_ntohs(x) LWIP_PLATFORM_HTONS(x)
  89. #define lwip_htonl(x) LWIP_PLATFORM_HTONL(x)
  90. #define lwip_ntohl(x) LWIP_PLATFORM_HTONL(x)
  91. #else /* LWIP_PLATFORM_BYTESWAP */
  92. u16_t lwip_htons(u16_t x);
  93. u16_t lwip_ntohs(u16_t x);
  94. u32_t lwip_htonl(u32_t x);
  95. u32_t lwip_ntohl(u32_t x);
  96. #endif /* LWIP_PLATFORM_BYTESWAP */
  97. /* These macros should be calculated by the preprocessor and are used
  98. with compile-time constants only (so that there is no little-endian
  99. overhead at runtime). */
  100. #define PP_HTONS(x) ((((x) & 0xff) << 8) | (((x) & 0xff00) >> 8))
  101. #define PP_NTOHS(x) PP_HTONS(x)
  102. #define PP_HTONL(x) ((((x) & 0xff) << 24) | \
  103. (((x) & 0xff00) << 8) | \
  104. (((x) & 0xff0000UL) >> 8) | \
  105. (((x) & 0xff000000UL) >> 24))
  106. #define PP_NTOHL(x) PP_HTONL(x)
  107. #endif /* BYTE_ORDER == BIG_ENDIAN */
  108. #ifdef __cplusplus
  109. }
  110. #endif
  111. #endif /* __LWIP_DEF_H__ */