2 * Copyright 2016-2018 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
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"
19 # ifndef OPENSSL_DEV_NO_ATOMICS
20 # if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L \
21 && !defined(__STDC_NO_ATOMICS__)
22 # include <stdatomic.h>
23 # define HAVE_C11_ATOMICS
26 # if defined(HAVE_C11_ATOMICS) && defined(ATOMIC_INT_LOCK_FREE) \
27 && ATOMIC_INT_LOCK_FREE > 0
29 # define HAVE_ATOMICS 1
31 typedef _Atomic int CRYPTO_REF_COUNT;
33 static inline int CRYPTO_UP_REF(_Atomic int *val, int *ret, void *lock)
35 *ret = atomic_fetch_add_explicit(val, 1, memory_order_relaxed) + 1;
40 * Changes to shared structure other than reference counter have to be
41 * serialized. And any kind of serialization implies a release fence. This
42 * means that by the time reference counter is decremented all other
43 * changes are visible on all processors. Hence decrement itself can be
44 * relaxed. In case it hits zero, object will be destructed. Since it's
45 * last use of the object, destructor programmer might reason that access
46 * to mutable members doesn't have to be serialized anymore, which would
47 * otherwise imply an acquire fence. Hence conditional acquire fence...
49 static inline int CRYPTO_DOWN_REF(_Atomic int *val, int *ret, void *lock)
51 *ret = atomic_fetch_sub_explicit(val, 1, memory_order_relaxed) - 1;
53 atomic_thread_fence(memory_order_acquire);
57 # elif defined(__GNUC__) && defined(__ATOMIC_RELAXED) && __GCC_ATOMIC_INT_LOCK_FREE > 0
59 # define HAVE_ATOMICS 1
61 typedef int CRYPTO_REF_COUNT;
63 static __inline__ int CRYPTO_UP_REF(int *val, int *ret, void *lock)
65 *ret = __atomic_fetch_add(val, 1, __ATOMIC_RELAXED) + 1;
69 static __inline__ int CRYPTO_DOWN_REF(int *val, int *ret, 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, void *lock)
82 *ret = _InterlockedExchangeAdd((void *)val, 1) + 1;
86 static __inline int CRYPTO_DOWN_REF(volatile int *val, int *ret, void *lock)
88 *ret = _InterlockedExchangeAdd((void *)val, -1) - 1;
92 # elif defined(_MSC_VER) && _MSC_VER>=1200
94 # define HAVE_ATOMICS 1
96 typedef volatile int CRYPTO_REF_COUNT;
98 # if (defined(_M_ARM) && _M_ARM>=7 && !defined(_WIN32_WCE)) || defined(_M_ARM64)
100 # if defined(_M_ARM64) && !defined(_ARM_BARRIER_ISH)
101 # define _ARM_BARRIER_ISH _ARM64_BARRIER_ISH
104 static __inline int CRYPTO_UP_REF(volatile int *val, int *ret, void *lock)
106 *ret = _InterlockedExchangeAdd_nf(val, 1) + 1;
110 static __inline int CRYPTO_DOWN_REF(volatile int *val, int *ret, void *lock)
112 *ret = _InterlockedExchangeAdd_nf(val, -1) - 1;
114 __dmb(_ARM_BARRIER_ISH);
118 # if !defined(_WIN32_WCE)
119 # pragma intrinsic(_InterlockedExchangeAdd)
121 # if _WIN32_WCE >= 0x600
122 extern long __cdecl _InterlockedExchangeAdd(long volatile*, long);
124 /* under Windows CE we still have old-style Interlocked* functions */
125 extern long __cdecl InterlockedExchangeAdd(long volatile*, long);
126 # define _InterlockedExchangeAdd InterlockedExchangeAdd
130 static __inline int CRYPTO_UP_REF(volatile int *val, int *ret, void *lock)
132 *ret = _InterlockedExchangeAdd(val, 1) + 1;
136 static __inline int CRYPTO_DOWN_REF(volatile int *val, int *ret, void *lock)
138 *ret = _InterlockedExchangeAdd(val, -1) - 1;
144 # endif /* !OPENSSL_DEV_NO_ATOMICS */
147 * All the refcounting implementations above define HAVE_ATOMICS, so if it's
148 * still undefined here (such as when OPENSSL_DEV_NO_ATMOICS is defined), it
149 * means we need to implement a fallback. This fallback uses locks.
151 # ifndef HAVE_ATOMICS
153 typedef int CRYPTO_REF_COUNT;
155 # define CRYPTO_UP_REF(val, ret, lock) CRYPTO_atomic_add(val, 1, ret, lock)
156 # define CRYPTO_DOWN_REF(val, ret, lock) CRYPTO_atomic_add(val, -1, ret, lock)
160 # if !defined(NDEBUG) && !defined(OPENSSL_NO_STDIO)
161 # define REF_ASSERT_ISNT(test) \
162 (void)((test) ? (OPENSSL_die("refcount error", __FILE__, __LINE__), 1) : 0)
164 # define REF_ASSERT_ISNT(i)
168 # define REF_PRINT_COUNT(a, b) \
169 fprintf(stderr, "%p:%4d:%s\n", b, b->references, a)
171 # define REF_PRINT_COUNT(a, b)