xrootd
XrdSysPlatform.hh
Go to the documentation of this file.
1 #ifndef __XRDSYS_PLATFORM_H__
2 #define __XRDSYS_PLATFORM_H__
3 /******************************************************************************/
4 /* */
5 /* X r d S y s P l a t f o r m . h h */
6 /* */
7 /* (c) 2004 by the Board of Trustees of the Leland Stanford, Jr., University */
8 /* Produced by Andrew Hanushevsky for Stanford University under contract */
9 /* DE-AC02-76-SFO0515 with the Department of Energy */
10 /* */
11 /* This file is part of the XRootD software suite. */
12 /* */
13 /* XRootD is free software: you can redistribute it and/or modify it under */
14 /* the terms of the GNU Lesser General Public License as published by the */
15 /* Free Software Foundation, either version 3 of the License, or (at your */
16 /* option) any later version. */
17 /* */
18 /* XRootD is distributed in the hope that it will be useful, but WITHOUT */
19 /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
20 /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public */
21 /* License for more details. */
22 /* */
23 /* You should have received a copy of the GNU Lesser General Public License */
24 /* along with XRootD in a file called COPYING.LESSER (LGPL license) and file */
25 /* COPYING (GPL license). If not, see <http://www.gnu.org/licenses/>. */
26 /* */
27 /* The copyright holder's institutional names and contributor's names may not */
28 /* be used to endorse or promote products derived from this software without */
29 /* specific prior written permission of the institution or contributor. */
30 /******************************************************************************/
31 
32 // Include stdlib so that ENDIAN macros are defined properly
33 //
34 #include <stdlib.h>
35 
36 #ifdef __linux__
37 #include <memory.h>
38 #include <string.h>
39 #include <sys/types.h>
40 #include <sys/param.h>
41 #include <byteswap.h>
42 #define MAXNAMELEN NAME_MAX
43 #endif
44 
45 #ifdef __APPLE__
46 #include <AvailabilityMacros.h>
47 #include <sys/types.h>
48 #include <sys/param.h>
49 #define fdatasync(x) fsync(x)
50 #define MAXNAMELEN NAME_MAX
51 #ifndef dirent64
52 # define dirent64 dirent
53 #endif
54 #ifndef off64_t
55 #define off64_t int64_t
56 #endif
57 #if (!defined(MAC_OS_X_VERSION_10_5) || \
58  MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_5)
59 #ifndef stat64
60 # define stat64 stat
61 #endif
62 #endif
63 #endif
64 
65 #ifdef __FreeBSD__
66 #include <sys/types.h>
67 #include <sys/param.h>
68 #define MAXNAMELEN NAME_MAX
69 #endif
70 
71 #ifdef __GNU__
72 #include <sys/types.h>
73 #include <sys/param.h>
74 // These are undefined on purpose in GNU/Hurd.
75 // The values below are the ones used in Linux.
76 // The proper fix is to rewrite the code to not use hardcoded values,
77 // but instead allocate memory dynamically at runtime when sizes are known.
78 // This is true also for systems where these constants are defined.
79 #define MAXNAMELEN 255
80 #define MAXPATHLEN 4096
81 #define MAXHOSTNAMELEN 64
82 #endif
83 
84 #ifdef WIN32
85 #define MAXNAMELEN 256
86 #define MAXPATHLEN 1024
87 #endif
88 
89 #ifdef __solaris__
90 #define posix_memalign(memp, algn, sz) \
91  ((*memp = memalign(algn, sz)) ? 0 : ENOMEM)
92 #define __USE_LEGACY_PROTOTYPES__ 1
93 #endif
94 
95 #if defined(__linux__) || defined(__APPLE__) || defined(__FreeBSD__) || defined(__GNU__)
96 
97 #define S_IAMB 0x1FF /* access mode bits */
98 
99 #if !defined(__FreeBSD__)
100 #define F_DUP2FD F_DUPFD
101 #endif
102 
103 #define STATFS statfs
104 #define STATFS_BUFF struct statfs
105 
106 #define FS_BLKFACT 4
107 
108 #define FLOCK_t struct flock
109 
110 typedef off_t offset_t;
111 
112 #define GTZ_NULL (struct timezone *)0
113 
114 #else
115 
116 #define STATFS statvfs
117 #define STATFS_BUFF struct statvfs
118 
119 #define FS_BLKFACT 1
120 
121 #define SHMDT_t char *
122 
123 #define FLOCK_t flock_t
124 
125 #define GTZ_NULL (void *)0
126 
127 #endif
128 
129 #ifdef __linux__
130 
131 #define SHMDT_t const void *
132 #endif
133 
134 // For alternative platforms
135 //
136 #ifdef __APPLE__
137 #ifndef POLLRDNORM
138 #define POLLRDNORM 0
139 #endif
140 #ifndef POLLRDBAND
141 #define POLLRDBAND 0
142 #endif
143 #ifndef POLLWRNORM
144 #define POLLWRNORM 0
145 #endif
146 #define O_LARGEFILE 0
147 #define memalign(pgsz,amt) valloc(amt)
148 #define posix_memalign(memp, algn, sz) \
149  ((*memp = memalign(algn, sz)) ? 0 : ENOMEM)
150 #define SHMDT_t void *
151 #ifndef EDEADLOCK
152 #define EDEADLOCK EDEADLK
153 #endif
154 #endif
155 
156 #ifdef __FreeBSD__
157 #define O_LARGEFILE 0
158 typedef off_t off64_t;
159 #define memalign(pgsz,amt) valloc(amt)
160 #endif
161 
162 // Only sparc platforms have structure alignment problems w/ optimization
163 // so the h2xxx() variants are used when converting network streams.
164 
165 #if defined(_BIG_ENDIAN) || defined(__BIG_ENDIAN__) || \
166  defined(__IEEE_BIG_ENDIAN) || \
167  (defined(__BYTE_ORDER) && __BYTE_ORDER == __BIG_ENDIAN)
168 #define Xrd_Big_Endian
169 #ifndef htonll
170 #define htonll(_x_) _x_
171 #endif
172 #ifndef h2nll
173 #define h2nll(_x_, _y_) memcpy((void *)&_y_,(const void *)&_x_,sizeof(long long))
174 #endif
175 #ifndef ntohll
176 #define ntohll(_x_) _x_
177 #endif
178 #ifndef n2hll
179 #define n2hll(_x_, _y_) memcpy((void *)&_y_,(const void *)&_x_,sizeof(long long))
180 #endif
181 
182 #elif defined(_LITTLE_ENDIAN) || defined(__LITTLE_ENDIAN__) || \
183  defined(__IEEE_LITTLE_ENDIAN) || \
184  (defined(__BYTE_ORDER) && __BYTE_ORDER == __LITTLE_ENDIAN)
185 #if !defined(__GNUC__) || defined(__APPLE__)
186 
187 #if !defined(__sun) || (defined(__sun) && (!defined(_LP64) || defined(__SunOS_5_10)))
188 extern "C" unsigned long long Swap_n2hll(unsigned long long x);
189 #ifndef htonll
190 #define htonll(_x_) Swap_n2hll(_x_)
191 #endif
192 #ifndef ntohll
193 #define ntohll(_x_) Swap_n2hll(_x_)
194 #endif
195 #endif
196 
197 #else
198 
199 #ifndef htonll
200 #define htonll(_x_) __bswap_64(_x_)
201 #endif
202 #ifndef ntohll
203 #define ntohll(_x_) __bswap_64(_x_)
204 #endif
205 
206 #endif
207 
208 #ifndef h2nll
209 #define h2nll(_x_, _y_) memcpy((void *)&_y_,(const void *)&_x_,sizeof(long long));\
210  _y_ = htonll(_y_)
211 #endif
212 #ifndef n2hll
213 #define n2hll(_x_, _y_) memcpy((void *)&_y_,(const void *)&_x_,sizeof(long long));\
214  _y_ = ntohll(_y_)
215 #endif
216 
217 #else
218 #ifndef WIN32
219 #error Unable to determine target architecture endianness!
220 #endif
221 #endif
222 
223 #ifndef HAVE_STRLCPY
224 extern "C"
225 {extern size_t strlcpy(char *dst, const char *src, size_t size);}
226 #endif
227 
228 //
229 // To make socklen_t portable use SOCKLEN_t
230 //
231 #if defined(__solaris__) && !defined(__linux__)
232 # if __GNUC__ >= 3 || __GNUC_MINOR__ >= 90
233 # define XR__SUNGCC3
234 # endif
235 #endif
236 #if defined(__linux__)
237 # include <features.h>
238 # if __GNU_LIBRARY__ == 6
239 # ifndef XR__GLIBC
240 # define XR__GLIBC
241 # endif
242 # endif
243 #endif
244 #if defined(__GNU__)
245 # define XR__GLIBC
246 #endif
247 #if defined(_AIX) || \
248  (defined(XR__SUNGCC3) && !defined(__arch64__))
249 # define SOCKLEN_t size_t
250 #elif defined(XR__GLIBC) || \
251  defined(__FreeBSD__) || \
252  (defined(XR__SUNGCC3) && defined(__arch64__)) || defined(__APPLE__) || \
253  (defined(__sun) && defined(_SOCKLEN_T))
254 # ifndef SOCKLEN_t
255 # define SOCKLEN_t socklen_t
256 # endif
257 #elif !defined(SOCKLEN_t)
258 # define SOCKLEN_t int
259 #endif
260 
261 #ifdef _LP64
262 #define PTR2INT(x) static_cast<int>((long long)x)
263 #else
264 #define PTR2INT(x) int(x)
265 #endif
266 
267 #ifdef WIN32
268 #include "XrdSys/XrdWin32.hh"
269 #define Netdata_t void *
270 #define Sokdata_t char *
271 #define IOV_INIT(data,dlen) dlen,data
272 #define MAKEDIR(path,mode) mkdir(path)
273 #define CHMOD(path, mode) {}
274 #define net_errno WSAGetLastError()
275 #else
276 #define O_BINARY 0
277 #define Netdata_t char *
278 #define Sokdata_t void *
279 #define IOV_INIT(data,dlen) data,dlen
280 #define MAKEDIR(path,mode) mkdir(path,mode)
281 #define CHMOD(path, mode) chmod(path,mode)
282 #define net_errno errno
283 #endif
284 
285 // The following gets arround a relative new gcc compiler bug
286 //
287 #define XRDABS(x) (x < 0 ? -x : x)
288 
289 #ifndef LT_MODULE_EXT
290 #define LT_MODULE_EXT ".so"
291 #endif
292 
293 #endif // __XRDSYS_PLATFORM_H__
strlcpy
size_t strlcpy(char *dst, const char *src, size_t size)