2 * Copyright (c) 2015 Grzegorz Kostka (kostka.grzegorz@gmail.com)
3 * Copyright (c) 2015 Kaho Ng (ngkaho1234@gmail.com)
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * - The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 /** @addtogroup lwext4
35 * @brief Miscellaneous helpers.
47 /**************************************************************/
49 #define EXT4_DIV_ROUND_UP(x, y) (((x) + (y) - 1)/(y))
50 #define EXT4_ALIGN(x, y) ((y) * EXT4_DIV_ROUND_UP((x), (y)))
52 /****************************Endian conversion*****************/
54 static inline uint64_t reorder64(uint64_t n)
56 return ((n & 0xff) << 56) |
57 ((n & 0xff00) << 40) |
58 ((n & 0xff0000) << 24) |
59 ((n & 0xff000000LL) << 8) |
60 ((n & 0xff00000000LL) >> 8) |
61 ((n & 0xff0000000000LL) >> 24) |
62 ((n & 0xff000000000000LL) >> 40) |
63 ((n & 0xff00000000000000LL) >> 56);
66 static inline uint32_t reorder32(uint32_t n)
68 return ((n & 0xff) << 24) |
70 ((n & 0xff0000) >> 8) |
71 ((n & 0xff000000) >> 24);
74 static inline uint16_t reorder16(uint16_t n)
76 return ((n & 0xff) << 8) |
80 #ifdef CONFIG_BIG_ENDIAN
81 #define to_le64(_n) reorder64(_n)
82 #define to_le32(_n) reorder32(_n)
83 #define to_le16(_n) reorder16(_n)
85 #define to_be64(_n) _n
86 #define to_be32(_n) _n
87 #define to_be16(_n) _n
90 #define to_le64(_n) _n
91 #define to_le32(_n) _n
92 #define to_le16(_n) _n
94 #define to_be64(_n) reorder64(_n)
95 #define to_be32(_n) reorder32(_n)
96 #define to_be16(_n) reorder16(_n)
99 /****************************Access macros to ext4 structures*****************/
101 #define ext4_get32(s, f) to_le32((s)->f)
102 #define ext4_get16(s, f) to_le16((s)->f)
103 #define ext4_get8(s, f) (s)->f
105 #define ext4_set32(s, f, v) \
107 (s)->f = to_le32(v); \
109 #define ext4_set16(s, f, v) \
111 (s)->f = to_le16(v); \
114 (s, f, v) do { (s)->f = (v); } \
117 /****************************Access macros to jbd2 structures*****************/
119 #define jbd_get32(s, f) to_be32((s)->f)
120 #define jbd_get16(s, f) to_be16((s)->f)
121 #define jbd_get8(s, f) (s)->f
123 #define jbd_set32(s, f, v) \
125 (s)->f = to_be32(v); \
127 #define jbd_set16(s, f, v) \
129 (s)->f = to_be16(v); \
132 (s, f, v) do { (s)->f = (v); } \
137 #define __unused __attribute__ ((__unused__))
144 #define offsetof(type, field) \
145 ((size_t)(&(((type *)0)->field)))
152 #endif /* EXT4_MISC_H_ */