Coverage Report

Created: 2026-09-14 07:12

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