Example #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. */
}
Example #2
0
/***
Receive a message from a socket.
@function recvfrom
@int fd socket descriptor to act on
@int count maximum number of bytes to receive
@treturn[1] int received bytes
@treturn[1] sockaddr address of message source, if successful
@return[2] nil
@treturn[2] string error message
@treturn[2] int errnum
@see recvfrom(2)
*/
static int
Precvfrom(lua_State *L)
{
	void *ud, *buf;
	socklen_t salen;
	struct sockaddr_storage sa;
	int r;
	int fd = checkint(L, 1);
	int count = checkint(L, 2);
	lua_Alloc lalloc;

	checknargs(L, 2);
	lalloc = lua_getallocf(L, &ud);

	/* Reset errno in case lalloc doesn't set it */
	errno = 0;
	if ((buf = lalloc(ud, NULL, 0, count)) == NULL && count > 0)
		return pusherror(L, "lalloc");

	salen = sizeof(sa);
	r = recvfrom(fd, buf, count, 0, (struct sockaddr *)&sa, &salen);
	if (r < 0)
	{
		lalloc(ud, buf, count, 0);
		return pusherror(L, NULL);
	}

	lua_pushlstring(L, buf, r);
	lalloc(ud, buf, count, 0);
	return 1 + pushsockaddrinfo(L, sa.ss_family, (struct sockaddr *)&sa);
}
	LuaInstance& LuaState::GetInstance(lua_State* internalState)
	{
		LuaInstance* instance;
		lua_getallocf(internalState, reinterpret_cast<void**>(&instance));

		return *instance;
	}
Example #4
0
/***
Receive a message from a socket.
@function recv
@int fd socket descriptor to act on
@int count maximum number of bytes to receive
@treturn[1] int received bytes, if successful
@return[2] nil
@treturn[2] string error message
@treturn[2] int errnum
@see recv(2)
*/
static int
Precv(lua_State *L)
{
	int fd = checkint(L, 1);
	int count = checkint(L, 2), ret;
	void *ud, *buf;
	lua_Alloc lalloc;

	checknargs(L, 2);
	lalloc = lua_getallocf(L, &ud);

	/* Reset errno in case lalloc doesn't set it */
	errno = 0;
	if ((buf = lalloc(ud, NULL, 0, count)) == NULL && count > 0)
		return pusherror(L, "lalloc");

	ret = recv(fd, buf, count, 0);
	if (ret < 0)
	{
		lalloc(ud, buf, count, 0);
		return pusherror(L, NULL);
	}

	lua_pushlstring(L, buf, ret);
	lalloc(ud, buf, count, 0);
	return 1;
}
Example #5
0
/*static*/ void LuaState::Destroy( LuaState* state )
{
	void* data;
	lua_Alloc reallocFunc = lua_getallocf(state->GetCState(), &data);
	state->~LuaState();
	(*reallocFunc)(data, state, sizeof(LuaState), 0, NULL, 0);
}
Example #6
0
/***
Create a unique temporary file.
@function mkstemp
@string templ pattern that ends in six 'X' characters
@treturn[1] int open file descriptor
@treturn[2] string path to file, if successful
@return[2] nil
@treturn[2] string error message
@treturn[2] int errnum
@see mkstemp(3)
@usage P.mkstemp 'wooXXXXXX'
*/
static int
Pmkstemp(lua_State *L)
{
	const char *path = luaL_checkstring(L, 1);
	size_t path_len = strlen(path) + 1;
	void *ud;
	lua_Alloc lalloc;
	char *tmppath;
	int r;
	checknargs(L, 1);
	lalloc = lua_getallocf(L, &ud);

	if ((tmppath = lalloc(ud, NULL, 0, path_len)) == NULL)
		return pusherror(L, "lalloc");
	strcpy(tmppath, path);
	r = mkstemp(tmppath);

	if (r != -1)
	{
		lua_pushinteger(L, r);
		lua_pushstring(L, tmppath);
	}

	lalloc(ud, tmppath, path_len, 0);
	return (r == -1) ? pusherror(L, path) : 2;
}
Example #7
0
static int Pgetgroups(lua_State *L)		/** getgroups() */
{
	int n_group_slots = getgroups(0, NULL);

	if (n_group_slots >= 0) {
		int n_groups;
		void *ud;
		gid_t *group;
		lua_Alloc lalloc = lua_getallocf(L, &ud);

		if ((group = lalloc(ud, NULL, 0, n_group_slots * sizeof *group)) == NULL)
			return 0;

		if ((n_groups = getgroups(n_group_slots, group)) >= 0) {
			int i;
			lua_createtable(L, n_groups, 0);
			for (i = 0; i < n_groups; i++) {
				lua_pushinteger(L, group[i]);
				lua_rawseti(L, -2, i + 1);
			}
			free (group);
			return 1;
		}

		free(group);
	}

	return 0;
}
Example #8
0
/***
Current working directory for this process.
@function getcwd
@treturn[1] string path of current working directory, if successful
@return[2] nil
@treturn[2] string error message
@treturn[2] int errnum
@see getcwd(3)
*/
static int
Pgetcwd(lua_State *L)
{
#ifdef __GNU__
	char *b = get_current_dir_name();
	checknargs(L, 0);
	if (b == NULL)
		/* we return the same error as below */
		return pusherror(L, ".");
	return pushstringresult(b);
#else
	long size = pathconf(".", _PC_PATH_MAX);
	void *ud;
	lua_Alloc lalloc;
	char *b, *r;
	checknargs(L, 0);
	lalloc = lua_getallocf(L, &ud);
	if (size == -1)
		size = _POSIX_PATH_MAX; /* FIXME: Retry if this is not long enough */
	if ((b = lalloc(ud, NULL, 0, (size_t)size + 1)) == NULL)
		return pusherror(L, "lalloc");
	r = getcwd(b, (size_t)size);
	if (r != NULL)
		lua_pushstring(L, b);
	lalloc(ud, b, (size_t)size + 1, 0);
	return (r == NULL) ? pusherror(L, ".") : 1;
#endif
}
Example #9
0
/***
Create a unique temporary directory.
@function mkdtemp
@string templ pattern that ends in six 'X' characters
@treturn[1] string path to directory, if successful
@return[2] nil
@treturn[2] string error message
@treturn[2] int errnum
@see mkdtemp(3)
*/
static int
Pmkdtemp(lua_State *L)
{
#if defined LPOSIX_2008_COMPLIANT
	const char *path = luaL_checkstring(L, 1);
	size_t path_len = strlen(path) + 1;
	void *ud;
	lua_Alloc lalloc;
	char *tmppath;
	char *r;
	checknargs(L, 1);
	lalloc = lua_getallocf(L, &ud);

	if ((tmppath = lalloc(ud, NULL, 0, path_len)) == NULL)
		return pusherror(L, "lalloc");
	strcpy(tmppath, path);

	if ((r = mkdtemp(tmppath)))
		lua_pushstring(L, tmppath);
	lalloc(ud, tmppath, path_len, 0);
	return (r == NULL) ? pusherror(L, path) : 1;
#else
	return binding_notimplemented(L, "mkdtemp", "C");
#endif
}
Example #10
0
/***
Send message to a message queue
@function msgsnd
@int id message queue identifier returned by @{msgget}
@int type arbitrary message type
@string message content
@int[opt=0] flags optionally `IPC_NOWAIT`
@treturn int 0, if successful
@return[2] nil
@treturn[2] string error message
@treturn[2] int errnum
@see msgsnd(2)
 */
