Coverage Report

Created: 2025-12-03 07:02

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