1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
|
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
#include "curl_setup.h"
#include "uint-table.h"
#ifdef DEBUGBUILD
#define CURL_UINT32_TBL_MAGIC 0x62757473
#endif
/* Clear the table, making it empty. */
UNITTEST void Curl_uint32_tbl_clear(struct uint32_tbl *tbl);
void Curl_uint32_tbl_init(struct uint32_tbl *tbl,
Curl_uint32_tbl_entry_dtor *entry_dtor)
{
memset(tbl, 0, sizeof(*tbl));
tbl->entry_dtor = entry_dtor;
tbl->last_key_added = UINT32_MAX;
#ifdef DEBUGBUILD
tbl->init = CURL_UINT32_TBL_MAGIC;
#endif
}
static void uint32_tbl_clear_rows(struct uint32_tbl *tbl,
uint32_t from,
uint32_t upto_excluding)
{
uint32_t i, end;
end = CURLMIN(upto_excluding, tbl->nrows);
for(i = from; i < end; ++i) {
if(tbl->rows[i]) {
if(tbl->entry_dtor)
tbl->entry_dtor(i, tbl->rows[i]);
tbl->rows[i] = NULL;
tbl->nentries--;
}
}
}
CURLcode Curl_uint32_tbl_resize(struct uint32_tbl *tbl, uint32_t nrows)
{
/* we use `tbl->nrows + 1` during iteration, want that to work */
DEBUGASSERT(tbl->init == CURL_UINT32_TBL_MAGIC);
if(!nrows)
return CURLE_BAD_FUNCTION_ARGUMENT;
if(nrows != tbl->nrows) {
void **rows = curlx_calloc(nrows, sizeof(void *));
if(!rows)
return CURLE_OUT_OF_MEMORY;
if(tbl->rows) {
memcpy(rows, tbl->rows, (CURLMIN(nrows, tbl->nrows) * sizeof(void *)));
if(nrows < tbl->nrows)
uint32_tbl_clear_rows(tbl, nrows, tbl->nrows);
curlx_free(tbl->rows);
}
tbl->rows = rows;
tbl->nrows = nrows;
}
return CURLE_OK;
}
void Curl_uint32_tbl_destroy(struct uint32_tbl *tbl)
{
DEBUGASSERT(tbl->init == CURL_UINT32_TBL_MAGIC);
Curl_uint32_tbl_clear(tbl);
curlx_free(tbl->rows);
memset(tbl, 0, sizeof(*tbl));
}
UNITTEST void Curl_uint32_tbl_clear(struct uint32_tbl *tbl)
{
DEBUGASSERT(tbl->init == CURL_UINT32_TBL_MAGIC);
uint32_tbl_clear_rows(tbl, 0, tbl->nrows);
DEBUGASSERT(!tbl->nentries);
tbl->last_key_added = UINT32_MAX;
}
uint32_t Curl_uint32_tbl_capacity(struct uint32_tbl *tbl)
{
return tbl->nrows;
}
uint32_t Curl_uint32_tbl_count(struct uint32_tbl *tbl)
{
return tbl->nentries;
}
void *Curl_uint32_tbl_get(struct uint32_tbl *tbl, uint32_t key)
{
return (key < tbl->nrows) ? tbl->rows[key] : NULL;
}
bool Curl_uint32_tbl_add(struct uint32_tbl *tbl, void *entry, uint32_t *pkey)
{
uint32_t key, start_pos;
DEBUGASSERT(tbl->init == CURL_UINT32_TBL_MAGIC);
if(!entry || !pkey)
return FALSE;
*pkey = UINT32_MAX;
if(tbl->nentries == tbl->nrows) /* full */
return FALSE;
start_pos = CURLMIN(tbl->last_key_added, tbl->nrows) + 1;
for(key = start_pos; key < tbl->nrows; ++key) {
if(!tbl->rows[key]) {
tbl->rows[key] = entry;
tbl->nentries++;
tbl->last_key_added = key;
*pkey = key;
return TRUE;
}
}
/* no free entry at or above tbl->maybe_next_key, wrap around */
for(key = 0; key < start_pos; ++key) {
if(!tbl->rows[key]) {
tbl->rows[key] = entry;
tbl->nentries++;
tbl->last_key_added = key;
*pkey = key;
return TRUE;
}
}
/* Did not find any free row? Should not happen */
DEBUGASSERT(0);
return FALSE;
}
void Curl_uint32_tbl_remove(struct uint32_tbl *tbl, uint32_t key)
{
uint32_tbl_clear_rows(tbl, key, key + 1);
}
bool Curl_uint32_tbl_contains(struct uint32_tbl *tbl, uint32_t key)
{
return (key < tbl->nrows) ? !!tbl->rows[key] : FALSE;
}
static bool uint32_tbl_next_at(struct uint32_tbl *tbl, uint32_t key,
uint32_t *pkey, void **pentry)
{
for(; key < tbl->nrows; ++key) {
if(tbl->rows[key]) {
*pkey = key;
*pentry = tbl->rows[key];
return TRUE;
}
}
*pkey = UINT32_MAX; /* always invalid */
*pentry = NULL;
return FALSE;
}
bool Curl_uint32_tbl_first(struct uint32_tbl *tbl,
uint32_t *pkey, void **pentry)
{
if(!pkey || !pentry)
return FALSE;
if(tbl->nentries && uint32_tbl_next_at(tbl, 0, pkey, pentry))
return TRUE;
DEBUGASSERT(!tbl->nentries);
*pkey = UINT32_MAX; /* always invalid */
*pentry = NULL;
return FALSE;
}
bool Curl_uint32_tbl_next(struct uint32_tbl *tbl, uint32_t last_key,
uint32_t *pkey, void **pentry)
{
if(!pkey || !pentry)
return FALSE;
if(uint32_tbl_next_at(tbl, last_key + 1, pkey, pentry))
return TRUE;
*pkey = UINT32_MAX; /* always invalid */
*pentry = NULL;
return FALSE;
}
|