Coverage Report

Created: 2025-10-10 06:31

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/PROJ/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
#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
0
{
42
0
  memset(tbl, 0, sizeof(*tbl));
43
0
  tbl->entry_dtor = entry_dtor;
44
0
  tbl->last_key_added = UINT_MAX;
45
#ifdef DEBUGBUILD
46
  tbl->init = CURL_UINT_TBL_MAGIC;
47
#endif
48
0
}
49
50
51
static void uint_tbl_clear_rows(struct uint_tbl *tbl,
52
                                unsigned int from,
53
                                unsigned int upto_excluding)
54
0
{
55
0
  unsigned int i, end;
56
57
0
  end = CURLMIN(upto_excluding, tbl->nrows);
58
0
  for(i = from; i < end; ++i) {
59
0
    if(tbl->rows[i]) {
60
0
      if(tbl->entry_dtor)
61
0
        tbl->entry_dtor(i, tbl->rows[i]);
62
0
      tbl->rows[i] = NULL;
63
0
      tbl->nentries--;
64
0
    }
65
0
  }
66
0
}
67
68
69
CURLcode Curl_uint_tbl_resize(struct uint_tbl *tbl, unsigned int nrows)
70
0
{
71
  /* we use `tbl->nrows + 1` during iteration, want that to work */
72
0
  DEBUGASSERT(tbl->init == CURL_UINT_TBL_MAGIC);
73
0
  if(!nrows)
74
0
    return CURLE_BAD_FUNCTION_ARGUMENT;
75
0
  if(nrows != tbl->nrows) {
76
0
    void **rows = calloc(nrows, sizeof(void *));
77
0
    if(!rows)
78
0
      return CURLE_OUT_OF_MEMORY;
79
0
    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
0
    tbl->rows = rows;
86
0
    tbl->nrows = nrows;
87
0
  }
88
0
  return CURLE_OK;
89
0
}
90
91
92
void Curl_uint_tbl_destroy(struct uint_tbl *tbl)
93
0
{
94
0
  DEBUGASSERT(tbl->init == CURL_UINT_TBL_MAGIC);
95
0
  Curl_uint_tbl_clear(tbl);
96
0
  free(tbl->rows);
97
0
  memset(tbl, 0, sizeof(*tbl));
98
0
}
99
100
UNITTEST void Curl_uint_tbl_clear(struct uint_tbl *tbl)
101
0
{
102
0
  DEBUGASSERT(tbl->init == CURL_UINT_TBL_MAGIC);
103
0
  uint_tbl_clear_rows(tbl, 0, tbl->nrows);
104
0
  DEBUGASSERT(!tbl->nentries);
105
0
  tbl->last_key_added = UINT_MAX;
106
0
}
107
108
109
unsigned int Curl_uint_tbl_capacity(struct uint_tbl *tbl)
110
0
{
111
0
  return tbl->nrows;
112
0
}
113
114
115
unsigned int Curl_uint_tbl_count(struct uint_tbl *tbl)
116
0
{
117
0
  return tbl->nentries;
118
0
}
119
120
121
void *Curl_uint_tbl_get(struct uint_tbl *tbl, unsigned int key)
122
0
{
123
0
  return (key < tbl->nrows) ? tbl->rows[key] : NULL;
124
0
}
125
126
127
bool Curl_uint_tbl_add(struct uint_tbl *tbl, void *entry, unsigned int *pkey)
128
0
{
129
0
  unsigned int key, start_pos;
130
131
0
  DEBUGASSERT(tbl->init == CURL_UINT_TBL_MAGIC);
132
0
  if(!entry || !pkey)
133
0
    return FALSE;
134
0
  *pkey = UINT_MAX;
135
0
  if(tbl->nentries == tbl->nrows)  /* full */
136
0
    return FALSE;
137
138
0
  start_pos = CURLMIN(tbl->last_key_added, tbl->nrows) + 1;
139
0
  for(key = start_pos; key < tbl->nrows; ++key) {
140
0
    if(!tbl->rows[key]) {
141
0
      tbl->rows[key] = entry;
142
0
      tbl->nentries++;
143
0
      tbl->last_key_added = key;
144
0
      *pkey = key;
145
0
      return TRUE;
146
0
    }
147
0
  }
148
  /* no free entry at or above tbl->maybe_next_key, wrap around */
149
0
  for(key = 0; key < start_pos; ++key) {
150
0
    if(!tbl->rows[key]) {
151
0
      tbl->rows[key] = entry;
152
0
      tbl->nentries++;
153
0
      tbl->last_key_added = key;
154
0
      *pkey = key;
155
0
      return TRUE;
156
0
    }
157
0
  }
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
0
{
166
0
  uint_tbl_clear_rows(tbl, key, key + 1);
167
0
}
168
169
170
bool Curl_uint_tbl_contains(struct uint_tbl *tbl, unsigned int key)
171
0
{
172
0
  return (key < tbl->nrows) ? !!tbl->rows[key] : FALSE;
173
0
}
174
175
176
static bool uint_tbl_next_at(struct uint_tbl *tbl, unsigned int key,
177
                             unsigned int *pkey, void **pentry)
178
0
{
179
0
  for(; key < tbl->nrows; ++key) {
180
0
    if(tbl->rows[key]) {
181
0
      *pkey = key;
182
0
      *pentry = tbl->rows[key];
183
0
      return TRUE;
184
0
    }
185
0
  }
186
0
  *pkey = UINT_MAX;  /* always invalid */
187
0
  *pentry = NULL;
188
0
  return FALSE;
189
0
}
190
191
bool Curl_uint_tbl_first(struct uint_tbl *tbl,
192
                         unsigned int *pkey, void **pentry)
193
0
{
194
0
  if(!pkey || !pentry)
195
0
    return FALSE;
196
0
  if(tbl->nentries && uint_tbl_next_at(tbl, 0, pkey, pentry))
197
0
    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
0
{
208
0
  if(!pkey || !pentry)
209
0
    return FALSE;
210
0
  if(uint_tbl_next_at(tbl, last_key + 1, pkey, pentry))
211
0
    return TRUE;
212
0
  *pkey = UINT_MAX;  /* always invalid */
213
0
  *pentry = NULL;
214
  return FALSE;
215
0
}