• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright (c) 2015, Google Inc.
2  *
3  * Permission to use, copy, modify, and/or distribute this software for any
4  * purpose with or without fee is hereby granted, provided that the above
5  * copyright notice and this permission notice appear in all copies.
6  *
7  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10  * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
14 
15 #include "internal.h"
16 
17 #include <assert.h>
18 #include <stdlib.h>
19 
20 
21 #if !defined(OPENSSL_C11_ATOMIC)
22 
23 static_assert((CRYPTO_refcount_t)-1 == CRYPTO_REFCOUNT_MAX,
24               "CRYPTO_REFCOUNT_MAX is incorrect");
25 
26 static struct CRYPTO_STATIC_MUTEX g_refcount_lock = CRYPTO_STATIC_MUTEX_INIT;
27 
CRYPTO_refcount_inc(CRYPTO_refcount_t * count)28 void CRYPTO_refcount_inc(CRYPTO_refcount_t *count) {
29   CRYPTO_STATIC_MUTEX_lock_write(&g_refcount_lock);
30   if (*count < CRYPTO_REFCOUNT_MAX) {
31     (*count)++;
32   }
33   CRYPTO_STATIC_MUTEX_unlock_write(&g_refcount_lock);
34 }
35 
CRYPTO_refcount_dec_and_test_zero(CRYPTO_refcount_t * count)36 int CRYPTO_refcount_dec_and_test_zero(CRYPTO_refcount_t *count) {
37   int ret;
38 
39   CRYPTO_STATIC_MUTEX_lock_write(&g_refcount_lock);
40   if (*count == 0) {
41     abort();
42   }
43   if (*count < CRYPTO_REFCOUNT_MAX) {
44     (*count)--;
45   }
46   ret = (*count == 0);
47   CRYPTO_STATIC_MUTEX_unlock_write(&g_refcount_lock);
48 
49   return ret;
50 }
51 
52 #endif  // OPENSSL_C11_ATOMIC
53