1*6236dae4SAndroid Build Coastguard Worker<!-- 2*6236dae4SAndroid Build Coastguard WorkerCopyright (C) Daniel Stenberg, <[email protected]>, et al. 3*6236dae4SAndroid Build Coastguard Worker 4*6236dae4SAndroid Build Coastguard WorkerSPDX-License-Identifier: curl 5*6236dae4SAndroid Build Coastguard Worker--> 6*6236dae4SAndroid Build Coastguard Worker 7*6236dae4SAndroid Build Coastguard Worker# `hash` 8*6236dae4SAndroid Build Coastguard Worker 9*6236dae4SAndroid Build Coastguard Worker #include "hash.h" 10*6236dae4SAndroid Build Coastguard Worker 11*6236dae4SAndroid Build Coastguard WorkerThis is the internal module for doing hash tables. A hash table uses a hash 12*6236dae4SAndroid Build Coastguard Workerfunction to compute an index. On each index there is a separate linked list of 13*6236dae4SAndroid Build Coastguard Workerentries. 14*6236dae4SAndroid Build Coastguard Worker 15*6236dae4SAndroid Build Coastguard WorkerCreate a hash table. Add items. Retrieve items. Remove items. Destroy table. 16*6236dae4SAndroid Build Coastguard Worker 17*6236dae4SAndroid Build Coastguard Worker## `Curl_hash_init` 18*6236dae4SAndroid Build Coastguard Worker 19*6236dae4SAndroid Build Coastguard Worker~~~c 20*6236dae4SAndroid Build Coastguard Workervoid Curl_hash_init(struct Curl_hash *h, 21*6236dae4SAndroid Build Coastguard Worker size_t slots, 22*6236dae4SAndroid Build Coastguard Worker hash_function hfunc, 23*6236dae4SAndroid Build Coastguard Worker comp_function comparator, 24*6236dae4SAndroid Build Coastguard Worker Curl_hash_dtor dtor); 25*6236dae4SAndroid Build Coastguard Worker~~~ 26*6236dae4SAndroid Build Coastguard Worker 27*6236dae4SAndroid Build Coastguard WorkerThe call initializes a `struct Curl_hash`. 28*6236dae4SAndroid Build Coastguard Worker 29*6236dae4SAndroid Build Coastguard Worker- `slots` is the number of entries to create in the hash table. Larger is 30*6236dae4SAndroid Build Coastguard Worker better (faster lookups) but also uses more memory. 31*6236dae4SAndroid Build Coastguard Worker- `hfunc` is a function pointer to a function that returns a `size_t` value as 32*6236dae4SAndroid Build Coastguard Worker a checksum for an entry in this hash table. Ideally, it returns a unique 33*6236dae4SAndroid Build Coastguard Worker value for every entry ever added to the hash table, but hash collisions are 34*6236dae4SAndroid Build Coastguard Worker handled. 35*6236dae4SAndroid Build Coastguard Worker- `comparator` is a function pointer to a function that compares two hash 36*6236dae4SAndroid Build Coastguard Worker table entries. It should return non-zero if the compared items are 37*6236dae4SAndroid Build Coastguard Worker identical. 38*6236dae4SAndroid Build Coastguard Worker- `dtor` is a function pointer to a destructor called when an entry is removed 39*6236dae4SAndroid Build Coastguard Worker from the table 40*6236dae4SAndroid Build Coastguard Worker 41*6236dae4SAndroid Build Coastguard Worker## `Curl_hash_add` 42*6236dae4SAndroid Build Coastguard Worker 43*6236dae4SAndroid Build Coastguard Worker~~~c 44*6236dae4SAndroid Build Coastguard Workervoid * 45*6236dae4SAndroid Build Coastguard WorkerCurl_hash_add(struct Curl_hash *h, void *key, size_t key_len, void *p) 46*6236dae4SAndroid Build Coastguard Worker~~~ 47*6236dae4SAndroid Build Coastguard Worker 48*6236dae4SAndroid Build Coastguard WorkerThis call adds an entry to the hash. `key` points to the hash key and 49*6236dae4SAndroid Build Coastguard Worker`key_len` is the length of the hash key. `p` is a custom pointer. 50*6236dae4SAndroid Build Coastguard Worker 51*6236dae4SAndroid Build Coastguard WorkerIf there already was a match in the hash, that data is replaced with this new 52*6236dae4SAndroid Build Coastguard Workerentry. 53*6236dae4SAndroid Build Coastguard Worker 54*6236dae4SAndroid Build Coastguard WorkerThis function also lazily allocates the table if needed, as it is not done in 55*6236dae4SAndroid Build Coastguard Workerthe `Curl_hash_init` function. 56*6236dae4SAndroid Build Coastguard Worker 57*6236dae4SAndroid Build Coastguard WorkerReturns NULL on error, otherwise it returns a pointer to `p`. 58*6236dae4SAndroid Build Coastguard Worker 59*6236dae4SAndroid Build Coastguard Worker## `Curl_hash_add2` 60*6236dae4SAndroid Build Coastguard Worker 61*6236dae4SAndroid Build Coastguard Worker~~~c 62*6236dae4SAndroid Build Coastguard Workervoid *Curl_hash_add2(struct Curl_hash *h, void *key, size_t key_len, void *p, 63*6236dae4SAndroid Build Coastguard Worker Curl_hash_elem_dtor dtor) 64*6236dae4SAndroid Build Coastguard Worker~~~ 65*6236dae4SAndroid Build Coastguard Worker 66*6236dae4SAndroid Build Coastguard WorkerThis works like `Curl_hash_add` but has an extra argument: `dtor`, which is a 67*6236dae4SAndroid Build Coastguard Workerdestructor call for this specific entry. When this entry is removed, this 68*6236dae4SAndroid Build Coastguard Workerfunction is called instead of the function stored for the whole hash table. 69*6236dae4SAndroid Build Coastguard Worker 70*6236dae4SAndroid Build Coastguard Worker## `Curl_hash_delete` 71*6236dae4SAndroid Build Coastguard Worker 72*6236dae4SAndroid Build Coastguard Worker~~~c 73*6236dae4SAndroid Build Coastguard Workerint Curl_hash_delete(struct Curl_hash *h, void *key, size_t key_len); 74*6236dae4SAndroid Build Coastguard Worker~~~ 75*6236dae4SAndroid Build Coastguard Worker 76*6236dae4SAndroid Build Coastguard WorkerThis function removes an entry from the hash table. If successful, it returns 77*6236dae4SAndroid Build Coastguard Workerzero. If the entry was not found, it returns 1. 78*6236dae4SAndroid Build Coastguard Worker 79*6236dae4SAndroid Build Coastguard Worker## `Curl_hash_pick` 80*6236dae4SAndroid Build Coastguard Worker 81*6236dae4SAndroid Build Coastguard Worker~~~c 82*6236dae4SAndroid Build Coastguard Workervoid *Curl_hash_pick(struct Curl_hash *h, void *key, size_t key_len); 83*6236dae4SAndroid Build Coastguard Worker~~~ 84*6236dae4SAndroid Build Coastguard Worker 85*6236dae4SAndroid Build Coastguard WorkerIf there is an entry in the hash that matches the given `key` with size of 86*6236dae4SAndroid Build Coastguard Worker`key_len`, that its custom pointer is returned. The pointer that was called 87*6236dae4SAndroid Build Coastguard Worker`p` when the entry was added. 88*6236dae4SAndroid Build Coastguard Worker 89*6236dae4SAndroid Build Coastguard WorkerIt returns NULL if there is no matching entry in the hash. 90*6236dae4SAndroid Build Coastguard Worker 91*6236dae4SAndroid Build Coastguard Worker## `Curl_hash_destroy` 92*6236dae4SAndroid Build Coastguard Worker 93*6236dae4SAndroid Build Coastguard Worker~~~c 94*6236dae4SAndroid Build Coastguard Workervoid Curl_hash_destroy(struct Curl_hash *h); 95*6236dae4SAndroid Build Coastguard Worker~~~ 96*6236dae4SAndroid Build Coastguard Worker 97*6236dae4SAndroid Build Coastguard WorkerThis function destroys a hash and cleanups up all its related data. Calling it 98*6236dae4SAndroid Build Coastguard Workermultiple times is fine. 99*6236dae4SAndroid Build Coastguard Worker 100*6236dae4SAndroid Build Coastguard Worker## `Curl_hash_clean` 101*6236dae4SAndroid Build Coastguard Worker 102*6236dae4SAndroid Build Coastguard Worker~~~c 103*6236dae4SAndroid Build Coastguard Workervoid Curl_hash_clean(struct Curl_hash *h); 104*6236dae4SAndroid Build Coastguard Worker~~~ 105*6236dae4SAndroid Build Coastguard Worker 106*6236dae4SAndroid Build Coastguard WorkerThis function removes all the entries in the given hash. 107*6236dae4SAndroid Build Coastguard Worker 108*6236dae4SAndroid Build Coastguard Worker## `Curl_hash_clean_with_criterium` 109*6236dae4SAndroid Build Coastguard Worker 110*6236dae4SAndroid Build Coastguard Worker~~~c 111*6236dae4SAndroid Build Coastguard Workervoid 112*6236dae4SAndroid Build Coastguard WorkerCurl_hash_clean_with_criterium(struct Curl_hash *h, void *user, 113*6236dae4SAndroid Build Coastguard Worker int (*comp)(void *, void *)) 114*6236dae4SAndroid Build Coastguard Worker~~~ 115*6236dae4SAndroid Build Coastguard Worker 116*6236dae4SAndroid Build Coastguard WorkerThis function removes all the entries in the given hash that matches the 117*6236dae4SAndroid Build Coastguard Workercriterion. The provided `comp` function determines if the criteria is met by 118*6236dae4SAndroid Build Coastguard Workerreturning non-zero. 119*6236dae4SAndroid Build Coastguard Worker 120*6236dae4SAndroid Build Coastguard Worker## `Curl_hash_count` 121*6236dae4SAndroid Build Coastguard Worker 122*6236dae4SAndroid Build Coastguard Worker~~~c 123*6236dae4SAndroid Build Coastguard Workersize_t Curl_hash_count(struct Curl_hash *h) 124*6236dae4SAndroid Build Coastguard Worker~~~ 125*6236dae4SAndroid Build Coastguard Worker 126*6236dae4SAndroid Build Coastguard WorkerReturns the number of entries stored in the hash. 127*6236dae4SAndroid Build Coastguard Worker 128*6236dae4SAndroid Build Coastguard Worker## `Curl_hash_start_iterate` 129*6236dae4SAndroid Build Coastguard Worker 130*6236dae4SAndroid Build Coastguard Worker~~~c 131*6236dae4SAndroid Build Coastguard Workervoid Curl_hash_start_iterate(struct Curl_hash *hash, 132*6236dae4SAndroid Build Coastguard Worker struct Curl_hash_iterator *iter): 133*6236dae4SAndroid Build Coastguard Worker~~~ 134*6236dae4SAndroid Build Coastguard Worker 135*6236dae4SAndroid Build Coastguard WorkerThis function initializes a `struct Curl_hash_iterator` that `iter` points to. 136*6236dae4SAndroid Build Coastguard WorkerIt can then be used to iterate over all the entries in the hash. 137*6236dae4SAndroid Build Coastguard Worker 138*6236dae4SAndroid Build Coastguard Worker## `Curl_hash_next_element` 139*6236dae4SAndroid Build Coastguard Worker 140*6236dae4SAndroid Build Coastguard Worker~~~c 141*6236dae4SAndroid Build Coastguard Workerstruct Curl_hash_element * 142*6236dae4SAndroid Build Coastguard WorkerCurl_hash_next_element(struct Curl_hash_iterator *iter); 143*6236dae4SAndroid Build Coastguard Worker~~~ 144*6236dae4SAndroid Build Coastguard Worker 145*6236dae4SAndroid Build Coastguard WorkerGiven the iterator `iter`, this function returns a pointer to the next hash 146*6236dae4SAndroid Build Coastguard Workerentry if there is one, or NULL if there is no more entries. 147*6236dae4SAndroid Build Coastguard Worker 148*6236dae4SAndroid Build Coastguard WorkerCalled repeatedly, it iterates over all the entries in the hash table. 149*6236dae4SAndroid Build Coastguard Worker 150*6236dae4SAndroid Build Coastguard WorkerNote: it only guarantees functionality if the hash table remains untouched 151*6236dae4SAndroid Build Coastguard Workerduring its iteration. 152*6236dae4SAndroid Build Coastguard Worker 153*6236dae4SAndroid Build Coastguard Worker# `curl_off_t` dedicated hash functions 154*6236dae4SAndroid Build Coastguard Worker 155*6236dae4SAndroid Build Coastguard Worker## `Curl_hash_offt_init` 156*6236dae4SAndroid Build Coastguard Worker 157*6236dae4SAndroid Build Coastguard Worker~~~c 158*6236dae4SAndroid Build Coastguard Workervoid Curl_hash_offt_init(struct Curl_hash *h, 159*6236dae4SAndroid Build Coastguard Worker size_t slots, 160*6236dae4SAndroid Build Coastguard Worker Curl_hash_dtor dtor); 161*6236dae4SAndroid Build Coastguard Worker~~~ 162*6236dae4SAndroid Build Coastguard Worker 163*6236dae4SAndroid Build Coastguard WorkerInitializes a hash table for `curl_off_t` values. Pass in desired number of 164*6236dae4SAndroid Build Coastguard Worker`slots` and `dtor` function. 165*6236dae4SAndroid Build Coastguard Worker 166*6236dae4SAndroid Build Coastguard Worker## `Curl_hash_offt_set` 167*6236dae4SAndroid Build Coastguard Worker 168*6236dae4SAndroid Build Coastguard Worker~~~c 169*6236dae4SAndroid Build Coastguard Workervoid *Curl_hash_offt_set(struct Curl_hash *h, curl_off_t id, void *elem); 170*6236dae4SAndroid Build Coastguard Worker~~~ 171*6236dae4SAndroid Build Coastguard Worker 172*6236dae4SAndroid Build Coastguard WorkerAssociate a custom `elem` pointer with the given `id`. 173*6236dae4SAndroid Build Coastguard Worker 174*6236dae4SAndroid Build Coastguard Worker## `Curl_hash_offt_remove` 175*6236dae4SAndroid Build Coastguard Worker 176*6236dae4SAndroid Build Coastguard Worker~~~c 177*6236dae4SAndroid Build Coastguard Workerint Curl_hash_offt_remove(struct Curl_hash *h, curl_off_t id); 178*6236dae4SAndroid Build Coastguard Worker~~~ 179*6236dae4SAndroid Build Coastguard Worker 180*6236dae4SAndroid Build Coastguard WorkerRemove the `id` from the hash. 181*6236dae4SAndroid Build Coastguard Worker 182*6236dae4SAndroid Build Coastguard Worker## `Curl_hash_offt_get` 183*6236dae4SAndroid Build Coastguard Worker 184*6236dae4SAndroid Build Coastguard Worker~~~c 185*6236dae4SAndroid Build Coastguard Workervoid *Curl_hash_offt_get(struct Curl_hash *h, curl_off_t id); 186*6236dae4SAndroid Build Coastguard Worker~~~ 187*6236dae4SAndroid Build Coastguard Worker 188*6236dae4SAndroid Build Coastguard WorkerGet the pointer associated with the specified `id`. 189