[PPL-devel] GMP memory allocation problem in SWI-Prolog 5.6.38 and following versions

Jan Wielemaker wielemak at science.uva.nl
Tue Oct 2 11:52:48 CEST 2007


Roberto,

On Tuesday 02 October 2007 10:25, Roberto Bagnara wrote:
> Jan Wielemaker wrote:
> > Rebinding the allocation functions is done to allow SWI-Prolog to detect
> > and act upon GMP memory overflows. Unfortunately GMP is very weak in
> > allowing applications to act gracefully on overflows. I see no reason
> > for problems as long as you initialize Prolog before touching any GMP
> > number.
>
> This is not always possible, unfortunately.

Most of the time it is, certainly with open source :-)

> > If that should be the case anyway, we must add something to the
> > foreign interface that allows for initializing Prolog without rebinding
> > the GMP allocation.
>
> This is certainly useful.  But what happens if, in some computation in
> the C++ code that has nothing to do with SWI-Prolog,  pl_abort() is
> called (I am looking at pl-gmp.c)?

Actually it never calls pl_abort(), unless something is really wrong. It
does call longjmp() back to the Prolog context :-) Note that it only
does so if it cannot allocate. The only thing you are allowed to do from
the GMP allocation functions is stop the process.  Prolog knows a few
cases where it can safely do better (discussed on the GMP mailinglist),
by throwing a Prolog resource_error exception.

> Perhaps I am missing something, but this will break the overall
> application completely.  I think SWI-Prolog should have his own
> private version of GMP if it wants to change the allocation functions.

Thats not possible, at least not using ELF binaries as in Linux.  Unless
you make a copy of the GMP sources and rename all global identifiers.

> In other words, setting the allocation functions on a shared GMP can
> only be done at the overall application level: if every component
> does that on its own, things are going to break.

True. Same goes for signals and a few more of these things. The thing
is, the way Prolog uses GMP, it *can* deal with GMP caused memory
overflows. This can be exploited as long as Prolog is the only component
in the application using GMP or at least it is possible to initialise
Prolog GMP handling before GMP numbers are used elsewhere.  This deals
with most applications.

Anyway for the others, I added 

	PL_action(PL_GMP_SET_ALLOC_FUNCTIONS, TRUE)

if you want Prolog's GMP allocation initialised *now* (without
initializing the rest of SWI-Prolog).  or

	PL_action(PL_GMP_SET_ALLOC_FUNCTIONS, FALSE)

If you want to stop Prolog from setting the allocation functions.  Must be
called before PL_initialise(), of course.

I also added some comments to the section of the manual dealing with GMP
and Prolog.  On Windows the situation is entirely different :-(

Please install from CVS.  Do not forget a "make clean"!

	Cheers --- Jan




More information about the PPL-devel mailing list