static int
Pmsgsnd(lua_State *L)
{
	void *ud;
	lua_Alloc lalloc = lua_getallocf(L, &ud);
	struct {
		long mtype;
		char mtext[0];
	} *msg;
	size_t len;
	size_t msgsz;
	ssize_t r;

	int msgid = checkint(L, 1);
	long msgtype = checklong(L, 2);
	const char *msgp = luaL_checklstring(L, 3, &len);
	int msgflg = optint(L, 4, 0);

	checknargs(L, 4);

	msgsz = sizeof(long) + len;

	if ((msg = lalloc(ud, NULL, 0, msgsz)) == NULL)
		return pusherror(L, "lalloc");

	msg->mtype = msgtype;
	memcpy(msg->mtext, msgp, len);

	r = msgsnd(msgid, msg, msgsz, msgflg);
	lua_pushinteger(L, r);

	lalloc(ud, msg, msgsz, 0);

	return (r == -1 ? pusherror(L, NULL) : 1);
}
Example #11
0
/***
Receive message from a message queue
@function msgrcv
@int id message queue identifier returned by @{msgget}
@int size maximum message size
@int type message type (optional, default - 0)
@int[opt=0] flags bitwise OR of zero or more of `IPC_NOWAIT`, `MSG_EXCEPT`
  and `MSG_NOERROR`
@treturn[1] int message type from @{msgsnd}
@treturn[1] string message text, if successful
@return[2] nil
@treturn[2] string error message
@treturn[2] int errnum
@see msgrcv(2)
 */
