1 /**
2  * @file
3  * various utility macros
4  */
5 
6 /*
7  * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without modification,
11  * are permitted provided that the following conditions are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright notice,
14  *    this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright notice,
16  *    this list of conditions and the following disclaimer in the documentation
17  *    and/or other materials provided with the distribution.
18  * 3. The name of the author may not be used to endorse or promote products
19  *    derived from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
22  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
24  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
26  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
29  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
30  * OF SUCH DAMAGE.
31  *
32  * This file is part of the lwIP TCP/IP stack.
33  *
34  * Author: Adam Dunkels <adam@sics.se>
35  *
36  */
37 
38 /**
39  * @defgroup perf Performance measurement
40  * @ingroup sys_layer
41  * All defines related to this section must not be placed in lwipopts.h,
42  * but in arch/perf.h!
43  * Measurement calls made throughout lwip, these can be defined to nothing.
44  * - PERF_START: start measuring something.
45  * - PERF_STOP(x): stop measuring something, and record the result.
46  */
47 
48 #ifndef LWIP_HDR_DEF_H
49 #define LWIP_HDR_DEF_H
50 
51 /* arch.h might define NULL already */
52 #include "lwip/arch.h"
53 #include "lwip/opt.h"
54 #if LWIP_PERF
55 #include "arch/perf.h"
56 #else /* LWIP_PERF */
57 #define PERF_START    /* null definition */
58 #define PERF_STOP(x)  /* null definition */
59 #endif /* LWIP_PERF */
60 
61 #ifdef __cplusplus
62 extern "C" {
63 #endif
64 
65 #define LWIP_MAX(x , y)  (((x) > (y)) ? (x) : (y))
66 #define LWIP_MIN(x , y)  (((x) < (y)) ? (x) : (y))
67 
68 /* Get the number of entries in an array ('x' must NOT be a pointer!) */
69 #define LWIP_ARRAYSIZE(x) (sizeof(x)/sizeof((x)[0]))
70 
71 /** Create u32_t value from bytes */
72 #define LWIP_MAKEU32(a,b,c,d) (((u32_t)((a) & 0xff) << 24) | \
73                                ((u32_t)((b) & 0xff) << 16) | \
74                                ((u32_t)((c) & 0xff) << 8)  | \
75                                 (u32_t)((d) & 0xff))
76 
77 #ifndef NULL
78 #ifdef __cplusplus
79 #define NULL 0
80 #else
81 #define NULL ((void *)0)
82 #endif
83 #endif
84 
85 #if BYTE_ORDER == BIG_ENDIAN
86 #define lwip_htons(x) ((u16_t)(x))
87 #define lwip_ntohs(x) ((u16_t)(x))
88 #define lwip_htonl(x) ((u32_t)(x))
89 #define lwip_ntohl(x) ((u32_t)(x))
90 #define PP_HTONS(x)   ((u16_t)(x))
91 #define PP_NTOHS(x)   ((u16_t)(x))
92 #define PP_HTONL(x)   ((u32_t)(x))
93 #define PP_NTOHL(x)   ((u32_t)(x))
94 #else /* BYTE_ORDER != BIG_ENDIAN */
95 #ifndef lwip_htons
96 u16_t lwip_htons(u16_t x);
97 #endif
98 #define lwip_ntohs(x) lwip_htons(x)
99 
100 #ifndef lwip_htonl
101 u32_t lwip_htonl(u32_t x);
102 #endif
103 #define lwip_ntohl(x) lwip_htonl(x)
104 
105 /* These macros should be calculated by the preprocessor and are used
106    with compile-time constants only (so that there is no little-endian
107    overhead at runtime). */
108 #define PP_HTONS(x) ((u16_t)((((x) & (u16_t)0x00ffU) << 8) | (((x) & (u16_t)0xff00U) >> 8)))
109 #define PP_NTOHS(x) PP_HTONS(x)
110 #define PP_HTONL(x) ((((x) & (u32_t)0x000000ffUL) << 24) | \
111                      (((x) & (u32_t)0x0000ff00UL) <<  8) | \
112                      (((x) & (u32_t)0x00ff0000UL) >>  8) | \
113                      (((x) & (u32_t)0xff000000UL) >> 24))
114 #define PP_NTOHL(x) PP_HTONL(x)
115 #endif /* BYTE_ORDER == BIG_ENDIAN */
116 
117 /* Provide usual function names as macros for users, but this can be turned off */
118 #ifndef LWIP_DONT_PROVIDE_BYTEORDER_FUNCTIONS
119 #define htons(x) lwip_htons(x)
120 #define ntohs(x) lwip_ntohs(x)
121 #define htonl(x) lwip_htonl(x)
122 #define ntohl(x) lwip_ntohl(x)
123 #endif
124 
125 /* Functions that are not available as standard implementations.
126  * In cc.h, you can #define these to implementations available on
127  * your platform to save some code bytes if you use these functions
128  * in your application, too.
129  */
130 
131 #ifndef lwip_itoa
132 /* This can be #defined to itoa() or snprintf(result, bufsize, "%d", number) depending on your platform */
133 void  lwip_itoa(char* result, size_t bufsize, int number);
134 #endif
135 #ifndef lwip_strnicmp
136 /* This can be #defined to strnicmp() or strncasecmp() depending on your platform */
137 int   lwip_strnicmp(const char* str1, const char* str2, size_t len);
138 #endif
139 #ifndef lwip_stricmp
140 /* This can be #defined to stricmp() or strcasecmp() depending on your platform */
141 int   lwip_stricmp(const char* str1, const char* str2);
142 #endif
143 #ifndef lwip_strnstr
144 /* This can be #defined to strnstr() depending on your platform */
145 char* lwip_strnstr(const char* buffer, const char* token, size_t n);
146 #endif
147 #ifndef lwip_strnistr
148 /* This can be #defined to strnistr() depending on your platform */
149 char* lwip_strnistr(const char* buffer, const char* token, size_t n);
150 #endif
151 
152 #ifdef __cplusplus
153 }
154 #endif
155 
156 #endif /* LWIP_HDR_DEF_H */
157