1e1051a39Sopenharmony_ci/*
2e1051a39Sopenharmony_ci * Copyright 2016-2019 The OpenSSL Project Authors. All Rights Reserved.
3e1051a39Sopenharmony_ci *
4e1051a39Sopenharmony_ci * Licensed under the OpenSSL license (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
12e1051a39Sopenharmony_ci/* Used to checking reference counts, most while doing perl5 stuff :-) */
13e1051a39Sopenharmony_ci# if defined(OPENSSL_NO_STDIO)
14e1051a39Sopenharmony_ci#  if defined(REF_PRINT)
15e1051a39Sopenharmony_ci#   error "REF_PRINT requires stdio"
16e1051a39Sopenharmony_ci#  endif
17e1051a39Sopenharmony_ci# endif
18e1051a39Sopenharmony_ci
19e1051a39Sopenharmony_ci# if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L \
20e1051a39Sopenharmony_ci     && !defined(__STDC_NO_ATOMICS__)
21e1051a39Sopenharmony_ci#  include <stdatomic.h>
22e1051a39Sopenharmony_ci#  define HAVE_C11_ATOMICS
23e1051a39Sopenharmony_ci# endif
24e1051a39Sopenharmony_ci
25e1051a39Sopenharmony_ci# if defined(HAVE_C11_ATOMICS) && defined(ATOMIC_INT_LOCK_FREE) \
26e1051a39Sopenharmony_ci     && ATOMIC_INT_LOCK_FREE > 0
27e1051a39Sopenharmony_ci
28e1051a39Sopenharmony_ci#  define HAVE_ATOMICS 1
29e1051a39Sopenharmony_ci
30e1051a39Sopenharmony_citypedef _Atomic int CRYPTO_REF_COUNT;
31e1051a39Sopenharmony_ci
32e1051a39Sopenharmony_cistatic inline int CRYPTO_UP_REF(_Atomic int *val, int *ret, void *lock)
33e1051a39Sopenharmony_ci{
34e1051a39Sopenharmony_ci    *ret = atomic_fetch_add_explicit(val, 1, memory_order_relaxed) + 1;
35e1051a39Sopenharmony_ci    return 1;
36e1051a39Sopenharmony_ci}
37e1051a39Sopenharmony_ci
38e1051a39Sopenharmony_ci/*
39e1051a39Sopenharmony_ci * Changes to shared structure other than reference counter have to be
40e1051a39Sopenharmony_ci * serialized. And any kind of serialization implies a release fence. This
41e1051a39Sopenharmony_ci * means that by the time reference counter is decremented all other
42e1051a39Sopenharmony_ci * changes are visible on all processors. Hence decrement itself can be
43e1051a39Sopenharmony_ci * relaxed. In case it hits zero, object will be destructed. Since it's
44e1051a39Sopenharmony_ci * last use of the object, destructor programmer might reason that access
45e1051a39Sopenharmony_ci * to mutable members doesn't have to be serialized anymore, which would
46e1051a39Sopenharmony_ci * otherwise imply an acquire fence. Hence conditional acquire fence...
47e1051a39Sopenharmony_ci */
48e1051a39Sopenharmony_cistatic inline int CRYPTO_DOWN_REF(_Atomic int *val, int *ret, 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, 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, void *lock)
69e1051a39Sopenharmony_ci{
70e1051a39Sopenharmony_ci    *ret = __atomic_fetch_sub(val, 1, __ATOMIC_RELAXED) - 1;
71e1051a39Sopenharmony_ci    if (*ret == 0)
72e1051a39Sopenharmony_ci        __atomic_thread_fence(__ATOMIC_ACQUIRE);
73e1051a39Sopenharmony_ci    return 1;
74e1051a39Sopenharmony_ci}
75e1051a39Sopenharmony_ci
76e1051a39Sopenharmony_ci# elif defined(_MSC_VER) && _MSC_VER>=1200
77e1051a39Sopenharmony_ci
78e1051a39Sopenharmony_ci#  define HAVE_ATOMICS 1
79e1051a39Sopenharmony_ci
80e1051a39Sopenharmony_citypedef volatile int CRYPTO_REF_COUNT;
81e1051a39Sopenharmony_ci
82e1051a39Sopenharmony_ci#  if (defined(_M_ARM) && _M_ARM>=7 && !defined(_WIN32_WCE)) || defined(_M_ARM64)
83e1051a39Sopenharmony_ci#   include <intrin.h>
84e1051a39Sopenharmony_ci#   if defined(_M_ARM64) && !defined(_ARM_BARRIER_ISH)
85e1051a39Sopenharmony_ci#    define _ARM_BARRIER_ISH _ARM64_BARRIER_ISH
86e1051a39Sopenharmony_ci#   endif
87e1051a39Sopenharmony_ci
88e1051a39Sopenharmony_cistatic __inline int CRYPTO_UP_REF(volatile int *val, int *ret, void *lock)
89e1051a39Sopenharmony_ci{
90e1051a39Sopenharmony_ci    *ret = _InterlockedExchangeAdd_nf(val, 1) + 1;
91e1051a39Sopenharmony_ci    return 1;
92e1051a39Sopenharmony_ci}
93e1051a39Sopenharmony_ci
94e1051a39Sopenharmony_cistatic __inline int CRYPTO_DOWN_REF(volatile int *val, int *ret, void *lock)
95e1051a39Sopenharmony_ci{
96e1051a39Sopenharmony_ci    *ret = _InterlockedExchangeAdd_nf(val, -1) - 1;
97e1051a39Sopenharmony_ci    if (*ret == 0)
98e1051a39Sopenharmony_ci        __dmb(_ARM_BARRIER_ISH);
99e1051a39Sopenharmony_ci    return 1;
100e1051a39Sopenharmony_ci}
101e1051a39Sopenharmony_ci#  else
102e1051a39Sopenharmony_ci#   if !defined(_WIN32_WCE)
103e1051a39Sopenharmony_ci#    pragma intrinsic(_InterlockedExchangeAdd)
104e1051a39Sopenharmony_ci#   else
105e1051a39Sopenharmony_ci#    if _WIN32_WCE >= 0x600
106e1051a39Sopenharmony_ci      extern long __cdecl _InterlockedExchangeAdd(long volatile*, long);
107e1051a39Sopenharmony_ci#    else
108e1051a39Sopenharmony_ci      /* under Windows CE we still have old-style Interlocked* functions */
109e1051a39Sopenharmony_ci      extern long __cdecl InterlockedExchangeAdd(long volatile*, long);
110e1051a39Sopenharmony_ci#     define _InterlockedExchangeAdd InterlockedExchangeAdd
111e1051a39Sopenharmony_ci#    endif
112e1051a39Sopenharmony_ci#   endif
113e1051a39Sopenharmony_ci
114e1051a39Sopenharmony_cistatic __inline int CRYPTO_UP_REF(volatile int *val, int *ret, void *lock)
115e1051a39Sopenharmony_ci{
116e1051a39Sopenharmony_ci    *ret = _InterlockedExchangeAdd(val, 1) + 1;
117e1051a39Sopenharmony_ci    return 1;
118e1051a39Sopenharmony_ci}
119e1051a39Sopenharmony_ci
120e1051a39Sopenharmony_cistatic __inline int CRYPTO_DOWN_REF(volatile int *val, int *ret, void *lock)
121e1051a39Sopenharmony_ci{
122e1051a39Sopenharmony_ci    *ret = _InterlockedExchangeAdd(val, -1) - 1;
123e1051a39Sopenharmony_ci    return 1;
124e1051a39Sopenharmony_ci}
125e1051a39Sopenharmony_ci#  endif
126e1051a39Sopenharmony_ci
127e1051a39Sopenharmony_ci# else
128e1051a39Sopenharmony_ci
129e1051a39Sopenharmony_citypedef int CRYPTO_REF_COUNT;
130e1051a39Sopenharmony_ci
131e1051a39Sopenharmony_ci# define CRYPTO_UP_REF(val, ret, lock) CRYPTO_atomic_add(val, 1, ret, lock)
132e1051a39Sopenharmony_ci# define CRYPTO_DOWN_REF(val, ret, lock) CRYPTO_atomic_add(val, -1, ret, lock)
133e1051a39Sopenharmony_ci
134e1051a39Sopenharmony_ci# endif
135e1051a39Sopenharmony_ci
136e1051a39Sopenharmony_ci# if !defined(NDEBUG) && !defined(OPENSSL_NO_STDIO)
137e1051a39Sopenharmony_ci#  define REF_ASSERT_ISNT(test) \
138e1051a39Sopenharmony_ci    (void)((test) ? (OPENSSL_die("refcount error", __FILE__, __LINE__), 1) : 0)
139e1051a39Sopenharmony_ci# else
140e1051a39Sopenharmony_ci#  define REF_ASSERT_ISNT(i)
141e1051a39Sopenharmony_ci# endif
142e1051a39Sopenharmony_ci
143e1051a39Sopenharmony_ci# ifdef REF_PRINT
144e1051a39Sopenharmony_ci#  define REF_PRINT_COUNT(a, b) \
145e1051a39Sopenharmony_ci        fprintf(stderr, "%p:%4d:%s\n", b, b->references, a)
146e1051a39Sopenharmony_ci# else
147e1051a39Sopenharmony_ci#  define REF_PRINT_COUNT(a, b)
148e1051a39Sopenharmony_ci# endif
149e1051a39Sopenharmony_ci
150e1051a39Sopenharmony_ci#endif
151