static int
Pmsgrcv(lua_State *L)
{
	int msgid = checkint(L, 1);
	size_t msgsz = checkint(L, 2);
	long msgtyp = optint(L, 3, 0);
	int msgflg = optint(L, 4, 0);

	void *ud;
	lua_Alloc lalloc;
	struct {
		long mtype;
		char mtext[0];
	} *msg;

	checknargs(L, 4);
	lalloc = lua_getallocf(L, &ud);

	if ((msg = lalloc(ud, NULL, 0, msgsz)) == NULL)
		return pusherror(L, "lalloc");

	int res = msgrcv(msgid, msg, msgsz, msgtyp, msgflg);
	if (res != -1)
	{
		lua_pushinteger(L, msg->mtype);
		lua_pushlstring(L, msg->mtext, res - sizeof(long));
	}
	lalloc(ud, msg, msgsz, 0);

	return (res == -1) ? pusherror(L, NULL) : 2;
}
Example #12
0
LUALIB_API void luaL_freetemporary(lua_State *L, void *memo, size_t size)
{
	void *userdata;
	lua_Alloc alloc = lua_getallocf(L, &userdata);
	memo = alloc(userdata, memo, size, 0);
	assert(memo == NULL);
}
Example #13
0
NzLuaInstance* NzLuaInstance::GetInstance(lua_State* state)
{
	NzLuaInstance* instance;
	lua_getallocf(state, reinterpret_cast<void**>(&instance));

	return instance;
}
Example #14
0
void *Lmalloc(lua_State *L, size_t size) {
  void *ud;
  lua_Alloc lalloc = lua_getallocf(L, &ud);
  void *p = lalloc(L, NULL, 0, size);
  if(p == NULL)
    luaL_error(L, "malloc failed");
  return p;
}
Example #15
0
void *mp_realloc(lua_State *L, void *target, size_t osize,size_t nsize) {
    void *(*local_realloc) (void *, void *, size_t osize, size_t nsize) = NULL;
    void *ud;

    local_realloc = lua_getallocf(L, &ud);

    return local_realloc(ud, target, osize, nsize);
}
Example #16
0
//==============================================================================
void LuaBinder::luaFree(lua_State* l, void* ptr)
{
	void* ud;
	lua_getallocf(l, &ud);
	ANKI_ASSERT(ud);
	LuaBinder* binder = static_cast<LuaBinder*>(ud);

	binder->m_alloc.getMemoryPool().free(ptr);
}
Example #17
0
static MainRenderer* getMainRenderer(lua_State* l)
{
	LuaBinder* binder = nullptr;
	lua_getallocf(l, reinterpret_cast<void**>(&binder));

	ScriptManager* scriptManager = reinterpret_cast<ScriptManager*>(binder->getParent());

	return &scriptManager->getMainRenderer();
}
Example #18
0
void LuaState::newthread_handler(lua_State *L, lua_State *L1)
{
	void* data;
	lua_Alloc reallocFunc = lua_getallocf(L, &data);
	LuaState* state = (LuaState*)(*reallocFunc)(data, NULL, 0, sizeof(LuaState), "CoLuaState", 0);
	::new(state) LuaState(L, L1);

//    luastateopen_thread(L);
}
Example #19
0
//==============================================================================
void* LuaBinder::luaAlloc(lua_State* l, size_t size, U32 alignment)
{
	void* ud;
	lua_getallocf(l, &ud);
	ANKI_ASSERT(ud);
	LuaBinder* binder = static_cast<LuaBinder*>(ud);

	return binder->m_alloc.getMemoryPool().allocate(size, alignment);
}
Example #20
0
void luajack_malloc_init(lua_State *L)
    {
    if(alloc)
        luaL_error(L, UNEXPECTED_ERROR);
    alloc = lua_getallocf(L, &ud);
	rt_alloc = luajack_allocf(&rt_ud);
	if(!rt_alloc)
		luajack_error("cannot initialize luajack_malloc()");
    }
	void LuaInstance::TimeLimiter(lua_State* internalState, lua_Debug* debug)
	{
		NazaraUnused(debug);

		LuaInstance* instance;
		lua_getallocf(internalState, reinterpret_cast<void**>(&instance));

		if (instance->m_clock.GetMilliseconds() > instance->GetTimeLimit())
			luaL_error(internalState, "maximum execution time exceeded");
	}
