From 632e9a8987961769fe478b0f60251f4fda5ca732 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Thu, 16 Feb 2012 17:13:09 +0400 Subject: [PATCH] Add support of buffered IO reset to support persistent connections. --- src/buffer.c | 28 ++++++++++++++++++++-------- src/buffer.h | 6 ++++++ src/fstring.c | 15 +++++++++++++++ src/fstring.h | 5 +++++ 4 files changed, 46 insertions(+), 8 deletions(-) diff --git a/src/buffer.c b/src/buffer.c index 2413fbbeb..764f82e39 100644 --- a/src/buffer.c +++ b/src/buffer.c @@ -353,12 +353,12 @@ read_buffers (gint fd, rspamd_io_dispatcher_t * d, gboolean skip_read) } if (d->in_buf == NULL) { - d->in_buf = memory_pool_alloc (d->pool, sizeof (rspamd_buffer_t)); + d->in_buf = memory_pool_alloc_tmp (d->pool, sizeof (rspamd_buffer_t)); if (d->policy == BUFFER_LINE || d->policy == BUFFER_ANY) { - d->in_buf->data = fstralloc (d->pool, BUFSIZ); + d->in_buf->data = fstralloc_tmp (d->pool, BUFSIZ); } else { - d->in_buf->data = fstralloc (d->pool, d->nchars + 1); + d->in_buf->data = fstralloc_tmp (d->pool, d->nchars + 1); } d->in_buf->pos = d->in_buf->data->begin; } @@ -634,7 +634,7 @@ rspamd_set_dispatcher_policy (rspamd_io_dispatcher_t * d, enum io_policy policy, /* Resize input buffer if needed */ if (policy == BUFFER_CHARACTER && nchars != 0) { if (d->in_buf && d->in_buf->data->size < nchars) { - tmp = fstralloc (d->pool, d->nchars + 1); + tmp = fstralloc_tmp (d->pool, d->nchars + 1); memcpy (tmp->begin, d->in_buf->data->begin, d->in_buf->data->len); t = d->in_buf->pos - d->in_buf->data->begin; tmp->len = d->in_buf->data->len; @@ -644,7 +644,7 @@ rspamd_set_dispatcher_policy (rspamd_io_dispatcher_t * d, enum io_policy policy, } else if (policy == BUFFER_LINE || policy == BUFFER_ANY) { if (d->in_buf && d->nchars < BUFSIZ) { - tmp = fstralloc (d->pool, BUFSIZ); + tmp = fstralloc_tmp (d->pool, BUFSIZ); memcpy (tmp->begin, d->in_buf->data->begin, d->in_buf->data->len); t = d->in_buf->pos - d->in_buf->data->begin; tmp->len = d->in_buf->data->len; @@ -663,20 +663,23 @@ rspamd_dispatcher_write (rspamd_io_dispatcher_t * d, void *data, size_t len, gbo { rspamd_buffer_t *newbuf; - newbuf = memory_pool_alloc (d->pool, sizeof (rspamd_buffer_t)); + newbuf = memory_pool_alloc_tmp (d->pool, sizeof (rspamd_buffer_t)); if (len == 0) { /* Assume NULL terminated */ len = strlen ((gchar *)data); } if (!allocated) { - newbuf->data = fstralloc (d->pool, len); + newbuf->data = memory_pool_alloc_tmp (d->pool, sizeof (f_str_t)); + newbuf->data->begin = memory_pool_alloc_tmp (d->pool, len); + newbuf->data->size = len; + newbuf->data->len = len; /* We need to copy data to temporary internal buffer to avoid using of stack variables */ memcpy (newbuf->data->begin, data, len); } else { - newbuf->data = memory_pool_alloc (d->pool, sizeof (f_str_t)); + newbuf->data = memory_pool_alloc_tmp (d->pool, sizeof (f_str_t)); newbuf->data->begin = data; newbuf->data->size = len; } @@ -742,6 +745,15 @@ rspamd_dispatcher_restore (rspamd_io_dispatcher_t * d) } } +void +rspamd_dispacther_cleanup (rspamd_io_dispatcher_t *d) +{ + g_queue_clear (d->out_buffers); + /* Cleanup temporary data */ + memory_pool_cleanup_tmp (d->pool); + d->in_buf = NULL; +} + #undef debug_ip /* diff --git a/src/buffer.h b/src/buffer.h index 96e38d471..ce2cf3d2e 100644 --- a/src/buffer.h +++ b/src/buffer.h @@ -123,4 +123,10 @@ void rspamd_dispatcher_restore (rspamd_io_dispatcher_t *d); */ void rspamd_remove_dispatcher (rspamd_io_dispatcher_t *dispatcher); +/** + * Cleanup dispatcher freeing all temporary data + * @param dispatcher pointer to dispatcher's object + */ +void rspamd_dispacther_cleanup (rspamd_io_dispatcher_t *dispatcher); + #endif diff --git a/src/fstring.c b/src/fstring.c index c2d48603b..b3050d8c2 100644 --- a/src/fstring.c +++ b/src/fstring.c @@ -272,6 +272,21 @@ fstralloc (memory_pool_t * pool, size_t len) return res; } +/* + * Allocate memory for f_str_t from temporary pool + */ +f_str_t * +fstralloc_tmp (memory_pool_t * pool, size_t len) +{ + f_str_t *res = memory_pool_alloc_tmp (pool, sizeof (f_str_t)); + + res->begin = memory_pool_alloc_tmp (pool, len); + + res->size = len; + res->len = 0; + return res; +} + /* * Truncate string to its len */ diff --git a/src/fstring.h b/src/fstring.h index 566cbdc32..c55c3627a 100644 --- a/src/fstring.h +++ b/src/fstring.h @@ -78,6 +78,11 @@ gint fstrpush_unichar (f_str_t *dest, gunichar c); */ f_str_t* fstralloc (memory_pool_t *pool, size_t len); +/* + * Allocate memory for f_str_t from temporary pool + */ +f_str_t* fstralloc_tmp (memory_pool_t *pool, size_t len); + /* * Truncate string to its len */ -- 2.39.5