Whamcloud - gitweb
A solid part of the MDS request processing infrastructure.
[fs/lustre-release.git] / lustre / include / linux / lustre_lib.h
1 #ifndef _LUSTRE_LIB_H
2 #define _LUSTRE_LIB_H
3
4 #include <asm/types.h>
5
6 #ifndef __KERNEL__
7 # include <string.h>
8 #endif
9
10 #undef MIN
11 #define MIN(a,b) (((a)<(b)) ? (a): (b))
12 #undef MAX
13 #define MAX(a,b) (((a)>(b)) ? (a): (b))
14 #define MKSTR(ptr) ((ptr))? (ptr) : ""
15
16 static inline int size_round (int val)
17 {
18         return (val + 3) & (~0x3);
19 }
20
21 static inline size_t round_strlen(char *fset)
22 {
23         return size_round(strlen(fset) + 1);
24 }
25
26 #ifdef __KERNEL__
27 static inline char *strdup(char *str)
28 {
29         char *tmp = kmalloc(strlen(str) + 1, GFP_KERNEL);
30         if (tmp)
31                 memcpy(tmp, str, strlen(str) + 1);
32                 
33         return NULL;
34 }
35 #endif
36
37 #ifdef __KERNEL__
38 # define NTOH__u32(var) le32_to_cpu(var)
39 # define NTOH__u64(var) le64_to_cpu(var)
40 # define HTON__u32(var) cpu_to_le32(var)
41 # define HTON__u64(var) cpu_to_le64(var)
42 #else
43 # include <glib.h>
44 # define NTOH__u32(var) GUINT32_FROM_LE(var)
45 # define NTOH__u64(var) GUINT64_FROM_LE(var)
46 # define HTON__u32(var) GUINT32_TO_LE(var)
47 # define HTON__u64(var) GUINT64_TO_LE(var)
48 #endif
49
50 /* 
51  * copy sizeof(type) bytes from pointer to var and move ptr forward.
52  * return EFAULT if pointer goes beyond end
53  */
54 #define UNLOGV(var,type,ptr,end)                \
55 do {                                            \
56         var = *(type *)ptr;                     \
57         ptr += sizeof(type);                    \
58         if (ptr > end )                         \
59                 return -EFAULT;                 \
60 } while (0)
61
62 /* the following two macros convert to little endian */
63 /* type MUST be __u32 or __u64 */
64 #define LUNLOGV(var,type,ptr,end)               \
65 do {                                            \
66         var = NTOH##type(*(type *)ptr);         \
67         ptr += sizeof(type);                    \
68         if (ptr > end )                         \
69                 return -EFAULT;                 \
70 } while (0)
71
72 /* now log values */
73 #define LOGV(var,type,ptr)                      \
74 do {                                            \
75         *((type *)ptr) = var;                   \
76         ptr += sizeof(type);                    \
77 } while (0)
78
79 /* and in network order */
80 #define LLOGV(var,type,ptr)                     \
81 do {                                            \
82         *((type *)ptr) = HTON##type(var);       \
83         ptr += sizeof(type);                    \
84 } while (0)
85
86
87 /* 
88  * set var to point at (type *)ptr, move ptr forward with sizeof(type)
89  * return from function with EFAULT if ptr goes beyond end
90  */
91 #define UNLOGP(var,type,ptr,end)                \
92 do {                                            \
93         var = (type *)ptr;                      \
94         ptr += sizeof(type);                    \
95         if (ptr > end )                         \
96                 return -EFAULT;                 \
97 } while (0)
98
99 #define LOGP(var,type,ptr)                      \
100 do {                                            \
101         memcpy(ptr, var, sizeof(type));         \
102         ptr += sizeof(type);                    \
103 } while (0)
104
105 /* 
106  * set var to point at (char *)ptr, move ptr forward by size_round(len);
107  * return from function with EFAULT if ptr goes beyond end
108  */
109 #define UNLOGL(var,type,len,ptr,end)            \
110 do {                                            \
111         var = (type *)ptr;                      \
112         ptr += size_round(len * sizeof(type));  \
113         if (ptr > end )                         \
114                 return -EFAULT;                 \
115 } while (0)
116
117
118 #define LOGL(var,len,ptr)                               \
119 do {                                                    \
120         memcpy((char *)ptr, (const char *)var, len);    \
121         ptr += size_round(len);                         \
122 } while (0)
123
124 #endif /* _LUSTRE_LIB_H */