New behaviour of lua_Alloc missing in list of API changes

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

New behaviour of lua_Alloc missing in list of API changes

Jan Behrens-2
Hi,

when porting a program from Lua 5.1 to Lua 5.2, I experienced crashes within
my custom memory allocator function, which I passed to lua_newstate.

I finally found out that the expected behaviour of the allocator function has
changed in Lua 5.2. See: http://www.lua.org/work/doc/manual.html#lua_Alloc

Unfortunatly this is not documented in the list of changes in the API:
http://www.lua.org/work/doc/manual.html#8.3

I suggest to add this missing information in Section 8.3 of the manual.


Regards
Jan Behrens

Reply | Threaded
Open this post in threaded view
|

Re: New behaviour of lua_Alloc missing in list of API changes

Roberto Ierusalimschy
> when porting a program from Lua 5.1 to Lua 5.2, I experienced crashes within
> my custom memory allocator function, which I passed to lua_newstate.
>
> I finally found out that the expected behaviour of the allocator function has
> changed in Lua 5.2. See: http://www.lua.org/work/doc/manual.html#lua_Alloc
>
> Unfortunatly this is not documented in the list of changes in the API:
> http://www.lua.org/work/doc/manual.html#8.3
>
> I suggest to add this missing information in Section 8.3 of the manual.

Thanks. We will correct that.

-- Roberto

Reply | Threaded
Open this post in threaded view
|

Re: New behaviour of lua_Alloc missing in list of API changes

Jan Behrens-2
On Tuesday 29 March 2011 19:30:33 Roberto Ierusalimschy wrote:

> > when porting a program from Lua 5.1 to Lua 5.2, I experienced crashes
> > within my custom memory allocator function, which I passed to
> > lua_newstate.
> >
> > I finally found out that the expected behaviour of the allocator function
> > has changed in Lua 5.2. See:
> > http://www.lua.org/work/doc/manual.html#lua_Alloc
> >
> > Unfortunatly this is not documented in the list of changes in the API:
> > http://www.lua.org/work/doc/manual.html#8.3
> >
> > I suggest to add this missing information in Section 8.3 of the manual.
>
> Thanks. We will correct that.
>
> -- Roberto

My own lua_Alloc function started with code similar to the following:

if ((*counter + nsize - osize) > PGLUA_MEMORYLIMIT) return NULL;
*counter += nsize - osize;

I realized that due to the new interface I need to make another distinction of
cases to track memory usage (depending on what osize really means). According
to my own (personal) taste, it would be nice to have an extra argument passed
to the lua_Alloc function, instead of (ab)using osize for that. Old code may
be broken anyway due to the subtle changes, which have been already
introduced.


Regards
Jan Behrens

Reply | Threaded
Open this post in threaded view
|

Re: New behaviour of lua_Alloc missing in list of API changes

Jan Behrens-2
On Tuesday 05 April 2011 21:25:37 Jan Behrens wrote:

> On Tuesday 29 March 2011 19:30:33 Roberto Ierusalimschy wrote:
> > > when porting a program from Lua 5.1 to Lua 5.2, I experienced crashes
> > > within my custom memory allocator function, which I passed to
> > > lua_newstate.
> > >
> > > I finally found out that the expected behaviour of the allocator
> > > function has changed in Lua 5.2. See:
> > > http://www.lua.org/work/doc/manual.html#lua_Alloc
> > >
> > > Unfortunatly this is not documented in the list of changes in the API:
> > > http://www.lua.org/work/doc/manual.html#8.3
> > >
> > > I suggest to add this missing information in Section 8.3 of the manual.
> >
> > Thanks. We will correct that.
> >
> > -- Roberto
>
> My own lua_Alloc function started with code similar to the following:
>
> if ((*counter + nsize - osize) > PGLUA_MEMORYLIMIT) return NULL;
> *counter += nsize - osize;
>
> I realized that due to the new interface I need to make another distinction
> of cases to track memory usage (depending on what osize really means).
> According to my own (personal) taste, it would be nice to have an extra
> argument passed to the lua_Alloc function, instead of (ab)using osize for
> that. Old code may be broken anyway due to the subtle changes, which have
> been already introduced.

I rethought my last posting. Maybe it'd be a bad idea to add an extra
argument, as writing something like  if (!ptr) osize = 0;  is not a big deal
when implementing an allocation function.

Jan Behrens

Reply | Threaded
Open this post in threaded view
|

Re: New behaviour of lua_Alloc missing in list of API changes

Shawn Fox
In reply to this post by Jan Behrens-2

On Tue, Apr 5, 2011 at 2:25 PM, Jan Behrens <[hidden email]> wrote:
My own lua_Alloc function started with code similar to the following:

if ((*counter + nsize - osize) > PGLUA_MEMORYLIMIT) return NULL;
*counter += nsize - osize;

I realized that due to the new interface I need to make another distinction of
cases to track memory usage (depending on what osize really means). According
to my own (personal) taste, it would be nice to have an extra argument passed
to the lua_Alloc function, instead of (ab)using osize for that. Old code may
be broken anyway due to the subtle changes, which have been already
introduced.

What really should happen is that there should be an API function which allows a memory cap to be set, something like

lua_setmaxmemory(L,8388608);

Setting a memory cap is such a common thing to do that it really should be trivial to do it.  Not that writing a custom allocator is a huge deal but it does require some development/testing effort that would be better spent elsewhere.
Reply | Threaded
Open this post in threaded view
|

Re: New behaviour of lua_Alloc missing in list of API changes

Luiz Henrique de Figueiredo
> What really should happen is that there should be an API function which
> allows a memory cap to be set, something like
>
> lua_setmaxmemory(L,8388608);
>
> Setting a memory cap is such a common thing to do that it really should be
> trivial to do it.  Not that writing a custom allocator is a huge deal but it
> does require some development/testing effort that would be better spent
> elsewhere.

Just adapt the one in lauxlib.c:
        http://www.lua.org/source/5.1/lauxlib.c.html#l_alloc