1e1051a39Sopenharmony_ci/*
2e1051a39Sopenharmony_ci * Copyright 2016-2023 The OpenSSL Project Authors. All Rights Reserved.
3e1051a39Sopenharmony_ci *
4e1051a39Sopenharmony_ci * Licensed under the Apache License 2.0 (the "License").  You may not use
5e1051a39Sopenharmony_ci * this file except in compliance with the License.  You can obtain a copy
6e1051a39Sopenharmony_ci * in the file LICENSE in the source distribution or at
7e1051a39Sopenharmony_ci * https://www.openssl.org/source/license.html
8e1051a39Sopenharmony_ci */
9e1051a39Sopenharmony_ci#ifndef OSSL_INTERNAL_REFCOUNT_H
10e1051a39Sopenharmony_ci# define OSSL_INTERNAL_REFCOUNT_H
11e1051a39Sopenharmony_ci# pragma once
12e1051a39Sopenharmony_ci
13e1051a39Sopenharmony_ci# include <openssl/e_os2.h>
14e1051a39Sopenharmony_ci# include <openssl/trace.h>
15e1051a39Sopenharmony_ci
16e1051a39Sopenharmony_ci# if defined(OPENSSL_THREADS) && !defined(OPENSSL_DEV_NO_ATOMICS)
17e1051a39Sopenharmony_ci#  if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L \
18e1051a39Sopenharmony_ci      && !defined(__STDC_NO_ATOMICS__)
19e1051a39Sopenharmony_ci#   include <stdatomic.h>
20e1051a39Sopenharmony_ci#   define HAVE_C11_ATOMICS
21e1051a39Sopenharmony_ci#  endif
22e1051a39Sopenharmony_ci
23e1051a39Sopenharmony_ci#  if defined(HAVE_C11_ATOMICS) && defined(ATOMIC_INT_LOCK_FREE) \
24e1051a39Sopenharmony_ci      && ATOMIC_INT_LOCK_FREE > 0
25e1051a39Sopenharmony_ci
26e1051a39Sopenharmony_ci#   define HAVE_ATOMICS 1
27e1051a39Sopenharmony_ci
28e1051a39Sopenharmony_citypedef _Atomic int CRYPTO_REF_COUNT;
29e1051a39Sopenharmony_ci
30e1051a39Sopenharmony_cistatic inline int CRYPTO_UP_REF(_Atomic int *val, int *ret,
31e1051a39Sopenharmony_ci                                ossl_unused void *lock)
32e1051a39Sopenharmony_ci{
33e1051a39Sopenharmony_ci    *ret = atomic_fetch_add_explicit(val, 1, memory_order_relaxed) + 1;
34e1051a39Sopenharmony_ci    return 1;
35e1051a39Sopenharmony_ci}
36e1051a39Sopenharmony_ci
37e1051a39Sopenharmony_ci/*
38e1051a39Sopenharmony_ci * Changes to shared structure other than reference counter have to be
39e1051a39Sopenharmony_ci * serialized. And any kind of serialization implies a release fence. This
40e1051a39Sopenharmony_ci * means that by the time reference counter is decremented all other
41e1051a39Sopenharmony_ci * changes are visible on all processors. Hence decrement itself can be
42e1051a39Sopenharmony_ci * relaxed. In case it hits zero, object will be destructed. Since it's
43e1051a39Sopenharmony_ci * last use of the object, destructor programmer might reason that access
44e1051a39Sopenharmony_ci * to mutable members doesn't have to be serialized anymore, which would
45e1051a39Sopenharmony_ci * otherwise imply an acquire fence. Hence conditional acquire fence...
46e1051a39Sopenharmony_ci */
47e1051a39Sopenharmony_cistatic inline int CRYPTO_DOWN_REF(_Atomic int *val, int *ret,
48e1051a39Sopenharmony_ci                                  ossl_unused void *lock)
49e1051a39Sopenharmony_ci{
50e1051a39Sopenharmony_ci    *ret = atomic_fetch_sub_explicit(val, 1, memory_order_relaxed) - 1;
51e1051a39Sopenharmony_ci    if (*ret == 0)
52e1051a39Sopenharmony_ci        atomic_thread_fence(memory_order_acquire);
53e1051a39Sopenharmony_ci    return 1;
54e1051a39Sopenharmony_ci}
55e1051a39Sopenharmony_ci
56e1051a39Sopenharmony_ci#  elif defined(__GNUC__) && defined(__ATOMIC_RELAXED) && __GCC_ATOMIC_INT_LOCK_FREE > 0
57e1051a39Sopenharmony_ci
58e1051a39Sopenharmony_ci#   define HAVE_ATOMICS 1
59e1051a39Sopenharmony_ci
60e1051a39Sopenharmony_citypedef int CRYPTO_REF_COUNT;
61e1051a39Sopenharmony_ci
62e1051a39Sopenharmony_cistatic __inline__ int CRYPTO_UP_REF(int *val, int *ret, ossl_unused void *lock)
63e1051a39Sopenharmony_ci{
64e1051a39Sopenharmony_ci    *ret = __atomic_fetch_add(val, 1, __ATOMIC_RELAXED) + 1;
65e1051a39Sopenharmony_ci    return 1;
66e1051a39Sopenharmony_ci}
67e1051a39Sopenharmony_ci
68e1051a39Sopenharmony_cistatic __inline__ int CRYPTO_DOWN_REF(int *val, int *ret,
69e1051a39Sopenharmony_ci                                      ossl_unused void *lock)
70e1051a39Sopenharmony_ci{
71e1051a39Sopenharmony_ci    *ret = __atomic_fetch_sub(val, 1, __ATOMIC_RELAXED) - 1;
72e1051a39Sopenharmony_ci    if (*ret == 0)
73e1051a39Sopenharmony_ci        __atomic_thread_fence(__ATOMIC_ACQUIRE);
74e1051a39Sopenharmony_ci    return 1;
75e1051a39Sopenharmony_ci}
76e1051a39Sopenharmony_ci#  elif defined(__ICL) && defined(_WIN32)
77e1051a39Sopenharmony_ci#   define HAVE_ATOMICS 1
78e1051a39Sopenharmony_citypedef volatile int CRYPTO_REF_COUNT;
79e1051a39Sopenharmony_ci
80e1051a39Sopenharmony_cistatic __inline int CRYPTO_UP_REF(volatile int *val, int *ret,
81e1051a39Sopenharmony_ci                                  ossl_unused void *lock)
82e1051a39Sopenharmony_ci{
83e1051a39Sopenharmony_ci    *ret = _InterlockedExchangeAdd((void *)val, 1) + 1;
84e1051a39Sopenharmony_ci    return 1;
85e1051a39Sopenharmony_ci}
86e1051a39Sopenharmony_ci
87e1051a39Sopenharmony_cistatic __inline int CRYPTO_DOWN_REF(volatile int *val, int *ret,
88e1051a39Sopenharmony_ci                                    ossl_unused void *lock)
89e1051a39Sopenharmony_ci{
90e1051a39Sopenharmony_ci    *ret = _InterlockedExchangeAdd((void *)val, -1) - 1;
91e1051a39Sopenharmony_ci    return 1;
92e1051a39Sopenharmony_ci}
93e1051a39Sopenharmony_ci
94e1051a39Sopenharmony_ci#  elif defined(_MSC_VER) && _MSC_VER>=1200
95e1051a39Sopenharmony_ci
96e1051a39Sopenharmony_ci#   define HAVE_ATOMICS 1
97e1051a39Sopenharmony_ci
98e1051a39Sopenharmony_citypedef volatile int CRYPTO_REF_COUNT;
99e1051a39Sopenharmony_ci
100e1051a39Sopenharmony_ci#   if (defined(_M_ARM) && _M_ARM>=7 && !defined(_WIN32_WCE)) || defined(_M_ARM64)
101e1051a39Sopenharmony_ci#    include <intrin.h>
102e1051a39Sopenharmony_ci#    if defined(_M_ARM64) && !defined(_ARM_BARRIER_ISH)
103e1051a39Sopenharmony_ci#     define _ARM_BARRIER_ISH _ARM64_BARRIER_ISH
104e1051a39Sopenharmony_ci#    endif
105e1051a39Sopenharmony_ci
106e1051a39Sopenharmony_cistatic __inline int CRYPTO_UP_REF(volatile int *val, int *ret,
107e1051a39Sopenharmony_ci                                  ossl_unused void *lock)
108e1051a39Sopenharmony_ci{
109e1051a39Sopenharmony_ci    *ret = _InterlockedExchangeAdd_nf(val, 1) + 1;
110e1051a39Sopenharmony_ci    return 1;
111e1051a39Sopenharmony_ci}
112e1051a39Sopenharmony_ci
113e1051a39Sopenharmony_cistatic __inline int CRYPTO_DOWN_REF(volatile int *val, int *ret,
114e1051a39Sopenharmony_ci                                    ossl_unused void *lock)
115e1051a39Sopenharmony_ci{
116e1051a39Sopenharmony_ci    *ret = _InterlockedExchangeAdd_nf(val, -1) - 1;
117e1051a39Sopenharmony_ci    if (*ret == 0)
118e1051a39Sopenharmony_ci        __dmb(_ARM_BARRIER_ISH);
119e1051a39Sopenharmony_ci    return 1;
120e1051a39Sopenharmony_ci}
121e1051a39Sopenharmony_ci#   else
122e1051a39Sopenharmony_ci#    if !defined(_WIN32_WCE)
123e1051a39Sopenharmony_ci#     pragma intrinsic(_InterlockedExchangeAdd)
124e1051a39Sopenharmony_ci#    else
125e1051a39Sopenharmony_ci#     if _WIN32_WCE >= 0x600
126e1051a39Sopenharmony_ci       extern long __cdecl _InterlockedExchangeAdd(long volatile*, long);
127e1051a39Sopenharmony_ci#     else
128e1051a39Sopenharmony_ci       /* under Windows CE we still have old-style Interlocked* functions */
129e1051a39Sopenharmony_ci       extern long __cdecl InterlockedExchangeAdd(long volatile*, long);
130e1051a39Sopenharmony_ci#      define _InterlockedExchangeAdd InterlockedExchangeAdd
131e1051a39Sopenharmony_ci#     endif
132e1051a39Sopenharmony_ci#    endif
133e1051a39Sopenharmony_ci
134e1051a39Sopenharmony_cistatic __inline int CRYPTO_UP_REF(volatile int *val, int *ret,
135e1051a39Sopenharmony_ci                                  ossl_unused void *lock)
136e1051a39Sopenharmony_ci{
137e1051a39Sopenharmony_ci    *ret = _InterlockedExchangeAdd(val, 1) + 1;
138e1051a39Sopenharmony_ci    return 1;
139e1051a39Sopenharmony_ci}
140e1051a39Sopenharmony_ci
141e1051a39Sopenharmony_cistatic __inline int CRYPTO_DOWN_REF(volatile int *val, int *ret,
142e1051a39Sopenharmony_ci                                    ossl_unused void *lock)
143e1051a39Sopenharmony_ci{
144e1051a39Sopenharmony_ci    *ret = _InterlockedExchangeAdd(val, -1) - 1;
145e1051a39Sopenharmony_ci    return 1;
146e1051a39Sopenharmony_ci}
147e1051a39Sopenharmony_ci#   endif
148e1051a39Sopenharmony_ci
149e1051a39Sopenharmony_ci#  endif
150e1051a39Sopenharmony_ci# endif  /* !OPENSSL_DEV_NO_ATOMICS */
151e1051a39Sopenharmony_ci
152e1051a39Sopenharmony_ci/*
153e1051a39Sopenharmony_ci * All the refcounting implementations above define HAVE_ATOMICS, so if it's
154e1051a39Sopenharmony_ci * still undefined here (such as when OPENSSL_DEV_NO_ATOMICS is defined), it
155e1051a39Sopenharmony_ci * means we need to implement a fallback.  This fallback uses locks.
156e1051a39Sopenharmony_ci */
157e1051a39Sopenharmony_ci# ifndef HAVE_ATOMICS
158e1051a39Sopenharmony_ci
159e1051a39Sopenharmony_citypedef int CRYPTO_REF_COUNT;
160e1051a39Sopenharmony_ci
161e1051a39Sopenharmony_ci# define CRYPTO_UP_REF(val, ret, lock) CRYPTO_atomic_add(val, 1, ret, lock)
162e1051a39Sopenharmony_ci# define CRYPTO_DOWN_REF(val, ret, lock) CRYPTO_atomic_add(val, -1, ret, lock)
163e1051a39Sopenharmony_ci
164e1051a39Sopenharmony_ci# endif
165e1051a39Sopenharmony_ci
166e1051a39Sopenharmony_ci# if !defined(NDEBUG) && !defined(OPENSSL_NO_STDIO)
167e1051a39Sopenharmony_ci#  define REF_ASSERT_ISNT(test) \
168e1051a39Sopenharmony_ci    (void)((test) ? (OPENSSL_die("refcount error", __FILE__, __LINE__), 1) : 0)
169e1051a39Sopenharmony_ci# else
170e1051a39Sopenharmony_ci#  define REF_ASSERT_ISNT(i)
171e1051a39Sopenharmony_ci# endif
172e1051a39Sopenharmony_ci
173e1051a39Sopenharmony_ci# define REF_PRINT_EX(text, count, object) \
174e1051a39Sopenharmony_ci    OSSL_TRACE3(REF_COUNT, "%p:%4d:%s\n", (object), (count), (text));
175e1051a39Sopenharmony_ci# define REF_PRINT_COUNT(text, object) \
176e1051a39Sopenharmony_ci    REF_PRINT_EX(text, object->references, (void *)object)
177e1051a39Sopenharmony_ci
178e1051a39Sopenharmony_ci#endif
179