]> git.ipfire.org Git - thirdparty/openssl.git/blob - include/internal/refcount.h
Update copyright year
[thirdparty/openssl.git] / include / internal / refcount.h
1 /*
2 * Copyright 2016-2021 The OpenSSL Project Authors. All Rights Reserved.
3 *
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
8 */
9 #ifndef OSSL_INTERNAL_REFCOUNT_H
10 # define OSSL_INTERNAL_REFCOUNT_H
11 # pragma once
12
13 # include <openssl/e_os2.h>
14
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"
19 # endif
20 # endif
21
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
27 # endif
28
29 # if defined(HAVE_C11_ATOMICS) && defined(ATOMIC_INT_LOCK_FREE) \
30 && ATOMIC_INT_LOCK_FREE > 0
31
32 # define HAVE_ATOMICS 1
33
34 typedef _Atomic int CRYPTO_REF_COUNT;
35
36 static inline int CRYPTO_UP_REF(_Atomic int *val, int *ret,
37 ossl_unused void *lock)
38 {
39 *ret = atomic_fetch_add_explicit(val, 1, memory_order_relaxed) + 1;
40 return 1;
41 }
42
43 /*
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...
52 */
53 static inline int CRYPTO_DOWN_REF(_Atomic int *val, int *ret,
54 ossl_unused void *lock)
55 {
56 *ret = atomic_fetch_sub_explicit(val, 1, memory_order_relaxed) - 1;
57 if (*ret == 0)
58 atomic_thread_fence(memory_order_acquire);
59 return 1;
60 }
61
62 # elif defined(__GNUC__) && defined(__ATOMIC_RELAXED) && __GCC_ATOMIC_INT_LOCK_FREE > 0
63
64 # define HAVE_ATOMICS 1
65
66 typedef int CRYPTO_REF_COUNT;
67
68 static __inline__ int CRYPTO_UP_REF(int *val, int *ret, ossl_unused void *lock)
69 {
70 *ret = __atomic_fetch_add(val, 1, __ATOMIC_RELAXED) + 1;
71 return 1;
72 }
73
74 static __inline__ int CRYPTO_DOWN_REF(int *val, int *ret,
75 ossl_unused void *lock)
76 {
77 *ret = __atomic_fetch_sub(val, 1, __ATOMIC_RELAXED) - 1;
78 if (*ret == 0)
79 __atomic_thread_fence(__ATOMIC_ACQUIRE);
80 return 1;
81 }
82 # elif defined(__ICL) && defined(_WIN32)
83 # define HAVE_ATOMICS 1
84 typedef volatile int CRYPTO_REF_COUNT;
85
86 static __inline int CRYPTO_UP_REF(volatile int *val, int *ret,
87 ossl_unused void *lock)
88 {
89 *ret = _InterlockedExchangeAdd((void *)val, 1) + 1;
90 return 1;
91 }
92
93 static __inline int CRYPTO_DOWN_REF(volatile int *val, int *ret,
94 ossl_unused void *lock)
95 {
96 *ret = _InterlockedExchangeAdd((void *)val, -1) - 1;
97 return 1;
98 }
99
100 # elif defined(_MSC_VER) && _MSC_VER>=1200
101
102 # define HAVE_ATOMICS 1
103
104 typedef volatile int CRYPTO_REF_COUNT;
105
106 # if (defined(_M_ARM) && _M_ARM>=7 && !defined(_WIN32_WCE)) || defined(_M_ARM64)
107 # include <intrin.h>
108 # if defined(_M_ARM64) && !defined(_ARM_BARRIER_ISH)
109 # define _ARM_BARRIER_ISH _ARM64_BARRIER_ISH
110 # endif
111
112 static __inline int CRYPTO_UP_REF(volatile int *val, int *ret,
113 ossl_unused void *lock)
114 {
115 *ret = _InterlockedExchangeAdd_nf(val, 1) + 1;
116 return 1;
117 }
118
119 static __inline int CRYPTO_DOWN_REF(volatile int *val, int *ret,
120 ossl_unused void *lock)
121 {
122 *ret = _InterlockedExchangeAdd_nf(val, -1) - 1;
123 if (*ret == 0)
124 __dmb(_ARM_BARRIER_ISH);
125 return 1;
126 }
127 # else
128 # if !defined(_WIN32_WCE)
129 # pragma intrinsic(_InterlockedExchangeAdd)
130 # else
131 # if _WIN32_WCE >= 0x600
132 extern long __cdecl _InterlockedExchangeAdd(long volatile*, long);
133 # else
134 /* under Windows CE we still have old-style Interlocked* functions */
135 extern long __cdecl InterlockedExchangeAdd(long volatile*, long);
136 # define _InterlockedExchangeAdd InterlockedExchangeAdd
137 # endif
138 # endif
139
140 static __inline int CRYPTO_UP_REF(volatile int *val, int *ret,
141 ossl_unused void *lock)
142 {
143 *ret = _InterlockedExchangeAdd(val, 1) + 1;
144 return 1;
145 }
146
147 static __inline int CRYPTO_DOWN_REF(volatile int *val, int *ret,
148 ossl_unused void *lock)
149 {
150 *ret = _InterlockedExchangeAdd(val, -1) - 1;
151 return 1;
152 }
153 # endif
154
155 # endif
156 # endif /* !OPENSSL_DEV_NO_ATOMICS */
157
158 /*
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.
162 */
163 # ifndef HAVE_ATOMICS
164
165 typedef int CRYPTO_REF_COUNT;
166
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)
169
170 # endif
171
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)
175 # else
176 # define REF_ASSERT_ISNT(i)
177 # endif
178
179 # ifdef REF_PRINT
180 # define REF_PRINT_COUNT(a, b) \
181 fprintf(stderr, "%p:%4d:%s\n", (void*)b, b->references, a)
182 # else
183 # define REF_PRINT_COUNT(a, b)
184 # endif
185
186 #endif