ngtcp2_macro.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. * ngtcp2
  3. *
  4. * Copyright (c) 2017 ngtcp2 contributors
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining
  7. * a copy of this software and associated documentation files (the
  8. * "Software"), to deal in the Software without restriction, including
  9. * without limitation the rights to use, copy, modify, merge, publish,
  10. * distribute, sublicense, and/or sell copies of the Software, and to
  11. * permit persons to whom the Software is furnished to do so, subject to
  12. * the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be
  15. * included in all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  20. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  21. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  22. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  23. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. */
  25. #ifndef NGTCP2_MACRO_H
  26. #define NGTCP2_MACRO_H
  27. #ifdef HAVE_CONFIG_H
  28. # include <config.h>
  29. #endif /* defined(HAVE_CONFIG_H) */
  30. #include <stddef.h>
  31. #include <ngtcp2/ngtcp2.h>
  32. #define ngtcp2_struct_of(ptr, type, member) \
  33. ((type *)(void *)((char *)(ptr) - offsetof(type, member)))
  34. /* ngtcp2_list_insert inserts |T| before |*PD|. The contract is that
  35. this is singly linked list, and the next element is pointed by next
  36. field of the previous element. |PD| must be a pointer to the
  37. pointer to the next field of the previous element of |*PD|: if C is
  38. the previous element of |PD|, PD = &C->next. */
  39. #define ngtcp2_list_insert(T, PD) \
  40. do { \
  41. (T)->next = *(PD); \
  42. *(PD) = (T); \
  43. } while (0)
  44. /*
  45. * ngtcp2_arraylen returns the number of elements in array |A|.
  46. */
  47. #define ngtcp2_arraylen(A) (sizeof(A) / sizeof(A[0]))
  48. #define ngtcp2_max_def(SUFFIX, T) \
  49. static inline T ngtcp2_max_##SUFFIX(T a, T b) { return a < b ? b : a; }
  50. ngtcp2_max_def(int8, int8_t)
  51. ngtcp2_max_def(int16, int16_t)
  52. ngtcp2_max_def(int32, int32_t)
  53. ngtcp2_max_def(int64, int64_t)
  54. ngtcp2_max_def(uint8, uint8_t)
  55. ngtcp2_max_def(uint16, uint16_t)
  56. ngtcp2_max_def(uint32, uint32_t)
  57. ngtcp2_max_def(uint64, uint64_t)
  58. ngtcp2_max_def(size, size_t)
  59. #define ngtcp2_min_def(SUFFIX, T) \
  60. static inline T ngtcp2_min_##SUFFIX(T a, T b) { return a < b ? a : b; }
  61. ngtcp2_min_def(int8, int8_t)
  62. ngtcp2_min_def(int16, int16_t)
  63. ngtcp2_min_def(int32, int32_t)
  64. ngtcp2_min_def(int64, int64_t)
  65. ngtcp2_min_def(uint8, uint8_t)
  66. ngtcp2_min_def(uint16, uint16_t)
  67. ngtcp2_min_def(uint32, uint32_t)
  68. ngtcp2_min_def(uint64, uint64_t)
  69. ngtcp2_min_def(size, size_t)
  70. #endif /* !defined(NGTCP2_MACRO_H) */