diff options
author | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2016-10-08 13:43:13 +0100 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2016-10-08 16:35:42 +0100 |
commit | 47e27ce0f8ecf8dff0f9abc0e0fb466a64fdc736 (patch) | |
tree | 5b1f5cbb74b0accc910985c373b9dc4fb41d7c1f | |
parent | f98bb456ab710bc3fa99cdbe5383f39c00dcd480 (diff) | |
download | rspamd-47e27ce0f8ecf8dff0f9abc0e0fb466a64fdc736.tar.gz rspamd-47e27ce0f8ecf8dff0f9abc0e0fb466a64fdc736.zip |
[Feature] Add mmap support to lua_text
-rw-r--r-- | src/lua/lua_common.h | 4 | ||||
-rw-r--r-- | src/lua/lua_html.c | 4 | ||||
-rw-r--r-- | src/lua/lua_mimepart.c | 8 | ||||
-rw-r--r-- | src/lua/lua_regexp.c | 4 | ||||
-rw-r--r-- | src/lua/lua_task.c | 21 | ||||
-rw-r--r-- | src/lua/lua_tcp.c | 6 | ||||
-rw-r--r-- | src/lua/lua_util.c | 24 |
7 files changed, 44 insertions, 27 deletions
diff --git a/src/lua/lua_common.h b/src/lua/lua_common.h index 6fdff5990..3f2fa6ba5 100644 --- a/src/lua/lua_common.h +++ b/src/lua/lua_common.h @@ -64,10 +64,12 @@ struct rspamd_lua_ip { rspamd_inet_addr_t *addr; }; +#define RSPAMD_TEXT_FLAG_OWN (1 << 0) +#define RSPAMD_TEXT_FLAG_MMAPED (1 << 1) struct rspamd_lua_text { const gchar *start; guint len; - gboolean own; + guint flags; }; struct rspamd_lua_url { diff --git a/src/lua/lua_html.c b/src/lua/lua_html.c index bad085450..1e1ab10ae 100644 --- a/src/lua/lua_html.c +++ b/src/lua/lua_html.c @@ -355,7 +355,7 @@ lua_html_push_block (lua_State *L, struct html_block *bl) t = lua_newuserdata (L, sizeof (*t)); t->start = bl->style.start; t->len = bl->style.len; - t->own = FALSE; + t->flags = 0; lua_settable (L, -3); } } @@ -562,7 +562,7 @@ lua_html_tag_get_content (lua_State *L) rspamd_lua_setclass (L, "rspamd{text}", -1); t->start = tag->content; t->len = tag->content_length; - t->own = FALSE; + t->flags = 0; } else { lua_pushnil (L); diff --git a/src/lua/lua_mimepart.c b/src/lua/lua_mimepart.c index a2b6cc49c..e8c7bd9c8 100644 --- a/src/lua/lua_mimepart.c +++ b/src/lua/lua_mimepart.c @@ -357,7 +357,7 @@ lua_textpart_get_content (lua_State * L) rspamd_lua_setclass (L, "rspamd{text}", -1); t->start = part->content->data; t->len = part->content->len; - t->own = FALSE; + t->flags = 0; return 1; } @@ -377,7 +377,7 @@ lua_textpart_get_raw_content (lua_State * L) rspamd_lua_setclass (L, "rspamd{text}", -1); t->start = part->orig->data; t->len = part->orig->len; - t->own = FALSE; + t->flags = 0; return 1; } @@ -397,7 +397,7 @@ lua_textpart_get_content_oneline (lua_State * L) rspamd_lua_setclass (L, "rspamd{text}", -1); t->start = part->stripped_content->data; t->len = part->stripped_content->len; - t->own = FALSE; + t->flags = 0; return 1; } @@ -609,7 +609,7 @@ lua_mimepart_get_content (lua_State * L) rspamd_lua_setclass (L, "rspamd{text}", -1); t->start = part->content->data; t->len = part->content->len; - t->own = FALSE; + t->flags = 0; return 1; } diff --git a/src/lua/lua_regexp.c b/src/lua/lua_regexp.c index 80b752172..1e6122b2e 100644 --- a/src/lua/lua_regexp.c +++ b/src/lua/lua_regexp.c @@ -640,7 +640,7 @@ lua_regexp_split (lua_State *L) rspamd_lua_setclass (L, "rspamd{text}", -1); t->start = old_start; t->len = start - old_start; - t->own = FALSE; + t->flags = 0; } lua_rawseti (L, -2, ++i); @@ -665,7 +665,7 @@ lua_regexp_split (lua_State *L) rspamd_lua_setclass (L, "rspamd{text}", -1); t->start = end; t->len = (data + len) - end; - t->own = FALSE; + t->flags = 0; } lua_rawseti (L, -2, ++i); diff --git a/src/lua/lua_task.c b/src/lua/lua_task.c index 4706d69a0..a3354acb6 100644 --- a/src/lua/lua_task.c +++ b/src/lua/lua_task.c @@ -25,6 +25,7 @@ #include "email_addr.h" #include "utlist.h" #include "cryptobox.h" +#include "unix-std.h" /*** * @module rspamd_task @@ -1184,7 +1185,7 @@ lua_task_get_content (lua_State * L) rspamd_lua_setclass (L, "rspamd{text}", -1); t->len = task->msg.len; t->start = task->msg.begin; - t->own = FALSE; + t->flags = 0; } else { return luaL_error (L, "invalid arguments"); @@ -1213,7 +1214,7 @@ lua_task_get_rawbody (lua_State * L) t->start = task->msg.begin; } - t->own = FALSE; + t->flags = 0; } else { return luaL_error (L, "invalid arguments"); @@ -1311,7 +1312,7 @@ lua_task_get_request_header (lua_State *L) rspamd_lua_setclass (L, "rspamd{text}", -1); t->start = hdr->begin; t->len = hdr->len; - t->own = FALSE; + t->flags = 0; return 1; } @@ -1490,7 +1491,7 @@ lua_task_get_raw_headers (lua_State *L) rspamd_lua_setclass (L, "rspamd{text}", -1); t->start = task->raw_headers_content.begin; t->len = task->raw_headers_content.len; - t->own = FALSE; + t->flags = 0; } else { return luaL_error (L, "invalid arguments"); @@ -3473,8 +3474,16 @@ lua_text_gc (lua_State *L) { struct rspamd_lua_text *t = lua_check_text (L, 1); - if (t != NULL && t->own) { - g_free ((gpointer)t->start); + if (t != NULL) { + if (t->flags & RSPAMD_TEXT_FLAG_OWN) { + if (t->flags & RSPAMD_TEXT_FLAG_MMAPED) { + munmap ((gpointer)t->start, t->len); + } + else { + g_free ((gpointer)t->start); + } + } + } return 0; diff --git a/src/lua/lua_tcp.c b/src/lua/lua_tcp.c index 23140b79a..c80c311f8 100644 --- a/src/lua/lua_tcp.c +++ b/src/lua/lua_tcp.c @@ -199,7 +199,7 @@ lua_tcp_push_data (struct lua_tcp_cbdata *cbd, const guint8 *str, gsize len) rspamd_lua_setclass (cbd->L, "rspamd{text}", -1); t->start = (const gchar *)str; t->len = len; - t->own = FALSE; + t->flags = 0; /* Connection */ pcbd = lua_newuserdata (cbd->L, sizeof (*pcbd)); *pcbd = cbd; @@ -467,9 +467,9 @@ lua_tcp_arg_toiovec (lua_State *L, gint pos, rspamd_mempool_t *pool, vec->iov_base = (void *)t->start; vec->iov_len = t->len; - if (t->own) { + if (t->flags & RSPAMD_TEXT_FLAG_OWN) { /* Steal ownership */ - t->own = FALSE; + t->flags = 0; rspamd_mempool_add_destructor (pool, g_free, (void *)t->start); } } diff --git a/src/lua/lua_util.c b/src/lua/lua_util.c index 9c37bc861..4ce7cbe64 100644 --- a/src/lua/lua_util.c +++ b/src/lua/lua_util.c @@ -627,7 +627,7 @@ lua_util_encode_base64 (lua_State *L) t->start = out; t->len = outlen; /* Need destruction */ - t->own = TRUE; + t->flags = RSPAMD_TEXT_FLAG_OWN; } else { lua_pushnil (L); @@ -657,8 +657,8 @@ lua_util_decode_base64 (lua_State *L) s = t->start; inlen = t->len; zero_copy = TRUE; - if (t->own) { - t->own = FALSE; + if (t->flags & RSPAMD_TEXT_FLAG_OWN) { + t->flags = 0; grab_own = TRUE; } } @@ -672,7 +672,13 @@ lua_util_decode_base64 (lua_State *L) rspamd_lua_setclass (L, "rspamd{text}", -1); t->start = s; t->len = outlen; - t->own = grab_own; + + if (grab_own) { + t->flags |= RSPAMD_TEXT_FLAG_OWN; + } + else { + t->flags = 0; + } } else { t = lua_newuserdata (L, sizeof (*t)); @@ -682,7 +688,7 @@ lua_util_decode_base64 (lua_State *L) outlen = g_base64_decode_step (s, inlen, (guchar *)t->start, &state, &save); t->len = outlen; - t->own = TRUE; + t->flags = RSPAMD_TEXT_FLAG_OWN; } } else { @@ -725,7 +731,7 @@ lua_util_encode_base32 (lua_State *L) t->start = out; t->len = outlen; /* Need destruction */ - t->own = TRUE; + t->flags = RSPAMD_TEXT_FLAG_OWN; } else { lua_pushnil (L); @@ -759,7 +765,7 @@ lua_util_decode_base32 (lua_State *L) rspamd_lua_setclass (L, "rspamd{text}", -1); t->start = rspamd_decode_base32 (s, inlen, &outlen); t->len = outlen; - t->own = TRUE; + t->flags = RSPAMD_TEXT_FLAG_OWN; } else { lua_pushnil (L); @@ -793,7 +799,7 @@ lua_util_decode_url (lua_State *L) t->start = g_malloc (inlen); memcpy ((char *)t->start, s, inlen); t->len = rspamd_decode_url ((char *)t->start, s, inlen); - t->own = TRUE; + t->flags = RSPAMD_TEXT_FLAG_OWN; } else { lua_pushnil (L); @@ -938,7 +944,7 @@ lua_util_parse_html (lua_State *L) rspamd_lua_setclass (L, "rspamd{text}", -1); t->start = res->data; t->len = res->len; - t->own = TRUE; + t->flags = RSPAMD_TEXT_FLAG_OWN; g_byte_array_free (res, FALSE); g_byte_array_free (in, TRUE); |