netbsd: rebase cm at NetBSD 4.0
[openafs.git] / src / config / stds.h
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 #ifndef OPENAFS_AFS_CONFIG_STDS_H
11 #define OPENAFS_AFS_CONFIG_STDS_H       1
12
13 #include <afs/param.h>
14 #include <sys/types.h>
15
16 #define IN                      /* indicates a parameter is read in */
17 #define OUT                     /* indicates a parameter is sent out (a ptr) */
18 #define INOUT                   /* indicates a parameter is read in and sent out (a ptr) */
19
20 #ifndef MACRO_BEGIN
21 #define MACRO_BEGIN     do {
22 #endif
23 #ifndef MACRO_END
24 #define MACRO_END       } while (0)
25 #endif
26
27 typedef void *opaque;
28
29 #ifndef _ATT4
30 #if defined(__HIGHC__)
31 /*
32  * keep HC from complaining about the use of "old-style" function definitions
33  * with prototypes
34  */
35 pragma Off(Prototype_override_warnings);
36 #endif /* defined(__HIGHC__) */
37 #endif
38
39 /* Now some types to enhance portability.  Always use these on the wire or when
40  * laying out shared structures on disk. */
41
42 /* Imagine that this worked...
43 #if (sizeof(long) != 4) || (sizeof(short) != 2)
44 #error We require size of long and pointers to be equal
45 #endif */
46
47 #define MAX_AFS_INT32 0x7FFFFFFF
48 #define MIN_AFS_INT32 (-MAX_AFS_INT32 - 1)
49 #define MAX_AFS_UINT32 0xFFFFFFFF
50 #define MAX_AFS_INT64 0x7FFFFFFFFFFFFFFFL
51 #define MIN_AFS_INT64 (-MAX_AFS_INT64 - 1)
52 #define MAX_AFS_UINT64 0xFFFFFFFFFFFFFFFFL
53
54 typedef short afs_int16;
55 typedef unsigned short afs_uint16;
56 #ifdef  AFS_64BIT_ENV
57 typedef int afs_int32;
58 typedef unsigned int afs_uint32;
59 #if defined(AFS_NT40_ENV) && defined(_MSC_VER)
60 typedef __int64 afs_int64;
61 typedef unsigned __int64 afs_uint64;
62 #else
63 typedef long long afs_int64;
64 typedef unsigned long long afs_uint64;
65 #endif
66 #define ZeroInt64(a)       (a = 0)
67 #define AssignInt64(a, b) *(b) = (a) 
68 #define IncInt64(a) (*(a))++
69 #define IncUInt64(a) (*(a))++
70 #define DecInt64(a) (*(a))--
71 #define DecUInt64(a) (*(a))--
72 #define GTInt64(a,b) ((a) > (b))
73 #define GEInt64(a,b) ((a) >= (b))
74 #define LEInt64(a,b) ((a) <= (b))
75 #define LTInt64(a,b) ((a) < (b))
76 #define AddInt64(a,b,c) *(c) = (afs_int64)(a) + (afs_int64)(b)
77 #define AddUInt64(a,b,c) *(c) = (afs_uint64)(a) + (afs_uint64)(b)
78 #define SubtractInt64(a,b,c) *(c) = (afs_int64)(a) - (afs_int64)(b)
79 #define SubtractUInt64(a,b,c) *(c) = (afs_uint64)(a) - (afs_uint64)(b)
80 #define CompareInt64(a,b) (afs_int64)(a) - (afs_int64)(b)
81 #define CompareUInt64(a,b) (afs_uint64)(a) - (afs_uint64)(b)
82 #define NonZeroInt64(a)                (a)
83 #define Int64ToInt32(a)    (a) & MAX_AFS_UINT32
84 #define FillInt64(t,h,l) (t) = ((afs_int64)(h) << 32) | (l);
85 #define SplitInt64(t,h,l) (h) = ((afs_int64)t) >> 32; (l) = (t) & MAX_AFS_UINT32;
86 #define RoundInt64ToInt32(a)    (a > MAX_AFS_UINT32) ? MAX_AFS_UINT32 : a;
87 #define RoundInt64ToInt31(a)    (a > MAX_AFS_INT32) ? MAX_AFS_INT32 : a;
88 #else /* AFS_64BIT_ENV */
89 typedef long afs_int32;
90 typedef unsigned long afs_uint32;
91
92 struct Int64 {
93     afs_int32 high;
94     afs_uint32 low;
95 };
96 typedef struct Int64 afs_int64;
97
98 struct u_Int64 {
99     afs_uint32 high;
100     afs_uint32 low;
101 };
102 typedef struct u_Int64 afs_uint64;
103 #define ZeroInt64(a) ((a).high = (a).low = 0)
104 #define AssignInt64(a, b) (b)->high = (a).high; (b)->low = (a).low
105 #define IncInt64(a) ((++((a)->low)) ? 0 : (a)->high++ )
106 #define IncUInt64(a) ((++((a)->low)) ? 0 : (a)->high++ )
107 #define DecInt64(a) (((a)->low)-- ? 0 : (a)->high-- )
108 #define DecUInt64(a) (((a)->low)-- ? 0 : (a)->high-- )
109 #define GTInt64(a,b) (((a).high > (b).high) || (((a).high == (b).high) && ((a).low > (b).low)))
110 #define GEInt64(a,b) (((a).high > (b).high) || (((a).high == (b).high) && ((a).low >= (b).low)))
111 #define LEInt64(a,b) (((a).high < (b).high) || (((a).high == (b).high) && ((a).low <= (b).low)))
112 #define LTInt64(a,b) (((a).high < (b).high) || (((a).high == (b).high) && ((a).low < (b).low)))
113 #define CompareInt64(a,b) (((afs_int32)(a).high - (afs_int32)(b).high) || (((a).high == (b).high) && ((a).low - (b).low))) 
114 #define AddInt64(a, b, c) {  afs_int64 _a, _b; _a = a; _b = b; (c)->low = _a.low + _b.low; (c)->high = _a.high + _b.high + ((c)->low < _b.low); } 
115 #define SubtractInt64(a, b, c) { afs_int64 _a, _b; _a = a; _b = b; (c)->low = _a.low - _b.low;  (c)->high = _a.high - _b.high - (_a.low < _b.low); } 
116 #define CompareUInt64(a,b) (((afs_uint32)(a).high - (afs_uint32)(b).high) || (((a).high == (b).high) && ((a).low - (b).low))) 
117 #define AddUInt64(a, b, c) {  afs_uint64 _a, _b; _a = a; _b = b; (c)->low = _a.low + _b.low; (c)->high = _a.high + _b.high + ((c)->low < _b.low); } 
118 #define SubtractUInt64(a, b, c) { afs_uint64 _a, _b; _a = a; _b = b; (c)->low = _a.low - _b.low;  (c)->high = _a.high - _b.high - (_a.low < _b.low); } 
119 #define NonZeroInt64(a)   (a).low || (a).high
120 #define Int64ToInt32(a)    (a).low
121 #define FillInt64(t,h,l) (t).high = (h); (t).low = (l);
122 #define SplitInt64(t,h,l) (h) = (t).high; (l) = (t).low;
123 #define RoundInt64ToInt32(a)    (a.high > 0) ? MAX_AFS_UINT32 : a.low;
124 #define RoundInt64ToInt31(a)    (a.high > 0) ? MAX_AFS_INT32 : a.low;
125 #endif /* AFS_64BIT_ENV */
126
127 /* AFS_64BIT_CLIENT should presently be set only for AFS_64BIT_ENV systems */
128
129 #ifdef AFS_64BIT_CLIENT
130 typedef afs_int64 afs_size_t;
131 typedef afs_uint64 afs_offs_t;
132 #else /* AFS_64BIT_CLIENT */
133 typedef afs_int32 afs_size_t;
134 typedef afs_uint32 afs_offs_t;
135 #endif /* AFS_64BIT_CLIENT */
136
137 typedef afs_int64 afs_foff_t;
138 typedef afs_uint64 afs_fsize_t;
139 typedef afs_int64 afs_sfsize_t;
140 #define SplitOffsetOrSize(t,h,l) SplitInt64(t,h,l)
141
142 /* Maximum integer sizes.  Also what is expected by %lld, %llu in
143  * afs_snprintf. */
144 #ifdef AFS_64BIT_CLIENT
145 typedef afs_int64 afs_intmax_t;
146 typedef afs_uint64 afs_uintmax_t;
147 #else /* !AFS_64BIT_CLIENT */
148 typedef afs_int32 afs_intmax_t;
149 typedef afs_uint32 afs_uintmax_t;
150 #endif /* !AFS_64BIT_CLIENT */
151
152 /* you still have to include <netinet/in.h> to make these work */
153
154 #define hton32 htonl
155 #define hton16 htons
156 #define ntoh32 ntohl
157 #define ntoh16 ntohs
158
159
160 /* Since there is going to be considerable use of 64 bit integers we provide
161  * some assistence in this matter.  The hyper type is supposed to be compatible
162  * with the afsHyper type: the same macros will work on both. */
163
164 #if     defined(AFS_64BIT_ENV) && 0
165
166 typedef unsigned long afs_hyper_t;
167
168 #define hcmp(a,b)       ((a) < (b) ? -1 : ((a) > (b) ? 1 : 0))
169 #define hsame(a,b)      ((a) == (b))
170 #define hiszero(a)      ((a) == 0)
171 #define hfitsin32(a)    ((a) & 0xffffffff00000000) == 0)
172 #define hset(a,b)       ((a) = (b))
173 #define hzero(a)        ((a) = 0)
174 #define hones(a)        ((a) = ~((unsigned long)0))
175 #define hget32(i,a)     ((i) = (unsigned int)(a))
176 #define hget64(hi,lo,a) ((lo) = ((unsigned int)(a)), (hi) = ((a) & (0xffffffff00000000)))
177 #define hset32(a,i)     ((a) = ((unsigned int)(i)))
178 #define hset64(a,hi,lo) ((a) = (((hi) << 32) | (lo)))
179 #define hgetlo(a)       ((a) & 0xffffffff)
180 #define hgethi(a)       (((unsigned int)(a)) >> 32)
181 #define hadd(a,b)       ((a) += (b))
182 /* XXX */
183 #define hadd32(a,b)     ((a) += (b))
184 #define hshlft(a,n)     ((a)<<(n))
185
186 #else /* AFS_64BIT_ENV */
187
188 typedef struct afs_hyper_t {    /* unsigned 64 bit integers */
189     unsigned int high;
190     unsigned int low;
191 } afs_hyper_t;
192
193 #define hcmp(a,b) ((a).high<(b).high? -1 : ((a).high > (b).high? 1 : \
194     ((a).low <(b).low? -1 : ((a).low > (b).low? 1 : 0))))
195 #define hsame(a,b) ((a).low == (b).low && (a).high == (b).high)
196 #define hiszero(a) ((a).low == 0 && (a).high == 0)
197 #define hfitsin32(a) ((a).high == 0)
198 #define hset(a,b) ((a) = (b))
199 #define hzero(a) ((a).low = 0, (a).high = 0)
200 #define hones(a) ((a).low = 0xffffffff, (a).high = 0xffffffff)
201 #define hget32(i,a) ((i) = (a).low)
202 #define hget64(hi,lo,a) ((lo) = (a).low, (hi) = (a).high)
203 #define hset32(a,i) ((a).high = 0, (a).low = (i))
204 #define hset64(a,hi,lo) ((a).high = (hi), (a).low = (lo))
205 #define hgetlo(a) ((a).low)
206 #define hgethi(a) ((a).high)
207 #define hshlft(a,n)                                                        \
208      { /*Shift Left n bits*/                                               \
209         int s = sizeof((a).low) * 8; /*#bits*/                             \
210         if ((n) <= 0) { /*No shift*/                                       \
211         } else if ((n) >= 2*s) { /*Shift off all bits*/                    \
212            (a).high = (a).low = 0;                                         \
213         } else if ((n) < s) { /*Part of low shifted into high*/            \
214            (a).high = ((a).high<<(n)) | (((a).low>>(s-(n))) & (1<<(n))-1); \
215            (a).low  = (a).low << (n);                                      \
216         } else if ((n) >= s) { /*All of low shifted into high plus some*/  \
217            (a).high = (a).low << ((n)-s);                                  \
218            (a).low=0;                                                      \
219         }                                                                  \
220      }
221
222 /* The algorithm here is to check for two cases that cause a carry.  If the top
223  * two bits are different then if the sum has the top bit off then there must
224  * have been a carry.  If the top bits are both one then there is always a
225  * carry.  We assume 32 bit ints and twos complement arithmetic. */
226
227 #define SIGN 0x80000000
228 #define hadd32(a,i) \
229     ((void)((((a).low ^ (int)(i)) & SIGN) \
230       ? (((((a).low + (int)(i)) & SIGN) == 0) && (a).high++) \
231       : (((a).low & (int)(i) & SIGN) && (a).high++)), \
232      (a).low += (int)(i))
233
234 #define hadd(a,b) (hadd32(a,(b).low), (a).high += (b).high)
235 #endif /* AFS_64BIT_ENV */
236
237 #if !defined(KERNEL) || defined(UKERNEL)
238 #ifndef AFS_NT40_ENV
239 #define max(a, b)               ((a) < (b) ? (b) : (a))
240 #define min(a, b)               ((a) > (b) ? (b) : (a))
241 #endif
242 /*#define abs(x)                  ((x) >= 0 ? (x) : -(x))*/
243 #endif
244
245 /* minumum length of string to pass to int_to_base64 */
246 typedef char b64_string_t[8];
247 #ifndef AFS_NT40_ENV
248 #if defined(AFS_HPUX_ENV) || defined(AFS_USR_HPUX_ENV) || (defined(AFS_SUN_ENV) && !defined(AFS_SUN5_ENV))
249 char *int_to_base64();
250 int base64_to_int();
251 #else
252 char *int_to_base64(b64_string_t s, int a);
253 int base64_to_int(char *s);
254 #endif
255 #endif /* AFS_NT40_ENV */
256 /*
257  * The afsUUID data type is built in to RX
258  */
259 struct afsUUID {
260     afs_uint32 time_low;
261     afs_uint16 time_mid;
262     afs_uint16 time_hi_and_version;
263     char clock_seq_hi_and_reserved;
264     char clock_seq_low;
265     char node[6];
266 };
267 typedef struct afsUUID afsUUID;
268
269 /* for now, demand attach fileserver is only support on unix pthreads builds */
270 #if defined(DEMAND_ATTACH_ENABLE) && defined(AFS_PTHREAD_ENV) && !defined(AFS_NT40_ENV)
271 #define AFS_DEMAND_ATTACH_FS 1
272 #endif
273
274 /* A macro that can be used when printf'ing 64 bit integers, as Unix and 
275  * windows use a different format string
276  */
277 #ifdef AFS_NT40_ENV
278 # define AFS_INT64_FMT "I64d"
279 # define AFS_UINT64_FMT "I64u"
280 # define AFS_PTR_FMT   "p"
281 # define AFS_SIZET_FMT "Iu"
282 #else
283 # define AFS_INT64_FMT "lld"
284 # define AFS_UINT64_FMT "llu"
285 # define AFS_PTR_FMT   "p"
286 # ifdef PRINTF_TAKES_Z_LEN
287 #  define AFS_SIZET_FMT "zu"
288 # else
289 #  define AFS_SIZET_FMT "lu"
290 # endif /* PRINTF_TAKES_Z_LEN */
291 #endif /* AFS_NT40_ENV */
292
293 /* Functions to safely cast afs_int32 and afs_uint32 so they can be used in 
294  * printf statemements with %ld and %lu
295  */
296 #ifdef AFS_NT40_ENV
297 #define static_inline __inline static
298 #define hdr_static_inline(x) __inline static x
299 #elif defined(AFS_HPUX_ENV) || defined(AFS_USR_HPUX_ENV)
300 /* The HPUX compiler can segfault on 'static __inline', so fall back to
301  * just 'static' so we can at least compile */
302 #define static_inline static
303 #define hdr_static_inline(x) static x
304 #elif defined(AFS_AIX_ENV) || defined(AFS_USR_AIX_ENV)
305 #define static_inline static
306 #define hdr_static_inline(x) static x
307 #elif defined(AFS_SGI_ENV) || defined(AFS_USR_SGI_ENV)
308 #define static_inline static
309 #define hdr_static_inline(x) x
310 #elif defined(AFS_NBSD_ENV)
311 #define static_inline static __inline __attribute__((always_inline))
312 #define hdr_static_inline(x) static __inline __attribute__((always_inline)) x
313 #else
314 #define static_inline static inline
315 #define hdr_static_inline(x) static inline x
316 #endif
317
318 hdr_static_inline(long) afs_printable_int32_ld(afs_int32 d) { return (long) d; }
319
320 hdr_static_inline(unsigned long) afs_printable_uint32_lu(afs_uint32 d) { return (unsigned long) d; }
321
322 #ifdef AFS_64BITUSERPOINTER_ENV
323 #define afs_pointer_to_int(p)      ((afs_uint32)  (afs_uint64) (p))
324 #define afs_int_to_pointer(i)     ((void *) (afs_uint64) (i))
325 #else
326 #define afs_pointer_to_int(p)      ((afs_uint32)   (p))
327 #define afs_int_to_pointer(i)      ((void *)  (i))
328 #endif
329
330 #if !defined(__GNUC__) || __GNUC__ < 2
331 #define AFS_UNUSED
332 #define AFS_ATTRIBUTE_FORMAT(style,x,y)
333 #else
334 #define AFS_UNUSED __attribute__((unused))
335 #define AFS_ATTRIBUTE_FORMAT(style,x,y) __attribute__((format(style, x, y)))
336 #endif
337
338 #endif /* OPENAFS_CONFIG_AFS_STDS_H */