4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
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
27 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
31 * This file is part of Lustre, http://www.lustre.org/
32 * Lustre is a trademark of Sun Microsystems, Inc.
34 * libcfs/include/libcfs/winnt/portals_utils.h
36 * Basic library routines.
39 #ifndef __LIBCFS_WINNT_PORTALS_UTILS_H__
40 #define __LIBCFS_WINNT_PORTALS_UTILS_H__
42 #ifndef cfs_is_flag_set
43 #define cfs_is_flag_set(x,f) (((x)&(f))==(f))
47 #define cfs_set_flag(x,f) ((x) |= (f))
50 #ifndef cfs_clear_flag
51 #define cfs_clear_flag(x,f) ((x) &= ~(f))
54 static inline __u32 do_div64(__u64 * n, __u64 b)
63 #define do_div(n, b) do_div64(&(n), (__u64)b)
67 #include <libcfs/winnt/winnt-types.h>
69 char * strsep(char **s, const char *ct);
70 char * ul2dstr(ulong_ptr_t address, char *buf, int len);
72 #define simple_strtol(a1, a2, a3) strtol(a1, a2, a3)
73 #define simple_strtoll(a1, a2, a3) (__s64)strtoull(a1, a2, a3)
74 #define simple_strtoull(a1, a2, a3) strtoull(a1, a2, a3)
76 unsigned long simple_strtoul(const char *cp,char **endp, unsigned int base);
78 static inline int set_bit(int nr, void * addr)
80 (((volatile ULONG *) addr)[nr >> 5]) |= (1UL << (nr & 31));
81 return *((int *) addr);
84 static inline int test_bit(int nr, void * addr)
86 return (int)(((1UL << (nr & 31)) & (((volatile ULONG *) addr)[nr >> 5])) != 0);
89 static inline int clear_bit(int nr, void * addr)
91 (((volatile ULONG *) addr)[nr >> 5]) &= (~(1UL << (nr & 31)));
92 return *((int *) addr);
95 static inline int test_and_set_bit(int nr, volatile void *addr)
99 volatile unsigned char *ADDR = addr;
102 mask = 1 << (nr & 0x07);
103 rc = ((mask & *ADDR) != 0);
109 #define ext2_set_bit(nr, addr) (set_bit(nr, addr), 0)
110 #define ext2_clear_bit(nr, addr) (clear_bit(nr, addr), 0)
111 #define ext2_test_bit(nr, addr) test_bit(nr, addr)
113 static inline int ffs(int x)
142 static inline unsigned long __cfs_ffs(unsigned long word)
146 #if BITS_PER_LONG == 64
147 if ((word & 0xffffffff) == 0) {
152 if ((word & 0xffff) == 0) {
156 if ((word & 0xff) == 0) {
160 if ((word & 0xf) == 0) {
164 if ((word & 0x3) == 0) {
168 if ((word & 0x1) == 0)
174 * fls - find last (most-significant) bit set
175 * @x: the word to search
177 * This is defined the same way as ffs.
178 * Note fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32.
187 if (!(x & 0xffff0000u)) {
191 if (!(x & 0xff000000u)) {
195 if (!(x & 0xf0000000u)) {
199 if (!(x & 0xc0000000u)) {
203 if (!(x & 0x80000000u)) {
210 static inline unsigned find_first_bit(const unsigned long *addr,
216 unsigned long val = *addr++;
218 return __cfs_ffs(val) + x;
219 x += (sizeof(*addr)<<3);
224 static inline void read_random(char *buf, int len)
226 ULONG Seed = (ULONG)(ULONG_PTR) buf;
227 Seed = RtlRandom(&Seed);
229 if (len > sizeof(ULONG)) {
230 memcpy(buf, &Seed, sizeof(ULONG));
231 len -= sizeof(ULONG);
232 buf += sizeof(ULONG);
234 memcpy(buf, &Seed, len);
241 #define cfs_get_random_bytes_prim(buf, len) read_random(buf, len)
243 /* do NOT use function or expression as parameters ... */
246 #define min_t(type,x,y) (type)(x) < (type)(y) ? (x): (y)
250 #define max_t(type,x,y) (type)(x) < (type)(y) ? (y): (x)
254 #define NIPQUAD(addr) \
255 ((unsigned char *)&addr)[0], \
256 ((unsigned char *)&addr)[1], \
257 ((unsigned char *)&addr)[2], \
258 ((unsigned char *)&addr)[3]
260 #define HIPQUAD(addr) \
261 ((unsigned char *)&addr)[3], \
262 ((unsigned char *)&addr)[2], \
263 ((unsigned char *)&addr)[1], \
264 ((unsigned char *)&addr)[0]
266 static int cfs_copy_from_user(void *to, void *from, int c)
272 static int cfs_copy_to_user(void *to, const void *from, int c)
279 clear_user(void __user *to, unsigned long n)
285 #define put_user(x, ptr) \
292 #define get_user(x,ptr) \
298 #define cfs_num_physpages (64 * 1024)
299 #define CFS_NUM_CACHEPAGES cfs_num_physpages
303 #define unlink _unlink
306 #define fdopen _fdopen
307 #define strdup _strdup
308 #define fileno _fileno
309 #define isattry _isattry
312 #endif /* !__KERNEL__ */
314 int cfs_error_code(NTSTATUS);
316 static inline int vsnprintf(char *buf, size_t cnt,
317 const char *fmt, va_list va)
321 #ifdef TRUE /* using msvcrt from windkk 3790 */
322 rc = _vsnprintf(buf, cnt, fmt, va);
324 rc = _vsnprintf_s(buf, cnt, cnt, fmt, va);
331 static inline int snprintf(char *buf, size_t cnt,
332 const char *fmt, ...)
338 rc = vsnprintf(buf, cnt, fmt, va);