Coverage Report

Created: 2026-05-30 06:06

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
0
#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
0
{
35
0
  memset(tbl, 0, sizeof(*tbl));
36
0
  tbl->entry_dtor = entry_dtor;
37
0
  tbl->last_key_added = UINT32_MAX;
38
0
#ifdef DEBUGBUILD
39
0
  tbl->init = CURL_UINT32_TBL_MAGIC;
40
0
#endif
41
0
}
42
43
static void uint32_tbl_clear_rows(struct uint32_tbl *tbl,
44
                                  uint32_t from,
45
                                  uint32_t upto_excluding)
46
0
{
47
0
  uint32_t i, end;
48
49
0
  end = CURLMIN(upto_excluding, tbl->nrows);
50
0
  for(i = from; i < end; ++i) {
51
0
    if(tbl->rows[i]) {
52
0
      if(tbl->entry_dtor)
53
0
        tbl->entry_dtor(i, tbl->rows[i]);
54
0
      tbl->rows[i] = NULL;
55
0
      tbl->nentries--;
56
0
    }
57
0
  }
58
0
}
59
60
CURLcode Curl_uint32_tbl_resize(struct uint32_tbl *tbl, uint32_t nrows)
61
0
{
62
  /* we use `tbl->nrows + 1` during iteration, want that to work */
63
0
  DEBUGASSERT(tbl->init == CURL_UINT32_TBL_MAGIC);
64
0
  if(!nrows)
65
0
    return CURLE_BAD_FUNCTION_ARGUMENT;
66
0
  if(nrows != tbl->nrows) {
67
0
    void **rows = curlx_calloc(nrows, sizeof(void *));
68
0
    if(!rows)
69
0
      return CURLE_OUT_OF_MEMORY;
70
0
    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
0
    tbl->rows = rows;
77
0
    tbl->nrows = nrows;
78
0
  }
79
0
  return CURLE_OK;
80
0
}
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
0
{
89
0
  DEBUGASSERT(tbl->init == CURL_UINT32_TBL_MAGIC);
90
0
  uint32_tbl_clear_rows(tbl, 0, tbl->nrows);
91
0
  DEBUGASSERT(!tbl->nentries);
92
0
  tbl->last_key_added = UINT32_MAX;
93
0
}
94
95
void Curl_uint32_tbl_destroy(struct uint32_tbl *tbl)
96
0
{
97
0
  DEBUGASSERT(tbl->init == CURL_UINT32_TBL_MAGIC);
98
0
  uint32_tbl_clear(tbl);
99
0
  curlx_free(tbl->rows);
100
0
  memset(tbl, 0, sizeof(*tbl));
101
0
}
102
103
uint32_t Curl_uint32_tbl_capacity(struct uint32_tbl *tbl)
104
0
{
105
0
  return tbl->nrows;
106
0
}
107
108
uint32_t Curl_uint32_tbl_count(struct uint32_tbl *tbl)
109
0
{
110
0
  return tbl->nentries;
111
0
}
112
113
void *Curl_uint32_tbl_get(struct uint32_tbl *tbl, uint32_t key)
114
0
{
115
0
  return (key < tbl->nrows) ? tbl->rows[key] : NULL;
116
0
}
117
118
bool Curl_uint32_tbl_add(struct uint32_tbl *tbl, void *entry, uint32_t *pkey)
119
0
{
120
0
  uint32_t key, start_pos;
121
122
0
  DEBUGASSERT(tbl->init == CURL_UINT32_TBL_MAGIC);
123
0
  if(!entry || !pkey)
124
0
    return FALSE;
125
0
  *pkey = UINT32_MAX;
126
0
  if(tbl->nentries == tbl->nrows)  /* full */
127
0
    return FALSE;
128
129
0
  start_pos = CURLMIN(tbl->last_key_added, tbl->nrows) + 1;
130
0
  for(key = start_pos; key < tbl->nrows; ++key) {
131
0
    if(!tbl->rows[key]) {
132
0
      tbl->rows[key] = entry;
133
0
      tbl->nentries++;
134
0
      tbl->last_key_added = key;
135
0
      *pkey = key;
136
0
      return TRUE;
137
0
    }
138
0
  }
139
  /* no free entry at or above tbl->maybe_next_key, wrap around */
140
0
  for(key = 0; key < start_pos; ++key) {
141
0
    if(!tbl->rows[key]) {
142
0
      tbl->rows[key] = entry;
143
0
      tbl->nentries++;
144
0
      tbl->last_key_added = key;
145
0
      *pkey = key;
146
0
      return TRUE;
147
0
    }
148
0
  }
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
0
{
156
0
  uint32_tbl_clear_rows(tbl, key, key + 1);
157
0
}
158
159
bool Curl_uint32_tbl_contains(struct uint32_tbl *tbl, uint32_t key)
160
0
{
161
0
  return (key < tbl->nrows) ? !!tbl->rows[key] : FALSE;
162
0
}
163
164
static bool uint32_tbl_next_at(struct uint32_tbl *tbl, uint32_t key,
165
                               uint32_t *pkey, void **pentry)
166
0
{
167
0
  for(; key < tbl->nrows; ++key) {
168
0
    if(tbl->rows[key]) {
169
0
      *pkey = key;
170
0
      *pentry = tbl->rows[key];
171
0
      return TRUE;
172
0
    }
173
0
  }
174
0
  *pkey = UINT32_MAX;  /* always invalid */
175
0
  *pentry = NULL;
176
0
  return FALSE;
177
0
}
178
179
bool Curl_uint32_tbl_first(struct uint32_tbl *tbl,
180
                           uint32_t *pkey, void **pentry)
181
0
{
182
0
  if(!pkey || !pentry)
183
0
    return FALSE;
184
0
  if(tbl->nentries && uint32_tbl_next_at(tbl, 0, pkey, pentry))
185
0
    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
0
{
195
0
  if(!pkey || !pentry)
196
0
    return FALSE;
197
0
  if(uint32_tbl_next_at(tbl, last_key + 1, pkey, pentry))
198
0
    return TRUE;
199
0
  *pkey = UINT32_MAX;  /* always invalid */
200
0
  *pentry = NULL;
201
  return FALSE;
202
0
}