Coverage Report

Created: 2026-09-01 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/curl/lib/uint-table.c
Line
Count
Source
1
/***************************************************************************
2
 *                                  _   _ ____  _
3
 *  Project                     ___| | | |  _ \| |
4
 *                             / __| | | | |_) | |
5
 *                            | (__| |_| |  _ <| |___
6
 *                             \___|\___/|_| \_\_____|
7
 *
8
 * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
9
 *
10
 * This software is licensed as described in the file COPYING, which
11
 * you should have received as part of this distribution. The terms
12
 * are also available at https://curl.se/docs/copyright.html.
13
 *
14
 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15
 * copies of the Software, and permit persons to whom the Software is
16
 * furnished to do so, under the terms of the COPYING file.
17
 *
18
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19
 * KIND, either express or implied.
20
 *
21
 * SPDX-License-Identifier: curl
22
 *
23
 ***************************************************************************/
24
#include "curl_setup.h"
25
26
#include "uint-table.h"
27
28
#ifdef DEBUGBUILD
29
21.0k
#define CURL_UINT32_TBL_MAGIC  0x62757473
30
#endif
31
32
void Curl_uint32_tbl_init(struct uint32_tbl *tbl,
33
                          Curl_uint32_tbl_entry_dtor *entry_dtor)
34
21.0k
{
35
21.0k
  memset(tbl, 0, sizeof(*tbl));
36
21.0k
  tbl->entry_dtor = entry_dtor;
37
21.0k
  tbl->last_key_added = UINT32_MAX;
38
21.0k
#ifdef DEBUGBUILD
39
21.0k
  tbl->init = CURL_UINT32_TBL_MAGIC;
40
21.0k
#endif
41
21.0k
}
42
43
static void uint32_tbl_clear_rows(struct uint32_tbl *tbl,
44
                                  uint32_t from,
45
                                  uint32_t upto_excluding)
46
63.2k
{
47
63.2k
  uint32_t i, end;
48
49
63.2k
  end = CURLMIN(upto_excluding, tbl->nrows);
50
2.80M
  for(i = from; i < end; ++i) {
51
2.74M
    if(tbl->rows[i]) {
52
42.1k
      if(tbl->entry_dtor)
53
0
        tbl->entry_dtor(i, tbl->rows[i]);
54
42.1k
      tbl->rows[i] = NULL;
55
42.1k
      tbl->nentries--;
56
42.1k
    }
57
2.74M
  }
58
63.2k
}
59
60
CURLcode Curl_uint32_tbl_resize(struct uint32_tbl *tbl, uint32_t nrows)
61
21.0k
{
62
  /* we use `tbl->nrows + 1` during iteration, want that to work */
63
21.0k
  DEBUGASSERT(tbl->init == CURL_UINT32_TBL_MAGIC);
64
21.0k
  if(!nrows)
65
0
    return CURLE_BAD_FUNCTION_ARGUMENT;
66
21.0k
  if(nrows != tbl->nrows) {
67
21.0k
    void **rows = curlx_calloc(nrows, sizeof(void *));
68
21.0k
    if(!rows)
69
0
      return CURLE_OUT_OF_MEMORY;
70
21.0k
    if(tbl->rows) {
71
0
      memcpy(rows, tbl->rows, (CURLMIN(nrows, tbl->nrows) * sizeof(void *)));
72
0
      if(nrows < tbl->nrows)
73
0
        uint32_tbl_clear_rows(tbl, nrows, tbl->nrows);
74
0
      curlx_free(tbl->rows);
75
0
    }
76
21.0k
    tbl->rows = rows;
77
21.0k
    tbl->nrows = nrows;
78
21.0k
  }
79
21.0k
  return CURLE_OK;
80
21.0k
}
81
82
/* Clear the table, making it empty.
83
84
   @unittest 3212
85
 */
