Coverage Report

Created: 2026-12-01 06:27

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
42.0k
#define CURL_UINT32_TBL_MAGIC  0x62757473
30
#endif
31
32
void Curl_uint32_tbl_init(struct uint32_tbl *tbl)
33
42.0k
{
34
42.0k
  memset(tbl, 0, sizeof(*tbl));
35
42.0k
  tbl->last_key_added = UINT32_MAX;
36
42.0k
#ifdef DEBUGBUILD
37
42.0k
  tbl->init = CURL_UINT32_TBL_MAGIC;
38
42.0k
#endif
39
42.0k
}
40
41
static void uint32_tbl_clear_rows(struct uint32_tbl *tbl,
42
                                  uint32_t from,
43
                                  uint32_t upto_excluding)
44
104k
{
45
104k
  uint32_t i, end;
46
47
104k
  end = CURLMIN(upto_excluding, tbl->nrows);
48
3.18M
  for(i = from; i < end; ++i) {
49
3.08M
    if(tbl->rows[i]) {
50
62.5k
      tbl->rows[i] = NULL;
51
62.5k
      tbl->nentries--;
52
62.5k
    }
53
3.08M
  }
54
104k
}
55
56
CURLcode Curl_uint32_tbl_resize(struct uint32_tbl *tbl, uint32_t nrows)
57
41.4k
{
58
  /* we use `tbl->nrows + 1` during iteration, want that to work */
59
41.4k
  DEBUGASSERT(tbl->init == CURL_UINT32_TBL_MAGIC);
60
41.4k
  if(!nrows)
61
0
    return CURLE_BAD_FUNCTION_ARGUMENT;
62
41.4k
  if(nrows != tbl->nrows) {
63
41.4k
    void **rows = curlx_calloc(nrows, sizeof(void *));
64
41.4k
    if(!rows)
65
7
      return CURLE_OUT_OF_MEMORY;
66
41.3k
    if(tbl->rows) {
67
0
      memcpy(rows, tbl->rows, (CURLMIN(nrows, tbl->nrows) * sizeof(void *)));
68
0
      if(nrows < tbl->nrows)
69
0
        uint32_tbl_clear_rows(tbl, nrows, tbl->nrows);
70
0
      curlx_free(tbl->rows);
71
0
    }
72
41.3k
    tbl->rows = rows;
73
41.3k
    tbl->nrows = nrows;
74
41.3k
  }
75
41.3k
  return CURLE_OK;
76
41.4k
}
77
78
/* Clear the table, making it empty.
79
80
   @unittest 3212
81
 */
