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.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. * Copyright (c) 2001-2018 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) ((type) (uintptr_t) (const volatile void *) (var))
  35. #endif
  36. #ifndef containerof
  37. /**
  38. * Return a pointer to a structure that contains the provided variable.
  39. *
  40. * @param x Pointer to data that is a field of the container structure.
  41. * @param s Type of the container structure.
  42. * @param m Name of the member field.
  43. * @return Pointer to the container structure.
  44. */
  45. # define containerof(x, s, m) \
  46. cast_ptr(s *, (const volatile char *) (x) - offsetof(s, m))
  47. #endif
  48. static inline bool
  49. is_filled(const char *ptr, char fill, size_t size)
  50. {
  51. while (size--)
  52. if (*ptr++ != fill)
  53. return false;
  54. return true;
  55. }
  56. # define IS_ARRAY_ZERO(arr_) \
  57. is_filled((const char *) (arr_), 0, sizeof(arr_) + MUST_BE_ARRAY(arr_))
  58. #endif /* !STRACE_MACROS_H */