refcount.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #include <contrib/libs/openssl/redef.h>
  2. /*
  3. * Copyright 2016-2019 The OpenSSL Project Authors. All Rights Reserved.
  4. *
  5. * Licensed under the OpenSSL license (the "License"). You may not use
  6. * this file except in compliance with the License. You can obtain a copy
  7. * in the file LICENSE in the source distribution or at
  8. * https://www.openssl.org/source/license.html
  9. */
  10. #ifndef OSSL_INTERNAL_REFCOUNT_H
  11. # define OSSL_INTERNAL_REFCOUNT_H
  12. /* Used to checking reference counts, most while doing perl5 stuff :-) */
  13. # if defined(OPENSSL_NO_STDIO)
  14. # if defined(REF_PRINT)
  15. # error "REF_PRINT requires stdio"
  16. # endif
  17. # endif
  18. # if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L \
  19. && !defined(__STDC_NO_ATOMICS__)
  20. # include <stdatomic.h>
  21. # define HAVE_C11_ATOMICS
  22. # endif
  23. # if defined(HAVE_C11_ATOMICS) && defined(ATOMIC_INT_LOCK_FREE) \
  24. && ATOMIC_INT_LOCK_FREE > 0
  25. # define HAVE_ATOMICS 1
  26. typedef _Atomic int CRYPTO_REF_COUNT;
  27. static inline int CRYPTO_UP_REF(_Atomic int *val, int *ret, void *lock)
  28. {
  29. *ret = atomic_fetch_add_explicit(val, 1, memory_order_relaxed) + 1;
  30. return 1;
  31. }
  32. /*
  33. * Changes to shared structure other than reference counter have to be
  34. * serialized. And any kind of serialization implies a release fence. This
  35. * means that by the time reference counter is decremented all other
  36. * changes are visible on all processors. Hence decrement itself can be
  37. * relaxed. In case it hits zero, object will be destructed. Since it's
  38. * last use of the object, destructor programmer might reason that access
  39. * to mutable members doesn't have to be serialized anymore, which would
  40. * otherwise imply an acquire fence. Hence conditional acquire fence...
  41. */
  42. static inline int CRYPTO_DOWN_REF(_Atomic int *val, int *ret, void *lock)
  43. {
  44. *ret = atomic_fetch_sub_explicit(val, 1, memory_order_relaxed) - 1;
  45. if (*ret == 0)
  46. atomic_thread_fence(memory_order_acquire);
  47. return 1;
  48. }
  49. # elif defined(__GNUC__) && defined(__ATOMIC_RELAXED) && __GCC_ATOMIC_INT_LOCK_FREE > 0
  50. # define HAVE_ATOMICS 1
  51. typedef int CRYPTO_REF_COUNT;
  52. static __inline__ int CRYPTO_UP_REF(int *val, int *ret, void * /*lock*/)
  53. {
  54. *ret = __atomic_fetch_add(val, 1, __ATOMIC_RELAXED) + 1;
  55. return 1;
  56. }
  57. static __inline__ int CRYPTO_DOWN_REF(int *val, int *ret, void * /*lock*/)
  58. {
  59. *ret = __atomic_fetch_sub(val, 1, __ATOMIC_RELAXED) - 1;
  60. if (*ret == 0)
  61. __atomic_thread_fence(__ATOMIC_ACQUIRE);
  62. return 1;
  63. }
  64. # elif defined(_MSC_VER) && _MSC_VER>=1200
  65. # define HAVE_ATOMICS 1
  66. typedef volatile int CRYPTO_REF_COUNT;
  67. # if (defined(_M_ARM) && _M_ARM>=7 && !defined(_WIN32_WCE)) || defined(_M_ARM64)
  68. # include <intrin.h>
  69. # if defined(_M_ARM64) && !defined(_ARM_BARRIER_ISH)
  70. # define _ARM_BARRIER_ISH _ARM64_BARRIER_ISH
  71. # endif
  72. static __inline int CRYPTO_UP_REF(volatile int *val, int *ret, void *lock)
  73. {
  74. *ret = _InterlockedExchangeAdd_nf(val, 1) + 1;
  75. return 1;
  76. }
  77. static __inline int CRYPTO_DOWN_REF(volatile int *val, int *ret, void *lock)
  78. {
  79. *ret = _InterlockedExchangeAdd_nf(val, -1) - 1;
  80. if (*ret == 0)
  81. __dmb(_ARM_BARRIER_ISH);
  82. return 1;
  83. }
  84. # else
  85. # if !defined(_WIN32_WCE)
  86. # pragma intrinsic(_InterlockedExchangeAdd)
  87. # else
  88. # if _WIN32_WCE >= 0x600
  89. extern long __cdecl _InterlockedExchangeAdd(long volatile*, long);
  90. # else
  91. /* under Windows CE we still have old-style Interlocked* functions */
  92. extern long __cdecl InterlockedExchangeAdd(long volatile*, long);
  93. # define _InterlockedExchangeAdd InterlockedExchangeAdd
  94. # endif
  95. # endif
  96. static __inline int CRYPTO_UP_REF(volatile int *val, int *ret, void *lock)
  97. {
  98. *ret = _InterlockedExchangeAdd(val, 1) + 1;
  99. return 1;
  100. }
  101. static __inline int CRYPTO_DOWN_REF(volatile int *val, int *ret, void *lock)
  102. {
  103. *ret = _InterlockedExchangeAdd(val, -1) - 1;
  104. return 1;
  105. }
  106. # endif
  107. # else
  108. typedef int CRYPTO_REF_COUNT;
  109. # define CRYPTO_UP_REF(val, ret, lock) CRYPTO_atomic_add(val, 1, ret, lock)
  110. # define CRYPTO_DOWN_REF(val, ret, lock) CRYPTO_atomic_add(val, -1, ret, lock)
  111. # endif
  112. # if !defined(NDEBUG) && !defined(OPENSSL_NO_STDIO)
  113. # define REF_ASSERT_ISNT(test) \
  114. (void)((test) ? (OPENSSL_die("refcount error", __FILE__, __LINE__), 1) : 0)
  115. # else
  116. # define REF_ASSERT_ISNT(i)
  117. # endif
  118. # ifdef REF_PRINT
  119. # define REF_PRINT_COUNT(a, b) \
  120. fprintf(stderr, "%p:%4d:%s\n", b, b->references, a)
  121. # else
  122. # define REF_PRINT_COUNT(a, b)
  123. # endif
  124. #endif