Coverage Report

Created: 2025-07-11 06:33

/src/PROJ/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
0
{
43
0
  memset(tbl, 0, sizeof(*tbl));
44
0
  tbl->entry_dtor = entry_dtor;
45
0
  tbl->last_key_added = UINT_MAX;
46
#ifdef DEBUGBUILD
47
  tbl->init = CURL_UINT_TBL_MAGIC;
48
#endif
49
0
}
50
51
52
static void uint_tbl_clear_rows(struct uint_tbl *tbl,
53
                                unsigned int from,
54
                                unsigned int upto_excluding)
55
0
{
56
0
  unsigned int i, end;
57
58
0
  end = CURLMIN(upto_excluding, tbl->nrows);
59
0
  for(i = from; i < end; ++i) {
60
0
    if(tbl->rows[i]) {
61
0
      if(tbl->entry_dtor)
62
0
        tbl->entry_dtor(i, tbl->rows[i]);
63
0
      tbl->rows[i] = NULL;
64
0
      tbl->nentries--;
65
0
    }
66
0
  }
67
0
}
68
69
70
CURLcode Curl_uint_tbl_resize(struct uint_tbl *tbl, unsigned int nrows)
71
0
{
72
  /* we use `tbl->nrows + 1` during iteration, want that to work */
73
0
  DEBUGASSERT(tbl->init == CURL_UINT_TBL_MAGIC);
74
0
  if(!nrows)
75
0
    return CURLE_BAD_FUNCTION_ARGUMENT;
76
0
  if(nrows != tbl->nrows) {
77
0
    void **rows = calloc(nrows, sizeof(void *));
78
0
    if(!rows)
79
0
      return CURLE_OUT_OF_MEMORY;
80
0
    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
0
    tbl->rows = rows;
87
0
    tbl->nrows = nrows;
88
0
  }
89
0
  return CURLE_OK;
90
0
}
91
92
93
void Curl_uint_tbl_destroy(struct uint_tbl *tbl)
94
0
{
95
0
  DEBUGASSERT(tbl->init == CURL_UINT_TBL_MAGIC);
96
0
  Curl_uint_tbl_clear(tbl);
97
0
  free(tbl->rows);
98
0
  memset(tbl, 0, sizeof(*tbl));
99
0
}
100
101
UNITTEST void Curl_uint_tbl_clear(struct uint_tbl *tbl)
102
0
{
103
0
  DEBUGASSERT(tbl->init == CURL_UINT_TBL_MAGIC);
104
0
  uint_tbl_clear_rows(tbl, 0, tbl->nrows);
105
0
  DEBUGASSERT(!tbl->nentries);
106
0
  tbl->last_key_added = UINT_MAX;
107
0
}
108
109
110
unsigned int Curl_uint_tbl_capacity(struct uint_tbl *tbl)
111
0
{
112
0
  return tbl->nrows;
113
0
}
114
115
116
unsigned int Curl_uint_tbl_count(struct uint_tbl *tbl)
117
0
{
118
0
  return tbl->nentries;
119
0
}
120
121
122
void *Curl_uint_tbl_get(struct uint_tbl *tbl, unsigned int key)
123
0
{
124
0
  return (key < tbl->nrows) ? tbl->rows[key] : NULL;
125
0
}
126
127
128
bool Curl_uint_tbl_add(struct uint_tbl *tbl, void *entry, unsigned int *pkey)
129
0
{
130
0
  unsigned int key, start_pos;
131
132
0
  DEBUGASSERT(tbl->init == CURL_UINT_TBL_MAGIC);
133
0
  if(!entry || !pkey)
134
0
    return FALSE;
135
0
  *pkey = UINT_MAX;
136
0
  if(tbl->nentries == tbl->nrows)  /* full */
137
0
    return FALSE;
138
139
0
  start_pos = CURLMIN(tbl->last_key_added, tbl->nrows) + 1;
140
0
  for(key = start_pos; key < tbl->nrows; ++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
  /* no free entry at or above tbl->maybe_next_key, wrap around */
150
0
  for(key = 0; key < start_pos; ++key) {
151
0
    if(!tbl->rows[key]) {
152
0
      tbl->rows[key] = entry;
153
0
      tbl->nentries++;
154
0
      tbl->last_key_added = key;
155
0
      *pkey = key;
156
0
      return TRUE;
157
0
    }
158
0
  }
159
  /* Did not find any free row? Should not happen */
160
0
  DEBUGASSERT(0);
161
0
  return FALSE;
162
0
}
163
164
165
void Curl_uint_tbl_remove(struct uint_tbl *tbl, unsigned int key)
166
0
{
167
0
  uint_tbl_clear_rows(tbl, key, key + 1);
168
0
}
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
0
{
180
0
  for(; key < tbl->nrows; ++key) {
181
0
    if(tbl->rows[key]) {
182
0
      *pkey = key;
183
0
      *pentry = tbl->rows[key];
184
0
      return TRUE;
185
0
    }
186
0
  }
187
0
  *pkey = UINT_MAX;  /* always invalid */
188
0
  *pentry = NULL;
189
0
  return FALSE;
190
0
}
191
192
bool Curl_uint_tbl_first(struct uint_tbl *tbl,
193
                         unsigned int *pkey, void **pentry)
194
0
{
195
0
  if(!pkey || !pentry)
196
0
    return FALSE;
197
0
  if(tbl->nentries && uint_tbl_next_at(tbl, 0, pkey, pentry))
198
0
    return TRUE;
199
0
  DEBUGASSERT(!tbl->nentries);
200
0
  *pkey = UINT_MAX;  /* always invalid */
201
0
  *pentry = NULL;
202
0
  return FALSE;
203
0
}
204
205
206
bool Curl_uint_tbl_next(struct uint_tbl *tbl, unsigned int last_key,
207
                        unsigned int *pkey, void **pentry)
208
0
{
209
0
  if(!pkey || !pentry)
210
0
    return FALSE;
211
0
  if(uint_tbl_next_at(tbl, last_key + 1, pkey, pentry))
212
0
    return TRUE;
213
0
  *pkey = UINT_MAX;  /* always invalid */
214
0
  *pentry = NULL;
215
0
  return FALSE;
216
0
}