NetSurf
Data Structures | Typedefs | Functions
hashmap.h File Reference
#include <stdint.h>
#include <stdbool.h>
Include dependency graph for hashmap.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  hashmap_parameters_t
 Parameters for hashmaps. More...
 

Typedefs

typedef struct hashmap_s hashmap_t
 Generic hashmap. More...
 
typedef void *(* hashmap_key_clone_t) (void *)
 Key cloning function type. More...
 
typedef void(* hashmap_key_destroy_t) (void *)
 Key destructor function type. More...
 
typedef uint32_t(* hashmap_key_hash_t) (void *)
 Key hashing function type. More...
 
typedef bool(* hashmap_key_eq_t) (void *, void *)
 Key comparison function type. More...
 
typedef void *(* hashmap_value_alloc_t) (void *)
 Value allocation function type. More...
 
typedef void(* hashmap_value_destroy_t) (void *)
 Value destructor function type. More...
 
typedef bool(* hashmap_iteration_cb_t) (void *, void *, void *)
 Hashmap iteration callback function type. More...
 

Functions

hashmap_thashmap_create (hashmap_parameters_t *params)
 Create a hashmap. More...
 
void hashmap_destroy (hashmap_t *hashmap)
 Destroy a hashmap. More...
 
void * hashmap_lookup (hashmap_t *hashmap, void *key)
 Look up a key in a hashmap. More...
 
void * hashmap_insert (hashmap_t *hashmap, void *key)
 Create an entry in a hashmap. More...
 
bool hashmap_remove (hashmap_t *hashmap, void *key)
 Remove an entry from the hashmap. More...
 
bool hashmap_iterate (hashmap_t *hashmap, hashmap_iteration_cb_t cb, void *ctx)
 Iterate the hashmap. More...
 
size_t hashmap_count (hashmap_t *hashmap)
 Get the number of entries in this map. More...
 

Typedef Documentation

◆ hashmap_iteration_cb_t

typedef bool(* hashmap_iteration_cb_t) (void *, void *, void *)

Hashmap iteration callback function type.

First parameter is the key, second is the value. The final parameter is the context pointer for the iteration.

Return true to stop iterating early

Definition at line 72 of file hashmap.h.

◆ hashmap_key_clone_t

typedef void *(* hashmap_key_clone_t) (void *)

Key cloning function type.

Definition at line 37 of file hashmap.h.

◆ hashmap_key_destroy_t

typedef void(* hashmap_key_destroy_t) (void *)

Key destructor function type.

Definition at line 42 of file hashmap.h.

◆ hashmap_key_eq_t

typedef bool(* hashmap_key_eq_t) (void *, void *)

Key comparison function type.

Definition at line 52 of file hashmap.h.

◆ hashmap_key_hash_t

typedef uint32_t(* hashmap_key_hash_t) (void *)

Key hashing function type.

Definition at line 47 of file hashmap.h.

◆ hashmap_t

typedef struct hashmap_s hashmap_t

Generic hashmap.

Hashmaps take ownership of the keys inserted into them by means of a clone function in their parameters. They also manage the value memory directly.

Definition at line 32 of file hashmap.h.

◆ hashmap_value_alloc_t

typedef void *(* hashmap_value_alloc_t) (void *)

Value allocation function type.

Definition at line 57 of file hashmap.h.

◆ hashmap_value_destroy_t

typedef void(* hashmap_value_destroy_t) (void *)

Value destructor function type.

Definition at line 62 of file hashmap.h.

Function Documentation

◆ hashmap_count()

size_t hashmap_count ( hashmap_t hashmap)

Get the number of entries in this map.

Parameters
hashmapThe hashmap to retrieve the entry count from
Returns
The number of entries in the hashmap

Definition at line 252 of file hashmap.c.

References hashmap_s::entry_count.

Referenced by store_evict().

Here is the caller graph for this function:

◆ hashmap_create()

hashmap_t * hashmap_create ( hashmap_parameters_t params)

Create a hashmap.

The provided hashmap parameter table will be used for map operations which need to allocate/free etc.

Parameters
paramsThe hashmap parameters for this map

Definition at line 67 of file hashmap.c.

References hashmap_s::bucket_count, hashmap_s::buckets, DEFAULT_HASHMAP_BUCKETS, hashmap_s::entry_count, and hashmap_s::params.

Referenced by fetch_curl_register(), and read_entries().

Here is the caller graph for this function:

