Node:Initializing the Prolog Engine, Next:Loading Prolog Code, Previous:User-defined Main Programs, Up:User-defined Main Programs
The Prolog Engine is initialized by calling SP_initialize(). This
must be done before any interface functions are called, except
SP_force_interactive,
SP_set_memalloc_hooks,
SP_set_wcx_hooks,
SP_set_user_stream_post_hook and
SP_set_user_stream_hook. The function will allocate data areas
used by Prolog, initialize command line arguments so that they can be
accessed by the argv Prolog flag, and load the Runtime
Library. It is called like this:
int SP_initialize(int argc, char **argv, char *boot_path)
boot_path should be the name of a directory, equivalent to
$SP_PATH/bin. If boot_path is NULL,
SP_initialize() will look up the value of the environment
variable SP_PATH and look for the file
$SP_PATH/bin/sprt.sav which contains the Runtime Library.
It returns SP_SUCCESS if initialization was successful, and
SP_ERROR otherwise. If initialization was successful, further
calls to SP_initialize() will be no-ops (and return
SP_SUCCESS).
To unload the SICStus emulator, SP_deinitalize() can be called.
void SP_deinitialize(void)
SP_deinitialize() will make a best effort to restore the system
to the state it was in at the time of calling
SP_initialize(). This involves unloading foreign resources,
shutting down the emulator by
calling halt/0,
and deallocate
memory used by Prolog. SP_deinitialize() is idempotent as well,
i.e. it is a no-op unless SICStus has actually been initialized.
You may also call SP_force_interactive() before calling
SP_initialize(). This will force the I/O built-in predicates to
treat the standard input stream as a terminal, even if it does not
appear to be a terminal. Same as the -i option in development
systems. (see Start).
void SP_force_interactive(void)
You may also call SP_set_memalloc_hooks() before calling
SP_initialize(). This will define the bottom layer of Prolog's
memory manager, in case your application has special requirements.
typedef void *(SP_AllocHook)(unsigned int size,
unsigned int align,
unsigned int *actual_sizep);
typedef void *(SP_ReAllocHook)(void *ptr,
unsigned int oldsize,
unsigned int newsize,
unsigned int align,
unsigned int *actual_sizep);
typedef int (SP_FreeHook)(void *ptr,
unsigned int size);
void SP_set_memalloc_hooks(int usage,
SP_AllocHook *init_alloc_hook,
SP_AllocHook *alloc_hook,
SP_ReAllocHook *realloc_hook,
SP_FreeHook *free_hook)
The effect of SP_set_memalloc_hooks is controlled by the value of
usage, which should be one of:
MM_USE_MALLOC
malloc()/free().
The other arguments are ignored.
Same as the -m option in development systems. (see Start).
MM_USE_SBRK
sbrk().
The default for UNIX development systems; not available for Windows.
The other arguments are ignored.
This option seems to work well in practice but standard UNIX documents
sbrk() as incompatible with malloc() etc and it is not
safe to use if more than one thread allocates memory. These concerns are
the reason this option is no longer the default for run-time systems.
MM_USE_SPARSE
VirtualAlloc()/VirtualFree().
The default for Windows; not available for UNIX.
The other arguments are ignored.
MM_USE_DEFAULT
MM_USE_MALLOC on UNIX and MM_USE_SPARSE on Windows. The
default for run-time systems.
MM_USE_OTHER
In the latter case, the other arguments should be functions as specified below. Pointer arguments and values should be aligned pointers.
alloc_hook
size bytes aligned at align in it. align is
guaranteed to be a power of 2. The actual size of the piece of memory
should be returned in *actual_sizep. Should return NULL
if it cannot allocate any more memory.
init_alloc_hook
alloc_hook. It will be called initially
whereas alloc_hook will be called subsequently. It can do
whatever initialization that this layer of memory management wants to do.
realloc_hook
ptr is the pointer, oldsize its current size. The
function must allocate and return a pointer to a piece of memory that
has at least newsize bytes aligned at align in it, and
that has the same contents as the old block up to the lesser of
oldsize and newsize. align is guaranteed to be a
power of 2. The actual size of the piece of memory should be returned
in *actual_sizep. Should return NULL if it cannot
allocate any more memory, or if it cannot reclaim the old block in a
meaningful way. In that case, Prolog will use the other functions.
The realloc_hook argument is currently ignored but may be used in
future versions of SICStus.
free_hook
The default bottom layers look at the environment variables
PROLOGINITSIZE, PROLOGINCSIZE, PROLOGKEEPSIZE and
PROLOGMAXSIZE. They are useful to customize the default memory
manager. If users redefine the bottom layer, they can choose to ignore
these environment variables. See Environment Variables.
Node:Initializing the Prolog engine, Next:Loading the Prolog code, Previous:Setting up the interface, Up:How to use the interface
The Visual Basic interface must be explicitly initialized: you must call
PrologInit() before calling any other interface function. The
PrologInit function loads and initializes the interface. It
returns 1 if the initialization was successful, and -1 otherwise.