| 1 |
// Copyright (c) 2015, Google Inc. |
| 2 |
// SPDX-License-Identifier: ISC |
| 3 |
|
| 4 |
#include "internal.h" |
| 5 |
|
| 6 |
#include <stdlib.h> |
| 7 |
|
| 8 |
#include <openssl/type_check.h> |
| 9 |
|
| 10 |
|
| 11 |
#if !defined(OPENSSL_C11_ATOMIC) && !defined(OPENSSL_WINDOWS_ATOMIC) |
| 12 |
|
| 13 |
OPENSSL_STATIC_ASSERT((CRYPTO_refcount_t)-1 == CRYPTO_REFCOUNT_MAX, |
| 14 |
CRYPTO_REFCOUNT_MAX_is_incorrect) |
| 15 |
|
| 16 |
static struct CRYPTO_STATIC_MUTEX g_refcount_lock = CRYPTO_STATIC_MUTEX_INIT; |
| 17 |
|
| 18 |
void CRYPTO_refcount_inc(CRYPTO_refcount_t *count) { |
| 19 |
CRYPTO_STATIC_MUTEX_lock_write(&g_refcount_lock); |
| 20 |
if (*count < CRYPTO_REFCOUNT_MAX) { |
| 21 |
(*count)++; |
| 22 |
} |
| 23 |
CRYPTO_STATIC_MUTEX_unlock_write(&g_refcount_lock); |
| 24 |
} |
| 25 |
|
| 26 |
int CRYPTO_refcount_dec_and_test_zero(CRYPTO_refcount_t *count) { |
| 27 |
int ret; |
| 28 |
|
| 29 |
CRYPTO_STATIC_MUTEX_lock_write(&g_refcount_lock); |
| 30 |
if (*count == 0) { |
| 31 |
abort(); |
| 32 |
} |
| 33 |
if (*count < CRYPTO_REFCOUNT_MAX) { |
| 34 |
(*count)--; |
| 35 |
} |
| 36 |
ret = (*count == 0); |
| 37 |
CRYPTO_STATIC_MUTEX_unlock_write(&g_refcount_lock); |
| 38 |
|
| 39 |
return ret; |
| 40 |
} |
| 41 |
|
| 42 |
#endif // !OPENSSL_C11_ATOMIC && !OPENSSL_WINDOWS_ATOMICS |