/src/boringssl/crypto/refcount.cc
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright 2015 The BoringSSL Authors |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | | // you may not use this file except in compliance with the License. |
5 | | // You may obtain a copy of the License at |
6 | | // |
7 | | // https://www.apache.org/licenses/LICENSE-2.0 |
8 | | // |
9 | | // Unless required by applicable law or agreed to in writing, software |
10 | | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | | // See the License for the specific language governing permissions and |
13 | | // limitations under the License. |
14 | | |
15 | | #include "internal.h" |
16 | | |
17 | | #include <assert.h> |
18 | | #include <stdlib.h> |
19 | | |
20 | | |
21 | 1.82M | void CRYPTO_refcount_inc(CRYPTO_refcount_t *count) { |
22 | 1.82M | uint32_t expected = CRYPTO_atomic_load_u32(count); |
23 | | |
24 | 1.82M | while (expected != CRYPTO_REFCOUNT_MAX) { |
25 | 1.82M | uint32_t new_value = expected + 1; |
26 | 1.82M | if (CRYPTO_atomic_compare_exchange_weak_u32(count, &expected, new_value)) { |
27 | 1.82M | break; |
28 | 1.82M | } |
29 | 1.82M | } |
30 | 1.82M | } |
31 | | |
32 | 3.52M | int CRYPTO_refcount_dec_and_test_zero(CRYPTO_refcount_t *count) { |
33 | 3.52M | uint32_t expected = CRYPTO_atomic_load_u32(count); |
34 | | |
35 | 3.52M | for (;;) { |
36 | 3.52M | if (expected == 0) { |
37 | 0 | abort(); |
38 | 3.52M | } else if (expected == CRYPTO_REFCOUNT_MAX) { |
39 | 0 | return 0; |
40 | 3.52M | } else { |
41 | 3.52M | const uint32_t new_value = expected - 1; |
42 | 3.52M | if (CRYPTO_atomic_compare_exchange_weak_u32(count, &expected, |
43 | 3.52M | new_value)) { |
44 | 3.52M | return new_value == 0; |
45 | 3.52M | } |
46 | 3.52M | } |
47 | 3.52M | } |
48 | 3.52M | } |