Kurt Roeckx | 2f545ae | 2016-08-27 16:01:08 +0200 | [diff] [blame] | 1 | /* |
Matt Caswell | a28d06f | 2021-02-18 14:57:13 +0000 | [diff] [blame] | 2 | * Copyright 2016-2021 The OpenSSL Project Authors. All Rights Reserved. |
Kurt Roeckx | 2f545ae | 2016-08-27 16:01:08 +0200 | [diff] [blame] | 3 | * |
Richard Levitte | 48f4ad7 | 2018-12-06 13:12:35 +0100 | [diff] [blame] | 4 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
Kurt Roeckx | 2f545ae | 2016-08-27 16:01:08 +0200 | [diff] [blame] | 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 | */ |
Dr. Matthias St. Pierre | ae4186b | 2019-09-28 00:45:57 +0200 | [diff] [blame] | 9 | #ifndef OSSL_INTERNAL_REFCOUNT_H |
| 10 | # define OSSL_INTERNAL_REFCOUNT_H |
FdaSilvaYY | 3a111aa | 2021-02-06 22:14:03 +0100 | [diff] [blame] | 11 | # pragma once |
Kurt Roeckx | 2f545ae | 2016-08-27 16:01:08 +0200 | [diff] [blame] | 12 | |
Pauli | a7e6a3d | 2020-12-01 11:30:10 +1000 | [diff] [blame] | 13 | # include <openssl/e_os2.h> |
Rich Salz | a935791 | 2021-05-19 11:09:49 -0400 | [diff] [blame] | 14 | # include <openssl/trace.h> |
Pauli | cd420b0 | 2017-08-22 07:17:35 +1000 | [diff] [blame] | 15 | |
Richard Levitte | 503d474 | 2019-03-14 09:59:00 +0100 | [diff] [blame] | 16 | # ifndef OPENSSL_DEV_NO_ATOMICS |
| 17 | # if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L \ |
| 18 | && !defined(__STDC_NO_ATOMICS__) |
| 19 | # include <stdatomic.h> |
| 20 | # define HAVE_C11_ATOMICS |
| 21 | # endif |
Kurt Roeckx | 2f545ae | 2016-08-27 16:01:08 +0200 | [diff] [blame] | 22 | |
Richard Levitte | 503d474 | 2019-03-14 09:59:00 +0100 | [diff] [blame] | 23 | # if defined(HAVE_C11_ATOMICS) && defined(ATOMIC_INT_LOCK_FREE) \ |
| 24 | && ATOMIC_INT_LOCK_FREE > 0 |
Kurt Roeckx | 2f545ae | 2016-08-27 16:01:08 +0200 | [diff] [blame] | 25 | |
Richard Levitte | 503d474 | 2019-03-14 09:59:00 +0100 | [diff] [blame] | 26 | # define HAVE_ATOMICS 1 |
Kurt Roeckx | 2f545ae | 2016-08-27 16:01:08 +0200 | [diff] [blame] | 27 | |
| 28 | typedef _Atomic int CRYPTO_REF_COUNT; |
| 29 | |
Pauli | a7e6a3d | 2020-12-01 11:30:10 +1000 | [diff] [blame] | 30 | static inline int CRYPTO_UP_REF(_Atomic int *val, int *ret, |
| 31 | ossl_unused void *lock) |
Kurt Roeckx | 2f545ae | 2016-08-27 16:01:08 +0200 | [diff] [blame] | 32 | { |
| 33 | *ret = atomic_fetch_add_explicit(val, 1, memory_order_relaxed) + 1; |
| 34 | return 1; |
| 35 | } |
| 36 | |
Andy Polyakov | 96d7852 | 2018-08-08 11:10:11 +0200 | [diff] [blame] | 37 | /* |
| 38 | * Changes to shared structure other than reference counter have to be |
| 39 | * serialized. And any kind of serialization implies a release fence. This |
| 40 | * means that by the time reference counter is decremented all other |
| 41 | * changes are visible on all processors. Hence decrement itself can be |
| 42 | * relaxed. In case it hits zero, object will be destructed. Since it's |
| 43 | * last use of the object, destructor programmer might reason that access |
| 44 | * to mutable members doesn't have to be serialized anymore, which would |
| 45 | * otherwise imply an acquire fence. Hence conditional acquire fence... |
| 46 | */ |
Pauli | a7e6a3d | 2020-12-01 11:30:10 +1000 | [diff] [blame] | 47 | static inline int CRYPTO_DOWN_REF(_Atomic int *val, int *ret, |
| 48 | ossl_unused void *lock) |
Kurt Roeckx | 2f545ae | 2016-08-27 16:01:08 +0200 | [diff] [blame] | 49 | { |
Andy Polyakov | 96d7852 | 2018-08-08 11:10:11 +0200 | [diff] [blame] | 50 | *ret = atomic_fetch_sub_explicit(val, 1, memory_order_relaxed) - 1; |
Kurt Roeckx | 2f545ae | 2016-08-27 16:01:08 +0200 | [diff] [blame] | 51 | if (*ret == 0) |
| 52 | atomic_thread_fence(memory_order_acquire); |
| 53 | return 1; |
| 54 | } |
| 55 | |
Richard Levitte | 503d474 | 2019-03-14 09:59:00 +0100 | [diff] [blame] | 56 | # elif defined(__GNUC__) && defined(__ATOMIC_RELAXED) && __GCC_ATOMIC_INT_LOCK_FREE > 0 |
Kurt Roeckx | 2f545ae | 2016-08-27 16:01:08 +0200 | [diff] [blame] | 57 | |
Richard Levitte | 503d474 | 2019-03-14 09:59:00 +0100 | [diff] [blame] | 58 | # define HAVE_ATOMICS 1 |
Kurt Roeckx | 2f545ae | 2016-08-27 16:01:08 +0200 | [diff] [blame] | 59 | |
| 60 | typedef int CRYPTO_REF_COUNT; |
| 61 | |
Pauli | a7e6a3d | 2020-12-01 11:30:10 +1000 | [diff] [blame] | 62 | static __inline__ int CRYPTO_UP_REF(int *val, int *ret, ossl_unused void *lock) |
Kurt Roeckx | 2f545ae | 2016-08-27 16:01:08 +0200 | [diff] [blame] | 63 | { |
| 64 | *ret = __atomic_fetch_add(val, 1, __ATOMIC_RELAXED) + 1; |
| 65 | return 1; |
| 66 | } |
| 67 | |
Pauli | a7e6a3d | 2020-12-01 11:30:10 +1000 | [diff] [blame] | 68 | static __inline__ int CRYPTO_DOWN_REF(int *val, int *ret, |
| 69 | ossl_unused void *lock) |
Kurt Roeckx | 2f545ae | 2016-08-27 16:01:08 +0200 | [diff] [blame] | 70 | { |
Andy Polyakov | 96d7852 | 2018-08-08 11:10:11 +0200 | [diff] [blame] | 71 | *ret = __atomic_fetch_sub(val, 1, __ATOMIC_RELAXED) - 1; |
Kurt Roeckx | 2f545ae | 2016-08-27 16:01:08 +0200 | [diff] [blame] | 72 | if (*ret == 0) |
| 73 | __atomic_thread_fence(__ATOMIC_ACQUIRE); |
| 74 | return 1; |
| 75 | } |
Shane Lontis | ecae057 | 2019-09-18 15:26:19 +1000 | [diff] [blame] | 76 | # elif defined(__ICL) && defined(_WIN32) |
| 77 | # define HAVE_ATOMICS 1 |
| 78 | typedef volatile int CRYPTO_REF_COUNT; |
| 79 | |
Pauli | a7e6a3d | 2020-12-01 11:30:10 +1000 | [diff] [blame] | 80 | static __inline int CRYPTO_UP_REF(volatile int *val, int *ret, |
| 81 | ossl_unused void *lock) |
Shane Lontis | ecae057 | 2019-09-18 15:26:19 +1000 | [diff] [blame] | 82 | { |
| 83 | *ret = _InterlockedExchangeAdd((void *)val, 1) + 1; |
| 84 | return 1; |
| 85 | } |
| 86 | |
Pauli | a7e6a3d | 2020-12-01 11:30:10 +1000 | [diff] [blame] | 87 | static __inline int CRYPTO_DOWN_REF(volatile int *val, int *ret, |
| 88 | ossl_unused void *lock) |
Shane Lontis | ecae057 | 2019-09-18 15:26:19 +1000 | [diff] [blame] | 89 | { |
| 90 | *ret = _InterlockedExchangeAdd((void *)val, -1) - 1; |
| 91 | return 1; |
| 92 | } |
Kurt Roeckx | 2f545ae | 2016-08-27 16:01:08 +0200 | [diff] [blame] | 93 | |
Richard Levitte | 503d474 | 2019-03-14 09:59:00 +0100 | [diff] [blame] | 94 | # elif defined(_MSC_VER) && _MSC_VER>=1200 |
Andy Polyakov | 96d7852 | 2018-08-08 11:10:11 +0200 | [diff] [blame] | 95 | |
Richard Levitte | 503d474 | 2019-03-14 09:59:00 +0100 | [diff] [blame] | 96 | # define HAVE_ATOMICS 1 |
Andy Polyakov | 96d7852 | 2018-08-08 11:10:11 +0200 | [diff] [blame] | 97 | |
| 98 | typedef volatile int CRYPTO_REF_COUNT; |
| 99 | |
Soujyu Tanaka | 88ffc8d | 2019-03-27 15:55:32 +0900 | [diff] [blame] | 100 | # if (defined(_M_ARM) && _M_ARM>=7 && !defined(_WIN32_WCE)) || defined(_M_ARM64) |
Richard Levitte | 503d474 | 2019-03-14 09:59:00 +0100 | [diff] [blame] | 101 | # include <intrin.h> |
| 102 | # if defined(_M_ARM64) && !defined(_ARM_BARRIER_ISH) |
| 103 | # define _ARM_BARRIER_ISH _ARM64_BARRIER_ISH |
| 104 | # endif |
Andy Polyakov | 96d7852 | 2018-08-08 11:10:11 +0200 | [diff] [blame] | 105 | |
Pauli | a7e6a3d | 2020-12-01 11:30:10 +1000 | [diff] [blame] | 106 | static __inline int CRYPTO_UP_REF(volatile int *val, int *ret, |
| 107 | ossl_unused void *lock) |
Andy Polyakov | 96d7852 | 2018-08-08 11:10:11 +0200 | [diff] [blame] | 108 | { |
| 109 | *ret = _InterlockedExchangeAdd_nf(val, 1) + 1; |
| 110 | return 1; |
| 111 | } |
| 112 | |
Pauli | a7e6a3d | 2020-12-01 11:30:10 +1000 | [diff] [blame] | 113 | static __inline int CRYPTO_DOWN_REF(volatile int *val, int *ret, |
| 114 | ossl_unused void *lock) |
Andy Polyakov | 96d7852 | 2018-08-08 11:10:11 +0200 | [diff] [blame] | 115 | { |
| 116 | *ret = _InterlockedExchangeAdd_nf(val, -1) - 1; |
| 117 | if (*ret == 0) |
| 118 | __dmb(_ARM_BARRIER_ISH); |
| 119 | return 1; |
| 120 | } |
Richard Levitte | 503d474 | 2019-03-14 09:59:00 +0100 | [diff] [blame] | 121 | # else |
Soujyu Tanaka | 88ffc8d | 2019-03-27 15:55:32 +0900 | [diff] [blame] | 122 | # if !defined(_WIN32_WCE) |
| 123 | # pragma intrinsic(_InterlockedExchangeAdd) |
| 124 | # else |
| 125 | # if _WIN32_WCE >= 0x600 |
| 126 | extern long __cdecl _InterlockedExchangeAdd(long volatile*, long); |
| 127 | # else |
Patrick Steuer | aa447d6 | 2019-04-07 13:30:26 +0200 | [diff] [blame] | 128 | /* under Windows CE we still have old-style Interlocked* functions */ |
Soujyu Tanaka | 88ffc8d | 2019-03-27 15:55:32 +0900 | [diff] [blame] | 129 | extern long __cdecl InterlockedExchangeAdd(long volatile*, long); |
| 130 | # define _InterlockedExchangeAdd InterlockedExchangeAdd |
| 131 | # endif |
| 132 | # endif |
Andy Polyakov | 96d7852 | 2018-08-08 11:10:11 +0200 | [diff] [blame] | 133 | |
Pauli | a7e6a3d | 2020-12-01 11:30:10 +1000 | [diff] [blame] | 134 | static __inline int CRYPTO_UP_REF(volatile int *val, int *ret, |
| 135 | ossl_unused void *lock) |
Andy Polyakov | 96d7852 | 2018-08-08 11:10:11 +0200 | [diff] [blame] | 136 | { |
| 137 | *ret = _InterlockedExchangeAdd(val, 1) + 1; |
| 138 | return 1; |
| 139 | } |
| 140 | |
Pauli | a7e6a3d | 2020-12-01 11:30:10 +1000 | [diff] [blame] | 141 | static __inline int CRYPTO_DOWN_REF(volatile int *val, int *ret, |
| 142 | ossl_unused void *lock) |
Andy Polyakov | 96d7852 | 2018-08-08 11:10:11 +0200 | [diff] [blame] | 143 | { |
| 144 | *ret = _InterlockedExchangeAdd(val, -1) - 1; |
| 145 | return 1; |
| 146 | } |
Richard Levitte | 503d474 | 2019-03-14 09:59:00 +0100 | [diff] [blame] | 147 | # endif |
Andy Polyakov | 96d7852 | 2018-08-08 11:10:11 +0200 | [diff] [blame] | 148 | |
Richard Levitte | 503d474 | 2019-03-14 09:59:00 +0100 | [diff] [blame] | 149 | # endif |
| 150 | # endif /* !OPENSSL_DEV_NO_ATOMICS */ |
| 151 | |
| 152 | /* |
| 153 | * All the refcounting implementations above define HAVE_ATOMICS, so if it's |
FdaSilvaYY | 200ae2e | 2020-06-19 23:33:24 +0200 | [diff] [blame] | 154 | * still undefined here (such as when OPENSSL_DEV_NO_ATOMICS is defined), it |
Richard Levitte | 503d474 | 2019-03-14 09:59:00 +0100 | [diff] [blame] | 155 | * means we need to implement a fallback. This fallback uses locks. |
| 156 | */ |
| 157 | # ifndef HAVE_ATOMICS |
Kurt Roeckx | 2f545ae | 2016-08-27 16:01:08 +0200 | [diff] [blame] | 158 | |
| 159 | typedef int CRYPTO_REF_COUNT; |
| 160 | |
Kurt Roeckx | 2f545ae | 2016-08-27 16:01:08 +0200 | [diff] [blame] | 161 | # define CRYPTO_UP_REF(val, ret, lock) CRYPTO_atomic_add(val, 1, ret, lock) |
| 162 | # define CRYPTO_DOWN_REF(val, ret, lock) CRYPTO_atomic_add(val, -1, ret, lock) |
| 163 | |
| 164 | # endif |
Pauli | cd420b0 | 2017-08-22 07:17:35 +1000 | [diff] [blame] | 165 | |
| 166 | # if !defined(NDEBUG) && !defined(OPENSSL_NO_STDIO) |
| 167 | # define REF_ASSERT_ISNT(test) \ |
| 168 | (void)((test) ? (OPENSSL_die("refcount error", __FILE__, __LINE__), 1) : 0) |
| 169 | # else |
| 170 | # define REF_ASSERT_ISNT(i) |
| 171 | # endif |
| 172 | |
Rich Salz | a935791 | 2021-05-19 11:09:49 -0400 | [diff] [blame] | 173 | # define REF_PRINT_EX(text, count, object) \ |
| 174 | OSSL_TRACE3(REF_COUNT, "%p:%4d:%s\n", (object), (count), (text)); |
| 175 | # define REF_PRINT_COUNT(text, object) \ |
| 176 | REF_PRINT_EX(text, object->references, (void *)object) |
Pauli | cd420b0 | 2017-08-22 07:17:35 +1000 | [diff] [blame] | 177 | |
Kurt Roeckx | 2f545ae | 2016-08-27 16:01:08 +0200 | [diff] [blame] | 178 | #endif |