chashmap · Section 3
chmap_construct_mp
NAME
chmap_construct_mp - declare and create a hash map that takes its memory from a custom allocator
LIBRARY
C Collections (libccollections, -lccollections)
SYNOPSIS
#include <ccollections/chashmap.h> chmap_construct_mp(hm_name, KeyT, ValT, mmgmt_procs);
DESCRIPTION
Declares a hash map handle named hm_name, along with the companion type variables that pass KeyT and ValT on to every later type-inferred macro, and creates a map that takes all of its memory from mmgmt_procs. It is chmap_construct(3) with an allocator added, and the map uses its own hashing for KeyT. chmap_construct_ch(3) swaps the hashing instead, chmap_construct_full(3) takes both, and chmap_construct_mp_scoped(3) is this macro with a cleanup attribute that destroys the map at scope exit. The two-step form is chmap_declare(3) followed by chmap_init_mp(3). Use it when a creation failure has to be handled instead of being fatal.
PARAMETERS
hm_name Name for the map handle to declare
KeyT Key type; it tells every later macro how to read a key and,
with ValT, which implementation strategy the map uses
ValT Value type
mmgmt_procs Pointer to a ccol_memmgmt_procs, or NULL for the default
allocator
RETURN VALUE
None. The macro declares variables and is not an expression.
NOTES
Stops the program through ccol__fatal_err() if the creation fails All four members of ccol_memmgmt_procs must be non-NULL The macro copies the procedures into the map, so your own ccol_memmgmt_procs doesn't need to outlive the call, but the procedures themselves must stay callable for the whole life of the map Everything the map allocates later, its bucket array included, comes from these procedures, and everything it frees goes back to them Uses CHMAP_DEFAULT_INITIAL_BUCKET_ARRAY_SIZE (16) buckets chmap_destroy(3) destroys the map. The companion type variables live in the scope of the handle, so a function that receives the handle needs chmap_redeclare(3) inside it An array key or value type, such as int[2] or char[16], is a compile error, and the message says to wrap the array in a struct. The macros copy a key or a value by assignment and C cannot assign an array, so the map would store the address of the caller's array instead of its elements. A struct that holds the array works
EXAMPLES
ccol_memmgmt_procs procs = {
.malloc = my_malloc, .calloc = my_calloc,
.realloc = my_realloc, .free = my_free,
};
chmap_construct_mp(scores, char *, int, &procs);
int score = 42;
chmap_insert(scores, "alice", score);
chmap_destroy(scores);
SEE ALSO
chmap_construct(3), chmap_construct_mp_scoped(3), chmap_construct_ch(3), chmap_construct_full(3), chmap_init_mp(3), chmap_declare(3), chmap_redeclare(3), chashmap_create_full(3), chmap_destroy(3)