Stacks shifts or garbage collections may be forced if there is insufficient space on the stacks. Data is pushed on the stacks by most of the term construction functions and if the Prolog engine is called using PL_call() or PL_call_predicate().
By default the Prolog engine ensures the stacks have a minimum of
`min_free' (see stack_parameter/4) space when a foreign predicate is
activated. If this is insufficient, Prolog checks whether a garbage
collection may be performed or the stacks may be enlarged.
By default Prolog assumes it is not save to shift the stacks
or perform a garbage collection when foreign code is active.
Simple C-defined predicates that do not call Prolog and do not use the term-construction functions intensively do not need to use PL_lock() and PL_unlock(). C-defined predicates that perform call-back on Prolog or use the term construction primitives to (potentially) create large data structures should protect their variables and should defined the PL_FA_GCSAVE attribute (see PL_register_foreign()).