macros.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * Copyright (c) 2009-2018 Richard Braun.
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. *
  17. * Upstream site with license notes :
  18. * http://git.sceen.net/rbraun/librbraun.git/
  19. *
  20. *
  21. * Helper macros.
  22. *
  23. * This file is a top header in the inclusion hierarchy, and shouldn't include
  24. * other headers that may cause circular dependencies.
  25. *
  26. * TODO Improve documentation.
  27. */
  28. #ifndef KERN_MACROS_H
  29. #define KERN_MACROS_H
  30. #if !defined(__GNUC__) || (__GNUC__ < 4)
  31. #error "GCC 4+ required"
  32. #endif
  33. #ifndef __ASSEMBLER__
  34. #include <stdbool.h>
  35. #include <stddef.h>
  36. #endif
  37. // Attributes for variables that are mostly read and seldom changed.
  38. #define __read_mostly __section (".data.read_mostly")
  39. #define MACRO_BEGIN ({
  40. #define MACRO_END })
  41. #define __QUOTE(x) #x
  42. #define QUOTE(x) __QUOTE(x)
  43. #define CONCAT_(x, y) x ## y
  44. #define CONCAT(x, y) CONCAT_ (x, y)
  45. #ifdef __ASSEMBLER__
  46. #define DECL_CONST(x, s) x
  47. #else
  48. #define DECL_CONST(x, s) CONCAT (x, s)
  49. #endif
  50. #define STRLEN(x) (sizeof (x) - 1)
  51. #define ARRAY_SIZE(x) (sizeof (x) / sizeof ((x)[0]))
  52. #define MIN(a, b) ((a) < (b) ? (a) : (b))
  53. #define MAX(a, b) ((a) > (b) ? (a) : (b))
  54. #define SWAP(a, b) \
  55. do \
  56. { \
  57. _Auto a_ = *(a); \
  58. *(a) = *(b); \
  59. *(b) = a_; \
  60. } \
  61. while (0)
  62. #define DIV_CEIL(n, d) (((n) + (d) - 1) / (d))
  63. #define P2ALIGNED(x, a) (((x) & ((a) - 1)) == 0)
  64. #define ISP2(x) P2ALIGNED (x, x)
  65. #define P2ALIGN(x, a) ((x) & -(a)) // decreases if not aligned
  66. #define P2ROUND(x, a) (-(-(x) & -(a))) // increases if not aligned
  67. #define P2END(x, a) (-(~(x) & -(a))) // always increases
  68. #define structof(ptr, type, member) \
  69. ((type *)((char *)(ptr) - offsetof (type, member)))
  70. #define likely(expr) __builtin_expect (!!(expr), 1)
  71. #define unlikely(expr) __builtin_expect (!!(expr), 0)
  72. #define barrier() asm volatile ("" : : : "memory")
  73. // Allocate memory from the stack.
  74. #define alloca __builtin_alloca
  75. // The following macros may be provided by the C environment.
  76. #ifndef __noinline
  77. #define __noinline __attribute__ ((noinline))
  78. #endif
  79. #ifndef __always_inline
  80. #define __always_inline inline __attribute__ ((always_inline))
  81. #endif
  82. #ifndef __section
  83. #define __section(x) __attribute__ ((section(x)))
  84. #endif
  85. #ifndef __packed
  86. #define __packed __attribute__ ((packed))
  87. #endif
  88. #ifndef __unused
  89. #define __unused __attribute__ ((unused))
  90. #endif
  91. #ifndef __used
  92. #define __used __attribute__ ((used))
  93. #endif
  94. #ifndef __weak
  95. #define __weak __attribute__ ((weak))
  96. #endif
  97. #ifndef __fallthrough
  98. #if __GNUC__ >= 7
  99. #define __fallthrough __attribute__ ((fallthrough))
  100. #else
  101. #define __fallthrough
  102. #endif
  103. #endif
  104. // Standard offsetof.
  105. #define OFFSETOF __builtin_offsetof
  106. // Type inference.
  107. #define _Auto __auto_type
  108. // Unique identifiers.
  109. #define UNIQ(prefix) CONCAT (prefix, __COUNTER__)
  110. // Cleanup functions.
  111. #define CLEANUP(fct) __attribute__ ((cleanup (fct)))
  112. #endif