| 
									
										
										
										
											2016-08-27 22:01:08 +08:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2021-02-18 22:57:13 +08:00
										 |  |  |  * Copyright 2016-2021 The OpenSSL Project Authors. All Rights Reserved. | 
					
						
							| 
									
										
										
										
											2016-08-27 22:01:08 +08:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2018-12-06 20:12:35 +08:00
										 |  |  |  * Licensed under the Apache License 2.0 (the "License").  You may not use | 
					
						
							| 
									
										
										
										
											2016-08-27 22:01:08 +08:00
										 |  |  |  * this file except in compliance with the License.  You can obtain a copy | 
					
						
							|  |  |  |  * in the file LICENSE in the source distribution or at | 
					
						
							|  |  |  |  * https://www.openssl.org/source/license.html
 | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2019-09-28 06:45:57 +08:00
										 |  |  | #ifndef OSSL_INTERNAL_REFCOUNT_H
 | 
					
						
							|  |  |  | # define OSSL_INTERNAL_REFCOUNT_H
 | 
					
						
							| 
									
										
										
										
											2021-02-07 05:14:03 +08:00
										 |  |  | # pragma once
 | 
					
						
							| 
									
										
										
										
											2016-08-27 22:01:08 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-01 09:30:10 +08:00
										 |  |  | # include <openssl/e_os2.h>
 | 
					
						
							| 
									
										
										
										
											2021-05-19 23:09:49 +08:00
										 |  |  | # include <openssl/trace.h>
 | 
					
						
							| 
									
										
										
										
											2017-08-22 05:17:35 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-23 02:20:53 +08:00
										 |  |  | # if defined(OPENSSL_THREADS) && !defined(OPENSSL_DEV_NO_ATOMICS)
 | 
					
						
							| 
									
										
										
										
											2019-03-14 16:59:00 +08:00
										 |  |  | #  if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L \
 | 
					
						
							|  |  |  |       && !defined(__STDC_NO_ATOMICS__) | 
					
						
							|  |  |  | #   include <stdatomic.h>
 | 
					
						
							|  |  |  | #   define HAVE_C11_ATOMICS
 | 
					
						
							|  |  |  | #  endif
 | 
					
						
							| 
									
										
										
										
											2016-08-27 22:01:08 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-14 16:59:00 +08:00
										 |  |  | #  if defined(HAVE_C11_ATOMICS) && defined(ATOMIC_INT_LOCK_FREE) \
 | 
					
						
							|  |  |  |       && ATOMIC_INT_LOCK_FREE > 0 | 
					
						
							| 
									
										
										
										
											2016-08-27 22:01:08 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-14 16:59:00 +08:00
										 |  |  | #   define HAVE_ATOMICS 1
 | 
					
						
							| 
									
										
										
										
											2016-08-27 22:01:08 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-22 07:48:56 +08:00
										 |  |  | typedef struct { | 
					
						
							|  |  |  |     _Atomic int val; | 
					
						
							|  |  |  | } CRYPTO_REF_COUNT; | 
					
						
							| 
									
										
										
										
											2016-08-27 22:01:08 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-22 07:48:56 +08:00
										 |  |  | static inline int CRYPTO_UP_REF(CRYPTO_REF_COUNT *refcnt, int *ret) | 
					
						
							| 
									
										
										
										
											2016-08-27 22:01:08 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-06-22 07:48:56 +08:00
										 |  |  |     *ret = atomic_fetch_add_explicit(&refcnt->val, 1, memory_order_relaxed) + 1; | 
					
						
							| 
									
										
										
										
											2016-08-27 22:01:08 +08:00
										 |  |  |     return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-08 17:10:11 +08:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Changes to shared structure other than reference counter have to be | 
					
						
							|  |  |  |  * serialized. And any kind of serialization implies a release fence. This | 
					
						
							|  |  |  |  * means that by the time reference counter is decremented all other | 
					
						
							|  |  |  |  * changes are visible on all processors. Hence decrement itself can be | 
					
						
							|  |  |  |  * relaxed. In case it hits zero, object will be destructed. Since it's | 
					
						
							|  |  |  |  * last use of the object, destructor programmer might reason that access | 
					
						
							|  |  |  |  * to mutable members doesn't have to be serialized anymore, which would | 
					
						
							|  |  |  |  * otherwise imply an acquire fence. Hence conditional acquire fence... | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2023-06-22 07:48:56 +08:00
										 |  |  | static inline int CRYPTO_DOWN_REF(CRYPTO_REF_COUNT *refcnt, int *ret) | 
					
						
							| 
									
										
										
										
											2016-08-27 22:01:08 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-06-22 07:48:56 +08:00
										 |  |  |     *ret = atomic_fetch_sub_explicit(&refcnt->val, 1, memory_order_relaxed) - 1; | 
					
						
							| 
									
										
										
										
											2016-08-27 22:01:08 +08:00
										 |  |  |     if (*ret == 0) | 
					
						
							|  |  |  |         atomic_thread_fence(memory_order_acquire); | 
					
						
							|  |  |  |     return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-22 07:48:56 +08:00
										 |  |  | static inline int CRYPTO_GET_REF(CRYPTO_REF_COUNT *refcnt, int *ret) | 
					
						
							| 
									
										
										
										
											2023-04-27 22:52:44 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-06-22 07:48:56 +08:00
										 |  |  |     *ret = atomic_load_explicit(&refcnt->val, memory_order_relaxed); | 
					
						
							| 
									
										
										
										
											2023-04-27 22:52:44 +08:00
										 |  |  |     return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-14 16:59:00 +08:00
										 |  |  | #  elif defined(__GNUC__) && defined(__ATOMIC_RELAXED) && __GCC_ATOMIC_INT_LOCK_FREE > 0
 | 
					
						
							| 
									
										
										
										
											2016-08-27 22:01:08 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-14 16:59:00 +08:00
										 |  |  | #   define HAVE_ATOMICS 1
 | 
					
						
							| 
									
										
										
										
											2016-08-27 22:01:08 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-22 07:48:56 +08:00
										 |  |  | typedef struct { | 
					
						
							|  |  |  |     int val; | 
					
						
							|  |  |  | } CRYPTO_REF_COUNT; | 
					
						
							| 
									
										
										
										
											2016-08-27 22:01:08 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-22 07:48:56 +08:00
										 |  |  | static __inline__ int CRYPTO_UP_REF(CRYPTO_REF_COUNT *refcnt, int *ret) | 
					
						
							| 
									
										
										
										
											2016-08-27 22:01:08 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-06-22 07:48:56 +08:00
										 |  |  |     *ret = __atomic_fetch_add(&refcnt->val, 1, __ATOMIC_RELAXED) + 1; | 
					
						
							| 
									
										
										
										
											2016-08-27 22:01:08 +08:00
										 |  |  |     return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-22 07:48:56 +08:00
										 |  |  | static __inline__ int CRYPTO_DOWN_REF(CRYPTO_REF_COUNT *refcnt, int *ret) | 
					
						
							| 
									
										
										
										
											2016-08-27 22:01:08 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-06-22 07:48:56 +08:00
										 |  |  |     *ret = __atomic_fetch_sub(&refcnt->val, 1, __ATOMIC_RELAXED) - 1; | 
					
						
							| 
									
										
										
										
											2016-08-27 22:01:08 +08:00
										 |  |  |     if (*ret == 0) | 
					
						
							|  |  |  |         __atomic_thread_fence(__ATOMIC_ACQUIRE); | 
					
						
							|  |  |  |     return 1; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2023-04-27 22:52:44 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-22 07:48:56 +08:00
										 |  |  | static __inline__ int CRYPTO_GET_REF(CRYPTO_REF_COUNT *refcnt, int *ret) | 
					
						
							| 
									
										
										
										
											2023-04-27 22:52:44 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-06-22 07:48:56 +08:00
										 |  |  |     *ret = __atomic_load_n(&refcnt->val, __ATOMIC_RELAXED); | 
					
						
							| 
									
										
										
										
											2023-04-27 22:52:44 +08:00
										 |  |  |     return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-18 13:26:19 +08:00
										 |  |  | #  elif defined(__ICL) && defined(_WIN32)
 | 
					
						
							|  |  |  | #   define HAVE_ATOMICS 1
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-22 07:48:56 +08:00
										 |  |  | typedef struct { | 
					
						
							|  |  |  |     volatile int val; | 
					
						
							|  |  |  | } CRYPTO_REF_COUNT; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static __inline int CRYPTO_UP_REF(CRYPTO_REF_COUNT *refcnt, int *ret) | 
					
						
							| 
									
										
										
										
											2019-09-18 13:26:19 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-06-22 07:48:56 +08:00
										 |  |  |     *ret = _InterlockedExchangeAdd((void *)&refcnt->val, 1) + 1; | 
					
						
							| 
									
										
										
										
											2019-09-18 13:26:19 +08:00
										 |  |  |     return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-22 07:48:56 +08:00
										 |  |  | static __inline int CRYPTO_DOWN_REF(CRYPTO_REF_COUNT *val, int *refcnt) | 
					
						
							| 
									
										
										
										
											2019-09-18 13:26:19 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-06-22 07:48:56 +08:00
										 |  |  |     *ret = _InterlockedExchangeAdd((void *)&refcnt->val, -1) - 1; | 
					
						
							| 
									
										
										
										
											2019-09-18 13:26:19 +08:00
										 |  |  |     return 1; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2016-08-27 22:01:08 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-22 07:48:56 +08:00
										 |  |  | static __inline int CRYPTO_GET_REF(CRYPTO_REF_COUNT *refcnt, int *ret) | 
					
						
							| 
									
										
										
										
											2023-04-27 22:52:44 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-06-22 07:48:56 +08:00
										 |  |  |     *ret = _InterlockedOr((void *)&refcnt->val, 0); | 
					
						
							| 
									
										
										
										
											2023-04-27 22:52:44 +08:00
										 |  |  |     return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-14 16:59:00 +08:00
										 |  |  | #  elif defined(_MSC_VER) && _MSC_VER>=1200
 | 
					
						
							| 
									
										
										
										
											2018-08-08 17:10:11 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-14 16:59:00 +08:00
										 |  |  | #   define HAVE_ATOMICS 1
 | 
					
						
							| 
									
										
										
										
											2018-08-08 17:10:11 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-22 07:48:56 +08:00
										 |  |  | typedef struct { | 
					
						
							|  |  |  |     volatile int val; | 
					
						
							|  |  |  | } CRYPTO_REF_COUNT; | 
					
						
							| 
									
										
										
										
											2018-08-08 17:10:11 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-27 14:55:32 +08:00
										 |  |  | #   if (defined(_M_ARM) && _M_ARM>=7 && !defined(_WIN32_WCE)) || defined(_M_ARM64)
 | 
					
						
							| 
									
										
										
										
											2019-03-14 16:59:00 +08:00
										 |  |  | #    include <intrin.h>
 | 
					
						
							|  |  |  | #    if defined(_M_ARM64) && !defined(_ARM_BARRIER_ISH)
 | 
					
						
							|  |  |  | #     define _ARM_BARRIER_ISH _ARM64_BARRIER_ISH
 | 
					
						
							|  |  |  | #    endif
 | 
					
						
							| 
									
										
										
										
											2018-08-08 17:10:11 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-22 07:48:56 +08:00
										 |  |  | static __inline int CRYPTO_UP_REF(CRYPTO_REF_COUNT *refcnt, int *ret) | 
					
						
							| 
									
										
										
										
											2018-08-08 17:10:11 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-06-22 07:48:56 +08:00
										 |  |  |     *ret = _InterlockedExchangeAdd_nf(&refcnt->val, 1) + 1; | 
					
						
							| 
									
										
										
										
											2018-08-08 17:10:11 +08:00
										 |  |  |     return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-22 07:48:56 +08:00
										 |  |  | static __inline int CRYPTO_DOWN_REF(CRYPTO_REF_COUNT *refcnt, int *ret) | 
					
						
							| 
									
										
										
										
											2018-08-08 17:10:11 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-06-22 07:48:56 +08:00
										 |  |  |     *ret = _InterlockedExchangeAdd_nf(&refcnt->val, -1) - 1; | 
					
						
							| 
									
										
										
										
											2018-08-08 17:10:11 +08:00
										 |  |  |     if (*ret == 0) | 
					
						
							|  |  |  |         __dmb(_ARM_BARRIER_ISH); | 
					
						
							|  |  |  |     return 1; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2023-04-27 22:52:44 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-22 07:48:56 +08:00
										 |  |  | static __inline int CRYPTO_GET_REF(CRYPTO_REF_COUNT *refcnt, int *ret) | 
					
						
							| 
									
										
										
										
											2023-04-27 22:52:44 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-06-22 07:48:56 +08:00
										 |  |  |     *ret = _InterlockedOr_nf((void *)&refcnt->val, 0); | 
					
						
							| 
									
										
										
										
											2023-04-27 22:52:44 +08:00
										 |  |  |     return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-14 16:59:00 +08:00
										 |  |  | #   else
 | 
					
						
							| 
									
										
										
										
											2019-03-27 14:55:32 +08:00
										 |  |  | #    if !defined(_WIN32_WCE)
 | 
					
						
							|  |  |  | #     pragma intrinsic(_InterlockedExchangeAdd)
 | 
					
						
							|  |  |  | #    else
 | 
					
						
							|  |  |  | #     if _WIN32_WCE >= 0x600
 | 
					
						
							|  |  |  |        extern long __cdecl _InterlockedExchangeAdd(long volatile*, long); | 
					
						
							|  |  |  | #     else
 | 
					
						
							| 
									
										
										
										
											2019-04-07 19:30:26 +08:00
										 |  |  |        /* under Windows CE we still have old-style Interlocked* functions */ | 
					
						
							| 
									
										
										
										
											2019-03-27 14:55:32 +08:00
										 |  |  |        extern long __cdecl InterlockedExchangeAdd(long volatile*, long); | 
					
						
							|  |  |  | #      define _InterlockedExchangeAdd InterlockedExchangeAdd
 | 
					
						
							|  |  |  | #     endif
 | 
					
						
							|  |  |  | #    endif
 | 
					
						
							| 
									
										
										
										
											2018-08-08 17:10:11 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-22 07:48:56 +08:00
										 |  |  | static __inline int CRYPTO_UP_REF(CRYPTO_REF_COUNT *refcnt, int *ret) | 
					
						
							| 
									
										
										
										
											2018-08-08 17:10:11 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-06-22 07:48:56 +08:00
										 |  |  |     *ret = _InterlockedExchangeAdd(&refcnt->val, 1) + 1; | 
					
						
							| 
									
										
										
										
											2018-08-08 17:10:11 +08:00
										 |  |  |     return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-22 07:48:56 +08:00
										 |  |  | static __inline int CRYPTO_DOWN_REF(CRYPTO_REF_COUNT *refcnt, int *ret) | 
					
						
							| 
									
										
										
										
											2018-08-08 17:10:11 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-06-22 07:48:56 +08:00
										 |  |  |     *ret = _InterlockedExchangeAdd(&refcnt->val, -1) - 1; | 
					
						
							| 
									
										
										
										
											2018-08-08 17:10:11 +08:00
										 |  |  |     return 1; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2023-04-27 22:52:44 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-22 07:48:56 +08:00
										 |  |  | static __inline int CRYPTO_GET_REF(CRYPTO_REF_COUNT *refcnt, int *ret) | 
					
						
							| 
									
										
										
										
											2023-04-27 22:52:44 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-06-22 07:48:56 +08:00
										 |  |  |     *ret = _InterlockedExchangeAdd(&refcnt->val, 0); | 
					
						
							| 
									
										
										
										
											2023-04-27 22:52:44 +08:00
										 |  |  |     return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-14 16:59:00 +08:00
										 |  |  | #   endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-08 17:10:11 +08:00
										 |  |  | #  endif
 | 
					
						
							| 
									
										
										
										
											2019-03-14 16:59:00 +08:00
										 |  |  | # endif  /* !OPENSSL_DEV_NO_ATOMICS */
 | 
					
						
							| 
									
										
										
										
											2018-08-08 17:10:11 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-14 16:59:00 +08:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * All the refcounting implementations above define HAVE_ATOMICS, so if it's | 
					
						
							| 
									
										
										
										
											2020-06-20 05:33:24 +08:00
										 |  |  |  * still undefined here (such as when OPENSSL_DEV_NO_ATOMICS is defined), it | 
					
						
							| 
									
										
										
										
											2019-03-14 16:59:00 +08:00
										 |  |  |  * means we need to implement a fallback.  This fallback uses locks. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | # ifndef HAVE_ATOMICS
 | 
					
						
							| 
									
										
										
										
											2016-08-27 22:01:08 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-22 07:48:56 +08:00
										 |  |  | typedef struct { | 
					
						
							|  |  |  |     int val; | 
					
						
							|  |  |  | #  ifdef OPENSSL_THREADS
 | 
					
						
							|  |  |  |     CRYPTO_RWLOCK *lock; | 
					
						
							|  |  |  | #  endif
 | 
					
						
							|  |  |  | } CRYPTO_REF_COUNT; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #  ifdef OPENSSL_THREADS
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static ossl_unused ossl_inline int CRYPTO_UP_REF(CRYPTO_REF_COUNT *refcnt,  | 
					
						
							|  |  |  |                                                  int *ret) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return CRYPTO_atomic_add(&refcnt->val, 1, ret, refcnt->lock); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static ossl_unused ossl_inline int CRYPTO_DOWN_REF(CRYPTO_REF_COUNT *refcnt, | 
					
						
							|  |  |  |                                                    int *ret) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return CRYPTO_atomic_add(&refcnt->val, -1, ret, refcnt->lock); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static ossl_unused ossl_inline int CRYPTO_GET_REF(CRYPTO_REF_COUNT *refcnt, | 
					
						
							|  |  |  |                                                    int *ret) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return CRYPTO_atomic_load_int(&refcnt->val, ret, refcnt->lock); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #   define CRYPTO_NEW_FREE_DEFINED  1
 | 
					
						
							|  |  |  | static ossl_unused ossl_inline int CRYPTO_NEW_REF(CRYPTO_REF_COUNT *refcnt, int n) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     refcnt->val = n; | 
					
						
							|  |  |  |     refcnt->lock = CRYPTO_THREAD_lock_new(); | 
					
						
							|  |  |  |     if (refcnt->lock == NULL) { | 
					
						
							|  |  |  |         ERR_raise(ERR_LIB_CRYPTO, ERR_R_CRYPTO_LIB); | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static ossl_unused ossl_inline void CRYPTO_FREE_REF(CRYPTO_REF_COUNT *refcnt)                                  \ | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2023-07-02 15:19:17 +08:00
										 |  |  |     if (refcnt != NULL) | 
					
						
							|  |  |  |         CRYPTO_THREAD_lock_free(refcnt->lock); | 
					
						
							| 
									
										
										
										
											2023-06-22 07:48:56 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #  else     /* OPENSSL_THREADS */
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static ossl_unused ossl_inline int CRYPTO_UP_REF(CRYPTO_REF_COUNT *refcnt,  | 
					
						
							|  |  |  |                                                  int *ret) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     refcnt->val++; | 
					
						
							|  |  |  |     *ret = refcnt->val; | 
					
						
							|  |  |  |     return 1; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2016-08-27 22:01:08 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-22 07:48:56 +08:00
										 |  |  | static ossl_unused ossl_inline int CRYPTO_DOWN_REF(CRYPTO_REF_COUNT *refcnt, | 
					
						
							|  |  |  |                                                    int *ret) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     refcnt->val--; | 
					
						
							|  |  |  |     *ret = refcnt->val; | 
					
						
							|  |  |  |     return 1; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2016-08-27 22:01:08 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-22 07:48:56 +08:00
										 |  |  | static ossl_unused ossl_inline int CRYPTO_GET_REF(CRYPTO_REF_COUNT *refcnt, | 
					
						
							|  |  |  |                                                    int *ret) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     *ret = refcnt->val; | 
					
						
							|  |  |  |     return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #  endif    /* OPENSSL_THREADS */
 | 
					
						
							| 
									
										
										
										
											2016-08-27 22:01:08 +08:00
										 |  |  | # endif
 | 
					
						
							| 
									
										
										
										
											2017-08-22 05:17:35 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-22 07:48:56 +08:00
										 |  |  | # ifndef CRYPTO_NEW_FREE_DEFINED
 | 
					
						
							|  |  |  | static ossl_unused ossl_inline int CRYPTO_NEW_REF(CRYPTO_REF_COUNT *refcnt, int n) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     refcnt->val = n; | 
					
						
							|  |  |  |     return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static ossl_unused ossl_inline void CRYPTO_FREE_REF(CRYPTO_REF_COUNT *refcnt)                                  \ | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | # endif /* CRYPTO_NEW_FREE_DEFINED */
 | 
					
						
							|  |  |  | #undef CRYPTO_NEW_FREE_DEFINED
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-22 05:17:35 +08:00
										 |  |  | # if !defined(NDEBUG) && !defined(OPENSSL_NO_STDIO)
 | 
					
						
							|  |  |  | #  define REF_ASSERT_ISNT(test) \
 | 
					
						
							|  |  |  |     (void)((test) ? (OPENSSL_die("refcount error", __FILE__, __LINE__), 1) : 0) | 
					
						
							|  |  |  | # else
 | 
					
						
							|  |  |  | #  define REF_ASSERT_ISNT(i)
 | 
					
						
							|  |  |  | # endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-19 23:09:49 +08:00
										 |  |  | # define REF_PRINT_EX(text, count, object) \
 | 
					
						
							|  |  |  |     OSSL_TRACE3(REF_COUNT, "%p:%4d:%s\n", (object), (count), (text)); | 
					
						
							|  |  |  | # define REF_PRINT_COUNT(text, object) \
 | 
					
						
							| 
									
										
										
										
											2023-06-22 07:48:56 +08:00
										 |  |  |     REF_PRINT_EX(text, object->references.val, (void *)object) | 
					
						
							| 
									
										
										
										
											2017-08-22 05:17:35 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-27 22:01:08 +08:00
										 |  |  | #endif
 |