Ejemplo n.º 1
0
/**	(limit, counter) = memory_limit([limit] [, thread])
	If limit is set to 0 then there is no memory limit, the limit is removed.
	If limit is set > 0 then memory allocations are limited to this many bytes.
	If limit is omitted, just returns the current limit and counter.
	If a limit is set (limit>0) then the counter is reset to 0.
	counter is how many bytes have been allocated.
	Note: only works with one lua state, unless called with the same value on all states.
	When the limit is tripped, the lua allocator returns NULL and then the limit is removed.
	Note: consider emergency GC in lua 5.2.
	For strictness and/or accurate accounting, do a GC collection before setting the limit.
	To simply use the counter, set the limit very high, such as 0x7FFFFFFF.
	If thread is specified, the limit is set for the current state AND the thread.
*/
static int luafunc_memory_limit(lua_State *L)
{
	lua_State *lthread = lua_tothread(L, 2);
	if(lua_isnumber(L, 1))
	{
		lua_Integer lim = lua_tointeger(L, 1);
		if(0 == lim)
		{
			memLimit = 0;
		}
		else if(lim > 0)
		{
			void *ud;
			if(&memLimitLuaAlloc != lua_getallocf(L, &ud))
			{
				if(!realLuaAllocFunc)
				{
					realLuaAllocFunc = lua_getallocf(L, &ud);
					if(!realLuaAllocFunc)
					{
						_programError("luafunc_memory_limit: lua_getallocf returned NULL", 0);
						return 0;
					}
				}
				lua_setallocf(L, &memLimitLuaAlloc, ud);
				if(&memLimitLuaAlloc != lua_getallocf(L, NULL))
				{
					_programError("luafunc_memory_limit: lua_setallocf did not set the memory allocator", 0);
					return 0;
				}
			}
			if(lthread)
			{
				if(&memLimitLuaAlloc != lua_getallocf(lthread, &ud))
				{
					if(!realLuaAllocFunc)
					{
						_programError("luafunc_memory_limit: internal error, realLuaAllocFunc expected to be set", 0);
						return 0;
					}
					lua_setallocf(lthread, &memLimitLuaAlloc, ud);
					if(&memLimitLuaAlloc != lua_getallocf(lthread, NULL))
					{
						_programError("luafunc_memory_limit: lua_setallocf did not set the memory allocator", 0);
						return 0;
					}
				}
			}
			memLimit = lim;
			memAllocCounter = 0;
		}
	}
	lua_pushinteger(L, memLimit);
	lua_pushinteger(L, memAllocCounter);
	return 2; /* Number of return values. */
}
	LuaInstance::LuaInstance(LuaInstance&& instance) :
	LuaState(std::move(instance))
	{
		std::swap(m_memoryLimit, instance.m_memoryLimit);
		std::swap(m_memoryUsage, instance.m_memoryUsage);
		std::swap(m_timeLimit, instance.m_timeLimit);
		std::swap(m_clock, instance.m_clock);
		std::swap(m_level, instance.m_level);

		if (m_state)
			lua_setallocf(m_state, MemoryAllocator, this);

		if (instance.m_state)
			lua_setallocf(instance.m_state, MemoryAllocator, &instance);
	}
	LuaInstance& LuaInstance::operator=(LuaInstance&& instance)
	{
		LuaState::operator=(std::move(instance));

		std::swap(m_memoryLimit, instance.m_memoryLimit);
		std::swap(m_memoryUsage, instance.m_memoryUsage);
		std::swap(m_timeLimit, instance.m_timeLimit);
		std::swap(m_clock, instance.m_clock);
		std::swap(m_level, instance.m_level);

		if (m_state)
			lua_setallocf(m_state, MemoryAllocator, this);

		if (instance.m_state)
			lua_setallocf(instance.m_state, MemoryAllocator, &instance);

		return *this;
	}
