ext4_misc.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. /** @addtogroup lwext4
  30. * @{
  31. */
  32. /**
  33. * @file ext4_misc.h
  34. * @brief Miscellaneous helpers.
  35. */
  36. #ifndef EXT4_MISC_H_
  37. #define EXT4_MISC_H_
  38. #ifdef __cplusplus
  39. extern "C" {
  40. #endif
  41. #include <stdint.h>
  42. /**************************************************************/
  43. #define EXT4_DIV_ROUND_UP(x, y) (((x) + (y) - 1)/(y))
  44. #define EXT4_ALIGN(x, y) ((y) * EXT4_DIV_ROUND_UP((x), (y)))
  45. /****************************Endian conversion*****************/
  46. static inline uint64_t reorder64(uint64_t n)
  47. {
  48. return ((n & 0xff) << 56) |
  49. ((n & 0xff00) << 40) |
  50. ((n & 0xff0000) << 24) |
  51. ((n & 0xff000000LL) << 8) |
  52. ((n & 0xff00000000LL) >> 8) |
  53. ((n & 0xff0000000000LL) >> 24) |
  54. ((n & 0xff000000000000LL) >> 40) |
  55. ((n & 0xff00000000000000LL) >> 56);
  56. }
  57. static inline uint32_t reorder32(uint32_t n)
  58. {
  59. return ((n & 0xff) << 24) |
  60. ((n & 0xff00) << 8) |
  61. ((n & 0xff0000) >> 8) |
  62. ((n & 0xff000000) >> 24);
  63. }
  64. static inline uint16_t reorder16(uint16_t n)
  65. {
  66. return ((n & 0xff) << 8) |
  67. ((n & 0xff00) >> 8);
  68. }
  69. #ifdef CONFIG_BIG_ENDIAN
  70. #define to_le64(_n) reorder64(_n)
  71. #define to_le32(_n) reorder32(_n)
  72. #define to_le16(_n) reorder16(_n)
  73. #define to_be64(_n) _n
  74. #define to_be32(_n) _n
  75. #define to_be16(_n) _n
  76. #else
  77. #define to_le64(_n) _n
  78. #define to_le32(_n) _n
  79. #define to_le16(_n) _n
  80. #define to_be64(_n) reorder64(_n)
  81. #define to_be32(_n) reorder32(_n)
  82. #define to_be16(_n) reorder16(_n)
  83. #endif
  84. /****************************Access macros to ext4 structures*****************/
  85. #define ext4_get32(s, f) to_le32((s)->f)
  86. #define ext4_get16(s, f) to_le16((s)->f)
  87. #define ext4_get8(s, f) (s)->f
  88. #define ext4_set32(s, f, v) \
  89. do { \
  90. (s)->f = to_le32(v); \
  91. } while (0)
  92. #define ext4_set16(s, f, v) \
  93. do { \
  94. (s)->f = to_le16(v); \
  95. } while (0)
  96. #define ext4_set8 \
  97. (s, f, v) do { (s)->f = (v); } \
  98. while (0)
  99. /****************************Access macros to jbd2 structures*****************/
  100. #define jbd_get32(s, f) to_be32((s)->f)
  101. #define jbd_get16(s, f) to_be16((s)->f)
  102. #define jbd_get8(s, f) (s)->f
  103. #define jbd_set32(s, f, v) \
  104. do { \
  105. (s)->f = to_be32(v); \
  106. } while (0)
  107. #define jbd_set16(s, f, v) \
  108. do { \
  109. (s)->f = to_be16(v); \
  110. } while (0)
  111. #define jbd_set8 \
  112. (s, f, v) do { (s)->f = (v); } \
  113. while (0)
  114. #ifdef __GNUC__
  115. #ifndef __ext4_unused // "__unused" conflicts with riscv`s include/bits/signal.h
  116. #define __ext4_unused __attribute__ ((__unused__))
  117. #endif
  118. #else
  119. #define __ext4_unused
  120. #endif
  121. #ifndef offsetof
  122. #define offsetof(type, field) \
  123. ((size_t)(&(((type *)0)->field)))
  124. #endif
  125. #ifdef __cplusplus
  126. }
  127. #endif
  128. #endif /* EXT4_MISC_H_ */
  129. /**
  130. * @}
  131. */