Whamcloud - gitweb
b8678d9b459a087bb0b73ed438ddd834726beffc
[fs/lustre-release.git] / libcfs / include / libcfs / winnt / portals_utils.h
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2012, Intel Corporation.
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,
213                                           unsigned size)
214 {
215         unsigned x = 0;
216
217         while (x < size) {
218                 unsigned long val = *addr++;
219                 if (val)
220                         return __ffs(val) + x;
221                 x += (sizeof(*addr)<<3);
222         }
223         return x;
224 }
225
226 static inline void read_random(char *buf, int len)
227 {
228     ULONG   Seed = (ULONG)(ULONG_PTR) buf;
229     Seed = RtlRandom(&Seed);
230     while (len >0) {
231         if (len > sizeof(ULONG)) {
232             memcpy(buf, &Seed, sizeof(ULONG));
233             len -= sizeof(ULONG);
234             buf += sizeof(ULONG);
235         } else {
236             memcpy(buf, &Seed, len);
237             len = 0;
238             break;
239         } 
240     }
241 }
242
243 #define get_random_bytes(buf, len)  read_random(buf, len)
244
245 /* do NOT use function or expression as parameters ... */
246
247 #ifndef min_t
248 #define min_t(type,x,y) (type)(x) < (type)(y) ? (x): (y)
249 #endif
250
251 #ifndef max_t
252 #define max_t(type,x,y) (type)(x) < (type)(y) ? (y): (x)
253 #endif
254
255
256 #define NIPQUAD(addr)                       \
257         ((unsigned char *)&addr)[0],    \
258         ((unsigned char *)&addr)[1],    \
259         ((unsigned char *)&addr)[2],    \
260         ((unsigned char *)&addr)[3]
261
262 #define HIPQUAD(addr)                       \
263         ((unsigned char *)&addr)[3],    \
264         ((unsigned char *)&addr)[2],    \
265         ((unsigned char *)&addr)[1],    \
266         ((unsigned char *)&addr)[0]
267
268 static int copy_from_user(void *to, void *from, int c)
269 {
270         memcpy(to, from, c);
271         return 0;
272 }
273
274 static int copy_to_user(void *to, const void *from, int c)
275 {
276         memcpy(to, from, c);
277         return 0;
278 }
279
280 static unsigned long
281 clear_user(void __user *to, unsigned long n)
282 {
283     memset(to, 0, n);
284         return n;
285 }
286
287 #define put_user(x, ptr)        \
288 (                               \
289     *(ptr) = x,                 \
290     0                           \
291 )
292
293
294 #define get_user(x,ptr)         \
295 (                               \
296     x = *(ptr),                 \
297     0                           \
298 )
299
300 #define totalram_pages               (64 * 1024)
301 #define NUM_CACHEPAGES              totalram_pages
302
303 #else
304
305 #define unlink _unlink 
306 #define close  _close
307 #define open   _open
308 #define fdopen _fdopen
309 #define strdup _strdup
310 #define fileno _fileno
311 #define isattry _isattry
312 #define stat    _stat
313
314 #endif  /* !__KERNEL__ */
315
316 int cfs_error_code(NTSTATUS);
317
318 static inline int vsnprintf(char *buf, size_t cnt,
319                             const char *fmt, va_list va)
320 {
321     int rc;
322
323 #ifdef TRUE /* using msvcrt from windkk 3790 */
324     rc = _vsnprintf(buf, cnt, fmt, va);
325 #else
326     rc = _vsnprintf_s(buf, cnt, cnt, fmt, va);
327 #endif
328     if (rc == -1)
329         return cnt;
330     return rc;
331 }
332
333 static inline int snprintf(char *buf, size_t cnt, 
334                            const char *fmt, ...)
335 {
336     int         rc;
337     va_list     va;
338
339     va_start(va, fmt);
340     rc = vsnprintf(buf, cnt, fmt, va);
341     va_end(va);
342     return rc;
343 }
344
345 #endif