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>
15 /* Used to checking reference counts, most while doing perl5 stuff :-) */
16 # if defined(OPENSSL_NO_STDIO)
17 # if defined(REF_PRINT)
18 # error "REF_PRINT requires stdio"
22 # ifndef OPENSSL_DEV_NO_ATOMICS
23 # if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L \
24 && !defined(__STDC_NO_ATOMICS__)
25 # include <stdatomic.h>
26 # define HAVE_C11_ATOMICS
29 # if defined(HAVE_C11_ATOMICS) && defined(ATOMIC_INT_LOCK_FREE) \
30 && ATOMIC_INT_LOCK_FREE > 0
32 # define HAVE_ATOMICS 1
34 typedef _Atomic int CRYPTO_REF_COUNT;
36 static inline int CRYPTO_UP_REF(_Atomic int *val, int *ret,
37 ossl_unused void *lock)
39 *ret = atomic_fetch_add_explicit(val, 1, memory_order_relaxed) + 1;
44 * Changes to shared structure other than reference counter have to be
45 * serialized. And any kind of serialization implies a release fence. This
46 * means that by the time reference counter is decremented all other
47 * changes are visible on all processors. Hence decrement itself can be
48 * relaxed. In case it hits zero, object will be destructed. Since it's
49 * last use of the object, destructor programmer might reason that access
50 * to mutable members doesn't have to be serialized anymore, which would
51 * otherwise imply an acquire fence. Hence conditional acquire fence...
53 static inline int CRYPTO_DOWN_REF(_Atomic int *val, int *ret,
54 ossl_unused void *lock)
56 *ret = atomic_fetch_sub_explicit(val, 1, memory_order_relaxed) - 1;
58 atomic_thread_fence(memory_order_acquire);
62 # elif defined(__GNUC__) && defined(__ATOMIC_RELAXED) && __GCC_ATOMIC_INT_LOCK_FREE > 0
64 # define HAVE_ATOMICS 1
66 typedef int CRYPTO_REF_COUNT;
68 static __inline__ int CRYPTO_UP_REF(int *val, int *ret, ossl_unused void *lock)
70 *ret = __atomic_fetch_add(val, 1, __ATOMIC_RELAXED) + 1;
74 static __inline__ int CRYPTO_DOWN_REF(int *val, int *ret,
75 ossl_unused void *lock)
77 *ret = __atomic_fetch_sub(val, 1, __ATOMIC_RELAXED) - 1;
79 __atomic_thread_fence(__ATOMIC_ACQUIRE);
82 # elif defined(__ICL) && defined(_WIN32)
83 # define HAVE_ATOMICS 1
84 typedef volatile int CRYPTO_REF_COUNT;
86 static __inline int CRYPTO_UP_REF(volatile int *val, int *ret,
87 ossl_unused void *lock)
89 *ret = _InterlockedExchangeAdd((void *)val, 1) + 1;
93 static __inline int CRYPTO_DOWN_REF(volatile int *val, int *ret,
94 ossl_unused void *lock)
96 *ret = _InterlockedExchangeAdd((void *)val, -1) - 1;
100 # elif defined(_MSC_VER) && _MSC_VER>=1200
102 # define HAVE_ATOMICS 1
104 typedef volatile int CRYPTO_REF_COUNT;
106 # if (defined(_M_ARM) && _M_ARM>=7 && !defined(_WIN32_WCE)) || defined(_M_ARM64)
108 # if defined(_M_ARM64) && !defined(_ARM_BARRIER_ISH)
109 # define _ARM_BARRIER_ISH _ARM64_BARRIER_ISH
112 static __inline int CRYPTO_UP_REF(volatile int *val, int *ret,
113 ossl_unused void *lock)
115 *ret = _InterlockedExchangeAdd_nf(val, 1) + 1;
119 static __inline int CRYPTO_DOWN_REF(volatile int *val, int *ret,
120 ossl_unused void *lock)
122 *ret = _InterlockedExchangeAdd_nf(val, -1) - 1;
124 __dmb(_ARM_BARRIER_ISH);
128 # if !defined(_WIN32_WCE)
129 # pragma intrinsic(_InterlockedExchangeAdd)
131 # if _WIN32_WCE >= 0x600
132 extern long __cdecl _InterlockedExchangeAdd(long volatile*, long);
134 /* under Windows CE we still have old-style Interlocked* functions */
135 extern long __cdecl InterlockedExchangeAdd(long volatile*, long);
136 # define _InterlockedExchangeAdd InterlockedExchangeAdd
140 static __inline int CRYPTO_UP_REF(volatile int *val, int *ret,
141 ossl_unused void *lock)
143 *ret = _InterlockedExchangeAdd(val, 1) + 1;
147 static __inline int CRYPTO_DOWN_REF(volatile int *val, int *ret,
148 ossl_unused void *lock)
150 *ret = _InterlockedExchangeAdd(val, -1) - 1;
156 # endif /* !OPENSSL_DEV_NO_ATOMICS */
159 * All the refcounting implementations above define HAVE_ATOMICS, so if it's
160 * still undefined here (such as when OPENSSL_DEV_NO_ATOMICS is defined), it
161 * means we need to implement a fallback. This fallback uses locks.
163 # ifndef HAVE_ATOMICS
165 typedef int CRYPTO_REF_COUNT;
167 # define CRYPTO_UP_REF(val, ret, lock) CRYPTO_atomic_add(val, 1, ret, lock)
168 # define CRYPTO_DOWN_REF(val, ret, lock) CRYPTO_atomic_add(val, -1, ret, lock)
172 # if !defined(NDEBUG) && !defined(OPENSSL_NO_STDIO)
173 # define REF_ASSERT_ISNT(test) \
174 (void)((test) ? (OPENSSL_die("refcount error", __FILE__, __LINE__), 1) : 0)
176 # define REF_ASSERT_ISNT(i)
180 # define REF_PRINT_COUNT(a, b) \
181 fprintf(stderr, "%p:%4d:%s\n", (void*)b, b->references, a)
183 # define REF_PRINT_COUNT(a, b)