NetSurf
Data Structures | Macros | Typedefs | Functions
hashmap.c File Reference
#include <stdlib.h>
#include <string.h>
#include "utils/hashmap.h"
Include dependency graph for hashmap.c:

Go to the source code of this file.

Data Structures

struct  hashmap_entry_s
 Hashmaps have chains of entries in buckets. More...
 
struct  hashmap_s
 The content of a hashmap. More...
 

Macros

#define DEFAULT_HASHMAP_BUCKETS   (4091)
 The default number of buckets in the hashmaps we create. More...
 

Typedefs

typedef struct hashmap_entry_s hashmap_entry_t
 Hashmaps have chains of entries in buckets. 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...
 

Macro Definition Documentation

◆ DEFAULT_HASHMAP_BUCKETS

#define DEFAULT_HASHMAP_BUCKETS   (4091)

The default number of buckets in the hashmaps we create.

Definition at line 27 of file hashmap.c.

Typedef Documentation

◆ hashmap_entry_t

Hashmaps have chains of entries in buckets.

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: