cvector · Section 3
cvec_construct_mp_scoped
NAME
cvec_construct_mp_scoped - construct a vector with a custom allocator, destroyed automatically at scope exit
LIBRARY
C Collections (libccollections, -lccollections)
SYNOPSIS
#include <ccollections/cvector.h> cvec_construct_mp_scoped(v, type, mprocs);
DESCRIPTION
Declares a vector variable named v that holds elements of the given type, creates the vector through mprocs, and adds a cleanup handler that destroys the vector when control leaves the surrounding scope. It is cvec_construct_mp(3) plus that handler, with one more difference: The companion type variables are initialized to NULL here, while cvec_construct_mp(3) leaves them indeterminate on purpose. Everything else cvec_construct_mp(3) documents holds here too, including the private copy of the procedure table that the vector keeps and the stop of the program through ccol__fatal_err() on a failed creation. The variable is set to NULL before the vector is created, which makes the handler safe on a path that leaves the block between the declaration and a successful creation. The vector is destroyed on every exit from the scope: The closing brace, a return, a break, a continue or a goto out of the block. The variable is set to NULL as the vector is destroyed, so an explicit cvec_destroy(3) earlier in the block doesn't lead to a double free. The vector goes back through the same procedures that created it, so those functions must be callable at the end of the scope.
PARAMETERS
v Name of the vector variable to declare and create
type Element type, for example int, double or struct foo
mprocs Pointer to a ccol_memmgmt_procs. Its malloc, calloc, realloc
and free members must all be set. Pass NULL for the default
allocator.
RETURN VALUE
None. The macro expands to declarations followed by a statement, not to an expression.
NOTES
When the vector can't be created the macro stops the program through ccol__fatal_err(), with a message that names v and the underlying error. The macro relies on the cleanup attribute and so needs GCC or Clang. On a toolchain without that attribute, use cvec_construct_mp(3) with an explicit cvec_destroy(3). Since it expands to declarations and then a statement, the macro has to go where declarations are legal and can't be the body of an if or a loop without braces. v is pasted into the names of the companion variables, so it must be a plain identifier. If the vector holds pointers to heap buffers, the elements are not freed one by one, so free the element data before leaving the scope.
WARNINGS
The vector must not outlive its scope. Don't return v, and don't store it anywhere that code reads after the end of the block. When ownership has to leave the block, use cvec_construct_mp(3).
EXAMPLES
void process(ccol_memmgmt_procs *procs) { cvec_construct_mp_scoped(vec, int, procs); cvec_push(vec, 42); if (cvec_size(vec) != 1) { return; /* vec is destroyed here */ } } /* and here */
SEE ALSO
cvec_construct_mp(3), cvec_construct_scoped(3), cvec_declare_scoped(3), cvec_init_mp(3), cvec_destroy(3), cvector_create_full(3)