Re: Catching 'out of memory' using pcall()

  • From: Daniel Kolesa <quaker66@xxxxxxxxx>
  • To: luajit@xxxxxxxxxxxxx
  • Date: Thu, 13 Mar 2014 13:16:26 +0000

How about you just override the default LuaJIT allocator from C, keeping
track of the amount of allocated memory and then handling it when it tries
to go over that? Of course, on x64 you cannot use a custom allocator via
lua_newstate because LuaJIT requires use of its own allocator, what you CAN
do though is create the state with luaL_newstate, get the original alloc
func and data pointer via lua_getallocf and force a new alloc func and data
using lua_setallocf (which will keep track of memory AND then call into the
original alloc func, passing the original data)

Regards,

Daniel


2014-03-13 7:06 GMT+00:00 Dmitri Shubin <sbn@xxxxxxxxxxx>:

> Hi list, Mike!
>
> Is it possible to catch 'out of memory' condition using pcall() ?
>
> The test I tried shows a bit contradictory results:
>
> $ cat -n a.lua
>      1  local function foo()
>      2      local t = {}
>      3      local i = 0
>      4      while true do
>      5          t[i] = tostring(i)
>      6          i = i + 1
>      7      end
>      8  end
>      9
>     10  local r = { pcall(foo) }
>     11  print(r[1], r[2])
> $ ./src/luajit -v
> LuaJIT 2.0.3 -- Copyright (C) 2005-2014 Mike Pall. http://luajit.org/
> $ ./src/luajit a.lua
> PANIC: unprotected error in call to Lua API (not enough memory)
>
> But if I change line 5 to
>      5          t[i] = "x" .. tostring(i)
>
> I get
>
> $ ./src/luajit a.lua
> false   not enough memory
>
> I'm on Linux, x86_64
>
> Thanks!
>
>

Other related posts: