refcount.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * Copyright 2016-2021 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #ifndef OSSL_INTERNAL_REFCOUNT_H
  10. # define OSSL_INTERNAL_REFCOUNT_H
  11. # pragma once
  12. # include <openssl/e_os2.h>
  13. # include <openssl/trace.h>
  14. # ifndef OPENSSL_DEV_NO_ATOMICS
  15. # if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L \
  16. && !defined(__STDC_NO_ATOMICS__)
  17. # include <stdatomic.h>
  18. # define HAVE_C11_ATOMICS
  19. # endif
  20. # if defined(HAVE_C11_ATOMICS) && defined(ATOMIC_INT_LOCK_FREE) \
  21. && ATOMIC_INT_LOCK_FREE > 0
  22. # define HAVE_ATOMICS 1
  23. typedef _Atomic int CRYPTO_REF_COUNT;
  24. static inline int CRYPTO_UP_REF(_Atomic int *val, int *ret,
  25. ossl_unused void *lock)
  26. {
  27. *ret = atomic_fetch_add_explicit(val, 1, memory_order_relaxed) + 1;
  28. return 1;
  29. }
  30. /*
  31. * Changes to shared structure other than reference counter have to be
  32. * serialized. And any kind of serialization implies a release fence. This
  33. * means that by the time reference counter is decremented all other
  34. * changes are visible on all processors. Hence decrement itself can be
  35. * relaxed. In case it hits zero, object will be destructed. Since it's
  36. * last use of the object, destructor programmer might reason that access
  37. * to mutable members doesn't have to be serialized anymore, which would
  38. * otherwise imply an acquire fence. Hence conditional acquire fence...
  39. */
  40. static inline int CRYPTO_DOWN_REF(_Atomic int *val, int *ret,
  41. ossl_unused void *lock)
  42. {
  43. *ret = atomic_fetch_sub_explicit(val, 1, memory_order_relaxed) - 1;
  44. if (*ret == 0)
  45. atomic_thread_fence(memory_order_acquire);
  46. return 1;
  47. }
  48. # elif defined(__GNUC__) && defined(__ATOMIC_RELAXED) && __GCC_ATOMIC_INT_LOCK_FREE > 0
  49. # define HAVE_ATOMICS 1
  50. typedef int CRYPTO_REF_COUNT;
  51. static __inline__ int CRYPTO_UP_REF(int *val, int *ret, ossl_unused void *lock)
  52. {
  53. *ret = __atomic_fetch_add(val, 1, __ATOMIC_RELAXED) + 1;
  54. return 1;
  55. }
  56. static __inline__ int CRYPTO_DOWN_REF(int *val, int *ret,
  57. ossl_unused 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(__ICL) && defined(_WIN32)
  65. # define HAVE_ATOMICS 1
  66. typedef volatile int CRYPTO_REF_COUNT;
  67. static __inline int CRYPTO_UP_REF(volatile int *val, int *ret,
  68. ossl_unused void *lock)
  69. {
  70. *ret = _InterlockedExchangeAdd((void *)val, 1) + 1;
  71. return 1;
  72. }
  73. static __inline int CRYPTO_DOWN_REF(volatile int *val, int *ret,
  74. ossl_unused void *lock)
  75. {
  76. *ret = _InterlockedExchangeAdd((void *)val, -1) - 1;
  77. return 1;
  78. }
  79. # elif defined(_MSC_VER) && _MSC_VER>=1200
  80. # define HAVE_ATOMICS 1
  81. typedef volatile int CRYPTO_REF_COUNT;
  82. # if (defined(_M_ARM) && _M_ARM>=7 && !defined(_WIN32_WCE)) || defined(_M_ARM64)
  83. # include <intrin.h>
  84. # if defined(_M_ARM64) && !defined(_ARM_BARRIER_ISH)
  85. # define _ARM_BARRIER_ISH _ARM64_BARRIER_ISH
  86. # endif
  87. static __inline int CRYPTO_UP_REF(volatile int *val, int *ret,
  88. ossl_unused void *lock)
  89. {
  90. *ret = _InterlockedExchangeAdd_nf(val, 1) + 1;
  91. return 1;
  92. }
  93. static __inline int CRYPTO_DOWN_REF(volatile int *val, int *ret,
  94. ossl_unused void *lock)
  95. {
  96. *ret = _InterlockedExchangeAdd_nf(val, -1) - 1;
  97. if (*ret == 0)
  98. __dmb(_ARM_BARRIER_ISH);
  99. return 1;
  100. }
  101. # else
  102. # if !defined(_WIN32_WCE)
  103. # pragma intrinsic(_InterlockedExchangeAdd)
  104. # else
  105. # if _WIN32_WCE >= 0x600
  106. extern long __cdecl _InterlockedExchangeAdd(long volatile*, long);
  107. # else
  108. /* under Windows CE we still have old-style Interlocked* functions */
  109. extern long __cdecl InterlockedExchangeAdd(long volatile*, long);
  110. # define _InterlockedExchangeAdd InterlockedExchangeAdd
  111. # endif
  112. # endif
  113. static __inline int CRYPTO_UP_REF(volatile int *val, int *ret,
  114. ossl_unused void *lock)
  115. {
  116. *ret = _InterlockedExchangeAdd(val, 1) + 1;
  117. return 1;
  118. }
  119. static __inline int CRYPTO_DOWN_REF(volatile int *val, int *ret,
  120. ossl_unused void *lock)
  121. {
  122. *ret = _InterlockedExchangeAdd(val, -1) - 1;
  123. return 1;
  124. }
  125. # endif
  126. # endif
  127. # endif /* !OPENSSL_DEV_NO_ATOMICS */
  128. /*
  129. * All the refcounting implementations above define HAVE_ATOMICS, so if it's
  130. * still undefined here (such as when OPENSSL_DEV_NO_ATOMICS is defined), it
  131. * means we need to implement a fallback. This fallback uses locks.
  132. */
  133. # ifndef HAVE_ATOMICS
  134. typedef int CRYPTO_REF_COUNT;
  135. # define CRYPTO_UP_REF(val, ret, lock) CRYPTO_atomic_add(val, 1, ret, lock)
  136. # define CRYPTO_DOWN_REF(val, ret, lock) CRYPTO_atomic_add(val, -1, ret, lock)
  137. # endif
  138. # if !defined(NDEBUG) && !defined(OPENSSL_NO_STDIO)
  139. # define REF_ASSERT_ISNT(test) \
  140. (void)((test) ? (OPENSSL_die("refcount error", __FILE__, __LINE__), 1) : 0)
  141. # else
  142. # define REF_ASSERT_ISNT(i)
  143. # endif
  144. # define REF_PRINT_EX(text, count, object) \
  145. OSSL_TRACE3(REF_COUNT, "%p:%4d:%s\n", (object), (count), (text));
  146. # define REF_PRINT_COUNT(text, object) \
  147. REF_PRINT_EX(text, object->references, (void *)object)
  148. #endif