1 | /* |
2 | * Copyright (c) 2015 Grzegorz Kostka (kostka.grzegorz@gmail.com) |
3 | * Copyright (c) 2015 Kaho Ng (ngkaho1234@gmail.com) |
4 | * All rights reserved. |
5 | * |
6 | * Redistribution and use in source and binary forms, with or without |
7 | * modification, are permitted provided that the following conditions |
8 | * are met: |
9 | * |
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. |
17 | * |
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. |
28 | */ |
29 | |
30 | /** @addtogroup lwext4 |
31 | * @{ |
32 | */ |
33 | /** |
34 | * @file ext4_misc.h |
35 | * @brief Miscellaneous helpers. |
36 | */ |
37 | |
38 | #ifndef EXT4_MISC_H_ |
39 | #define EXT4_MISC_H_ |
40 | |
41 | #ifdef __cplusplus |
42 | extern "C" { |
43 | #endif |
44 | |
45 | #include <stdint.h> |
46 | |
47 | /**************************************************************/ |
48 | |
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))) |
51 | |
52 | /****************************Endian conversion*****************/ |
53 | |
54 | static inline uint64_t reorder64(uint64_t n) |
55 | { |
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); |
64 | } |
65 | |
66 | static inline uint32_t reorder32(uint32_t n) |
67 | { |
68 | return ((n & 0xff) << 24) | |
69 | ((n & 0xff00) << 8) | |
70 | ((n & 0xff0000) >> 8) | |
71 | ((n & 0xff000000) >> 24); |
72 | } |
73 | |
74 | static inline uint16_t reorder16(uint16_t n) |
75 | { |
76 | return ((n & 0xff) << 8) | |
77 | ((n & 0xff00) >> 8); |
78 | } |
79 | |
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) |
84 | |
85 | #define to_be64(_n) _n |
86 | #define to_be32(_n) _n |
87 | #define to_be16(_n) _n |
88 | |
89 | #else |
90 | #define to_le64(_n) _n |
91 | #define to_le32(_n) _n |
92 | #define to_le16(_n) _n |
93 | |
94 | #define to_be64(_n) reorder64(_n) |
95 | #define to_be32(_n) reorder32(_n) |
96 | #define to_be16(_n) reorder16(_n) |
97 | #endif |
98 | |
99 | /****************************Access macros to ext4 structures*****************/ |
100 | |
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 |
104 | |
105 | #define ext4_set32(s, f, v) \ |
106 | do { \ |
107 | (s)->f = to_le32(v); \ |
108 | } while (0) |
109 | #define ext4_set16(s, f, v) \ |
110 | do { \ |
111 | (s)->f = to_le16(v); \ |
112 | } while (0) |
113 | #define ext4_set8 \ |
114 | (s, f, v) do { (s)->f = (v); } \ |
115 | while (0) |
116 | |
117 | /****************************Access macros to jbd2 structures*****************/ |
118 | |
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 |
122 | |
123 | #define jbd_set32(s, f, v) \ |
124 | do { \ |
125 | (s)->f = to_be32(v); \ |
126 | } while (0) |
127 | #define jbd_set16(s, f, v) \ |
128 | do { \ |
129 | (s)->f = to_be16(v); \ |
130 | } while (0) |
131 | #define jbd_set8 \ |
132 | (s, f, v) do { (s)->f = (v); } \ |
133 | while (0) |
134 | |
135 | #ifdef __GNUC__ |
136 | #ifndef __unused |
137 | #define __unused __attribute__ ((__unused__)) |
138 | #endif |
139 | #else |
140 | #define __unused |
141 | #endif |
142 | |
143 | #ifndef offsetof |
144 | #define offsetof(type, field) \ |
145 | ((size_t)(&(((type *)0)->field))) |
146 | #endif |
147 | |
148 | #ifdef __cplusplus |
149 | } |
150 | #endif |
151 | |
152 | #endif /* EXT4_MISC_H_ */ |
153 | |
154 | /** |
155 | * @} |
156 | */ |
157 | |