82
UNITTEST void uint32_tbl_clear(struct uint32_tbl *tbl);
83
UNITTEST void uint32_tbl_clear(struct uint32_tbl *tbl)
84
42.0k
{
85
42.0k
  DEBUGASSERT(tbl->init == CURL_UINT32_TBL_MAGIC);
86
42.0k
  uint32_tbl_clear_rows(tbl, 0, tbl->nrows);
87
42.0k
  DEBUGASSERT(!tbl->nentries);
88
42.0k
  tbl->last_key_added = UINT32_MAX;
89
42.0k
}
90
91
void Curl_uint32_tbl_destroy(struct uint32_tbl *tbl)
92
42.0k
{
93
42.0k
  DEBUGASSERT(tbl->init == CURL_UINT32_TBL_MAGIC);
94
42.0k
  uint32_tbl_clear(tbl);
95
42.0k
  curlx_free(tbl->rows);
96
42.0k
  memset(tbl, 0, sizeof(*tbl));
97
42.0k
}
98
99
uint32_t Curl_uint32_tbl_capacity(struct uint32_tbl *tbl)
100
41.4k
{
101
41.4k
  return tbl->nrows;
102
41.4k
}
103
104
uint32_t Curl_uint32_tbl_count(struct uint32_tbl *tbl)
105
86.1k
{
106
86.1k
  return tbl->nentries;
107
86.1k
}
108
109
void *Curl_uint32_tbl_get(struct uint32_tbl *tbl, uint32_t key)
110
332k
{
111
332k
  return (key < tbl->nrows) ? tbl->rows[key] : NULL;
112
332k
}
113
114
bool Curl_uint32_tbl_add(struct uint32_tbl *tbl, void *entry, uint32_t *pkey)
115
62.5k
{
116
62.5k
  uint32_t key, start_pos;
117
118
62.5k
  DEBUGASSERT(tbl->init == CURL_UINT32_TBL_MAGIC);
119
62.5k
  if(!entry || !pkey)
120
0
    return FALSE;
121
62.5k
  *pkey = UINT32_MAX;
122
62.5k
  if(tbl->nentries == tbl->nrows)  /* full */
123
0
    return FALSE;
124
125
62.5k
  start_pos = CURLMIN(tbl->last_key_added, tbl->nrows) + 1;
126
62.5k
  for(key = start_pos; key < tbl->nrows; ++key) {
127
21.1k
    if(!tbl->rows[key]) {
128
21.1k
      tbl->rows[key] = entry;
129
21.1k
      tbl->nentries++;
130
21.1k
      tbl->last_key_added = key;
131
21.1k
      *pkey = key;
132
21.1k
      return TRUE;
133
21.1k
    }
134
21.1k
  }
135
  /* no free entry at or above tbl->maybe_next_key, wrap around */
136
41.3k
  for(key = 0; key < start_pos; ++key) {
137
41.3k
    if(!tbl->rows[key]) {
138
41.3k
      tbl->rows[key] = entry;
139
41.3k
      tbl->nentries++;
140
41.3k
      tbl->last_key_added = key;
141
41.3k
      *pkey = key;
142
41.3k
      return TRUE;
143
41.3k
    }
144
41.3k
  }
145
  /* Did not find any free row? Should not happen */
146
0
  DEBUGASSERT(0);
147
0
  return FALSE;
148
0
}
149
150
void Curl_uint32_tbl_remove(struct uint32_tbl *tbl, uint32_t key)
151
62.5k
{
152
62.5k
  if(key < tbl->nrows)
153
62.5k
    uint32_tbl_clear_rows(tbl, key, key + 1);
154
62.5k
}
155
156
bool Curl_uint32_tbl_contains(struct uint32_tbl *tbl, uint32_t key)
157
21.0k
{
158
21.0k
  return (key < tbl->nrows) ? !!tbl->rows[key] : FALSE;
159
21.0k
}
160
161
static bool uint32_tbl_next_at(struct uint32_tbl *tbl, uint32_t key,
162
                               uint32_t *pkey, void **pentry)
163
106k
{
164
5.43M
  for(; key < tbl->nrows; ++key) {
165
5.39M
    if(tbl->rows[key]) {
166
63.8k
      *pkey = key;
167
63.8k
      *pentry = tbl->rows[key];
168
63.8k
      return TRUE;
169
63.8k
    }
170
5.39M
  }
171
42.8k
  *pkey = UINT32_MAX;  /* always invalid */
172
42.8k
  *pentry = NULL;
173
42.8k
  return FALSE;
174
106k
}
175
176
bool Curl_uint32_tbl_first(struct uint32_tbl *tbl,
177
                           uint32_t *pkey, void **pentry)
178
83.4k
{
179
83.4k
  if(!pkey || !pentry)
180
0
    return FALSE;
181
83.4k
  if(tbl->nentries && uint32_tbl_next_at(tbl, 0, pkey, pentry))
182
42.8k
    return TRUE;
183
40.6k
  DEBUGASSERT(!tbl->nentries);
184
40.6k
  *pkey = UINT32_MAX;  /* always invalid */
185
40.6k
  *pentry = NULL;
186
40.6k
  return FALSE;
187
40.6k
}
188
189
bool Curl_uint32_tbl_next(struct uint32_tbl *tbl, uint32_t last_key,
190
                          uint32_t *pkey, void **pentry)
191
63.8k
{
192
63.8k
  if(!pkey || !pentry)
193
0
    return FALSE;
194
63.8k
  if(uint32_tbl_next_at(tbl, last_key + 1, pkey, pentry))
195
21.0k
    return TRUE;
196
42.8k
  *pkey = UINT32_MAX;  /* always invalid */
197
42.8k
  *pentry = NULL;
198
  return FALSE;
199
63.8k
}