lua-users home
lua-l archive

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


One key to reach real-time requirements is not to prevent Lua from dynamically allocating the stack (or any other object), but to provide an allocator function capable of providing memory in a fixed time. (Others are fine tuning garbage collection, preloading libraries and replacing/not using the io library after initializing, by the way).
This can be done using the official interface.
The allocator function is set using lua_newstate. Splitting allocation into a fixed size memory pool for frequently used small objects (basically everything but string), maybe <= 32 bytes (may depend on the Lua version), and another strategy for less frequently allocated larger objects seems to work. But the best strategy may depend on your system and should be verified experimentally.
In any case, you need to deal with Lua dynamically increasing and reducing not only the stack, but many other objects as well. Handle these allocations by a proper "special purpose" alloc/free implementation, instead of using the "general purpose" algorithms in a typical non-realtime standard C library.

On Wed, Sep 23, 2020 at 3:57 AM 孙世龙 sunshilong <sunshilong369@gmail.com> wrote:
Hi, Dibyendu Majumdar

>Stack can also be reallocated to reduce or grow as needed.
I want to make Lua meet the soft real-time requirement.
So I do not want to dynamically increase or reduce the stack indeed.

Best Regards
Sunshilong


On Wed, Sep 23, 2020 at 5:34 AM Dibyendu Majumdar
<mobile@majumdar.org.uk> wrote:
>
> On Tue, 22 Sep 2020 at 08:11, 孙世龙 sunshilong <sunshilong369@gmail.com> wrote:
>
>
> > How to comprehend that Lua automatically removes whatever is in the
> > stack below the results after the function returned?
> > How does it achieve this goal?
> > Any explanation or document helps.
> > I would be grateful if you could shed some light on this matter.
> >
>
> Lua's stack is a heap allocated structure. There is a pointer to the
> top of the stack in each Lua State - this is simply reset after a
> function call. Stack can also be reallocated to reduce or grow as
> needed.
>
> Regards