]>
git.ipfire.org Git - thirdparty/openssl.git/blob - include/internal/refcount.h
2 * Copyright 2016-2023 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>
15 # include <openssl/err.h>
17 # if defined(OPENSSL_THREADS) && !defined(OPENSSL_DEV_NO_ATOMICS)
18 # if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L \
19 && !defined(__STDC_NO_ATOMICS__)
20 # include <stdatomic.h>
21 # define HAVE_C11_ATOMICS
24 # if defined(HAVE_C11_ATOMICS) && defined(ATOMIC_INT_LOCK_FREE) \
25 && ATOMIC_INT_LOCK_FREE > 0
27 # define HAVE_ATOMICS 1
33 static inline int CRYPTO_UP_REF(CRYPTO_REF_COUNT
*refcnt
, int *ret
)
35 *ret
= atomic_fetch_add_explicit(&refcnt
->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(CRYPTO_REF_COUNT
*refcnt
, int *ret
)
51 *ret
= atomic_fetch_sub_explicit(&refcnt
->val
, 1, memory_order_relaxed
) - 1;
53 atomic_thread_fence(memory_order_acquire
);
57 static inline int CRYPTO_GET_REF(CRYPTO_REF_COUNT
*refcnt
, int *ret
)
59 *ret
= atomic_load_explicit(&refcnt
->val
, memory_order_relaxed
);
63 # elif defined(__GNUC__) && defined(__ATOMIC_RELAXED) && __GCC_ATOMIC_INT_LOCK_FREE > 0
65 # define HAVE_ATOMICS 1
71 static __inline__
int CRYPTO_UP_REF(CRYPTO_REF_COUNT
*refcnt
, int *ret
)
73 *ret
= __atomic_fetch_add(&refcnt
->val
, 1, __ATOMIC_RELAXED
) + 1;
77 static __inline__
int CRYPTO_DOWN_REF(CRYPTO_REF_COUNT
*refcnt
, int *ret
)
79 *ret
= __atomic_fetch_sub(&refcnt
->val
, 1, __ATOMIC_RELAXED
) - 1;
81 __atomic_thread_fence(__ATOMIC_ACQUIRE
);
85 static __inline__
int CRYPTO_GET_REF(CRYPTO_REF_COUNT
*refcnt
, int *ret
)
87 *ret
= __atomic_load_n(&refcnt
->val
, __ATOMIC_RELAXED
);
91 # elif defined(__ICL) && defined(_WIN32)
92 # define HAVE_ATOMICS 1
98 static __inline
int CRYPTO_UP_REF(CRYPTO_REF_COUNT
*refcnt
, int *ret
)
100 *ret
= _InterlockedExchangeAdd((void *)&refcnt
->val
, 1) + 1;
104 static __inline
int CRYPTO_DOWN_REF(CRYPTO_REF_COUNT
*val
, int *refcnt
)
106 *ret
= _InterlockedExchangeAdd((void *)&refcnt
->val
, -1) - 1;
110 static __inline
int CRYPTO_GET_REF(CRYPTO_REF_COUNT
*refcnt
, int *ret
)
112 *ret
= _InterlockedOr((void *)&refcnt
->val
, 0);
116 # elif defined(_MSC_VER) && _MSC_VER>=1200
118 # define HAVE_ATOMICS 1
124 # if (defined(_M_ARM) && _M_ARM>=7 && !defined(_WIN32_WCE)) || defined(_M_ARM64)
126 # if defined(_M_ARM64) && !defined(_ARM_BARRIER_ISH)
127 # define _ARM_BARRIER_ISH _ARM64_BARRIER_ISH
130 static __inline
int CRYPTO_UP_REF(CRYPTO_REF_COUNT
*refcnt
, int *ret
)
132 *ret
= _InterlockedExchangeAdd_nf(&refcnt
->val
, 1) + 1;
136 static __inline
int CRYPTO_DOWN_REF(CRYPTO_REF_COUNT
*refcnt
, int *ret
)
138 *ret
= _InterlockedExchangeAdd_nf(&refcnt
->val
, -1) - 1;
140 __dmb(_ARM_BARRIER_ISH
);
144 static __inline
int CRYPTO_GET_REF(CRYPTO_REF_COUNT
*refcnt
, int *ret
)
146 *ret
= _InterlockedOr_nf((void *)&refcnt
->val
, 0);
151 # if !defined(_WIN32_WCE)
152 # pragma intrinsic(_InterlockedExchangeAdd)
154 # if _WIN32_WCE >= 0x600
155 extern long __cdecl
_InterlockedExchangeAdd(long volatile*, long);
157 /* under Windows CE we still have old-style Interlocked* functions */
158 extern long __cdecl
InterlockedExchangeAdd(long volatile*, long);
159 # define _InterlockedExchangeAdd InterlockedExchangeAdd
163 static __inline
int CRYPTO_UP_REF(CRYPTO_REF_COUNT
*refcnt
, int *ret
)
165 *ret
= _InterlockedExchangeAdd(&refcnt
->val
, 1) + 1;
169 static __inline
int CRYPTO_DOWN_REF(CRYPTO_REF_COUNT
*refcnt
, int *ret
)
171 *ret
= _InterlockedExchangeAdd(&refcnt
->val
, -1) - 1;
175 static __inline
int CRYPTO_GET_REF(CRYPTO_REF_COUNT
*refcnt
, int *ret
)
177 *ret
= _InterlockedExchangeAdd(&refcnt
->val
, 0);
184 # endif /* !OPENSSL_DEV_NO_ATOMICS */
187 * All the refcounting implementations above define HAVE_ATOMICS, so if it's
188 * still undefined here (such as when OPENSSL_DEV_NO_ATOMICS is defined), it
189 * means we need to implement a fallback. This fallback uses locks.
191 # ifndef HAVE_ATOMICS
195 # ifdef OPENSSL_THREADS
200 # ifdef OPENSSL_THREADS
202 static ossl_unused ossl_inline
int CRYPTO_UP_REF(CRYPTO_REF_COUNT
*refcnt
,
205 return CRYPTO_atomic_add(&refcnt
->val
, 1, ret
, refcnt
->lock
);
208 static ossl_unused ossl_inline
int CRYPTO_DOWN_REF(CRYPTO_REF_COUNT
*refcnt
,
211 return CRYPTO_atomic_add(&refcnt
->val
, -1, ret
, refcnt
->lock
);
214 static ossl_unused ossl_inline
int CRYPTO_GET_REF(CRYPTO_REF_COUNT
*refcnt
,
217 return CRYPTO_atomic_load_int(&refcnt
->val
, ret
, refcnt
->lock
);
220 # define CRYPTO_NEW_FREE_DEFINED 1
221 static ossl_unused ossl_inline
int CRYPTO_NEW_REF(CRYPTO_REF_COUNT
*refcnt
, int n
)
224 refcnt
->lock
= CRYPTO_THREAD_lock_new();
225 if (refcnt
->lock
== NULL
) {
226 ERR_raise(ERR_LIB_CRYPTO
, ERR_R_CRYPTO_LIB
);
232 static ossl_unused ossl_inline
void CRYPTO_FREE_REF(CRYPTO_REF_COUNT
*refcnt
) \
235 CRYPTO_THREAD_lock_free(refcnt
->lock
);
238 # else /* OPENSSL_THREADS */
240 static ossl_unused ossl_inline
int CRYPTO_UP_REF(CRYPTO_REF_COUNT
*refcnt
,
248 static ossl_unused ossl_inline
int CRYPTO_DOWN_REF(CRYPTO_REF_COUNT
*refcnt
,
256 static ossl_unused ossl_inline
int CRYPTO_GET_REF(CRYPTO_REF_COUNT
*refcnt
,
263 # endif /* OPENSSL_THREADS */
266 # ifndef CRYPTO_NEW_FREE_DEFINED
267 static ossl_unused ossl_inline
int CRYPTO_NEW_REF(CRYPTO_REF_COUNT
*refcnt
, int n
)
273 static ossl_unused ossl_inline
void CRYPTO_FREE_REF(CRYPTO_REF_COUNT
*refcnt
) \
276 # endif /* CRYPTO_NEW_FREE_DEFINED */
277 #undef CRYPTO_NEW_FREE_DEFINED
279 # if !defined(NDEBUG) && !defined(OPENSSL_NO_STDIO)
280 # define REF_ASSERT_ISNT(test) \
281 (void)((test) ? (OPENSSL_die("refcount error", __FILE__, __LINE__), 1) : 0)
283 # define REF_ASSERT_ISNT(i)
286 # define REF_PRINT_EX(text, count, object) \
287 OSSL_TRACE3(REF_COUNT, "%p:%4d:%s\n", (object), (count), (text));
288 # define REF_PRINT_COUNT(text, object) \
289 REF_PRINT_EX(text, object->references.val, (void *)object)