cyaml · Section 3
cyaml_list_push
NAME
cyaml_list_push - append a child to a list
LIBRARY
C Collections (libccollections, -lccollections)
SYNOPSIS
#include <ccollections/cyaml.h> ccol_retval cyaml_list_push(cyaml seq, cyaml child);
DESCRIPTION
Appends child to the CYAML_LIST node seq. On success seq owns child, so don't free child after such a call. Each return code carries one rule about child. ccol_invalid_args always means that the function refused the arguments and didn't touch child at all, so the caller owns child and must destroy it itself. Every other failure takes child and deep-frees it before the function returns, so the caller must not destroy child after any of them. So the return code alone tells the caller what to do with child; a code that meant different things on different paths couldn't do that. These give ccol_invalid_args: A NULL child, a NULL seq, a seq that is not a CYAML_LIST, a child that is attached to a list or dictionary parent, and a child that is seq itself. An attached child would end up with two owners if the function took it, and each owner frees the node when its own parent is destroyed. A node you just created is always fine, and so is a fresh cyaml_clone(). A borrowed reference from cyaml_get(), cyaml_list_get() or cyaml_dictionary_get() never is, because each of those names a node that has a parent. child must not hold seq anywhere in its own subtree either, and the function doesn't check this. A cycle made that way turns a later cyaml_destroy(), cyaml_clone() or cyaml_serialize() call on the tree into undefined behaviour. The attachment rule above already excludes the case for every node that has a parent, so the only ancestor left unchecked is the one with no parent: The root of the tree that seq belongs to.
PARAMETERS
seq The target node, a CYAML_LIST. child The node to append.
RETURN VALUE
ccol_success, ccol_invalid_args, ccol_not_enough_memory, or ccol_container_full. ccol_invalid_args leaves child untouched and with the caller. It is returned when seq or child is NULL, when seq is not a list, when child is attached, or when child is seq itself. ccol_container_full means that the list holds the largest element count it can represent, which a real program doesn't reach. On every outcome other than ccol_invalid_args the call takes child.