Node:C Hooks, Next:Guile Hooks, Previous:Hook Reference, Up:Hooks
The hooks already described are intended to be populated by Scheme-level procedures. In addition to this, the Guile library provides an independent set of interfaces for the creation and manipulation of hooks that are designed to be populated by functions implemented in C.
The original motivation here was to provide a kind of hook that could safely be invoked at various points during garbage collection. Scheme-level hooks are unsuitable for this purpose as running them could itself require memory allocation, which would then invoke garbage collection recursively ... However, it is also the case that these hooks are easier to work with than the Scheme-level ones if you only want to register C functions with them. So if that is mainly what your code needs to do, you may prefer to use this interface.
To create a C hook, you should allocate storage for a structure of type
scm_t_c_hook
and then initialize it using scm_c_hook_init
.
scm_t_c_hook | C Type |
Data type for a C hook. The internals of this type should be treated as opaque. |
scm_t_c_hook_type | C Enum |
Enumeration of possible hook types, which are:
|
void scm_c_hook_init (scm_t_c_hook *hook, void *hook_data, scm_t_c_hook_type type) | C Function |
Initialize the C hook at memory pointed to by hook. type
should be one of the values of the scm_t_c_hook_type enumeration,
and controls how the hook functions will be called. hook_data is
a closure parameter that will be passed to all registered hook functions
when they are called.
|
To add or remove a C function from a C hook, use scm_c_hook_add
or scm_c_hook_remove
. A hook function must expect three
void *
parameters which are, respectively:
scm_c_hook_init
.
scm_c_hook_add
.
scm_c_hook_run
call that
runs the hook.
scm_t_c_hook_function | C Type |
Function type for a C hook function: takes three void *
parameters and returns a void * result.
|
void scm_c_hook_add (scm_t_c_hook *hook, scm_t_c_hook_function func, void *func_data, int appendp) | C Function |
Add function func, with function closure data func_data, to the C hook hook. The new function is appended to the hook's list of functions if appendp is non-zero, otherwise prepended. |
void scm_c_hook_remove (scm_t_c_hook *hook, scm_t_c_hook_function func, void *func_data) | C Function |
Remove function func, with function closure data func_data,
from the C hook hook. scm_c_hook_remove checks both
func and func_data so as to allow for the same func
being registered multiple times with different closure data.
|
Finally, to invoke a C hook, call the scm_c_hook_run
function
specifying the hook and the call closure data for this run:
void * scm_c_hook_run (scm_t_c_hook *hook, void *data) | C Function |
Run the C hook hook will call closure data data. Subject to
the variations for hook types SCM_C_HOOK_OR and
SCM_C_HOOK_AND , scm_c_hook_run calls hook's
registered functions in turn, passing them the hook's closure data, each
function's closure data, and the call closure data.
|