SP_set_memalloc_hooks()
[preinit]#include <sicstus/sicstus.h> typedef int SP_InitAllocHook(size_t alignment, void *cookie); typedef void SP_DeinitAllocHook(void *cookie); typedef void * SP_AllocHook(size_t size, size_t *actual_sizep, void *cookie); typedef int SP_FreeHook(void *ptr, size_t size, int force, void *cookie); int SP_set_memalloc_hooks(int hint, SP_InitAllocHook *init_alloc_hook, SP_DeinitAllocHook *deinit_alloc_hook, SP_AllocHook *alloc_hook, SP_FreeHook *free_hook, void *cookie);
Defines the Prolog memory manager's bottom layer.
Must be called before SP_initialize()
.
0
(zero)SP_SET_MEMALLOC_HOOKS_HINT_USE_MALLOC
malloc()
et al. will be used for all allocations. This is
useful, e.g. in combination with tools like valgrind.
In this case all the other arguments must be NULL
.
When this option is used some memory statistics, e.g.
statistics/[0,2]
, may be unavailable or misleading.
On some systems, SP_SET_MEMALLOC_HOOKS_HINT_USE_MALLOC
will be
used automatically when the SICStus run-time detects that it is being
run within valgrind (http://www.valgrind.org/). This
behaviour can be disabled by explicitly setting the system property (or environment
variable) SP_USE_MALLOC
to no
(see System Properties and Environment Variables). This feature is
currently (4.0.8) available on x86 and x86_64 Linux.
Please note: SICStus can only use malloc()
on
platforms where it is guaranteed to return sufficiently aligned memory
(2 * sizeof(void*)
). This is true at least on Linux, Mac OS X
and Windows (on Windows _aligned_malloc()
is used instead of
malloc()
). On platforms where alignment cannot be ensured
SICStus will not be able to allocate memory and initialization will
fail if SP_SET_MEMALLOC_HOOKS_HINT_USE_MALLOC
is used.
Please note: When
SP_SET_MEMALLOC_HOOKS_HINT_USE_MALLOC
is used
SP_deinitialize()
may not be able to reclaim all memory. This
is mainly a problem when SICstus is embedded into some other
application that continues to run after calling
SP_deinitialize()
. This limitations also means that leak
detecton tools like valgrind will report lare amounts of
leaked memory.
Since 4.0.5.
alignment
is guaranteed to be a power of 2,
and is used by alloc_hook
. earliest_start
(inclusive) and
latest_end
(exclusive) are the bounds within which
address-constrained memory blocks must fit. Both are aligned according
to alignment
and non-zero. The function can do whatever
initialization that this layer of memory management wants to do. It
should return non-zero if it succeeds, zero if the memory manager bottom
layer could not be initialized, in which case initialization of the
SICStus run-time will fail.
SP_deinitialize()
when the Prolog engine shuts down.
The function can do any necessary cleaning up.
size
bytes aligned at a multiple of alignment
. The
actual size of the piece of memory should be returned in
*actual_sizep
. Should return NULL
if it
cannot allocate a suitable piece of memory. Note that the memory
returned need not be aligned as long as there is room for an aligned
block of at least size
bytes.
alloc_hook
. If force
is non-zero,
free_hook
must accept the piece of memory; otherwise, it only
accepts it if it is able to return it to the operating system.
free_hook
should return non-zero iff it accepts the piece of
memory. Otherwise, the upper layer will keep using the memory as if
it were not freed.
SP_set_memalloc_hooks()
is passed to each
hook function. One possible use is to keep track of multiple
SICStus run-times within the same process.
Non-zero on success, Zero on error, e.g. if called after
SP_initialize()
.
The default bottom layers look at the system properties PROLOGINITSIZE, PROLOGINCSIZE, PROLOGKEEPSIZE and PROLOGMAXSIZE. They are useful for customizing the default memory manager. If you redefine the bottom layer, you can choose to ignore these system properties.
Initializing the Prolog Engine. System Properties and Environment Variables.