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 201 202 203 204 205 206 207 208 209 210 211 212 213 214
|
/***************************************************************************
* _ _ ____ _
* 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"
/* The last 3 #include files should be in this order */
#include "curl_printf.h"
#include "curl_memory.h"
#include "memdebug.h"
#ifdef DEBUGBUILD
#define CURL_UINT_TBL_MAGIC 0x62757473
#endif
void Curl_uint_tbl_init(struct uint_tbl *tbl,
Curl_uint_tbl_entry_dtor *entry_dtor)
{
memset(tbl, 0, sizeof(*tbl));
tbl->entry_dtor = entry_dtor;
tbl->last_key_added = UINT_MAX;
#ifdef DEBUGBUILD
tbl->init = CURL_UINT_TBL_MAGIC;
#endif
}
static void uint_tbl_clear_rows(struct uint_tbl *tbl,
unsigned int from,
unsigned int upto_excluding)
{
unsigned int 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_uint_tbl_resize(struct uint_tbl *tbl, unsigned int nrows)
{
/* we use `tbl->nrows + 1` during iteration, want that to work */
DEBUGASSERT(tbl->init == CURL_UINT_TBL_MAGIC);
if(!nrows || (nrows == UINT_MAX))
return CURLE_BAD_FUNCTION_ARGUMENT;
if(nrows != tbl->nrows) {
void **rows = 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)
uint_tbl_clear_rows(tbl, nrows, tbl->nrows);
free(tbl->rows);
}
tbl->rows = rows;
tbl->nrows = nrows;
}
return CURLE_OK;
}
void Curl_uint_tbl_destroy(struct uint_tbl *tbl)
{
DEBUGASSERT(tbl->init == CURL_UINT_TBL_MAGIC);
Curl_uint_tbl_clear(tbl);
free(tbl->rows);
memset(tbl, 0, sizeof(*tbl));
}
void Curl_uint_tbl_clear(struct uint_tbl *tbl)
{
DEBUGASSERT(tbl->init == CURL_UINT_TBL_MAGIC);
uint_tbl_clear_rows(tbl, 0, tbl->nrows);
DEBUGASSERT(!tbl->nentries);
tbl->last_key_added = UINT_MAX;
}
unsigned int Curl_uint_tbl_capacity(struct uint_tbl *tbl)
{
return tbl->nrows;
}
unsigned int Curl_uint_tbl_count(struct uint_tbl *tbl)
{
return tbl->nentries;
}
void *Curl_uint_tbl_get(struct uint_tbl *tbl, unsigned int key)
{
return (key < tbl->nrows) ? tbl->rows[key] : NULL;
}
bool Curl_uint_tbl_add(struct uint_tbl *tbl, void *entry, unsigned int *pkey)
{
unsigned int key, start_pos;
DEBUGASSERT(tbl->init == CURL_UINT_TBL_MAGIC);
if(!entry || !pkey)
return FALSE;
*pkey = UINT_MAX; /* always invalid */
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_uint_tbl_remove(struct uint_tbl *tbl, unsigned int key)
{
uint_tbl_clear_rows(tbl, key, key + 1);
}
bool Curl_uint_tbl_contains(struct uint_tbl *tbl, unsigned int key)
{
return (key < tbl->nrows) ? !!tbl->rows[key] : FALSE;
}
static bool uint_tbl_next_at(struct uint_tbl *tbl, unsigned int key,
unsigned int *pkey, void **pentry)
{
for(; key < tbl->nrows; ++key) {
if(tbl->rows[key]) {
*pkey = key;
*pentry = tbl->rows[key];
return TRUE;
}
}
*pkey = UINT_MAX; /* always invalid */
*pentry = NULL;
return FALSE;
}
bool Curl_uint_tbl_first(struct uint_tbl *tbl,
unsigned int *pkey, void **pentry)
{
if(!pkey || !pentry)
return FALSE;
if(tbl->nentries && uint_tbl_next_at(tbl, 0, pkey, pentry))
return TRUE;
DEBUGASSERT(!tbl->nentries);
*pkey = UINT_MAX; /* always invalid */
*pentry = NULL;
return FALSE;
}
bool Curl_uint_tbl_next(struct uint_tbl *tbl, unsigned int last_key,
unsigned int *pkey, void **pentry)
{
if(!pkey || !pentry)
return FALSE;
if(uint_tbl_next_at(tbl, last_key + 1, pkey, pentry))
return TRUE;
*pkey = UINT_MAX; /* always invalid */
*pentry = NULL;
return FALSE;
}
|