Whamcloud - gitweb
land v0.9.1 on HEAD, in preparation for a 1.0.x branch
[fs/lustre-release.git] / lnet / include / linux / portals_lib.h
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
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 _PORTALS_LIB_H
26 #define _PORTALS_LIB_H
27
28 #ifndef __KERNEL__
29 # include <string.h>
30 #else 
31 # include <asm/types.h>
32 #endif
33
34 #undef MIN
35 #define MIN(a,b) (((a)<(b)) ? (a): (b))
36 #undef MAX
37 #define MAX(a,b) (((a)>(b)) ? (a): (b))
38 #define MKSTR(ptr) ((ptr))? (ptr) : ""
39
40 static inline int size_round (int val)
41 {
42         return (val + 7) & (~0x7);
43 }
44
45 static inline int size_round16(int val)
46 {
47         return (val + 0xf) & (~0xf);
48 }
49
50 static inline int size_round32(int val)
51 {
52         return (val + 0x1f) & (~0x1f);
53 }
54
55 static inline int size_round0(int val)
56 {
57         if (!val)
58                 return 0;
59         return (val + 1 + 7) & (~0x7);
60 }
61
62 static inline size_t round_strlen(char *fset)
63 {
64         return size_round(strlen(fset) + 1);
65 }
66
67 #ifdef __KERNEL__
68 static inline char *strdup(const char *str)
69 {
70         int len = strlen(str) + 1;
71         char *tmp = kmalloc(len, GFP_KERNEL);
72         if (tmp)
73                 memcpy(tmp, str, len);
74
75         return tmp;
76 }
77 #endif
78
79 #ifdef __KERNEL__
80 # define NTOH__u32(var) le32_to_cpu(var)
81 # define NTOH__u64(var) le64_to_cpu(var)
82 # define HTON__u32(var) cpu_to_le32(var)
83 # define HTON__u64(var) cpu_to_le64(var)
84 #else
85 # define expansion_u64(var) \
86     ({  __u64 ret; \
87        switch (sizeof(var)) {   \
88        case 8: (ret) = (var); break; \
89        case 4: (ret) = (__u32)(var); break; \
90        case 2: (ret) = (__u16)(var); break; \
91        case 1: (ret) = (__u8)(var); break; \
92        };       \
93        (ret);     \
94     })
95 # define NTOH__u32(var) (var)
96 # define NTOH__u64(var) (expansion_u64(var))
97 # define HTON__u32(var) (var)
98 # define HTON__u64(var) (expansion_u64(var))
99 #endif
100
101 /* 
102  * copy sizeof(type) bytes from pointer to var and move ptr forward.
103  * return EFAULT if pointer goes beyond end
104  */
105 #define UNLOGV(var,type,ptr,end)                \
106 do {                                            \
107         var = *(type *)ptr;                     \
108         ptr += sizeof(type);                    \
109         if (ptr > end )                         \
110                 return -EFAULT;                 \
111 } while (0)
112
113 /* the following two macros convert to little endian */
114 /* type MUST be __u32 or __u64 */
115 #define LUNLOGV(var,type,ptr,end)               \
116 do {                                            \
117         var = NTOH##type(*(type *)ptr);         \
118         ptr += sizeof(type);                    \
119         if (ptr > end )                         \
120                 return -EFAULT;                 \
121 } while (0)
122
123 /* now log values */
124 #define LOGV(var,type,ptr)                      \
125 do {                                            \
126         *((type *)ptr) = var;                   \
127         ptr += sizeof(type);                    \
128 } while (0)
129
130 /* and in network order */
131 #define LLOGV(var,type,ptr)                     \
132 do {                                            \
133         *((type *)ptr) = HTON##type(var);       \
134         ptr += sizeof(type);                    \
135 } while (0)
136
137
138 /* 
139  * set var to point at (type *)ptr, move ptr forward with sizeof(type)
140  * return from function with EFAULT if ptr goes beyond end
141  */
142 #define UNLOGP(var,type,ptr,end)                \
143 do {                                            \
144         var = (type *)ptr;                      \
145         ptr += sizeof(type);                    \
146         if (ptr > end )                         \
147                 return -EFAULT;                 \
148 } while (0)
149
150 #define LOGP(var,type,ptr)                      \
151 do {                                            \
152         memcpy(ptr, var, sizeof(type));         \
153         ptr += sizeof(type);                    \
154 } while (0)
155
156 /* 
157  * set var to point at (char *)ptr, move ptr forward by size_round(len);
158  * return from function with EFAULT if ptr goes beyond end
159  */
160 #define UNLOGL(var,type,len,ptr,end)            \
161 do {                                            \
162         var = (type *)ptr;                      \
163         ptr += size_round(len * sizeof(type));  \
164         if (ptr > end )                         \
165                 return -EFAULT;                 \
166 } while (0)
167
168 #define UNLOGL0(var,type,len,ptr,end)                                   \
169 do {                                                                    \
170         UNLOGL(var,type,len,ptr,end);                                   \
171         if ( *((char *)ptr - size_round(len) + len - 1) != '\0')        \
172                 return -EFAULT;                                         \
173 } while (0)
174
175 #define LOGL(var,len,ptr)                                       \
176 do {                                                            \
177         if (var)                                                \
178                 memcpy((char *)ptr, (const char *)var, len);    \
179         ptr += size_round(len);                                 \
180 } while (0)
181
182 #define LOGU(var,len,ptr)                                       \
183 do {                                                            \
184         if (var)                                                \
185                 memcpy((char *)var, (const char *)ptr, len);    \
186         ptr += size_round(len);                                 \
187 } while (0)
188
189 #define LOGL0(var,len,ptr)                              \
190 do {                                                    \
191         if (!len)                                       \
192                 break;                                  \
193         memcpy((char *)ptr, (const char *)var, len);    \
194         *((char *)(ptr) + len) = 0;                     \
195         ptr += size_round(len + 1);                     \
196 } while (0)
197
198 #endif /* _PORTALS_LIB_H */