◆ hashmap_destroy()

void hashmap_destroy ( hashmap_t hashmap)

Destroy a hashmap.

After this, all keys and values will have been destroyed and all memory associated with this hashmap will be invalidated.

Parameters
hashmapThe hashmap to destroy

Definition at line 91 of file hashmap.c.

References hashmap_s::bucket_count, hashmap_s::buckets, hashmap_entry_s::key, hashmap_parameters_t::key_destroy, hashmap_entry_s::next, hashmap_s::params, hashmap_entry_s::value, and hashmap_parameters_t::value_destroy.

Referenced by fetch_curl_finalise(), finalise(), and initialise().

Here is the caller graph for this function:

◆ hashmap_insert()

void * hashmap_insert ( hashmap_t hashmap,
void *  key 
)

Create an entry in a hashmap.

This creates a blank value using the parameters and then associates it with a clone of the given key, inserting it into the hashmap. If a value was present for the given key already, then it is destroyed first.

NOTE: If allocation of the new value object fails, then any existing entry will be left alone, but NULL will be returned.

Parameters
hashmapThe hashmap to insert into
keyThe key to insert an entry for
Returns
The value pointer for that key, or NULL if allocation failed.

Definition at line 131 of file hashmap.c.

References hashmap_s::bucket_count, hashmap_s::buckets, hashmap_s::entry_count, hashmap_entry_s::key, hashmap_parameters_t::key_clone, hashmap_parameters_t::key_destroy, hashmap_parameters_t::key_eq, hashmap_entry_s::key_hash, hashmap_parameters_t::key_hash, hashmap_entry_s::next, hashmap_s::params, hashmap_entry_s::prevptr, hashmap_entry_s::value, hashmap_parameters_t::value_alloc, and hashmap_parameters_t::value_destroy.

Referenced by read_entries(), and set_store_entry().

Here is the caller graph for this function:

◆ hashmap_iterate()

bool hashmap_iterate ( hashmap_t hashmap,
hashmap_iteration_cb_t  cb,
void *  ctx 
)

Iterate the hashmap.

For each key/value pair in the hashmap, call the callback passing in the key and value. During iteration you MUST NOT mutate the hashmap.

Parameters
hashmapThe hashmap to iterate
cbThe callback for each key,value pair
ctxThe callback context
Returns
Whether or not we stopped iteration early

Definition at line 233 of file hashmap.c.

References hashmap_s::bucket_count, hashmap_s::buckets, and hashmap_entry_s::next.

Referenced by store_evict(), and write_entries().

Here is the caller graph for this function:

◆ hashmap_lookup()

void * hashmap_lookup ( hashmap_t hashmap,
void *  key 
)

Look up a key in a hashmap.

If the key has an associated value in the hashmap then the pointer to it is returned, otherwise NULL.

Parameters
hashmapThe hashmap to look up the key inside
keyThe key to look up in the hashmap
Returns
A pointer to the value if found, NULL otherwise

Definition at line 113 of file hashmap.c.

References hashmap_s::bucket_count, hashmap_s::buckets, hashmap_entry_s::key, hashmap_parameters_t::key_eq, hashmap_entry_s::key_hash, hashmap_parameters_t::key_hash, hashmap_entry_s::next, hashmap_s::params, and hashmap_entry_s::value.

Referenced by fetch_curl_report_certs_upstream(), get_store_entry(), and set_store_entry().

Here is the caller graph for this function:

◆ hashmap_remove()

bool hashmap_remove ( hashmap_t hashmap,
void *  key 
)

Remove an entry from the hashmap.

This will remove the entry for the given key from the hashmap If there is no such entry, this will safely do nothing. The value associated with the entry will be destroyed and so should not be used beyond calling this function.

Parameters
hashmapThe hashmap to remove the entry from
keyThe key to remove the entry for
Returns
true if an entry was removed, false otherwise

Definition at line 206 of file hashmap.c.

References hashmap_s::bucket_count, hashmap_s::buckets, hashmap_s::entry_count, hashmap_entry_s::key, hashmap_parameters_t::key_destroy, hashmap_parameters_t::key_eq, hashmap_entry_s::key_hash, hashmap_parameters_t::key_hash, hashmap_entry_s::next, hashmap_s::params, hashmap_entry_s::prevptr, hashmap_entry_s::value, and hashmap_parameters_t::value_destroy.

Referenced by invalidate_entry().

Here is the caller graph for this function: