Mirror of strace – the linux syscall tracer
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

macros.h 1.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. * Copyright (c) 2001-2019 The strace developers.
  3. * All rights reserved.
  4. *
  5. * SPDX-License-Identifier: LGPL-2.1-or-later
  6. */
  7. #ifndef STRACE_MACROS_H
  8. # define STRACE_MACROS_H
  9. # include <stdbool.h>
  10. # include <sys/types.h>
  11. # include "gcc_compat.h"
  12. # define ARRAY_SIZE(a_) (sizeof(a_) / sizeof((a_)[0]) + MUST_BE_ARRAY(a_))
  13. # define ARRSZ_PAIR(a_) a_, ARRAY_SIZE(a_)
  14. # define STRINGIFY(...) #__VA_ARGS__
  15. # define STRINGIFY_VAL(...) STRINGIFY(__VA_ARGS__)
  16. # ifndef MAX
  17. # define MAX(a, b) (((a) > (b)) ? (a) : (b))
  18. # endif
  19. # ifndef MIN
  20. # define MIN(a, b) (((a) < (b)) ? (a) : (b))
  21. # endif
  22. # define CLAMP(val, min, max) MIN(MAX(min, val), max)
  23. # ifndef ROUNDUP_DIV
  24. # define ROUNDUP_DIV(val_, div_) (((val_) + (div_) - 1) / (div_))
  25. # endif
  26. # ifndef ROUNDUP
  27. # define ROUNDUP(val_, div_) (ROUNDUP_DIV((val_), (div_)) * (div_))
  28. # endif
  29. # ifndef offsetofend
  30. # define offsetofend(type_, member_) \
  31. (offsetof(type_, member_) + sizeof(((type_ *)0)->member_))
  32. # endif
  33. # ifndef cast_ptr
  34. # define cast_ptr(type_, var_) \
  35. ((type_) (uintptr_t) (const volatile void *) (var_))
  36. # endif
  37. # ifndef containerof
  38. /**
  39. * Return a pointer to a structure that contains the provided variable.
  40. *
  41. * @param ptr_ Pointer to data that is a field of the container structure.
  42. * @param struct_ Type of the container structure.
  43. * @param member_ Name of the member field.
  44. * @return Pointer to the container structure.
  45. */
  46. # define containerof(ptr_, struct_, member_) \
  47. cast_ptr(struct_ *, \
  48. (const volatile char *) (ptr_) - offsetof(struct_, member_))
  49. # endif
  50. static inline bool
  51. is_filled(const char *ptr, char fill, size_t size)
  52. {
  53. while (size--)
  54. if (*ptr++ != fill)
  55. return false;
  56. return true;
  57. }
  58. # define IS_ARRAY_ZERO(arr_) \
  59. is_filled((const char *) (arr_), 0, sizeof(arr_) + MUST_BE_ARRAY(arr_))
  60. # ifndef BIT
  61. # define BIT(x_) (1U << (x_))
  62. # endif
  63. # define FLAG(name_) name_ = BIT(name_##_BIT)
  64. #endif /* !STRACE_MACROS_H */