Simon Cooksey / mbed-os
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers def.h Source File

def.h

Go to the documentation of this file.
00001 /**
00002  * @file
00003  * various utility macros
00004  */
00005 
00006 /*
00007  * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
00008  * All rights reserved.
00009  *
00010  * Redistribution and use in source and binary forms, with or without modification,
00011  * are permitted provided that the following conditions are met:
00012  *
00013  * 1. Redistributions of source code must retain the above copyright notice,
00014  *    this list of conditions and the following disclaimer.
00015  * 2. Redistributions in binary form must reproduce the above copyright notice,
00016  *    this list of conditions and the following disclaimer in the documentation
00017  *    and/or other materials provided with the distribution.
00018  * 3. The name of the author may not be used to endorse or promote products
00019  *    derived from this software without specific prior written permission.
00020  *
00021  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
00022  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
00023  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
00024  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00025  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
00026  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00027  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00028  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
00029  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
00030  * OF SUCH DAMAGE.
00031  *
00032  * This file is part of the lwIP TCP/IP stack.
00033  *
00034  * Author: Adam Dunkels <adam@sics.se>
00035  *
00036  */
00037 #ifndef LWIP_HDR_DEF_H
00038 #define LWIP_HDR_DEF_H
00039 
00040 /* arch.h might define NULL already */
00041 #include "lwip/arch.h"
00042 #include "lwip/opt.h"
00043 #if LWIP_PERF
00044 #include "arch/perf.h"
00045 #else /* LWIP_PERF */
00046 #define PERF_START    /* null definition */
00047 #define PERF_STOP(x)  /* null definition */
00048 #endif /* LWIP_PERF */
00049 
00050 #ifdef __cplusplus
00051 extern "C" {
00052 #endif
00053 
00054 #define LWIP_MAX(x , y)  (((x) > (y)) ? (x) : (y))
00055 #define LWIP_MIN(x , y)  (((x) < (y)) ? (x) : (y))
00056 
00057 /* Get the number of entries in an array ('x' must NOT be a pointer!) */
00058 #define LWIP_ARRAYSIZE(x) (sizeof(x)/sizeof((x)[0]))
00059 
00060 #ifndef NULL
00061 #ifdef __cplusplus
00062 #define NULL 0
00063 #else
00064 #define NULL ((void *)0)
00065 #endif
00066 #endif
00067 
00068 /* Endianess-optimized shifting of two u8_t to create one u16_t */
00069 #if BYTE_ORDER == LITTLE_ENDIAN
00070 #define LWIP_MAKE_U16(a, b) ((a << 8) | b)
00071 #else
00072 #define LWIP_MAKE_U16(a, b) ((b << 8) | a)
00073 #endif
00074 
00075 #ifndef LWIP_PLATFORM_BYTESWAP
00076 #define LWIP_PLATFORM_BYTESWAP 0
00077 #endif
00078 
00079 #ifndef LWIP_PREFIX_BYTEORDER_FUNCS
00080 /* workaround for naming collisions on some platforms */
00081 
00082 #ifdef htons
00083 #undef htons
00084 #endif /* htons */
00085 #ifdef htonl
00086 #undef htonl
00087 #endif /* htonl */
00088 #ifdef ntohs
00089 #undef ntohs
00090 #endif /* ntohs */
00091 #ifdef ntohl
00092 #undef ntohl
00093 #endif /* ntohl */
00094 
00095 #define htons(x) lwip_htons(x)
00096 #define ntohs(x) lwip_ntohs(x)
00097 #define htonl(x) lwip_htonl(x)
00098 #define ntohl(x) lwip_ntohl(x)
00099 #endif /* LWIP_PREFIX_BYTEORDER_FUNCS */
00100 
00101 #if BYTE_ORDER == BIG_ENDIAN
00102 #define lwip_htons(x) (x)
00103 #define lwip_ntohs(x) (x)
00104 #define lwip_htonl(x) (x)
00105 #define lwip_ntohl(x) (x)
00106 #define PP_HTONS(x) (x)
00107 #define PP_NTOHS(x) (x)
00108 #define PP_HTONL(x) (x)
00109 #define PP_NTOHL(x) (x)
00110 #else /* BYTE_ORDER != BIG_ENDIAN */
00111 #if LWIP_PLATFORM_BYTESWAP
00112 #define lwip_htons(x) LWIP_PLATFORM_HTONS(x)
00113 #define lwip_ntohs(x) LWIP_PLATFORM_HTONS(x)
00114 #define lwip_htonl(x) LWIP_PLATFORM_HTONL(x)
00115 #define lwip_ntohl(x) LWIP_PLATFORM_HTONL(x)
00116 #else /* LWIP_PLATFORM_BYTESWAP */
00117 u16_t lwip_htons(u16_t x);
00118 u16_t lwip_ntohs(u16_t x);
00119 u32_t lwip_htonl(u32_t x);
00120 u32_t lwip_ntohl(u32_t x);
00121 #endif /* LWIP_PLATFORM_BYTESWAP */
00122 
00123 /* These macros should be calculated by the preprocessor and are used
00124    with compile-time constants only (so that there is no little-endian
00125    overhead at runtime). */
00126 #define PP_HTONS(x) ((((x) & 0xff) << 8) | (((x) & 0xff00) >> 8))
00127 #define PP_NTOHS(x) PP_HTONS(x)
00128 #define PP_HTONL(x) ((((x) & 0xff) << 24) | \
00129                      (((x) & 0xff00) << 8) | \
00130                      (((x) & 0xff0000UL) >> 8) | \
00131                      (((x) & 0xff000000UL) >> 24))
00132 #define PP_NTOHL(x) PP_HTONL(x)
00133 
00134 #endif /* BYTE_ORDER == BIG_ENDIAN */
00135 
00136 #ifdef __cplusplus
00137 }
00138 #endif
00139 
00140 #endif /* LWIP_HDR_DEF_H */
00141