86
UNITTEST void uint32_tbl_clear(struct uint32_tbl *tbl);
87
UNITTEST void uint32_tbl_clear(struct uint32_tbl *tbl)
88
21.0k
{
89
21.0k
  DEBUGASSERT(tbl->init == CURL_UINT32_TBL_MAGIC);
90
21.0k
  uint32_tbl_clear_rows(tbl, 0, tbl->nrows);
91
21.0k
  DEBUGASSERT(!tbl->nentries);
92
21.0k
  tbl->last_key_added = UINT32_MAX;
93
21.0k
}
94
95
void Curl_uint32_tbl_destroy(struct uint32_tbl *tbl)
96
21.0k
{
97
21.0k
  DEBUGASSERT(tbl->init == CURL_UINT32_TBL_MAGIC);
98
21.0k
  uint32_tbl_clear(tbl);
99
21.0k
  curlx_free(tbl->rows);
100
21.0k
  memset(tbl, 0, sizeof(*tbl));
101
21.0k
}
102
103
uint32_t Curl_uint32_tbl_capacity(struct uint32_tbl *tbl)
104
21.0k
{
105
21.0k
  return tbl->nrows;
106
21.0k
}
107
108
uint32_t Curl_uint32_tbl_count(struct uint32_tbl *tbl)
109
47.3k
{
110
47.3k
  return tbl->nentries;
111
47.3k
}
112
113
void *Curl_uint32_tbl_get(struct uint32_tbl *tbl, uint32_t key)
114
97.6k
{
115
97.6k
  return (key < tbl->nrows) ? tbl->rows[key] : NULL;
116
97.6k
}
117
118
bool Curl_uint32_tbl_add(struct uint32_tbl *tbl, void *entry, uint32_t *pkey)
119
42.1k
{
120
42.1k
  uint32_t key, start_pos;
121
122
42.1k
  DEBUGASSERT(tbl->init == CURL_UINT32_TBL_MAGIC);
123
42.1k
  if(!entry || !pkey)
124
0
    return FALSE;
125
42.1k
  *pkey = UINT32_MAX;
126
42.1k
  if(tbl->nentries == tbl->nrows)  /* full */
127
0
    return FALSE;
128
129
42.1k
  start_pos = CURLMIN(tbl->last_key_added, tbl->nrows) + 1;
130
42.1k
  for(key = start_pos; key < tbl->nrows; ++key) {
131
21.0k
    if(!tbl->rows[key]) {
132
21.0k
      tbl->rows[key] = entry;
133
21.0k
      tbl->nentries++;
134
21.0k
      tbl->last_key_added = key;
135
21.0k
      *pkey = key;
136
21.0k
      return TRUE;
137
21.0k
    }
138
21.0k
  }
139
  /* no free entry at or above tbl->maybe_next_key, wrap around */
140
21.0k
  for(key = 0; key < start_pos; ++key) {
141
21.0k
    if(!tbl->rows[key]) {
142
21.0k
      tbl->rows[key] = entry;
143
21.0k
      tbl->nentries++;
144
21.0k
      tbl->last_key_added = key;
145
21.0k
      *pkey = key;
146
21.0k
      return TRUE;
147
21.0k
    }
148
21.0k
  }
149
  /* Did not find any free row? Should not happen */
150
0
  DEBUGASSERT(0);
151
0
  return FALSE;
152
0
}
153
154
void Curl_uint32_tbl_remove(struct uint32_tbl *tbl, uint32_t key)
155
42.1k
{
156
42.1k
  uint32_tbl_clear_rows(tbl, key, key + 1);
157
42.1k
}
158
159
bool Curl_uint32_tbl_contains(struct uint32_tbl *tbl, uint32_t key)
160
21.0k
{
161
21.0k
  return (key < tbl->nrows) ? !!tbl->rows[key] : FALSE;
162
21.0k
}
163
164
static bool uint32_tbl_next_at(struct uint32_tbl *tbl, uint32_t key,
165
                               uint32_t *pkey, void **pentry)
166
42.1k
{
167
2.72M
  for(; key < tbl->nrows; ++key) {
168
2.70M
    if(tbl->rows[key]) {
169
21.0k
      *pkey = key;
170
21.0k
      *pentry = tbl->rows[key];
171
21.0k
      return TRUE;
172
21.0k
    }
173
2.70M
  }
174
21.0k
  *pkey = UINT32_MAX;  /* always invalid */
175
21.0k
  *pentry = NULL;
176
21.0k
  return FALSE;
177
42.1k
}
178
179
bool Curl_uint32_tbl_first(struct uint32_tbl *tbl,
180
                           uint32_t *pkey, void **pentry)
181
21.0k
{
182
21.0k
  if(!pkey || !pentry)
183
0
    return FALSE;
184
21.0k
  if(tbl->nentries && uint32_tbl_next_at(tbl, 0, pkey, pentry))
185
21.0k
    return TRUE;
186
0
  DEBUGASSERT(!tbl->nentries);
187
0
  *pkey = UINT32_MAX;  /* always invalid */
188
0
  *pentry = NULL;
189
0
  return FALSE;
190
0
}
191
192
bool Curl_uint32_tbl_next(struct uint32_tbl *tbl, uint32_t last_key,
193
                          uint32_t *pkey, void **pentry)
194
21.0k
{
195
21.0k
  if(!pkey || !pentry)
196
0
    return FALSE;
197
21.0k
  if(uint32_tbl_next_at(tbl, last_key + 1, pkey, pentry))
198
0
    return TRUE;
199
21.0k
  *pkey = UINT32_MAX;  /* always invalid */
200
21.0k
  *pentry = NULL;
201
  return FALSE;
202
21.0k
}