Example #22
0
static void freememory(lua_State *L, void *memo)
{
	if (memo != NULL) {
		void *userdata;
		lua_Alloc alloc = lua_getallocf(L, &userdata);
		memo -= sizeof(size_t);
		memo = alloc(userdata, memo, *((size_t *)memo), 0);
		assert(memo == NULL);
	}
}
Example #23
0
static void *lupb_alloc_func(upb_alloc *alloc, void *ptr, size_t oldsize,
                             size_t size) {
  lupb_alloc *lalloc = (lupb_alloc*)alloc;
  void *ud;

  /* We read this every time in case the user changes the Lua alloc function. */
  lua_Alloc func = lua_getallocf(lalloc->L, &ud);

  return func(ud, ptr, oldsize, size);
}
Example #24
0
File: lauxlib.c Project: lua/lua
static void *resizebox (lua_State *L, int idx, size_t newsize) {
  void *ud;
  lua_Alloc allocf = lua_getallocf(L, &ud);
  UBox *box = (UBox *)lua_touserdata(L, idx);
  void *temp = allocf(ud, box->box, box->bsize, newsize);
  if (temp == NULL && newsize > 0)  /* allocation error? */
    luaL_error(L, "not enough memory for buffer allocation");
  box->box = temp;
  box->bsize = newsize;
  return temp;
}
static inline lua_Alloc
locked_getallocf(lunatik_State *Lk, void **ud)
{
	lua_Alloc alloc = NULL;

	lunatik_lock(Lk);
	alloc = lua_getallocf(Lk->L, ud);
	lunatik_unlock(Lk);

	return alloc;
}
Example #26
0
static int
Preadlink(lua_State *L)
{
	char *b;
	struct stat s;
	const char *path = luaL_checkstring(L, 1);
	void *ud;
	lua_Alloc lalloc;
	ssize_t n, bufsiz;
	int err;
	checknargs(L, 1);
	lalloc = lua_getallocf(L, &ud);

	errno = 0; /* ignore outstanding unreported errors */

	/* s.st_size is length of linkname, with no trailing \0 */
	if (lstat(path, &s) < 0)
		return pusherror(L, path);

	/* diagnose non-symlinks */
	if (!S_ISLNK(s.st_mode))
	{
		lua_pushnil(L);
		lua_pushfstring(L, "%s: not a symbolic link", path);
		lua_pushinteger(L, EINVAL);
		return 3;
	}

	/* allocate a buffer for linkname, with no trailing \0 */
	bufsiz = s.st_size > 0 ? s.st_size : PATH_MAX;
	if ((b = (char*)lalloc(ud, NULL, 0, bufsiz)) == NULL)
		return pusherror(L, "lalloc");

	n = readlink(path, b, bufsiz);
	err = errno; /* save readlink error code, if any */
	if (n > 0)
		lua_pushlstring(L, b, n);
	lalloc(ud, b, bufsiz, 0);

	/* report new errors from this function */
	if (n < 0)
	{
		errno = err; /* restore readlink error code */
		return pusherror(L, "readlink");
	}
	else if (n < s.st_size)
	{
		lua_pushnil(L);
		lua_pushfstring(L, "%s: readlink wrote only %d of %d bytes", path, n, s.st_size);
		return 2;
	}

	return 1;
}
Example #27
0
static void
delete_memoryfile_buffer (MemoryFile *f, lua_State *L) {
    lua_Alloc alloc;
    void *alloc_ud;

    if (f->buf) {
        alloc = lua_getallocf(L, &alloc_ud);
        f->buf = alloc(alloc_ud, f->buf, f->buf_max_size, 0);
    }

    f->buf_size = f->buf_max_size = f->buf_pos = 0;
}
Example #28
0
void
snlua_release(struct snlua *l) {
#ifdef PREALLOCMEM
	void * ud = NULL;
	lua_Alloc lalloc = lua_getallocf(l->L, &ud);
	assert(lalloc == skynet_lua_alloc);
	lua_close(l->L);
	skynet_lalloc_delete(ud);
#else
	lua_close(l->L);
#endif
	free(l);
}
void
lunatik_close(lunatik_State *Lk)
{
	void *ud;
	lua_Alloc alloc = NULL;

	lunatik_lock(Lk);
	alloc = lua_getallocf(Lk->L, &ud);
	lua_close(Lk->L);
	lunatik_unlock(Lk);

	LUNATIK_FREE(alloc, ud, Lk, sizeof(lunatik_State));
}	
Example #30
0
void buffer_init (TBuffer *buf, size_t sz, lua_State *L, TFreeList *fl) {
  void *ud;
  lua_Alloc lalloc = lua_getallocf(L, &ud);
  buf->arr = (char*) lalloc (ud, NULL, 0, sz);
  if (!buf->arr) {
    freelist_free (fl);
    luaL_error (L, "malloc failed");
  }
  buf->size = sz;
  buf->top = 0;
  buf->L = L;
  buf->freelist = fl;
  freelist_add (fl, buf);
}