REFCOUNT: Add support for querying refcount
[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 # include <openssl/trace.h>
15
16 # if defined(OPENSSL_THREADS) && !defined(OPENSSL_DEV_NO_ATOMICS)
17 #  if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L \
18       && !defined(__STDC_NO_ATOMICS__)
19 #   include <stdatomic.h>
20 #   define HAVE_C11_ATOMICS
21 #  endif
22
23 #  if defined(HAVE_C11_ATOMICS) && defined(ATOMIC_INT_LOCK_FREE) \
24       && ATOMIC_INT_LOCK_FREE > 0
25
26 #   define HAVE_ATOMICS 1
27
28 typedef _Atomic int CRYPTO_REF_COUNT;
29
30 static inline int CRYPTO_UP_REF(_Atomic int *val, int *ret,
31                                 ossl_unused void *lock)
32 {
33     *ret = atomic_fetch_add_explicit(val, 1, memory_order_relaxed) + 1;
34     return 1;
35 }
36
37 /*
38  * Changes to shared structure other than reference counter have to be
39  * serialized. And any kind of serialization implies a release fence. This
40  * means that by the time reference counter is decremented all other
41  * changes are visible on all processors. Hence decrement itself can be
42  * relaxed. In case it hits zero, object will be destructed. Since it's
43  * last use of the object, destructor programmer might reason that access
44  * to mutable members doesn't have to be serialized anymore, which would
45  * otherwise imply an acquire fence. Hence conditional acquire fence...
46  */
47 static inline int CRYPTO_DOWN_REF(_Atomic int *val, int *ret,
48                                   ossl_unused void *lock)
49 {
50     *ret = atomic_fetch_sub_explicit(val, 1, memory_order_relaxed) - 1;
51     if (*ret == 0)
52         atomic_thread_fence(memory_order_acquire);
53     return 1;
54 }
55
56 static inline int CRYPTO_GET_REF(_Atomic int *val, int *ret,
57                                  ossl_unused void *lock)
58 {
59     *ret = atomic_load_explicit(val, memory_order_relaxed);
60     return 1;
61 }
62
63 #  elif defined(__GNUC__) && defined(__ATOMIC_RELAXED) && __GCC_ATOMIC_INT_LOCK_FREE > 0
64
65 #   define HAVE_ATOMICS 1
66
67 typedef int CRYPTO_REF_COUNT;
68
69 static __inline__ int CRYPTO_UP_REF(int *val, int *ret, ossl_unused void *lock)
70 {
71     *ret = __atomic_fetch_add(val, 1, __ATOMIC_RELAXED) + 1;
72     return 1;
73 }
74
75 static __inline__ int CRYPTO_DOWN_REF(int *val, int *ret,
76                                       ossl_unused void *lock)
77 {
78     *ret = __atomic_fetch_sub(val, 1, __ATOMIC_RELAXED) - 1;
79     if (*ret == 0)
80         __atomic_thread_fence(__ATOMIC_ACQUIRE);
81     return 1;
82 }
83
84 static __inline__ int CRYPTO_GET_REF(int *val, int *ret, ossl_unused void *lock)
85 {
86     *ret = __atomic_load_n(val, __ATOMIC_RELAXED);
87     return 1;
88 }
89
90 #  elif defined(__ICL) && defined(_WIN32)
91 #   define HAVE_ATOMICS 1
92 typedef volatile int CRYPTO_REF_COUNT;
93
94 static __inline int CRYPTO_UP_REF(volatile int *val, int *ret,
95                                   ossl_unused void *lock)
96 {
97     *ret = _InterlockedExchangeAdd((void *)val, 1) + 1;
98     return 1;
99 }
100
101 static __inline int CRYPTO_DOWN_REF(volatile int *val, int *ret,
102                                     ossl_unused void *lock)
103 {
104     *ret = _InterlockedExchangeAdd((void *)val, -1) - 1;
105     return 1;
106 }
107
108 static __inline int CRYPTO_GET_REF(volatile int *val, int *ret,
109                                    ossl_unused void *lock)
110 {
111     *ret = _InterlockedOr((void *)val, 0);
112     return 1;
113 }
114
115 #  elif defined(_MSC_VER) && _MSC_VER>=1200
116
117 #   define HAVE_ATOMICS 1
118
119 typedef volatile int CRYPTO_REF_COUNT;
120
121 #   if (defined(_M_ARM) && _M_ARM>=7 && !defined(_WIN32_WCE)) || defined(_M_ARM64)
122 #    include <intrin.h>
123 #    if defined(_M_ARM64) && !defined(_ARM_BARRIER_ISH)
124 #     define _ARM_BARRIER_ISH _ARM64_BARRIER_ISH
125 #    endif
126
127 static __inline int CRYPTO_UP_REF(volatile int *val, int *ret,
128                                   ossl_unused void *lock)
129 {
130     *ret = _InterlockedExchangeAdd_nf(val, 1) + 1;
131     return 1;
132 }
133
134 static __inline int CRYPTO_DOWN_REF(volatile int *val, int *ret,
135                                     ossl_unused void *lock)
136 {
137     *ret = _InterlockedExchangeAdd_nf(val, -1) - 1;
138     if (*ret == 0)
139         __dmb(_ARM_BARRIER_ISH);
140     return 1;
141 }
142
143 static __inline int CRYPTO_GET_REF(volatile int *val, int *ret,
144                                    ossl_unused void *lock)
145 {
146     *ret = _InterlockedOr_nf((void *)val, 0);
147     return 1;
148 }
149
150 #   else
151 #    if !defined(_WIN32_WCE)
152 #     pragma intrinsic(_InterlockedExchangeAdd)
153 #    else
154 #     if _WIN32_WCE >= 0x600
155        extern long __cdecl _InterlockedExchangeAdd(long volatile*, long);
156 #     else
157        /* under Windows CE we still have old-style Interlocked* functions */
158        extern long __cdecl InterlockedExchangeAdd(long volatile*, long);
159 #      define _InterlockedExchangeAdd InterlockedExchangeAdd
160 #     endif
161 #    endif
162
163 static __inline int CRYPTO_UP_REF(volatile int *val, int *ret,
164                                   ossl_unused void *lock)
165 {
166     *ret = _InterlockedExchangeAdd(val, 1) + 1;
167     return 1;
168 }
169
170 static __inline int CRYPTO_DOWN_REF(volatile int *val, int *ret,
171                                     ossl_unused void *lock)
172 {
173     *ret = _InterlockedExchangeAdd(val, -1) - 1;
174     return 1;
175 }
176
177 static __inline int CRYPTO_GET_REF(volatile int *val, int *ret,
178                                    ossl_unused void *lock)
179 {
180     *ret = _InterlockedExchangeAdd(val, 0);
181     return 1;
182 }
183
184 #   endif
185
186 #  endif
187 # endif  /* !OPENSSL_DEV_NO_ATOMICS */
188
189 /*
190  * All the refcounting implementations above define HAVE_ATOMICS, so if it's
191  * still undefined here (such as when OPENSSL_DEV_NO_ATOMICS is defined), it
192  * means we need to implement a fallback.  This fallback uses locks.
193  */
194 # ifndef HAVE_ATOMICS
195
196 typedef int CRYPTO_REF_COUNT;
197
198 # define CRYPTO_UP_REF(val, ret, lock) CRYPTO_atomic_add(val, 1, ret, lock)
199 # define CRYPTO_DOWN_REF(val, ret, lock) CRYPTO_atomic_add(val, -1, ret, lock)
200
201 # endif
202
203 # if !defined(NDEBUG) && !defined(OPENSSL_NO_STDIO)
204 #  define REF_ASSERT_ISNT(test) \
205     (void)((test) ? (OPENSSL_die("refcount error", __FILE__, __LINE__), 1) : 0)
206 # else
207 #  define REF_ASSERT_ISNT(i)
208 # endif
209
210 # define REF_PRINT_EX(text, count, object) \
211     OSSL_TRACE3(REF_COUNT, "%p:%4d:%s\n", (object), (count), (text));
212 # define REF_PRINT_COUNT(text, object) \
213     REF_PRINT_EX(text, object->references, (void *)object)
214
215 #endif