macros.h 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * Copyright (c) 2009-2015 Richard Braun.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  15. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  16. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  17. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  18. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  19. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  20. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  21. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  23. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. *
  25. *
  26. * Helper macros.
  27. *
  28. * Upstream site with license notes :
  29. * http://git.sceen.net/rbraun/librbraun.git/
  30. */
  31. #ifndef _KERN_MACROS_H
  32. #define _KERN_MACROS_H
  33. #define MACRO_BEGIN ({
  34. #define MACRO_END })
  35. #define MACRO_RETURN if (1) return
  36. #define __QUOTE(x) #x
  37. #define QUOTE(x) __QUOTE(x)
  38. #ifdef __ASSEMBLER__
  39. #define DECL_CONST(x, s) x
  40. #else /* __ASSEMBLER__ */
  41. #define __DECL_CONST(x, s) x##s
  42. #define DECL_CONST(x, s) __DECL_CONST(x, s)
  43. #endif /* __ASSEMBLER__ */
  44. #define STRLEN(x) (sizeof(x) - 1)
  45. #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
  46. #define MIN(a, b) ((a) < (b) ? (a) : (b))
  47. #define MAX(a, b) ((a) > (b) ? (a) : (b))
  48. #define DIV_CEIL(n, d) (((n) + (d) - 1) / (d))
  49. #define P2ALIGNED(x, a) (((x) & ((a) - 1)) == 0)
  50. #define ISP2(x) P2ALIGNED(x, x)
  51. #define P2ALIGN(x, a) ((x) & -(a))
  52. #define P2ROUND(x, a) (-(-(x) & -(a)))
  53. #define P2END(x, a) (-(~(x) & -(a)))
  54. #define structof(ptr, type, member) \
  55. ((type *)((char *)(ptr) - offsetof(type, member)))
  56. #define access_once(x) (*(volatile typeof(x) *)&(x))
  57. #define alignof(x) __alignof__(x)
  58. #ifndef likely
  59. #define likely(expr) __builtin_expect(!!(expr), 1)
  60. #endif /* likely */
  61. #ifndef unlikely
  62. #define unlikely(expr) __builtin_expect(!!(expr), 0)
  63. #endif /* unlikely */
  64. #ifndef barrier
  65. #define barrier() asm volatile("" : : : "memory")
  66. #endif /* barrier */
  67. #define __noreturn __attribute__((noreturn))
  68. #define __aligned(x) __attribute__((aligned(x)))
  69. #define __always_inline inline __attribute__((always_inline))
  70. #ifndef __section
  71. #define __section(x) __attribute__((section(x)))
  72. #endif /* __section */
  73. #define __packed __attribute__((packed))
  74. #define __alias(x) __attribute__((alias(x)))
  75. #define __format_printf(fmt, args) \
  76. __attribute__((format(printf, fmt, args)))
  77. #endif /* _KERN_MACROS_H */