Ejemplo n.º 4
0
void luasandbox_alloc_delete_state(php_luasandbox_alloc * alloc, lua_State * L)
{
	// In 64-bit LuaJIT mode, restore the old allocator before calling 
	// lua_close() because lua_close() actually checks that the value of the 
	// function pointer is unchanged before destroying the underlying 
	// allocator. If the allocator has been changed, the mmap is not freed.
#ifdef LUASANDBOX_LJ_64
	lua_setallocf(L, alloc->old_alloc, alloc->old_alloc_ud);
#endif

	lua_close(L);
}
Ejemplo n.º 5
0
Archivo: luaexec.c Proyecto: mbr/luabox
int main(int argc, char **argv) {
	size_t memcount = 0;
	L = lua_newstate(l_alloc, &memcount);

	lua_setallocf(L, l_alloc, &memcount);

	luaopen_base(L);
	luaL_dofile(L, argv[1]);

	lua_close(L);

	return 0;
}
Ejemplo n.º 6
0
/* Main module function. Starts the library */
static int luamemprofiler_start(lua_State *L) {
  static lua_Alloc f;
  static void *ud;

  float memused;
  int usegraphics = 0;

  /* get the amount of memory expected to be used AND set enable graphics */
  memused = (float) lua_tonumber(L, 1);
  if (memused)
    usegraphics = 1;

  /* get default allocation function */
  f = lua_getallocf(L, &ud);

  /* check if start has been called before */
  if (f == lmp_alloc) {
    /* restore default allocation function and remove library finalizer */
    lmp_Alloc *s;
    lua_getfield(L, LUA_REGISTRYINDEX, "luamemprofiler_ud");
    s = (lmp_Alloc *) lua_touserdata(L, -1);
    lua_setallocf(L, s->f, s->ud);
    lua_getmetatable(L, -1);
    lua_pushnil(L);
    lua_setfield(L, -2, "__gc");

    lua_pushstring(L, "calling luamemprofiler start function twice");
    lua_error(L);
  }

  /* create data_structure and set finalizer */
  create_finalizer(L, f, ud);
  lua_setallocf(L, lmp_alloc, ud);

  /* L is in most cases the lowest address of the heap (easiest to access) */
  lmp_start((uintptr_t) L, memused, usegraphics);
  return 0;
}
Ejemplo n.º 7
0
lua_State * luasandbox_alloc_new_state(php_luasandbox_alloc * alloc, php_luasandbox_obj * sandbox)
{
	lua_State * L;
#ifdef LUASANDBOX_LJ_64
	// The 64-bit version of LuaJIT needs to use its own allocator
	L = luaL_newstate();
	if (L) {
		alloc->old_alloc = lua_getallocf(L, &alloc->old_alloc_ud);
		lua_setallocf(L, luasandbox_passthru_alloc, sandbox);
	}
#else
	L = lua_newstate(luasandbox_php_alloc, sandbox);
#endif
	return L;
}
Ejemplo n.º 8
0
/* restore default allocation function and stop the other modules */
static int luamemprofiler_stop(lua_State *L) {
  lmp_Alloc *s;

  /* get 'alloc' userdata and restore original allocation function */
  lua_pushstring(L, "luamemprofiler_ud");
  lua_rawget(L, LUA_REGISTRYINDEX);
  s = (lmp_Alloc*) lua_touserdata(L, -1);
  if (s == NULL) {
    lua_pushstring(L, "calling luamemprofiler stop function without calling start function");
    lua_error(L);
  }
  lua_pop(L, 1);
  lua_setallocf(L, s->f, s->ud);

  lmp_stop();
  return 0;
}
Ejemplo n.º 9
0
/*
** Called when main program ends.
** Restores lua_State original allocation function.
*/
static int finalize (lua_State *L) {
  lmp_Alloc *s;

  /* check lmp_Alloc */
  if (!lua_isuserdata(L, -1)) {
    lua_pushstring(L, "incorrect argument");
    lua_error(L);
  }

  /* get lmp_Alloc and restore original allocation function */
  s = (lmp_Alloc *) lua_touserdata(L, -1);
  if (s->f != lua_getallocf (L, NULL)) {
    lua_setallocf(L, s->f, s->ud);
    lmp_stop();
  }

  return 0;
}
Ejemplo n.º 10
0
void clua_setallocf(lua_State* L, void* goallocf)
{
	lua_setallocf(L,&allocwrapper,goallocf);
}
Ejemplo n.º 11
0
LUALIB_API lua_State *luaL_newstate (void) {
  lua_State *L = lua_newstate(l_alloc, NULL);
  lua_setallocf(L, l_alloc, L); /* allocator need lua_State. */
  if (L) lua_atpanic(L, &panic);
  return L;
}