C Collections

cbstmap · Section 3

cbmap_init_mp

cbmap_init_mp - create an ordered map with a custom allocator into an already declared handle

C Collections (libccollections, -lccollections)

#include <ccollections/cbstmap.h>
 
cbmap_init_mp(hm_name, mmgmt_procs);

Creates an ordered map that takes all of its memory from mmgmt_procs and
assigns it to hm_name. The caller must declare the handle first, with
cbmap_declare(3) or cbmap_declare_scoped(3). The key type and the value
type are not arguments; the macro reads them from the companion type
variables that the declare macro created.
 
It is cbmap_init(3) with an allocator added, and the keys keep the default
order for the declared key type. cbmap_init_cc(3) replaces the order
instead, and cbmap_init_full(3) takes both.
 
The one-step equivalents are cbmap_construct_mp(3) and
cbmap_construct_mp_scoped(3).

hm_name      The map handle that the caller declared earlier, which
             receives the new map
mmgmt_procs  Pointer to a ccol_memmgmt_procs, or NULL for the default
             allocator

None. The macro expands to a statement, not to an expression.

The macro stops the program through ccol__fatal_err() if the creation
fails.
 
None of the four members of ccol_memmgmt_procs may be NULL. The map
copies the procedures into itself, so the caller's ccol_memmgmt_procs
doesn't have to outlive the call, but the procedures themselves must stay
callable for the whole life of the map. Every tree node that the map
allocates later comes from them, and everything it frees goes back to
them.
 
The allocator doesn't change the order. The declared key type decides it,
as cbstmap_create_full(3) describes, and a walk goes in ascending key order.

ccol_memmgmt_procs procs = {
    .malloc = my_malloc, .calloc = my_calloc,
    .realloc = my_realloc, .free = my_free,
};
 
cbmap_declare(sorted, int, char *);
cbmap_init_mp(sorted, &procs);
int k = 42;
cbmap_insert(sorted, k, "hello");
cbmap_destroy(sorted);

cbmap_init(3), cbmap_init_cc(3), cbmap_init_full(3), cbmap_declare(3), cbmap_construct_mp(3), cbstmap_create_full(3), cbmap_destroy(3)