diff options
author | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2015-07-20 17:23:42 +0100 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2015-07-20 17:23:42 +0100 |
commit | 028de314820e841d9a892a5f15067617deaba8b8 (patch) | |
tree | 2ab3033104bca0cc910aad5f9b1d687b678f8c9f /src/lua | |
parent | cad17efeebbfa1bfda7da74e27518dfe07083c8a (diff) | |
download | rspamd-028de314820e841d9a892a5f15067617deaba8b8.tar.gz rspamd-028de314820e841d9a892a5f15067617deaba8b8.zip |
Add more advanced memory pool variables.
Diffstat (limited to 'src/lua')
-rw-r--r-- | src/lua/lua_mempool.c | 69 |
1 files changed, 64 insertions, 5 deletions
diff --git a/src/lua/lua_mempool.c b/src/lua/lua_mempool.c index 69b153046..08e1cd128 100644 --- a/src/lua/lua_mempool.c +++ b/src/lua/lua_mempool.c @@ -179,12 +179,70 @@ static int lua_mempool_set_variable (lua_State *L) { struct memory_pool_s *mempool = rspamd_lua_check_mempool (L, 1); - const gchar *var = luaL_checkstring (L, 2), - *value = luaL_checkstring (L, 3); + const gchar *var = luaL_checkstring (L, 2); + gpointer value; + gchar *vp; + union { + gdouble d; + gchar *s; + gboolean b; + } val; + + gint i, len = 0, type; + + if (mempool && var) { + + for (i = 3; i < lua_gettop (L); i ++) { + type = lua_type (L, i); + + if (type == LUA_TNUMBER) { + /* We have some ambiguity here between integer and double */ + len += sizeof (gdouble); + } + if (type == LUA_TBOOLEAN) { + len += sizeof (gboolean); + } + else if (type == LUA_TSTRING) { + len += sizeof (gchar *); + } + else { + msg_err ("cannot handle lua type %s", lua_typename (L, type)); + } + } + + if (len == 0) { + msg_err ("no values specified"); + } + else { + value = rspamd_mempool_alloc (mempool, len); + vp = value; + + for (i = 3; i < lua_gettop (L); i ++) { + type = lua_type (L, i); + + if (type == LUA_TNUMBER) { + val.d = lua_tonumber (L, i); + memcpy (vp, &val, sizeof (gdouble)); + vp += sizeof (gdouble); + } + else if (type == LUA_TBOOLEAN) { + val.b = lua_toboolean (L, i); + memcpy (vp, &val, sizeof (gboolean)); + vp += sizeof (gboolean); + } + else if (type == LUA_TSTRING) { + val.s = rspamd_mempool_strdup (mempool, lua_tostring (L, i)); + memcpy (vp, &val, sizeof (gchar *)); + vp += sizeof (gchar *); + } + else { + msg_err ("cannot handle lua type %s", lua_typename (L, type)); + } + } + + rspamd_mempool_set_variable (mempool, var, value, NULL); + } - if (mempool && var && value) { - rspamd_mempool_set_variable (mempool, var, - rspamd_mempool_strdup (mempool, value), NULL); return 0; } else { @@ -194,6 +252,7 @@ lua_mempool_set_variable (lua_State *L) return 1; } + static int lua_mempool_get_variable (lua_State *L) { |