htils 1
A small set of utilities for C programming.
Loading...
Searching...
No Matches
stringmap.c File Reference
#include <string.h>
#include <h2o/memory.h>
#include <htils/assert.h>
#include <htils/basictypes.h>
#include <h2otils/string.h>
#include <h2otils/stringmap.h>

Macros

#define DEFAULT_CAPACITY   16
 

Functions

static u64 hash_key (const h2o_string *key)
 Hash a key into an index.
 
static void sm_insert_direct (h2o_stringmap_t *map, const h2o_string *key, void *value, u64 vsize)
 Inserts a key-value pair into the stringmap directly.
 
static void h2o_sm_grow (h2o_stringmap_t *map)
 Grow a stringmap if it's too small.
 
h2o_stringmap_th2o_sm_new (h2o_mem_pool_t *pool, const u64 capacity)
 Initializes a new h2o_stringmap_t.
 
h2o_stringmap_result_t __h2o_sm_insert (h2o_stringmap_t *map, const h2o_string *key, const void *value, const u64 vsize)
 Insert key and value into the h2o_stringmap_t.
 
h2o_stringmap_result_t h2o_sm_kill (h2o_stringmap_t *map, const h2o_string *key)
 Remove / Kill entry at key from the h2o_stringmap_t.
 
void * h2o_sm_get (h2o_stringmap_t *map, const h2o_string *key)
 Get a V from the h2o_stringmap_t.
 

Macro Definition Documentation

◆ DEFAULT_CAPACITY

#define DEFAULT_CAPACITY   16

Function Documentation

◆ __h2o_sm_insert()

h2o_stringmap_result_t __h2o_sm_insert ( h2o_stringmap_t map,
const h2o_string key,
const void *  value,
u64  vsize 
)

Insert key and value into the h2o_stringmap_t.

Hashes and duplicates the key and sets the value, if the key already exists, it will be updated, if the capacity is too small, it will be automatically grown.

Note
This function is not meant to be run directly, and is called by the h2o_sm_insert() macro.
Parameters
mapThe h2o_stringmap_t to insert into.
keyThe key to insert.
valueThe value to associate with the key.
vsizeThe size of the value.
Precondition
  • map and key must be valid and cannot be null.
  • value can't be null, as it would lead to a null dereference.
Returns
The result of the insert (which will be either CREATED or UPDATED in this case).

◆ h2o_sm_get()

void * h2o_sm_get ( h2o_stringmap_t map,
const h2o_string key 
)

Get a V from the h2o_stringmap_t.

Gets an entry from the h2o_stringmap_t, if the entry doesn't exist, it will be null.

Parameters
mapThe h2o_stringmap_t to get from.
keyThe key to get.
Precondition
map and key must be valid and cannot be null.
Returns
The value associated with the key, or null if it doesn't exist or is dead.

◆ h2o_sm_grow()

static void h2o_sm_grow ( h2o_stringmap_t map)
static

Grow a stringmap if it's too small.

By doubling the capacity, allocating a new block using h2o_mem_alloc_pool(), and copying over all occupied entries.

Parameters
mapThe stringmap to grow.
Precondition
map must be valid and cannot be null.

◆ h2o_sm_kill()

h2o_stringmap_result_t h2o_sm_kill ( h2o_stringmap_t map,
const h2o_string key 
)

Remove / Kill entry at key from the h2o_stringmap_t.

Marks an entry as DEAD, if the key doesn't exist, it will return NOT_FOUND, otherwise it will return KILLED.

Parameters
mapThe h2o_stringmap_t to remove from.
keyThe key to remove.
Precondition
stringmap and key must be valid and cannot be null.
Returns
The result of the remove (which will be either KILLED or NOT_FOUND in this case).

◆ h2o_sm_new()

h2o_stringmap_t * h2o_sm_new ( h2o_mem_pool_t *  pool,
const u64  capacity 
)

Initializes a new h2o_stringmap_t.

Optionally use a nullable type for h2o_stringmaps.

With the capacity, and pool, if the given capacity is 0, it will use the built in default capacity, which is 16.

Parameters
poolThe memory pool to allocate from.
capacityThe capacity of the h2o_stringmap_t.
Precondition
pool must be valid and cannot be null.
Returns
A pointer to the new h2o_stringmap_t.

◆ hash_key()

static u64 hash_key ( const h2o_string key)
static

Hash a key into an index.

Hashes a key into an index, using the FNV-1a algorithm.

Parameters
keyThe key to hash.
Precondition
key must be valid and cannot be null.
Returns
The hash generated from the key.

◆ sm_insert_direct()

static void sm_insert_direct ( h2o_stringmap_t map,
const h2o_string key,
void *  value,
u64  vsize 
)
static

Inserts a key-value pair into the stringmap directly.

By first hashing the key, then iterating through the entries.

Parameters
mapThe h2o_stringmap_t to insert into.
keyThe key to insert.
valueThe value to insert.
vsizeThe size of the value.
Precondition
  • map, key, and value must be valid and cannot be null.
  • vsize must be greater than 0 and the explicit size of the value, since @value is a void * it has to be provided.