Coverage Report

Created: 2025-11-11 06:28

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
/* The last 2 #include files should be in this order */
29
#include "curl_memory.h"
30
#include "memdebug.h"
31
32
#ifdef DEBUGBUILD
33
159k
#define CURL_UINT_TBL_MAGIC  0x62757473
34
#endif
35
36
/* Clear the table, making it empty. */
37
UNITTEST void Curl_uint_tbl_clear(struct uint_tbl *tbl);
38
39
void Curl_uint_tbl_init(struct uint_tbl *tbl,
40
                        Curl_uint_tbl_entry_dtor *entry_dtor)
41
159k
{
42
159k
  memset(tbl, 0, sizeof(*tbl));
43
159k
  tbl->entry_dtor = entry_dtor;
44
159k
  tbl->last_key_added = UINT_MAX;
45
159k
#ifdef DEBUGBUILD
46
159k
  tbl->init = CURL_UINT_TBL_MAGIC;
47
159k
#endif
48
159k
}
49
50
51
static void uint_tbl_clear_rows(struct uint_tbl *tbl,
52
                                unsigned int from,
53
                                unsigned int upto_excluding)
54
486k
{
55
486k
  unsigned int i, end;
56
57
486k
  end = CURLMIN(upto_excluding, tbl->nrows);
58
82.3M
  for(i = from; i < end; ++i) {
59
81.8M
    if(tbl->rows[i]) {
60
327k
      if(tbl->entry_dtor)
61
0
        tbl->entry_dtor(i, tbl->rows[i]);
62
327k
      tbl->rows[i] = NULL;
63
327k
      tbl->nentries--;
64
327k
    }
65
81.8M
  }
66
486k
}
67
68
69
CURLcode Curl_uint_tbl_resize(struct uint_tbl *tbl, unsigned int nrows)
70
159k
{
71
  /* we use `tbl->nrows + 1` during iteration, want that to work */
72
159k
  DEBUGASSERT(tbl->init == CURL_UINT_TBL_MAGIC);
73
159k
  if(!nrows)
74
0
    return CURLE_BAD_FUNCTION_ARGUMENT;
75
159k
  if(nrows != tbl->nrows) {
76
159k
    void **rows = calloc(nrows, sizeof(void *));
77
159k
    if(!rows)
78
0
      return CURLE_OUT_OF_MEMORY;
79
159k
    if(tbl->rows) {
80
0
      memcpy(rows, tbl->rows, (CURLMIN(nrows, tbl->nrows) * sizeof(void *)));
81
0
      if(nrows < tbl->nrows)
82
0
        uint_tbl_clear_rows(tbl, nrows, tbl->nrows);
83
0
      free(tbl->rows);
84
0
    }
85
159k
    tbl->rows = rows;
86
159k
    tbl->nrows = nrows;
87
159k
  }
88
159k
  return CURLE_OK;
89
159k
}
90
91
92
void Curl_uint_tbl_destroy(struct uint_tbl *tbl)
93
159k
{
94
159k
  DEBUGASSERT(tbl->init == CURL_UINT_TBL_MAGIC);
95
159k
  Curl_uint_tbl_clear(tbl);
96
159k
  free(tbl->rows);
97
159k
  memset(tbl, 0, sizeof(*tbl));
98
159k
}
99
100
UNITTEST void Curl_uint_tbl_clear(struct uint_tbl *tbl)
101
159k
{
102
159k
  DEBUGASSERT(tbl->init == CURL_UINT_TBL_MAGIC);
103
159k
  uint_tbl_clear_rows(tbl, 0, tbl->nrows);
104
159k
  DEBUGASSERT(!tbl->nentries);
105
159k
  tbl->last_key_added = UINT_MAX;
106
159k
}
107
108
109
unsigned int Curl_uint_tbl_capacity(struct uint_tbl *tbl)
110
168k
{
111
168k
  return tbl->nrows;
112
168k
}
113
114
115
unsigned int Curl_uint_tbl_count(struct uint_tbl *tbl)
116
327k
{
117
327k
  return tbl->nentries;
118
327k
}
119
120
121
void *Curl_uint_tbl_get(struct uint_tbl *tbl, unsigned int key)
122
71.0M
{
123
71.0M
  return (key < tbl->nrows) ? tbl->rows[key] : NULL;
124
71.0M
}
125
126
127
bool Curl_uint_tbl_add(struct uint_tbl *tbl, void *entry, unsigned int *pkey)
128
327k
{
129
327k
  unsigned int key, start_pos;
130
131
327k
  DEBUGASSERT(tbl->init == CURL_UINT_TBL_MAGIC);
132
327k
  if(!entry || !pkey)
133
0
    return FALSE;
134
327k
  *pkey = UINT_MAX;
135
327k
  if(tbl->nentries == tbl->nrows)  /* full */
136
0
    return FALSE;
137
138
327k
  start_pos = CURLMIN(tbl->last_key_added, tbl->nrows) + 1;
139
327k
  for(key = start_pos; key < tbl->nrows; ++key) {
140
168k
    if(!tbl->rows[key]) {
141
168k
      tbl->rows[key] = entry;
142
168k
      tbl->nentries++;
143
168k
      tbl->last_key_added = key;
144
168k
      *pkey = key;
145
168k
      return TRUE;
146
168k
    }
147
168k
  }
148
  /* no free entry at or above tbl->maybe_next_key, wrap around */
149
159k
  for(key = 0; key < start_pos; ++key) {
150
159k
    if(!tbl->rows[key]) {
151
159k
      tbl->rows[key] = entry;
152
159k
      tbl->nentries++;
153
159k
      tbl->last_key_added = key;
154
159k
      *pkey = key;
155
159k
      return TRUE;
156
159k
    }
157
159k
  }
158
  /* Did not find any free row? Should not happen */
159
0
  DEBUGASSERT(0);
160
0
  return FALSE;
161
0
}
162
163
164
void Curl_uint_tbl_remove(struct uint_tbl *tbl, unsigned int key)
165
327k
{
166
327k
  uint_tbl_clear_rows(tbl, key, key + 1);
167
327k
}
168
169
170
bool Curl_uint_tbl_contains(struct uint_tbl *tbl, unsigned int key)
171
168k
{
172
168k
  return (key < tbl->nrows) ? !!tbl->rows[key] : FALSE;
173
168k
}
174
175
176
static bool uint_tbl_next_at(struct uint_tbl *tbl, unsigned int key,
177
                             unsigned int *pkey, void **pentry)
178
318k
{
179
81.6M
  for(; key < tbl->nrows; ++key) {
180
81.4M
    if(tbl->rows[key]) {
181
159k
      *pkey = key;
182
159k
      *pentry = tbl->rows[key];
183
159k
      return TRUE;
184
159k
    }
185
81.4M
  }
186
159k
  *pkey = UINT_MAX;  /* always invalid */
187
159k
  *pentry = NULL;
188
159k
  return FALSE;
189
318k
}
190
191
bool Curl_uint_tbl_first(struct uint_tbl *tbl,
192
                         unsigned int *pkey, void **pentry)
193
159k
{
194
159k
  if(!pkey || !pentry)
195
0
    return FALSE;
196
159k
  if(tbl->nentries && uint_tbl_next_at(tbl, 0, pkey, pentry))
197
159k
    return TRUE;
198
0
  DEBUGASSERT(!tbl->nentries);
199
0
  *pkey = UINT_MAX;  /* always invalid */
200
0
  *pentry = NULL;
201
0
  return FALSE;
202
0
}
203
204
205
bool Curl_uint_tbl_next(struct uint_tbl *tbl, unsigned int last_key,
206
                        unsigned int *pkey, void **pentry)
207
159k
{
208
159k
  if(!pkey || !pentry)
209
0
    return FALSE;
210
159k
  if(uint_tbl_next_at(tbl, last_key + 1, pkey, pentry))
211
0
    return TRUE;
212
159k
  *pkey = UINT_MAX;  /* always invalid */
213
159k
  *pentry = NULL;
214
  return FALSE;
215
159k
}