C Collections

chashmap · Section 3

chmap_init_mp

chmap_init_mp - create a hash map with a custom allocator into an already declared handle

C Collections (libccollections, -lccollections)

#include <ccollections/chashmap.h>
 
chmap_init_mp(hm_name, mmgmt_procs);

Creates a hash map that takes all of its memory from mmgmt_procs, and
assigns it to hm_name. The handle has to be declared first, with
chmap_declare(3) or chmap_declare_scoped(3). The key and value types are
not arguments, because the macro reads them from the companion type
variables that the declare macro created.
 
It is chmap_init(3) with an allocator added, and the map uses its own
hashing for the declared key type. chmap_init_ch(3) swaps the hashing
instead, and chmap_init_full(3) takes both.
 
The one-step equivalents are chmap_construct_mp(3) and
chmap_construct_mp_scoped(3).

hm_name      The already declared map handle to create the map into
mmgmt_procs  Pointer to a ccol_memmgmt_procs, or NULL for the default
             allocator

None. The macro expands to a statement and is not an expression.

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

ccol_memmgmt_procs procs = {
    .malloc = my_malloc, .calloc = my_calloc,
    .realloc = my_realloc, .free = my_free,
};
 
chmap_declare(scores, char *, int);
chmap_init_mp(scores, &procs);
int score = 42;
chmap_insert(scores, "alice", score);
chmap_destroy(scores);

chmap_init(3), chmap_init_ch(3), chmap_init_full(3), chmap_declare(3), chmap_construct_mp(3), chashmap_create_full(3), chmap_destroy(3)