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