lua-users home
lua-l archive

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



> -----Original Message-----
> From: lua-l-bounces@lists.lua.org [mailto:lua-l-bounces@lists.lua.org] On
> Behalf Of Tim Hill
> Sent: zaterdag 12 april 2014 1:47
> To: Lua mailing list
> Subject: Re: Algorithm help: passing strings between threads with minimal
> copying
> 
> 
> On Apr 9, 2014, at 1:10 AM, Rena <hyperhacker@gmail.com> wrote:
> 
> 
> 
> 
> The code path I'm imagining is:
> -receiveMessage()
> -find the next message in the queue
> -set a flag saying "we're receiving a message of length n"
> -lua_pushlstring(L, msg, n);
> -the allocator sees the flag is set, sees Lua asking for a string of length
> n, and gives it the message's memory block (with refcount set appropriately)
> -receiveMessage() returns
> But I wonder if it's possible that for whatever internal reasons, Lua
> happens to allocate an unrelated string as well during this time, also of
> length n?
> 
> If not then this would definitely be an interesting solution.
> 
> 
> --
> Sent from my Game Boy.
> 
> For the very reason you stated I think this is a fragile solution. You have
> no guarantee that Lua will directly call your allocator for the string push
> .. all sorts of other things could happen, and indeed change from release to
> release of Lua. It could intern the string. it could re-use an internal
> buffer cache. It could allocate some OTHER string internally before
> allocating space for your string.
> 
> -Tim

Slightly modified;
-receiveMessage()
-find the next message in the queue
-set a flag saying "we're receiving a message of length n"
-lua_pushlstring(L, msg, n);
-the allocator sees the flag is set, and allocates memory blocks (with refcount set appropriately), also keeps a list of elements allocated while flag set
-unset the flag
-check the list of allocated items (if more than 1), for the one containing the string. Remove all others from the refcount list.
-receiveMessage() returns

(still doesn't cover reusing internal buffer)

Question;
When Lua returns a string, it returns a 'const', is that already a copy of the original Lua string, or is it the actual Lua string? If so, could that returned pointer then be used to link it to the refcount?

Thijs