Coverage Report

Created: 2025-07-23 09:13

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