It is obligatory to call either of the two closing functions to discard a foreign frame. Foreign frames may be nested.
int count_atoms() { fid_t fid = PL_open_foreign_frame(); term_t goal = PL_new_term_ref(); term_t a1 = PL_new_term_ref(); term_t a2 = PL_new_term_ref(); functor_t s2 = PL_new_functor(PL_new_atom("statistics"), 2); int atoms; PL_put_atom_chars(a1, "atoms"); PL_cons_functor(goal, s2, a1, a2); PL_call(goal, NULL); /* call it in current module */ PL_get_integer(a2, &atoms); PL_discard_foreign_frame(fid); return atoms; }
12.4.13 String buffering
Many of the functions of the foreign language interface involve
strings. Some of these strings point into static memory like those
associated with atoms. These strings are valid as long as the atom is
protected against atom garbage collection, which generally implies the
atom must be locked using PL_register_atom()
or be part of an accessible term. Other strings are more volatile.
Several functions provide a BUF_* flag that can be set to either BUF_STACK
(default) or
BUF_MALLOC
. Strings returned by a function accepting
BUF_MALLOC
must be freed using PL_free().
Strings returned using BUF_STACK
are pushed on a stack that
is cleared when a foreign predicate returns control back to Prolog. More
fine grained control may be needed if functions that return strings are
called outside the context of a foreign predicate or a foreign predicate
creates many strings during its execution. Temporary strings are scoped
using these macros:
- void PL_STRINGS_MARK()
- void PL_STRINGS_RELEASE()
- These macros must be paired and create a C block ({...}). Any
string created using
BUF_STACK
after PL_STRINGS_MARK() is released by the corresponding PL_STRINGS_RELEASE(). These macros should be used like below. Note that strings returned by any of the Prolog functions between this pair may be invalidated.... PL_STRINGS_MARK(); <operations involving strings> PL_STRINGS_RELEASE(); ...
The Prolog flag string_stack_tripwire may be used to set a tripwire to help finding places where scoping strings may help reducing resources.
12.4.14 Foreign Code and Modules
Modules are identified via a unique handle. The following functions are available to query and manipulate modules.
- module_t PL_context()
- Return the module identifier of the context module of the currently
active foreign predicate. If there is no currently active predicate it
returns a handle to the
user
module. - int PL_strip_module(term_t +raw, module_t *m, term_t -plain)
- Utility function. If raw is a term, possibly holding the
module construct <module>
:
<rest>, this function will make plain a reference to <rest> and fill module * with <module>. For further nested module constructs the innermost module is returned via module *. If raw is not a module construct, raw will simply be put in plain. The value pointed to by m must be initialized before calling PL_strip_module(), either to the default module or toNULL
. ANULL
value is replaced by the current context module if raw carries no module. The following example shows how to obtain the plain term and module if the default module is the user module:{ module m = PL_new_module(PL_new_atom("user")); term_t plain = PL_new_term_ref(); PL_strip_module(term, &m, plain); ... }
- atom_t PL_module_name(module_t module)
- Return the name of module as an atom.
- module_t PL_new_module(atom_t name)
- Find an existing module or create a new module with the name name.
Currently aborts the process with a fatal error on failure.
Future versions may raise a resource exception and return
(module_t)0
.
12.4.15 Prolog exceptions in foreign code
This section discusses PL_exception() and PL_raise_exception(), the interface functions to detect and generate Prolog exceptions from C code. PL_raise_exception() from the C interface registers the exception term a