sig_ht.h

include/sig/base/sig_ht.h

SIG_HT_MIN_SIZE

#define SIG_HT_MIN_SIZE 8

sig_dtor_fn

typedef void (*sig_dtor_fn)(void *value);

sig_ht is NOT internally synchronized. The refcount (sig_ht_ref / sig_ht_destroy) only arbitrates which caller is responsible for tearing the table down when it's shared by pointer; it does not make concurrent sig_ht_add/update/get/remove/walk calls from multiple threads safe. A table touched from more than one thread must be protected by a mutex (e.g. sig_mutex from sig_thread.h) owned by the caller.

sig_ht_elem

typedef struct sig_ht_elem sig_ht_elem;

sig_ht_elem

struct sig_ht_elem {
    sig_ht_elem *next;
    char *key;
    void *value;
};

sig_ht

typedef struct {
    sig_refcount_h refcount;
    uint32_t num_elements;
    uint32_t num_buckets;
    sig_ht_elem **buckets;
    sig_dtor_fn dtor;
} sig_ht;

sig_ht_create

sig_status_t sig_ht_create(sig_ht ** out_ht, uint32_t initial_buckets, sig_dtor_fn dtor)
ParameterType
out_htsig_ht **
initial_bucketsuint32_t
dtorsig_dtor_fn

dtor may be NULL if stored values are not owned by the hashtable.

sig_ht_ref

void sig_ht_ref(sig_ht * ht)
ParameterType
htsig_ht *

Adds a reference to the hashtable's refcount (sig_refcount_h). Each call to sig_ht_ref() must be matched by an additional call to sig_ht_destroy() before the hashtable is actually freed.

sig_ht_destroy

void sig_ht_destroy(sig_ht * ht)
ParameterType
htsig_ht *

Releases a reference to the hashtable. sig_ht_create() starts the refcount at 1, so a container that is never shared via sig_ht_ref() is fully torn down (elements destroyed via dtor, buckets and the struct freed) on the first call, exactly as before. If other references remain, this call only decrements the count and returns without freeing or destroying anything.

sig_ht_add

sig_status_t sig_ht_add(sig_ht * ht, const char * key, void * value)
ParameterType
htsig_ht *
keyconst char *
valuevoid *

sig_ht_update

sig_status_t sig_ht_update(sig_ht * ht, const char * key, void * value)
ParameterType
htsig_ht *
keyconst char *
valuevoid *

sig_ht_get

sig_status_t sig_ht_get(sig_ht * ht, const char * key, void ** out_value)
ParameterType
htsig_ht *
keyconst char *
out_valuevoid **

sig_ht_remove

sig_status_t sig_ht_remove(sig_ht * ht, const char * key)
ParameterType
htsig_ht *
keyconst char *

sig_ht_count

uint32_t sig_ht_count(const sig_ht * ht)
ParameterType
htconst sig_ht *

sig_ht_walk_fn

typedef sig_status_t (*sig_ht_walk_fn)(const char *key, void *value, void *ctx);

sig_ht_walk

sig_status_t sig_ht_walk(sig_ht * ht, sig_ht_walk_fn func, void * ctx)
ParameterType
htsig_ht *
funcsig_ht_walk_fn
ctxvoid *