CK_HT_INIT(3)

πŸ“¦
version 0.7.2 Β· 2 versions
, in alpine 3.24, debian trixie, epel 10, fedora 43, fedora 44, nixos 26.05, nixpkgs unstable, opensuse-leap 16.0, ubuntu questing, ubuntu resolute, ubuntu stonking

πŸ’– ck_ht_init β€” bestie, let's initialize a hash table πŸ’–

okay so this is the ck_ht_init page from the CK_HT_INIT(3) manual, and it lives in the Concurrency Kit β€” that's libck, and you link it with -lck πŸ’…


✨ SYNOPSIS ✨

like, the whole vibe, exactly as it is:

#include <ck_ht.h>

typedef void
ck_ht_hash_cb_t(ck_ht_hash_t *h, const void *key, size_t key_length,
    uint64_t seed);

bool
ck_ht_init(ck_ht_t *ht, enum ck_ht_mode mode,
    ck_ht_hash_cb_t *hash_function, struct ck_malloc *allocator,
    uint64_t capacity, uint64_t seed);

🌸 DESCRIPTION 🌸

So. ck_ht_init() initializes the hash table that ht points at. That's it, that's the whole job, and it ate πŸ”₯

the mode situation πŸ‘‘

The mode argument says what kind of key-value pairs this table is gonna hold. mode can be one of these:

  • CK_HT_MODE_BYTESTRING β€” this table is meant to store key-value pointers where the key is a region of memory up to 65536 bytes long. That pointer gets dereferenced during hash table operations so keys can be compared. Entries of this table are expected to be interacted with using the ck_ht_entry_empty(3), ck_ht_entry_key(3), ck_ht_entry_key_length(3), ck_ht_entry_value(3), and ck_ht_entry_set(3) functions. Attempting a hash table operation with a key of value NULL or (void *)UINTPTR_MAX will result in undefined behavior. not me warning you bestie, this is real 😭
  • CK_HT_MODE_DIRECT β€” this table is meant to store key-value pointers where the key is a fixed width field compatible with the uintptr_t type. The key gets directly compared with other keys for equality. Entries of this table are expected to be interacted with using the ck_ht_entry_empty(3), ck_ht_entry_key_direct(3), ck_ht_entry_value_direct(3) and ck_ht_entry_set_direct(3) functions. Attempting a hash table operation with a key of value of 0 or UINTPTR_MAX will result in undefined behavior. πŸ’€

plus you can OR in a lil extra πŸŽ€

On top of all that, you may bitwise OR the mode flag with CK_HT_WORKLOAD_DELETE to say that the hash table is gonna have to handle a delete heavy workload. In that case, stronger bounds on latency can be provided, at the cost of approximately 13% higher memory usage. a small price, honestly.

the hash_function thing πŸ¦‹

The hash_function argument is a pointer to a user-specified hash function. It's optional β€” if NULL is specified, then the default hash function implementation gets used ( ck_ht_hash(3) ).

A user-specified hash function takes four arguments:

  • h is a pointer to a hash value object. The hash function is expected to update the value object of type uint64_t contained with-in the object pointed to by h.
  • key is a pointer to a key.
  • key_length is the length of the key.
  • seed is the initial seed associated with the hash table. This initial seed is specified by the user in ck_ht_init(3).

the allocator thing πŸ’œ

The allocator argument is a pointer to a structure containing malloc and free function pointers which respectively define the memory allocation and destruction functions to be used by the hash table being initialized. so like, your memory, your rules ✨

the capacity thing 🌈

The capacity argument represents the initial number of key-value pairs the hash table is expected to contain. And here's the tea: this argument is simply a hint, and the underlying implementation is free to allocate more or less memory than necessary to contain the number of entries capacity specifies. it's giving "suggestion, not a contract" πŸ™ˆ

the seed thing ⭐

The seed argument specifies the initial seed used by the underlying hash function. The user is free to choose a value of their choice. go wild bestie, pick whatever number speaks to you.

concurrency tea β˜•

The hash table is safe to access by multiple readers in the presence of one concurrent writer. Behavior is undefined in the presence of concurrent writers. one writer. ONE. count them. πŸ‘‘


🫢 RETURN VALUES 🫢

Upon successful completion ck_ht_init() returns a value of true, and otherwise returns a value of false to indicate an error. binary queen πŸ’…


πŸ’€ ERRORS πŸ’€

The behavior of ck_ht_init() is undefined if ht is not a pointer to a ck_ht_t object. like, don't do that. please.


🌟 SEE ALSO 🌟

the whole friend group, no cap:

Additional information available at http://concurrencykit.org/ β€” go peek, it's cute 🧚


and that's the page bestie!! πŸ’– this version was dated March 28, 2012 and filed under CK_HT_INIT(3) in the Concurrency Kit. mwah, go initialize something iconic βœ¨πŸŽ€πŸ¦‹

πŸ“¦ where this page comes from