C Collections

chashmap · Section 3

chmap_construct_mp_scoped

chmap_construct_mp_scoped - declare and create a hash map with a custom allocator, destroyed on scope exit

C Collections (libccollections, -lccollections)

#include <ccollections/chashmap.h>
 
chmap_construct_mp_scoped(hm_name, KeyT, ValT, mmgmt_procs);

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.
The handle gets the cleanup attribute, which destroys the map on every
exit from the enclosing scope, an early return included.
 
The cleanup attribute is the only difference from chmap_construct_mp(3).
chmap_construct_scoped(3) is the same macro with the default allocator,
chmap_construct_ch_scoped(3) takes a hashing procedure instead of an
allocator, and chmap_construct_full_scoped(3) takes both.

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

None. The macro declares variables and is not an expression.

Stops the program through ccol__fatal_err() if the creation fails
Needs GCC or Clang, because it uses the cleanup attribute
All four members of ccol_memmgmt_procs must be non-NULL, and the
procedures must stay callable for the whole life of the map, which here
ends at the closing brace of the enclosing scope
Everything the map allocates comes from these procedures, and the
destroy at scope exit gives all of it back to them
Leaving the scope destroys the map and clears the handle, so an explicit
chmap_destroy(3) earlier in the scope is fine as well
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

The handle must not outlive its scope. Do not return it and do not store
it anywhere that outlives the scope, because either way you hand out a
handle whose map is freed when the scope exits.

ccol_memmgmt_procs procs = {
    .malloc = my_malloc, .calloc = my_calloc,
    .realloc = my_realloc, .free = my_free,
};
 
void tally(char **words, size_t n) {
    chmap_construct_mp_scoped(seen, char *, int, &procs);
    int one = 1;
    for (size_t i = 0; i < n; i++) {
        chmap_insert(seen, words[i], one);
    }
}                                     /* seen is destroyed here */

chmap_construct_mp(3), chmap_construct_scoped(3), chmap_construct_ch_scoped(3), chmap_construct_full_scoped(3), chmap_declare_scoped(3), chmap_init_mp(3), chmap_redeclare(3), chmap_destroy(3)