1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2001 Cluster File Systems, Inc. <braam@clusterfs.com>
6 * This file is part of Lustre, http://www.lustre.org.
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.
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.
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.
21 * Basic library routines.
25 #ifndef _PORTALS_LIB_H
26 #define _PORTALS_LIB_H
31 # include <asm/types.h>
35 #define MIN(a,b) (((a)<(b)) ? (a): (b))
37 #define MAX(a,b) (((a)>(b)) ? (a): (b))
38 #define MKSTR(ptr) ((ptr))? (ptr) : ""
40 static inline int size_round (int val)
42 return (val + 7) & (~0x7);
45 static inline int size_round16(int val)
47 return (val + 0xf) & (~0xf);
50 static inline int size_round32(int val)
52 return (val + 0x1f) & (~0x1f);
55 static inline int size_round0(int val)
59 return (val + 1 + 7) & (~0x7);
62 static inline size_t round_strlen(char *fset)
64 return size_round(strlen(fset) + 1);
68 static inline char *strdup(const char *str)
70 int len = strlen(str) + 1;
71 char *tmp = kmalloc(len, GFP_KERNEL);
73 memcpy(tmp, str, len);
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)
85 # define expansion_u64(var) \
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; \
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))
102 * copy sizeof(type) bytes from pointer to var and move ptr forward.
103 * return EFAULT if pointer goes beyond end
105 #define UNLOGV(var,type,ptr,end) \
107 var = *(type *)ptr; \
108 ptr += sizeof(type); \
113 /* the following two macros convert to little endian */
114 /* type MUST be __u32 or __u64 */
115 #define LUNLOGV(var,type,ptr,end) \
117 var = NTOH##type(*(type *)ptr); \
118 ptr += sizeof(type); \
124 #define LOGV(var,type,ptr) \
126 *((type *)ptr) = var; \
127 ptr += sizeof(type); \
130 /* and in network order */
131 #define LLOGV(var,type,ptr) \
133 *((type *)ptr) = HTON##type(var); \
134 ptr += sizeof(type); \
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
142 #define UNLOGP(var,type,ptr,end) \
145 ptr += sizeof(type); \
150 #define LOGP(var,type,ptr) \
152 memcpy(ptr, var, sizeof(type)); \
153 ptr += sizeof(type); \
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
160 #define UNLOGL(var,type,len,ptr,end) \
163 ptr += size_round(len * sizeof(type)); \
168 #define UNLOGL0(var,type,len,ptr,end) \
170 UNLOGL(var,type,len,ptr,end); \
171 if ( *((char *)ptr - size_round(len) + len - 1) != '\0') \
175 #define LOGL(var,len,ptr) \
178 memcpy((char *)ptr, (const char *)var, len); \
179 ptr += size_round(len); \
182 #define LOGU(var,len,ptr) \
185 memcpy((char *)var, (const char *)ptr, len); \
186 ptr += size_round(len); \
189 #define LOGL0(var,len,ptr) \
193 memcpy((char *)ptr, (const char *)var, len); \
194 *((char *)(ptr) + len) = 0; \
195 ptr += size_round(len + 1); \
198 #endif /* _PORTALS_LIB_H */