Coverage Report

Created: 2026-10-28 07:17

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
893k
#define CURL_UINT32_TBL_MAGIC  0x62757473
30
#endif
31
32
void Curl_uint32_tbl_init(struct uint32_tbl *tbl)
33
893k
{
34
893k
  memset(tbl, 0, sizeof(*tbl));
35
893k
  tbl->last_key_added = UINT32_MAX;
36
893k
#ifdef DEBUGBUILD
37
893k
  tbl->init = CURL_UINT32_TBL_MAGIC;
38
893k
#endif
39
893k
}
40
41
static void uint32_tbl_clear_rows(struct uint32_tbl *tbl,
42
                                  uint32_t from,
43
                                  uint32_t upto_excluding)
44
2.23M
{
45
2.23M
  uint32_t i, end;
46
47
2.23M
  end = CURLMIN(upto_excluding, tbl->nrows);
48
65.8M
  for(i = from; i < end; ++i) {
49
63.6M
    if(tbl->rows[i]) {
50
1.34M
      tbl->rows[i] = NULL;
51
1.34M
      tbl->nentries--;
52
1.34M
    }
53
63.6M
  }
54
2.23M
}
55
56
CURLcode Curl_uint32_tbl_resize(struct uint32_tbl *tbl, uint32_t nrows)
57
802k
{
58
  /* we use `tbl->nrows + 1` during iteration, want that to work */
59
802k
  DEBUGASSERT(tbl->init == CURL_UINT32_TBL_MAGIC);
60
802k
  if(!nrows)
61
0
    return CURLE_BAD_FUNCTION_ARGUMENT;
62
802k
  if(nrows != tbl->nrows) {
63
802k
    void **rows = curlx_calloc(nrows, sizeof(void *));
64
802k
    if(!rows)
65
249
      return CURLE_OUT_OF_MEMORY;
66
802k
    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
802k
    tbl->rows = rows;
73
802k
    tbl->nrows = nrows;
74
802k
  }
75
802k
  return CURLE_OK;
76
802k
}
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
893k
{
85
893k
  DEBUGASSERT(tbl->init == CURL_UINT32_TBL_MAGIC);
86
893k
  uint32_tbl_clear_rows(tbl, 0, tbl->nrows);
87
893k
  DEBUGASSERT(!tbl->nentries);
88
893k
  tbl->last_key_added = UINT32_MAX;
89
893k
}
90
91
void Curl_uint32_tbl_destroy(struct uint32_tbl *tbl)
92
893k
{
93
893k
  DEBUGASSERT(tbl->init == CURL_UINT32_TBL_MAGIC);
94
893k
  uint32_tbl_clear(tbl);
95
893k
  curlx_free(tbl->rows);
96
893k
  memset(tbl, 0, sizeof(*tbl));
97
893k
}
98
99
uint32_t Curl_uint32_tbl_capacity(struct uint32_tbl *tbl)
100
901k
{
101
901k
  return tbl->nrows;
102
901k
}
103
104
uint32_t Curl_uint32_tbl_count(struct uint32_tbl *tbl)
105
2.00M
{
106
2.00M
  return tbl->nentries;
107
2.00M
}
108
109
void *Curl_uint32_tbl_get(struct uint32_tbl *tbl, uint32_t key)
110
10.0M
{
111
10.0M
  return (key < tbl->nrows) ? tbl->rows[key] : NULL;
112
10.0M
}
113
114
bool Curl_uint32_tbl_add(struct uint32_tbl *tbl, void *entry, uint32_t *pkey)
115
1.34M
{
116
1.34M
  uint32_t key, start_pos;
117
118
1.34M
  DEBUGASSERT(tbl->init == CURL_UINT32_TBL_MAGIC);
119
1.34M
  if(!entry || !pkey)
120
0
    return FALSE;
121
1.34M
  *pkey = UINT32_MAX;
122
1.34M
  if(tbl->nentries == tbl->nrows)  /* full */
123
0
    return FALSE;
124
125
1.34M
  start_pos = CURLMIN(tbl->last_key_added, tbl->nrows) + 1;
126
1.34M
  for(key = start_pos; key < tbl->nrows; ++key) {
127
544k
    if(!tbl->rows[key]) {
128
544k
      tbl->rows[key] = entry;
129
544k
      tbl->nentries++;
130
544k
      tbl->last_key_added = key;
131
544k
      *pkey = key;
132
544k
      return TRUE;
133
544k
    }
134
544k
  }
135
  /* no free entry at or above tbl->maybe_next_key, wrap around */
136
802k
  for(key = 0; key < start_pos; ++key) {
137
802k
    if(!tbl->rows[key]) {
138
802k
      tbl->rows[key] = entry;
139
802k
      tbl->nentries++;
140
802k
      tbl->last_key_added = key;
141
802k
      *pkey = key;
142
802k
      return TRUE;
143
802k
    }
144
802k
  }
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
1.34M
{
152
1.34M
  if(key < tbl->nrows)
153
1.34M
    uint32_tbl_clear_rows(tbl, key, key + 1);
154
1.34M
}
155
156
bool Curl_uint32_tbl_contains(struct uint32_tbl *tbl, uint32_t key)
157
485k
{
158
485k
  return (key < tbl->nrows) ? !!tbl->rows[key] : FALSE;
159
485k
}
160
161
static bool uint32_tbl_next_at(struct uint32_tbl *tbl, uint32_t key,
162
                               uint32_t *pkey, void **pentry)
163
1.74M
{
164
87.9M
  for(; key < tbl->nrows; ++key) {
165
87.2M
    if(tbl->rows[key]) {
166
1.00M
      *pkey = key;
167
1.00M
      *pentry = tbl->rows[key];
168
1.00M
      return TRUE;
169
1.00M
    }
170
87.2M
  }
171
743k
  *pkey = UINT32_MAX;  /* always invalid */
172
743k
  *pentry = NULL;
173
743k
  return FALSE;
174
1.74M
}
175
176
bool Curl_uint32_tbl_first(struct uint32_tbl *tbl,
177
                           uint32_t *pkey, void **pentry)
178
1.49M
{
179
1.49M
  if(!pkey || !pentry)
180
0
    return FALSE;
181
1.49M
  if(tbl->nentries && uint32_tbl_next_at(tbl, 0, pkey, pentry))
182
743k
    return TRUE;
183
755k
  DEBUGASSERT(!tbl->nentries);
184
755k
  *pkey = UINT32_MAX;  /* always invalid */
185
755k
  *pentry = NULL;
186
755k
  return FALSE;
187
755k
}
188
189
bool Curl_uint32_tbl_next(struct uint32_tbl *tbl, uint32_t last_key,
190
                          uint32_t *pkey, void **pentry)
191
1.00M
{
192
1.00M
  if(!pkey || !pentry)
193
0
    return FALSE;
194
1.00M
  if(uint32_tbl_next_at(tbl, last_key + 1, pkey, pentry))
195
259k
    return TRUE;
196
743k
  *pkey = UINT32_MAX;  /* always invalid */
197
743k
  *pentry = NULL;
198
  return FALSE;
199
1.00M
}