uint-table.h (3984B)
1 #ifndef HEADER_CURL_UINT_TABLE_H 2 #define HEADER_CURL_UINT_TABLE_H 3 /*************************************************************************** 4 * _ _ ____ _ 5 * Project ___| | | | _ \| | 6 * / __| | | | |_) | | 7 * | (__| |_| | _ <| |___ 8 * \___|\___/|_| \_\_____| 9 * 10 * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al. 11 * 12 * This software is licensed as described in the file COPYING, which 13 * you should have received as part of this distribution. The terms 14 * are also available at https://curl.se/docs/copyright.html. 15 * 16 * You may opt to use, copy, modify, merge, publish, distribute and/or sell 17 * copies of the Software, and permit persons to whom the Software is 18 * furnished to do so, under the terms of the COPYING file. 19 * 20 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY 21 * KIND, either express or implied. 22 * 23 * SPDX-License-Identifier: curl 24 * 25 ***************************************************************************/ 26 #include "curl_setup.h" 27 28 #include <curl/curl.h> 29 30 /* Destructor for a single table entry */ 31 typedef void Curl_uint_tbl_entry_dtor(unsigned int key, void *entry); 32 33 struct uint_tbl { 34 void **rows; /* array of void* holding entries */ 35 Curl_uint_tbl_entry_dtor *entry_dtor; 36 unsigned int nrows; /* length of `rows` array */ 37 unsigned int nentries; /* entries in table */ 38 unsigned int last_key_added; /* UINT_MAX or last key added */ 39 #ifdef DEBUGBUILD 40 int init; 41 #endif 42 }; 43 44 /* Initialize the table with 0 capacity. 45 * The optional `entry_dtor` is called when a table entry is removed, 46 * Passing NULL means no action is taken on removal. */ 47 void Curl_uint_tbl_init(struct uint_tbl *tbl, 48 Curl_uint_tbl_entry_dtor *entry_dtor); 49 50 /* Resize the table to change capacity `nmax`. When `nmax` is reduced, 51 * all present entries with key equal or larger to `nmax` are removed. */ 52 CURLcode Curl_uint_tbl_resize(struct uint_tbl *tbl, unsigned int nmax); 53 54 /* Destroy the table, freeing all entries. */ 55 void Curl_uint_tbl_destroy(struct uint_tbl *tbl); 56 57 /* Get the table capacity. */ 58 unsigned int Curl_uint_tbl_capacity(struct uint_tbl *tbl); 59 60 /* Get the number of entries in the table. */ 61 unsigned int Curl_uint_tbl_count(struct uint_tbl *tbl); 62 63 /* Get the entry for key or NULL if not present */ 64 void *Curl_uint_tbl_get(struct uint_tbl *tbl, unsigned int key); 65 66 /* Add a new entry to the table and assign it a free key. 67 * Returns FALSE if the table is full. 68 * 69 * Keys are assigned in a round-robin manner. 70 * No matter the capacity, UINT_MAX is never assigned. */ 71 bool Curl_uint_tbl_add(struct uint_tbl *tbl, void *entry, unsigned int *pkey); 72 73 /* Remove the entry with `key`. */ 74 void Curl_uint_tbl_remove(struct uint_tbl *tbl, unsigned int key); 75 76 /* Return TRUE if the table contains an tryn with that keys. */ 77 bool Curl_uint_tbl_contains(struct uint_tbl *tbl, unsigned int key); 78 79 /* Get the first entry in the table (with the smallest `key`). 80 * Returns FALSE if the table is empty. */ 81 bool Curl_uint_tbl_first(struct uint_tbl *tbl, 82 unsigned int *pkey, void **pentry); 83 84 /* Get the next key in the table, following `last_key` in natural order. 85 * Put another way, this is the smallest key greater than `last_key` in 86 * the table. `last_key` does not have to be present in the table. 87 * 88 * Returns FALSE when no such entry is in the table. 89 * 90 * This allows to iterate the table while being modified: 91 * - added keys higher than 'last_key' will be picked up by the iteration. 92 * - added keys lower than 'last_key' will not show up. 93 * - removed keys lower or equal to 'last_key' will not show up. 94 * - removed keys higher than 'last_key' will not be visited. */ 95 bool Curl_uint_tbl_next(struct uint_tbl *tbl, unsigned int last_key, 96 unsigned int *pkey, void **pentry); 97 98 #endif /* HEADER_CURL_UINT_TABLE_H */