12.3.89 SP_set_memalloc_hooks() [preinit]

Synopsis

     #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().

Arguments

hint
is reserved for future extensions. It should be zero.
init_alloc_hook
is called initially. 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.
deinit_alloc_hook
is called by SP_deinitialize() when the Prolog engine shuts down. The function can do any necessary cleaning up.
alloc_hook
must allocate and return a pointer to a piece of memory that contains at least 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.
free_hook
is called with a pointer to a piece of memory to be freed and its size as returned by 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.
cookie
can be used for any state needed by the memory hook functions. The value passed to 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.

Return Value

Non-zero on success, Zero on error, e.g. if called after SP_initialize().

Description

The default bottom layers look at the environment variables 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 environment variables. See too-sicstus.

See Also

Initializing the Prolog Engine.


Send feedback on this subject.