lua-users home
lua-l archive

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

I want to be sure that any Lua user trying to alloc too large memory
objects would see immediately.

In my "restricted RAM (60kB Lua alloc memory only)" application, I
want to limit the maximum alloc size to 5kB.

So e. g. the command
    str= string.rep( 'x', 6000)
should stop Lua with some "nice error info".

I use own alloc function.

My first try was, just to return zero, if size there is above 5000.

Then Lua stops with error "not enough memory" ... this is a bit not so
nice error, as line info missing (Lua alloc function does not contain
L, therefore I cannot invoike luaL_error(L, msg), as usually required
for this.

Looking at the call stack in this situation, the Lua function invoking
alloc is called
    void *luaM_malloc_ (lua_State *L, size_t size, int tag) { ...}  (in lmem.c)

This is fine, if I include there the 4 lines:
    if( size > 5000){ //2022-11-01 //Wf
      extern const char* cpctoolong;
      luaL_error( L, cpctoolong);
Then my Lua user gets a nice error message "too long" with program
line info where this happens.

Is this a "reasonable way" to attack this task?

Or some more "generalized way" possible?

(I am a bit disturbed, that there is no "standard procedure" for such
a limit, I think to present a safe and clear Lua programming
experience to the user, this somehow should be limited in any program
... just on larger systems of course with much higher limit value