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 InterMezzo, http://www.inter-mezzo.org.
8 * InterMezzo 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 * InterMezzo 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 InterMezzo; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 * Data structures unpacking/packing macros & inlines
25 #ifndef _INTERMEZZO_LIB_H
26 #define _INTERMEZZO_LIB_H
29 # include <linux/types.h>
32 # include <sys/types.h>
36 #define MIN(a,b) (((a)<(b)) ? (a): (b))
38 #define MAX(a,b) (((a)>(b)) ? (a): (b))
39 #define MKSTR(ptr) ((ptr))? (ptr) : ""
41 static inline int size_round (int val)
43 return (val + 3) & (~0x3);
46 static inline int size_round0(int val)
50 return (val + 1 + 3) & (~0x3);
53 static inline size_t round_strlen(char *fset)
55 return size_round(strlen(fset) + 1);
59 # define NTOH__u32(var) le32_to_cpu(var)
60 # define NTOH__u64(var) le64_to_cpu(var)
61 # define HTON__u32(var) cpu_to_le32(var)
62 # define HTON__u64(var) cpu_to_le64(var)
65 # define NTOH__u32(var) GUINT32_FROM_LE(var)
66 # define NTOH__u64(var) GUINT64_FROM_LE(var)
67 # define HTON__u32(var) GUINT32_TO_LE(var)
68 # define HTON__u64(var) GUINT64_TO_LE(var)
72 * copy sizeof(type) bytes from pointer to var and move ptr forward.
73 * return EFAULT if pointer goes beyond end
75 #define UNLOGV(var,type,ptr,end) \
78 ptr += sizeof(type); \
83 /* the following two macros convert to little endian */
84 /* type MUST be __u32 or __u64 */
85 #define LUNLOGV(var,type,ptr,end) \
87 var = NTOH##type(*(type *)ptr); \
88 ptr += sizeof(type); \
94 #define LOGV(var,type,ptr) \
96 *((type *)ptr) = var; \
97 ptr += sizeof(type); \
100 /* and in network order */
101 #define LLOGV(var,type,ptr) \
103 *((type *)ptr) = HTON##type(var); \
104 ptr += sizeof(type); \
109 * set var to point at (type *)ptr, move ptr forward with sizeof(type)
110 * return from function with EFAULT if ptr goes beyond end
112 #define UNLOGP(var,type,ptr,end) \
115 ptr += sizeof(type); \
120 #define LOGP(var,type,ptr) \
122 memcpy(ptr, var, sizeof(type)); \
123 ptr += sizeof(type); \
127 * set var to point at (char *)ptr, move ptr forward by size_round(len);
128 * return from function with EFAULT if ptr goes beyond end
130 #define UNLOGL(var,type,len,ptr,end) \
136 ptr += size_round(len * sizeof(type)); \
142 #define UNLOGL0(var,type,len,ptr,end) \
144 UNLOGL(var,type,len+1,ptr,end); \
145 if ( *((char *)ptr - size_round(len+1) + len) != '\0') \
149 #define LOGL(var,len,ptr) \
151 size_t __fill = size_round(len); \
152 /* Prevent data leakage. */ \
154 memset((char *)ptr, 0, __fill); \
155 memcpy((char *)ptr, (const char *)var, len); \
159 #define LOGL0(var,len,ptr) \
162 memcpy((char *)ptr, (const char *)var, len); \
163 *((char *)(ptr) + len) = 0; \
164 ptr += size_round(len + 1); \
167 #endif /* _INTERMEZZO_LIB_H */