lua-users home
lua-l archive

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



Hm, I was thinking of suggesting that (local buffer) to David, but that's way too bad design considering multithreaded applications.

But having the local buffer in stack (not 'static') is safe; David, is that an option?

{
	char buffer[80];
	int length = myapi(NULL, 0);
	assert( length <= sizeof(buffer) );
	(void) myapi(buffer, length);

	lua_pushlstring(L, buffer, length);
}

Of course, one can resort to the malloc code, if length turns out to be too big (instead of assertion).

-asko


Klaus Ripke kirjoitti 4.2.2006 kello 23.19:

On Sat, Feb 04, 2006 at 07:47:19PM +0200, Asko Kauppi wrote:
No, I think not (without patching the Lua C/API, that is).

David Given kirjoitti 2.2.2006 kello 19.44:

I'm trying to wrap a particular API function that writes a string
into a
buffer.
...
I'd like to avoid the temporary buffer. Is there any way of
persuading Lua to
allocate an *uninitialised* string of a particular length on the
stack, and
then giving me a pointer to it so I can fill in that data one's the
string's
been allocated?
Yet there is a standard workaround.

For short strings, which are 99% in most applications,
the overhead of the additional malloc by far outweights the
extra copy.

So you should see substantial speedup by providing a fixed local
buffer of 1K or so a/o using alloca, depending on platform.


best