From 0c7777aeb6c1b668e2b9e1fddf036dcb27f6db46 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Tue, 3 Jul 2012 15:43:46 +0200 Subject: util.pposix: Add setenv() --- util-src/pposix.c | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) (limited to 'util-src') diff --git a/util-src/pposix.c b/util-src/pposix.c index dae48390..a5a89d55 100644 --- a/util-src/pposix.c +++ b/util-src/pposix.c @@ -581,6 +581,37 @@ int lc_uname(lua_State* L) return 1; } +int lc_setenv(lua_State* L) +{ + const char *var = luaL_checkstring(L, 1); + const char *value; + + /* If the second argument is nil or nothing, unset the var */ + if(lua_isnoneornil(L, 2)) + { + if(unsetenv(var) != 0) + { + lua_pushnil(L); + lua_pushstring(L, strerror(errno)); + return 2; + } + lua_pushboolean(L, 1); + return 1; + } + + value = luaL_checkstring(L, 2); + + if(setenv(var, value, 1) != 0) + { + lua_pushnil(L); + lua_pushstring(L, strerror(errno)); + return 2; + } + + lua_pushboolean(L, 1); + return 1; +} + /* Register functions */ int luaopen_util_pposix(lua_State *L) @@ -612,6 +643,8 @@ int luaopen_util_pposix(lua_State *L) { "uname", lc_uname }, + { "setenv", lc_setenv }, + { NULL, NULL } }; -- cgit v1.2.3 From 364fd6915460a12a2fcdcee972da836ba0244e5a Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Sun, 8 Jul 2012 18:47:05 +0100 Subject: util.pposix: Add meminfo() binding to memory allocation stats provided by mallinfo() [compilation tested on Ubuntu...] --- util-src/pposix.c | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'util-src') diff --git a/util-src/pposix.c b/util-src/pposix.c index a5a89d55..8dfd6c75 100644 --- a/util-src/pposix.c +++ b/util-src/pposix.c @@ -34,6 +34,11 @@ #include "lua.h" #include "lauxlib.h" +#if (defined(_SVID_SOURCE) && !defined(WITHOUT_MALLINFO)) + #include + #define WITH_MALLINFO +#endif + /* Daemonization support */ static int lc_daemonize(lua_State *L) @@ -612,6 +617,25 @@ int lc_setenv(lua_State* L) return 1; } +#ifdef WITH_MALLINFO +int lc_meminfo(lua_State* L) +{ + struct mallinfo info = mallinfo(); + lua_newtable(L); + lua_pushinteger(L, info.arena); + lua_setfield(L, -2, "allocated"); + lua_pushinteger(L, info.hblkhd); + lua_setfield(L, -2, "allocated_mmap"); + lua_pushinteger(L, info.uordblks); + lua_setfield(L, -2, "used"); + lua_pushinteger(L, info.fordblks); + lua_setfield(L, -2, "unused"); + lua_pushinteger(L, info.keepcost); + lua_setfield(L, -2, "returnable"); + return 1; +} +#endif + /* Register functions */ int luaopen_util_pposix(lua_State *L) @@ -645,6 +669,10 @@ int luaopen_util_pposix(lua_State *L) { "setenv", lc_setenv }, +#ifdef WITH_MALLINFO + { "meminfo", lc_meminfo }, +#endif + { NULL, NULL } }; -- cgit v1.2.3 From 1c1baa42c8a2c047f938a442d776f33320013c0c Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Sun, 8 Jul 2012 18:54:30 +0100 Subject: util.pposix: Add comments to mallinfo fields we use, so I don't forget tomorrow what they mean --- util-src/pposix.c | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'util-src') diff --git a/util-src/pposix.c b/util-src/pposix.c index 8dfd6c75..65f8b4ab 100644 --- a/util-src/pposix.c +++ b/util-src/pposix.c @@ -622,14 +622,20 @@ int lc_meminfo(lua_State* L) { struct mallinfo info = mallinfo(); lua_newtable(L); + /* This is the total size of memory allocated with sbrk by malloc, in bytes. */ lua_pushinteger(L, info.arena); lua_setfield(L, -2, "allocated"); + /* This is the total size of memory allocated with mmap, in bytes. */ lua_pushinteger(L, info.hblkhd); lua_setfield(L, -2, "allocated_mmap"); + /* This is the total size of memory occupied by chunks handed out by malloc. */ lua_pushinteger(L, info.uordblks); lua_setfield(L, -2, "used"); + /* This is the total size of memory occupied by free (not in use) chunks. */ lua_pushinteger(L, info.fordblks); lua_setfield(L, -2, "unused"); + /* This is the size of the top-most releasable chunk that normally borders the + end of the heap (i.e., the high end of the virtual address space's data segment). */ lua_pushinteger(L, info.keepcost); lua_setfield(L, -2, "returnable"); return 1; -- cgit v1.2.3