Whamcloud - gitweb
b=16150
[fs/lustre-release.git] / libcfs / include / libcfs / winnt / portals_utils.h
1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=4:tabstop=4
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * libcfs/include/libcfs/winnt/portals_utils.h
37  *
38  * Basic library routines.
39  */
40
41 #ifndef __LIBCFS_WINNT_PORTALS_UTILS_H__
42 #define __LIBCFS_WINNT_PORTALS_UTILS_H__
43
44 #ifndef cfs_is_flag_set
45 #define cfs_is_flag_set(x,f) (((x)&(f))==(f))
46 #endif
47
48 #ifndef cfs_set_flag
49 #define cfs_set_flag(x,f)    ((x) |= (f))
50 #endif
51
52 #ifndef cfs_clear_flag
53 #define cfs_clear_flag(x,f)  ((x) &= ~(f))
54 #endif
55
56 static inline __u32 do_div64(__u64 * n, __u64 b) 
57 {
58     __u64   mod;
59
60     mod = *n % b;
61     *n  = *n / b;
62     return (__u32)mod;
63
64
65 #define do_div(n, b) do_div64(&(n), (__u64)b)
66 #ifdef __KERNEL__
67
68 #include <stdlib.h>
69 #include <libcfs/winnt/winnt-types.h>
70
71 char * strsep(char **s, const char *ct);
72 char * ul2dstr(ulong_ptr_t address, char *buf, int len);
73
74 #define simple_strtol(a1, a2, a3)               strtol(a1, a2, a3)
75 #define simple_strtoll(a1, a2, a3)              (__s64)strtoull(a1, a2, a3)
76 #define simple_strtoull(a1, a2, a3)             strtoull(a1, a2, a3)
77
78 unsigned long simple_strtoul(const char *cp,char **endp, unsigned int base);
79
80 static inline int set_bit(int nr, void * addr)
81 {
82     (((volatile ULONG *) addr)[nr >> 5]) |= (1UL << (nr & 31));
83     return *((int *) addr);
84 }
85
86 static inline int test_bit(int nr, void * addr)
87 {
88     return (int)(((1UL << (nr & 31)) & (((volatile ULONG *) addr)[nr >> 5])) != 0);
89 }
90
91 static inline int clear_bit(int nr, void * addr)
92 {
93     (((volatile ULONG *) addr)[nr >> 5]) &= (~(1UL << (nr & 31)));
94     return *((int *) addr);
95 }
96
97 static inline int test_and_set_bit(int nr, volatile void *addr)
98 {
99     int rc;
100     unsigned char  mask;
101     volatile unsigned char *ADDR = addr;
102
103     ADDR += nr >> 3;
104     mask = 1 << (nr & 0x07);
105     rc = ((mask & *ADDR) != 0);
106     *ADDR |= mask;
107
108     return rc;
109 }
110
111 #define ext2_set_bit(nr,addr)   (set_bit(nr, addr), 0)
112 #define ext2_clear_bit(nr,addr) (clear_bit(nr, addr), 0)
113 #define ext2_test_bit(nr,addr)  test_bit(nr, addr)
114
115 static inline int ffs(int x)
116 {
117         int r = 1;
118
119         if (!x)
120                 return 0;
121         if (!(x & 0xffff)) {
122                 x >>= 16;
123                 r += 16;
124         }
125         if (!(x & 0xff)) {
126                 x >>= 8;
127                 r += 8;
128         }
129         if (!(x & 0xf)) {
130                 x >>= 4;
131                 r += 4;
132         }
133         if (!(x & 3)) {
134                 x >>= 2;
135                 r += 2;
136         }
137         if (!(x & 1)) {
138                 x >>= 1;
139                 r += 1;
140         }
141         return r;
142 }
143
144 static inline unsigned long __ffs(unsigned long word)
145 {
146         int num = 0;
147
148 #if BITS_PER_LONG == 64
149         if ((word & 0xffffffff) == 0) {
150                 num += 32;
151                 word >>= 32;
152         }
153 #endif
154         if ((word & 0xffff) == 0) {
155                 num += 16;
156                 word >>= 16;
157         }
158         if ((word & 0xff) == 0) {
159                 num += 8;
160                 word >>= 8;
161         }
162         if ((word & 0xf) == 0) {
163                 num += 4;
164                 word >>= 4;
165         }
166         if ((word & 0x3) == 0) {
167                 num += 2;
168                 word >>= 2;
169         }
170         if ((word & 0x1) == 0)
171                 num += 1;
172         return num;
173 }
174
175 /**
176  * fls - find last (most-significant) bit set
177  * @x: the word to search
178  *
179  * This is defined the same way as ffs.
180  * Note fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32.
181  */
182 static inline
183 int fls(int x)
184 {
185         int r = 32;
186
187         if (!x)
188                 return 0;
189         if (!(x & 0xffff0000u)) {
190                 x <<= 16;
191                 r -= 16;
192         }
193         if (!(x & 0xff000000u)) {
194                 x <<= 8;
195                 r -= 8;
196         }
197         if (!(x & 0xf0000000u)) {
198                 x <<= 4;
199                 r -= 4;
200         }
201         if (!(x & 0xc0000000u)) {
202                 x <<= 2;
203                 r -= 2;
204         }
205         if (!(x & 0x80000000u)) {
206                 x <<= 1;
207                 r -= 1;
208         }
209         return r;
210 }
211
212 static inline unsigned find_first_bit(const unsigned long *addr, unsigned size)
213 {
214         unsigned x = 0;
215
216         while (x < size) {
217                 unsigned long val = *addr++;
218                 if (val)
219                         return __ffs(val) + x;
220                 x += (sizeof(*addr)<<3);
221         }
222         return x;
223 }
224
225 static inline void read_random(char *buf, int len)
226 {
227     ULONG   Seed = (ULONG)(ULONG_PTR) buf;
228     Seed = RtlRandom(&Seed);
229     while (len >0) {
230         if (len > sizeof(ULONG)) {
231             memcpy(buf, &Seed, sizeof(ULONG));
232             len -= sizeof(ULONG);
233             buf += sizeof(ULONG);
234         } else {
235             memcpy(buf, &Seed, len);
236             len = 0;
237             break;
238         } 
239     }
240 }
241
242 #define get_random_bytes(buf, len)  read_random(buf, len)
243
244 /* do NOT use function or expression as parameters ... */
245
246 #ifndef min_t
247 #define min_t(type,x,y) (type)(x) < (type)(y) ? (x): (y)
248 #endif
249
250 #ifndef max_t
251 #define max_t(type,x,y) (type)(x) < (type)(y) ? (y): (x)
252 #endif
253
254
255 #define NIPQUAD(addr)                       \
256         ((unsigned char *)&addr)[0],    \
257         ((unsigned char *)&addr)[1],    \
258         ((unsigned char *)&addr)[2],    \
259         ((unsigned char *)&addr)[3]
260
261 #define HIPQUAD(addr)                       \
262         ((unsigned char *)&addr)[3],    \
263         ((unsigned char *)&addr)[2],    \
264         ((unsigned char *)&addr)[1],    \
265         ((unsigned char *)&addr)[0]
266
267 static int copy_from_user(void *to, void *from, int c) 
268 {
269     memcpy(to, from, c);
270     return 0;
271 }
272
273 static int copy_to_user(void *to, const void *from, int c) 
274 {
275     memcpy(to, from, c);
276     return 0;
277 }
278
279 static unsigned long
280 clear_user(void __user *to, unsigned long n)
281 {
282     memset(to, 0, n);
283         return n;
284 }
285
286 #define put_user(x, ptr)        \
287 (                               \
288     *(ptr) = x,                 \
289     0                           \
290 )
291
292
293 #define get_user(x,ptr)         \
294 (                               \
295     x = *(ptr),                 \
296     0                           \
297 )
298
299 #define num_physpages                   (64 * 1024)
300
301 #else
302
303 #define unlink _unlink 
304 #define close  _close
305 #define open   _open
306 #define fdopen _fdopen
307 #define strdup _strdup
308 #define fileno _fileno
309 #define isattry _isattry
310 #define stat    _stat
311
312 #endif  /* !__KERNEL__ */
313
314 int cfs_error_code(NTSTATUS);
315
316 static inline int vsnprintf(char *buf, size_t cnt,
317                             const char *fmt, va_list va)
318 {
319     int rc;
320
321 #ifdef TRUE /* using msvcrt from windkk 3790 */
322     rc = _vsnprintf(buf, cnt, fmt, va);
323 #else
324     rc = _vsnprintf_s(buf, cnt, cnt, fmt, va);
325 #endif
326     if (rc == -1)
327         return cnt;
328     return rc;
329 }
330
331 static inline int snprintf(char *buf, size_t cnt, 
332                            const char *fmt, ...)
333 {
334     int         rc;
335     va_list     va;
336
337     va_start(va, fmt);
338     rc = vsnprintf(buf, cnt, fmt, va);
339     va_end(va);
340     return rc;
341 }
342
343 #endif