[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: Locking Table Member Additions
- From: Sean Conner <sean@...>
- Date: Tue, 17 Jun 2014 17:29:14 -0400
It was thus said that the Great Leinen, Rick once stated:
> -----Original Message-----
> From: firstname.lastname@example.org [mailto:email@example.com] On Behalf Of Tom N Harris
> Sent: Tuesday, June 17, 2014 1:49 PM
> To: firstname.lastname@example.org
> Subject: Re: Locking Table Member Additions
> On Tuesday, June 17, 2014 05:04:29 PM Leinen, Rick wrote:
> > The Kinetis microcontroller I am targeting has 1MB of internal flash
> > and 256KB of internal SRAM. Neither is a lot, but I have much more
> > flash. By defining the table in C, I am hoping that I will be using
> > less RAM since the Lua script will be smaller. This may prove out to
> > be negligible, but that is the thinking at the time.
> The Lua runtime will allocate a table from the same memory pool whether the definition is made in a script or by C API calls. What you want to do is create userdata to allow scripts to access the message data in the C structures. You then have control over where that memory is allocated. You also save time by not copying data when dispatching the message.
> tom <email@example.com>
> Thanks Tom,
> This sound like it may be a better solution. I looked at userdata, but I
> got the impression that it allowed Lua to use a C generated types. It
> wasn't clear to me if both Lua and C could access the data.
It's a bit involved, as you need to handle both __index and __newindex in
C. You can see an example here:
The Lua code "sees" a table with the following fields:
core = ... ,
cpu = ... ,
data = ... ,
fsize = ... ,
nofile = ... ,
stack = ... ,
vm = ...
It's a Lua table, but it's always empty as the metatable associated with it
hooks the __index and __newindex methods to call getrlimit() or setrlimit().
It could just as well be a userdata.
I do the same trick in the same file for the process id and priority:
__index returns both, whereas __newindex only sets the priority; it
ignores any attempt to change the process id.
Another example is in my network wrapper for Lua:
where I again intercept index and __newindex for a socket userdata, such as:
sock.reuseaddr = true
sock.recvbuffer = 65536 --have kernel buffer up to 64k for us
such that it calls setsockopt(). Hopefully, that will give you enough
> As far as saving memory, I assumed that the same amount of memory would be
> used whether it was created in C or Lua. I was trying to keep the size of
> the Lua script down. When a script is passed to a Lua state, is the full
> ASCII text pulled into RAM and then parsed, or does it do it line-by-line?
It depends on how the script is loaded, but luaL_loadfile() reads in
LUA_BUFFERSIZE bytes at a time. LUA_BUFFERSIZE is set in lua.h.