Coverage Report

Created: 2026-12-01 06:25

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/curl/lib/curl_refcount.h
Line
Count
Source
1
#ifndef HEADER_CURL_REFCOUNT_H
2
#define HEADER_CURL_REFCOUNT_H
3
/***************************************************************************
4
 *                                  _   _ ____  _
5
 *  Project                     ___| | | |  _ \| |
6
 *                             / __| | | | |_) | |
7
 *                            | (__| |_| |  _ <| |___
8
 *                             \___|\___/|_| \_\_____|
9
 *
10
 * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
11
 *
12
 * This software is licensed as described in the file COPYING, which
13
 * you should have received as part of this distribution. The terms
14
 * are also available at https://curl.se/docs/copyright.html.
15
 *
16
 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
17
 * copies of the Software, and permit persons to whom the Software is
18
 * furnished to do so, under the terms of the COPYING file.
19
 *
20
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
21
 * KIND, either express or implied.
22
 *
23
 * SPDX-License-Identifier: curl
24
 *
25
 ***************************************************************************/
26
#include "curl_setup.h"
27
28
/* Reference counter for objects that an easy handle shares with
29
 * connections, like `struct Curl_peer` and `struct Curl_creds`. Once a
30
 * transfer has been removed from its multi handle, the application may use
31
 * the easy handle in another thread (libcurl-thread(3)) while the multi
32
 * handle keeps using the connection, so the counter is updated atomically.
33
 * Without atomics, curl_refcount.c updates it under a lock from easy_lock.h,
34
 * so it is thread-safe whenever libcurl reports CURL_VERSION_THREADSAFE.
35
 *
36
 * Curl_refcount_decr() returns the new value of the counter.
37
 */
38
39
#ifdef _WIN32
40
41
typedef LONG volatile curl_refcount;
42
43
#define Curl_refcount_init(c, v) (*(c) = (LONG)(v))
44
#define Curl_refcount_get(c)     ((uint32_t)*(c))
45
#define Curl_refcount_incr(c)    ((uint32_t)InterlockedIncrement(c))
46
#define Curl_refcount_decr(c)    ((uint32_t)InterlockedDecrement(c))
47
48
#elif defined(HAVE_ATOMIC) && defined(HAVE_STDATOMIC_H)
49
#include <stdatomic.h>
50
51
typedef atomic_uint curl_refcount;
52
53
87
#define Curl_refcount_init(c, v) atomic_init(c, v)
54
0
#define Curl_refcount_get(c)     atomic_load_explicit(c, memory_order_relaxed)
55
#define Curl_refcount_incr(c) \
56
123
  (atomic_fetch_add_explicit(c, 1U, memory_order_relaxed) + 1U)
57
#define Curl_refcount_decr(c) \
58
210
  (atomic_fetch_sub_explicit(c, 1U, memory_order_acq_rel) - 1U)
59
60
#else /* see curl_refcount.c */
61
62
typedef uint32_t curl_refcount;
63
64
#define Curl_refcount_init(c, v) (*(c) = (v))
65
uint32_t Curl_refcount_get(curl_refcount *c);
66
uint32_t Curl_refcount_incr(curl_refcount *c);
67
uint32_t Curl_refcount_decr(curl_refcount *c);
68
69
#endif
70
71
#endif /* HEADER_CURL_REFCOUNT_H */