lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Dirk Feytons wrote:

A potential problem I see with this is that if lua_pushlstring()
raises an error I leak the buffer. The chance that this happens is
rather small but since this code will run on an embedded platform I
really can't afford it.

I think the luaL_Buffer() mechanism uses the C stack internally. One
of the problems with embedding Lua is that you're never really sure
how much stack you need, but at least you can look at the source if
you're really interested.

Your code could be written without malloc and use a small local
array like this and achieve the same result as lua_Buffer() like
this:

static int get_data(lua_State* L)
{
 char buffer[LOCAL_BUFFER_SIZE};
 int n = luaL_optint(L, 1, 512 /* some arbitrary default value */);

 n = read_data(&buffer[0], n);
 lua_pushlstring(L, &buffer[0], n);
 return 1;
}

I'm not a big fan of malloc() in embedded systems, but sometimes
you have no choice :-)

If you knew that get_data() was called from only one instance of a task
then you could just allocate the buffer in BSS.

Everything is a tradeoff...the fact that you can look at the
Lua source and figure out what it's doing is part of why I like
it so much!

Cheers, Ralph