curl/lib/hash_offt.h
Stefan Eissing 909af1a43b
multi: do transfer book keeping using mid
Change multi's book keeping of transfers to no longer use lists, but a
special table and bitsets for unsigned int values.

`multi-xfers` is the `uint_tbl` where `multi_add_handle()` inserts a new
transfer which assigns it a unique identifier `mid`. Use bitsets to keep
track of transfers that are in state "process" or "pending" or
"msgsent".

Use sparse bitsets to replace `conn->easyq` and event handlings tracking
of transfers per socket. Instead of pointers, keep the mids involved.

Provide base data structures and document them in docs/internal:
* `uint_tbl`: a table of transfers with `mid` as lookup key,
   handing out a mid for adds between 0 - capacity.
* `uint_bset`: a bitset keeping unsigned ints from 0 - capacity.
* `uint_spbset`: a sparse bitset for keeping a small number of
  unsigned int values
* `uint_hash`: for associating `mid`s with a pointer.

This makes the `mid` the recommended way to refer to transfers inside
the same multi without risk of running into a UAF.

Modifying table and bitsets is safe while iterating over them. Overall
memory requirements are lower as with the double linked list apprach.

Closes #16761
2025-04-17 17:28:38 +02:00

93 lines
3 KiB
C

#ifndef HEADER_CURL_HASH_OFFT_H
#define HEADER_CURL_HASH_OFFT_H
/***************************************************************************
* _ _ ____ _
* 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 <stddef.h>
#include "llist.h"
struct Curl_hash_offt_entry;
typedef void Curl_hash_offt_dtor(curl_off_t id, void *value);
/* Hash for `curl_off_t` as key */
struct Curl_hash_offt {
struct Curl_hash_offt_entry **table;
Curl_hash_offt_dtor *dtor;
size_t slots;
size_t size;
#ifdef DEBUGBUILD
int init;
#endif
};
void Curl_hash_offt_init(struct Curl_hash_offt *h,
size_t slots,
Curl_hash_offt_dtor *dtor);
void Curl_hash_offt_destroy(struct Curl_hash_offt *h);
bool Curl_hash_offt_set(struct Curl_hash_offt *h, curl_off_t id, void *value);
bool Curl_hash_offt_remove(struct Curl_hash_offt *h, curl_off_t id);
void *Curl_hash_offt_get(struct Curl_hash_offt *h, curl_off_t id);
void Curl_hash_offt_clear(struct Curl_hash_offt *h);
size_t Curl_hash_offt_count(struct Curl_hash_offt *h);
/* A version with unsigned int as key */
typedef void Curl_uint_hash_dtor(unsigned int id, void *value);
struct uint_hash_entry;
/* Hash for `unsigned int` as key */
struct uint_hash {
struct uint_hash_entry **table;
Curl_uint_hash_dtor *dtor;
unsigned int slots;
unsigned int size;
#ifdef DEBUGBUILD
int init;
#endif
};
void Curl_uint_hash_init(struct uint_hash *h,
unsigned int slots,
Curl_uint_hash_dtor *dtor);
void Curl_uint_hash_destroy(struct uint_hash *h);
bool Curl_uint_hash_set(struct uint_hash *h, unsigned int id, void *value);
bool Curl_uint_hash_remove(struct uint_hash *h, unsigned int id);
void *Curl_uint_hash_get(struct uint_hash *h, unsigned int id);
unsigned int Curl_uint_hash_count(struct uint_hash *h);
typedef bool Curl_uint_hash_visit_cb(unsigned int id, void *value,
void *user_data);
void Curl_uint_hash_visit(struct uint_hash *h,
Curl_uint_hash_visit_cb *cb,
void *user_data);
#endif /* HEADER_CURL_HASH_OFFT_H */