C-functions that manipulate Prolog terms manipulate pointers to the Prolog runtime stacks. Prolog implements two mechanisms for avoiding stack overflow: garbage collection and stack expansion. On machines that allow for it, Prolog will use virtual memory management to detect stack overflow and expand the runtime stacks. On other machines Prolog will reallocate the stacks and update all pointers to them. To do so, Prolog needs to know which variables of active C-functions contain references to Prolog data.
C-variables may be registered with the Prolog engine using the macro PL_lock() and unregistered using PL_unlock(). These macros should be perfectly balanced and any term locked in a C-function needs to be unlocked before control is passed back to Prolog.
A C-variable that is registered must hold valid Prolog data or `0'.