2 * Copyright 2016-2021 The OpenSSL Project Authors. All Rights Reserved.
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
9 #ifndef OSSL_INTERNAL_REFCOUNT_H
10 # define OSSL_INTERNAL_REFCOUNT_H
13 # include <openssl/e_os2.h>
14 # include <openssl/trace.h>
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
23 # if defined(HAVE_C11_ATOMICS) && defined(ATOMIC_INT_LOCK_FREE) \
24 && ATOMIC_INT_LOCK_FREE > 0
26 # define HAVE_ATOMICS 1
28 typedef _Atomic int CRYPTO_REF_COUNT;
30 static inline int CRYPTO_UP_REF(_Atomic int *val, int *ret,
31 ossl_unused void *lock)
33 *ret = atomic_fetch_add_explicit(val, 1, memory_order_relaxed) + 1;
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...
47 static inline int CRYPTO_DOWN_REF(_Atomic int *val, int *ret,
48 ossl_unused void *lock)
50 *ret = atomic_fetch_sub_explicit(val, 1, memory_order_relaxed) - 1;
52 atomic_thread_fence(memory_order_acquire);
56 # elif defined(__GNUC__) && defined(__ATOMIC_RELAXED) && __GCC_ATOMIC_INT_LOCK_FREE > 0
58 # define HAVE_ATOMICS 1
60 typedef int CRYPTO_REF_COUNT;
62 static __inline__ int CRYPTO_UP_REF(int *val, int *ret, ossl_unused void *lock)
64 *ret = __atomic_fetch_add(val, 1, __ATOMIC_RELAXED) + 1;
68 static __inline__ int CRYPTO_DOWN_REF(int *val, int *ret,
69 ossl_unused void *lock)
71 *ret = __atomic_fetch_sub(val, 1, __ATOMIC_RELAXED) - 1;
73 __atomic_thread_fence(__ATOMIC_ACQUIRE);
76 # elif defined(__ICL) && defined(_WIN32)
77 # define HAVE_ATOMICS 1
78 typedef volatile int CRYPTO_REF_COUNT;
80 static __inline int CRYPTO_UP_REF(volatile int *val, int *ret,
81 ossl_unused void *lock)
83 *ret = _InterlockedExchangeAdd((void *)val, 1) + 1;
87 static __inline int CRYPTO_DOWN_REF(volatile int *val, int *ret,
88 ossl_unused void *lock)
90 *ret = _InterlockedExchangeAdd((void *)val, -1) - 1;
94 # elif defined(_MSC_VER) && _MSC_VER>=1200
96 # define HAVE_ATOMICS 1
98 typedef volatile int CRYPTO_REF_COUNT;
100 # if (defined(_M_ARM) && _M_ARM>=7 && !defined(_WIN32_WCE)) || defined(_M_ARM64)
102 # if defined(_M_ARM64) && !defined(_ARM_BARRIER_ISH)
103 # define _ARM_BARRIER_ISH _ARM64_BARRIER_ISH
106 static __inline int CRYPTO_UP_REF(volatile int *val, int *ret,
107 ossl_unused void *lock)
109 *ret = _InterlockedExchangeAdd_nf(val, 1) + 1;
113 static __inline int CRYPTO_DOWN_REF(volatile int *val, int *ret,
114 ossl_unused void *lock)
116 *ret = _InterlockedExchangeAdd_nf(val, -1) - 1;
118 __dmb(_ARM_BARRIER_ISH);
122 # if !defined(_WIN32_WCE)
123 # pragma intrinsic(_InterlockedExchangeAdd)
125 # if _WIN32_WCE >= 0x600
126 extern long __cdecl _InterlockedExchangeAdd(long volatile*, long);
128 /* under Windows CE we still have old-style Interlocked* functions */
129 extern long __cdecl InterlockedExchangeAdd(long volatile*, long);
130 # define _InterlockedExchangeAdd InterlockedExchangeAdd
134 static __inline int CRYPTO_UP_REF(volatile int *val, int *ret,
135 ossl_unused void *lock)
137 *ret = _InterlockedExchangeAdd(val, 1) + 1;
141 static __inline int CRYPTO_DOWN_REF(volatile int *val, int *ret,
142 ossl_unused void *lock)
144 *ret = _InterlockedExchangeAdd(val, -1) - 1;
150 # endif /* !OPENSSL_DEV_NO_ATOMICS */
153 * All the refcounting implementations above define HAVE_ATOMICS, so if it's
154 * still undefined here (such as when OPENSSL_DEV_NO_ATOMICS is defined), it
155 * means we need to implement a fallback. This fallback uses locks.
157 # ifndef HAVE_ATOMICS
159 typedef int CRYPTO_REF_COUNT;
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)
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)
170 # define REF_ASSERT_ISNT(i)
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)