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 HEADER_INTERNAL_REFCOUNT_H
10 # define HEADER_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);
77 # elif defined(_MSC_VER) && _MSC_VER>=1200
79 # define HAVE_ATOMICS 1
81 typedef volatile int CRYPTO_REF_COUNT;
83 # if (defined(_M_ARM) && _M_ARM>=7 && !defined(_WIN32_WCE)) || defined(_M_ARM64)
85 # if defined(_M_ARM64) && !defined(_ARM_BARRIER_ISH)
86 # define _ARM_BARRIER_ISH _ARM64_BARRIER_ISH
89 static __inline int CRYPTO_UP_REF(volatile int *val, int *ret, void *lock)
91 *ret = _InterlockedExchangeAdd_nf(val, 1) + 1;
95 static __inline int CRYPTO_DOWN_REF(volatile int *val, int *ret, void *lock)
97 *ret = _InterlockedExchangeAdd_nf(val, -1) - 1;
99 __dmb(_ARM_BARRIER_ISH);
103 # if !defined(_WIN32_WCE)
104 # pragma intrinsic(_InterlockedExchangeAdd)
106 # if _WIN32_WCE >= 0x600
107 extern long __cdecl _InterlockedExchangeAdd(long volatile*, long);
109 /* under Windows CE we still have old-style Interlocked* functions */
110 extern long __cdecl InterlockedExchangeAdd(long volatile*, long);
111 # define _InterlockedExchangeAdd InterlockedExchangeAdd
115 static __inline int CRYPTO_UP_REF(volatile int *val, int *ret, void *lock)
117 *ret = _InterlockedExchangeAdd(val, 1) + 1;
121 static __inline int CRYPTO_DOWN_REF(volatile int *val, int *ret, void *lock)
123 *ret = _InterlockedExchangeAdd(val, -1) - 1;
129 # endif /* !OPENSSL_DEV_NO_ATOMICS */
132 * All the refcounting implementations above define HAVE_ATOMICS, so if it's
133 * still undefined here (such as when OPENSSL_DEV_NO_ATMOICS is defined), it
134 * means we need to implement a fallback. This fallback uses locks.
136 # ifndef HAVE_ATOMICS
138 typedef int CRYPTO_REF_COUNT;
140 # define CRYPTO_UP_REF(val, ret, lock) CRYPTO_atomic_add(val, 1, ret, lock)
141 # define CRYPTO_DOWN_REF(val, ret, lock) CRYPTO_atomic_add(val, -1, ret, lock)
145 # if !defined(NDEBUG) && !defined(OPENSSL_NO_STDIO)
146 # define REF_ASSERT_ISNT(test) \
147 (void)((test) ? (OPENSSL_die("refcount error", __FILE__, __LINE__), 1) : 0)
149 # define REF_ASSERT_ISNT(i)
153 # define REF_PRINT_COUNT(a, b) \
154 fprintf(stderr, "%p:%4d:%s\n", b, b->references, a)
156 # define REF_PRINT_COUNT(a, b)