Whamcloud - gitweb
c9ae107a3313e29f25130ff407f6e5df6a3f1879
[fs/lustre-release.git] / libcfs / include / libcfs / winnt / portals_utils.h
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
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
57 static inline __u32 __do_div(__u32 * n, __u32 b) 
58 {
59     __u32   mod;
60
61     mod = *n % b;
62     *n  = *n / b;
63     return mod;
64
65
66 #define do_div(n,base)  __do_div((__u32 *)&(n), (__u32) (base))
67
68 #ifdef __KERNEL__
69
70 #include <stdlib.h>
71 #include <libcfs/winnt/winnt-types.h>
72
73 char * strsep(char **s, const char *ct);
74 static inline size_t strnlen(const char * s, size_t count) {
75     size_t len = 0;
76     while(len < count && s[len++]);
77     return len;
78 }
79 char * ul2dstr(ulong_ptr address, char *buf, int len);
80
81 #define simple_strtol(a1, a2, a3)               strtol(a1, a2, a3)
82 #define simple_strtoll(a1, a2, a3)              (__s64)strtoull(a1, a2, a3)
83 #define simple_strtoull(a1, a2, a3)             strtoull(a1, a2, a3)
84
85 unsigned long simple_strtoul(const char *cp,char **endp, unsigned int base);
86
87 static inline int test_bit(int nr, void * addr)
88 {
89     return ((1UL << (nr & 31)) & (((volatile ULONG *) addr)[nr >> 5])) != 0;
90 }
91
92 static inline void clear_bit(int nr, void * addr)
93 {
94     (((volatile ULONG *) addr)[nr >> 5]) &= (~(1UL << (nr & 31)));
95 }
96
97
98 static inline void set_bit(int nr, void * addr)
99 {
100     (((volatile ULONG *) addr)[nr >> 5]) |= (1UL << (nr & 31));
101 }
102
103 static inline void read_random(char *buf, int len)
104 {
105     ULONG   Seed = (ULONG) buf;
106     Seed = RtlRandom(&Seed);
107     while (len >0) {
108         if (len > sizeof(ULONG)) {
109             memcpy(buf, &Seed, sizeof(ULONG));
110             len -= sizeof(ULONG);
111             buf += sizeof(ULONG);
112         } else {
113             memcpy(buf, &Seed, len);
114             len = 0;
115             break;
116         } 
117     }
118 }
119 #define get_random_bytes(buf, len)  read_random(buf, len)
120
121 /* do NOT use function or expression as parameters ... */
122
123 #ifndef min_t
124 #define min_t(type,x,y) (type)(x) < (type)(y) ? (x): (y)
125 #endif
126
127 #ifndef max_t
128 #define max_t(type,x,y) (type)(x) < (type)(y) ? (y): (x)
129 #endif
130
131
132 #define NIPQUAD(addr)                       \
133         ((unsigned char *)&addr)[0],    \
134         ((unsigned char *)&addr)[1],    \
135         ((unsigned char *)&addr)[2],    \
136         ((unsigned char *)&addr)[3]
137
138 #define HIPQUAD(addr)                       \
139         ((unsigned char *)&addr)[3],    \
140         ((unsigned char *)&addr)[2],    \
141         ((unsigned char *)&addr)[1],    \
142         ((unsigned char *)&addr)[0]
143
144 static int copy_from_user(void *to, void *from, int c) 
145 {
146     memcpy(to, from, c);
147     return 0;
148 }
149
150 static int copy_to_user(void *to, void *from, int c) 
151 {
152     memcpy(to, from, c);
153     return 0;
154 }
155
156
157 #define put_user(x, ptr)        \
158 (                               \
159     *(ptr) = x,                 \
160     0                           \
161 )
162
163
164 #define get_user(x,ptr)         \
165 (                               \
166     x = *(ptr),                 \
167     0                           \
168 )
169
170 #define num_physpages                   (64 * 1024)
171
172 #define snprintf  _snprintf
173 #define vsnprintf _vsnprintf
174
175
176 #endif  /* !__KERNEL__ */
177
178 int cfs_error_code(NTSTATUS);
179
180 #endif