Whamcloud - gitweb
Landing b_hd_newconfig on HEAD
[fs/lustre-release.git] / lnet / 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  *  Copyright (C) 2001 Cluster File Systems, Inc. <braam@clusterfs.com>
5  *
6  *   This file is part of Lustre, http://www.lustre.org.
7  *
8  *   Lustre is free software; you can redistribute it and/or
9  *   modify it under the terms of version 2 of the GNU General Public
10  *   License as published by the Free Software Foundation.
11  *
12  *   Lustre is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *   GNU General Public License for more details.
16  *
17  *   You should have received a copy of the GNU General Public License
18  *   along with Lustre; if not, write to the Free Software
19  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  *
21  * Basic library routines.
22  *
23  */
24
25 #ifndef __LIBCFS_WINNT_PORTALS_UTILS_H__
26 #define __LIBCFS_WINNT_PORTALS_UTILS_H__
27
28 #ifndef __LIBCFS_PORTALS_UTILS_H__
29 #error Do not #include this file directly. #include <libcfs/portals_utils.h> instead
30 #endif
31
32 #ifndef cfs_is_flag_set
33 #define cfs_is_flag_set(x,f) (((x)&(f))==(f))
34 #endif
35
36 #ifndef cfs_set_flag
37 #define cfs_set_flag(x,f)    ((x) |= (f))
38 #endif
39
40 #ifndef cfs_clear_flag
41 #define cfs_clear_flag(x,f)  ((x) &= ~(f))
42 #endif
43
44
45 static inline __u32 __do_div(__u32 * n, __u32 b) 
46 {
47     __u32   mod;
48
49     mod = *n % b;
50     *n  = *n / b;
51     return mod;
52
53
54 #define do_div(n,base)  __do_div((__u32 *)&(n), (__u32) (base))
55
56 #ifdef __KERNEL__
57
58 #include <stdlib.h>
59 #include <libcfs/winnt/winnt-types.h>
60
61 char * strsep(char **s, const char *ct);
62 static inline size_t strnlen(const char * s, size_t count) {
63     size_t len = 0;
64     while(len < count && s[len++]);
65     return len;
66 }
67 char * ul2dstr(ulong_ptr address, char *buf, int len);
68
69 #define simple_strtol(a1, a2, a3)               strtol(a1, a2, a3)
70 #define simple_strtoll(a1, a2, a3)              (__s64)strtoull(a1, a2, a3)
71 #define simple_strtoull(a1, a2, a3)             strtoull(a1, a2, a3)
72
73 unsigned long simple_strtoul(const char *cp,char **endp, unsigned int base);
74
75 static inline int test_bit(int nr, void * addr)
76 {
77     return ((1UL << (nr & 31)) & (((volatile ULONG *) addr)[nr >> 5])) != 0;
78 }
79
80 static inline void clear_bit(int nr, void * addr)
81 {
82     (((volatile ULONG *) addr)[nr >> 5]) &= (~(1UL << (nr & 31)));
83 }
84
85
86 static inline void set_bit(int nr, void * addr)
87 {
88     (((volatile ULONG *) addr)[nr >> 5]) |= (1UL << (nr & 31));
89 }
90
91 static inline void read_random(char *buf, int len)
92 {
93     ULONG   Seed = (ULONG) buf;
94     Seed = RtlRandom(&Seed);
95     while (len >0) {
96         if (len > sizeof(ULONG)) {
97             memcpy(buf, &Seed, sizeof(ULONG));
98             len -= sizeof(ULONG);
99             buf += sizeof(ULONG);
100         } else {
101             memcpy(buf, &Seed, len);
102             len = 0;
103             break;
104         } 
105     }
106 }
107 #define get_random_bytes(buf, len)  read_random(buf, len)
108
109 /* do NOT use function or expression as parameters ... */
110
111 #ifndef min_t
112 #define min_t(type,x,y) (type)(x) < (type)(y) ? (x): (y)
113 #endif
114
115 #ifndef max_t
116 #define max_t(type,x,y) (type)(x) < (type)(y) ? (y): (x)
117 #endif
118
119
120 #define NIPQUAD(addr)                       \
121         ((unsigned char *)&addr)[0],    \
122         ((unsigned char *)&addr)[1],    \
123         ((unsigned char *)&addr)[2],    \
124         ((unsigned char *)&addr)[3]
125
126 #define HIPQUAD(addr)                       \
127         ((unsigned char *)&addr)[3],    \
128         ((unsigned char *)&addr)[2],    \
129         ((unsigned char *)&addr)[1],    \
130         ((unsigned char *)&addr)[0]
131
132 static int copy_from_user(void *to, void *from, int c) 
133 {
134     memcpy(to, from, c);
135     return 0;
136 }
137
138 static int copy_to_user(void *to, void *from, int c) 
139 {
140     memcpy(to, from, c);
141     return 0;
142 }
143
144
145 #define put_user(x, ptr)        \
146 (                               \
147     *(ptr) = x,                 \
148     0                           \
149 )
150
151
152 #define get_user(x,ptr)         \
153 (                               \
154     x = *(ptr),                 \
155     0                           \
156 )
157
158 #define num_physpages                   (64 * 1024)
159
160 #define snprintf  _snprintf
161 #define vsnprintf _vsnprintf
162
163
164 #endif  /* !__KERNEL__ */
165
166 int cfs_error_code(NTSTATUS);
167
168 #endif