]>
git.ipfire.org Git - thirdparty/openssl.git/blob - include/internal/refcount.h
2 * Copyright 2016-2018 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 HEADER_INTERNAL_REFCOUNT_H
10 # define HEADER_INTERNAL_REFCOUNT_H
12 /* Used to checking reference counts, most while doing perl5 stuff :-) */
13 # if defined(OPENSSL_NO_STDIO)
14 # if defined(REF_PRINT)
15 # error "REF_PRINT requires stdio"
19 # if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L \
20 && !defined(__STDC_NO_ATOMICS__)
21 # include <stdatomic.h>
22 # define HAVE_C11_ATOMICS
25 # if defined(HAVE_C11_ATOMICS) && defined(ATOMIC_INT_LOCK_FREE) \
26 && ATOMIC_INT_LOCK_FREE > 0
28 # define HAVE_ATOMICS 1
30 typedef _Atomic
int CRYPTO_REF_COUNT
;
32 static inline int CRYPTO_UP_REF(_Atomic
int *val
, int *ret
, void *lock
)
34 *ret
= atomic_fetch_add_explicit(val
, 1, memory_order_relaxed
) + 1;
39 * Changes to shared structure other than reference counter have to be
40 * serialized. And any kind of serialization implies a release fence. This
41 * means that by the time reference counter is decremented all other
42 * changes are visible on all processors. Hence decrement itself can be
43 * relaxed. In case it hits zero, object will be destructed. Since it's
44 * last use of the object, destructor programmer might reason that access
45 * to mutable members doesn't have to be serialized anymore, which would
46 * otherwise imply an acquire fence. Hence conditional acquire fence...
48 static inline int CRYPTO_DOWN_REF(_Atomic
int *val
, int *ret
, void *lock
)
50 *ret
= atomic_fetch_sub_explicit(val
, 1, memory_order_relaxed
) - 1;
52 atomic_thread_fence(memory_order_acquire
);
56 # elif defined(__GNUC__) && defined(__ATOMIC_RELAXED) && __GCC_ATOMIC_INT_LOCK_FREE > 0
58 # define HAVE_ATOMICS 1
60 typedef int CRYPTO_REF_COUNT
;
62 static __inline__
int CRYPTO_UP_REF(int *val
, int *ret
, void *lock
)
64 *ret
= __atomic_fetch_add(val
, 1, __ATOMIC_RELAXED
) + 1;
68 static __inline__
int CRYPTO_DOWN_REF(int *val
, int *ret
, void *lock
)
70 *ret
= __atomic_fetch_sub(val
, 1, __ATOMIC_RELAXED
) - 1;
72 __atomic_thread_fence(__ATOMIC_ACQUIRE
);
76 # elif defined(_MSC_VER) && _MSC_VER>=1200
78 # define HAVE_ATOMICS 1
80 typedef volatile int CRYPTO_REF_COUNT
;
82 # if (defined(_M_ARM) && _M_ARM>=7) || defined(_M_ARM64)
84 # if defined(_M_ARM64) && !defined(_ARM_BARRIER_ISH)
85 # define _ARM_BARRIER_ISH _ARM64_BARRIER_ISH
88 static __inline
int CRYPTO_UP_REF(volatile int *val
, int *ret
, void *lock
)
90 *ret
= _InterlockedExchangeAdd_nf(val
, 1) + 1;
94 static __inline
int CRYPTO_DOWN_REF(volatile int *val
, int *ret
, void *lock
)
96 *ret
= _InterlockedExchangeAdd_nf(val
, -1) - 1;
98 __dmb(_ARM_BARRIER_ISH
);
102 # pragma intrinsic(_InterlockedExchangeAdd)
104 static __inline
int CRYPTO_UP_REF(volatile int *val
, int *ret
, void *lock
)
106 *ret
= _InterlockedExchangeAdd(val
, 1) + 1;
110 static __inline
int CRYPTO_DOWN_REF(volatile int *val
, int *ret
, void *lock
)
112 *ret
= _InterlockedExchangeAdd(val
, -1) - 1;
119 typedef int CRYPTO_REF_COUNT
;
121 # define CRYPTO_UP_REF(val, ret, lock) CRYPTO_atomic_add(val, 1, ret, lock)
122 # define CRYPTO_DOWN_REF(val, ret, lock) CRYPTO_atomic_add(val, -1, ret, lock)
126 # if !defined(NDEBUG) && !defined(OPENSSL_NO_STDIO)
127 # define REF_ASSERT_ISNT(test) \
128 (void)((test) ? (OPENSSL_die("refcount error", __FILE__, __LINE__), 1) : 0)
130 # define REF_ASSERT_ISNT(i)
134 # define REF_PRINT_COUNT(a, b) \
135 fprintf(stderr, "%p:%4d:%s\n", b, b->references, a)
137 # define REF_PRINT_COUNT(a, b)