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

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