lua-users home
lua-l archive

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


On Wed, Apr 9, 2014 at 4:05 AM, Ross Bencina <rossb-lists@audiomulch.com> wrote:
On 9/04/2014 5:48 PM, Rena wrote:
The `osize` field of lua_Alloc() when `ptr=NULL` tells you the type of
object being allocated. But I'm not sure how to detect that it's *that
particular string* Lua is allocating memory for, other than setting a
flag just before the lua_pushlstring() and having the allocator check
for that flag and a string of that length. But that seems quite fragile.

That was the kind of thing I had in mind.

Why do you think that it's fragile?

I was thinking you would define a C function to allocate these particular "shared strings". That function sets the flag, calls lua_pushlstring(), and then unsets the flag.

Alternatively, simply allocate all strings with the refcount (or all strings above a certain size).

Ross.


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.