[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: how to use lmem.h ?
- From: Mark Hamburg <mark@...>
- Date: Sun, 22 Feb 2009 10:45:09 -0800
One other thing to note about my recycled buffer approach:
Unless you need it for something else, you could set the environment
on the C function to be a weak table and then just access the value at
a fixed integer index. The size should be obtained from the userdata
to make this all multi-universe friendly. My first round of code here
wasn't multi-universe friendly since it used a global for the size.
lua_rawgeti( L, LUA_ENVIRONINDEX, 1 );
size = lua_objlen( L, -1 );
if( size < needSize ) {
lua_pop( L, 1 );
buf = lua_newuserdata( L, needSize );
lua_pushvalue( L, -1 );
lua_rawseti( L, LUA_ENVIRONINDEX, 1 );
} else {
buf = lua_touserdata( L, -1 );
}
That could obviously be wrapped up in a utility function.
One other thing to do is to inflate need size if we actually do an
allocation to make it less likely that we will have to come back
around and do another allocation. For example, one might insist that
it be at least 256 and a power of two up to 4096 at which point it
needs to be a multiple of 4096. (Actually, one would probably want to
do this with some knowledge of Lua's overhead on userdata size and the
behavior of the underlying memory allocator. For example, we might
actually want to be just a bit shy of these values.)
Mark