C Collections

cbstmap · Section 3

cbmap_construct_mp_scoped

cbmap_construct_mp_scoped - declare and create an ordered map with a custom allocator, destroyed on scope exit

C Collections (libccollections, -lccollections)

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

Declares an ordered map handle named hm_name with the companion type
variables that carry KeyT and ValT to every later type-inferred macro,
then creates a map that takes all of its memory from mmgmt_procs. The
handle also gets the cleanup attribute, which destroys the map on every
exit from the enclosing scope, an early return included.
 
Apart from that cleanup attribute it is exactly cbmap_construct_mp(3).
cbmap_construct_scoped(3) is the same macro with the default allocator,
cbmap_construct_cc_scoped(3) takes a comparison procedure where this one
takes an allocator, and cbmap_construct_full_scoped(3) takes both.

hm_name      Name for the map handle to declare
KeyT        Key type. Every later macro reads a key through it, and the
             container takes the order of the tree from it
ValT        Value type. Only the type-inferred macros read it; the
             library never passes it to the container
mmgmt_procs  Pointer to a ccol_memmgmt_procs, or NULL for the default
             allocator

None. The macro declares variables and doesn't expand to an expression.

The macro stops the program through ccol__fatal_err() if the creation
fails, and because of the cleanup attribute it needs GCC or Clang.
 
None of the four members of ccol_memmgmt_procs may be 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 destruction at the exit of
the scope gives all of it back to them.
 
A walk visits the elements in ascending key order, by the order that KeyT
selects, as cbstmap_begin_iter(3) and cbstmap_create_full(3) describe.
 
The exit of the scope destroys the map and clears the handle, so an
explicit cbmap_destroy(3) earlier in the scope is fine too.
 
The companion type variables follow the scope of the handle, so a function
that receives the handle needs cbmap_redeclare(3) there.
 
An array key type 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. Keys and values
are copied by assignment and C can't assign an array, so the map would
otherwise keep 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. Don't return it or store it
anywhere that lives longer than the scope: The cleanup attribute frees the
map when the scope exits, and the stored handle would then name a freed
map.

ccol_memmgmt_procs procs = {
    .malloc = my_malloc, .calloc = my_calloc,
    .realloc = my_realloc, .free = my_free,
};
 
void tally(int *ids, size_t n) {
    cbmap_construct_mp_scoped(seen, int, int, &procs);
    int one = 1;
    for (size_t i = 0; i < n; i++) {
        cbmap_insert(seen, ids[i], one);
    }
}                                 /* The macro destroys seen here */

cbmap_construct_mp(3), cbmap_construct_scoped(3), cbmap_construct_cc_scoped(3), cbmap_construct_full_scoped(3), cbmap_declare_scoped(3), cbmap_init_mp(3), cbmap_redeclare(3), cbstmap_begin_iter(3), cbmap_destroy(3)