From e2dfba3be4d4103cee944be98848ab52506231bf Mon Sep 17 00:00:00 2001 From: Chris Wilson Date: Mon, 17 Dec 2007 15:30:45 +0000 Subject: Add a small buffer for writes. --- src/lwp.c | 45 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 43 insertions(+), 2 deletions(-) diff --git a/src/lwp.c b/src/lwp.c index 86f81d1..e39588e 100644 --- a/src/lwp.c +++ b/src/lwp.c @@ -209,8 +209,11 @@ _lwp_read_time (void) return tv.tv_usec / 1000 + tv.tv_sec * 1000; } +static gchar _lwp_write_buf[8192]; +static guint _lwp_write_len; + static gboolean -_lwp_writen (int fd, gconstpointer _data, gsize len) +__lwp_writen (int fd, gconstpointer _data, gsize len) { const gchar *data = _data; @@ -233,6 +236,44 @@ _lwp_writen (int fd, gconstpointer _data, gsize len) return TRUE; } +static gboolean +_lwp_flush (int fd) +{ + gboolean ret; + + ret = __lwp_writen (fd, _lwp_write_buf, _lwp_write_len); + _lwp_write_len = 0; + + return ret; +} + +static gboolean +_lwp_close (int fd) +{ + gboolean ret = TRUE; + + ret &= _lwp_flush (fd); + ret &= close (fd) != -1; + + return ret; +} + +static gboolean +_lwp_writen (int fd, gconstpointer data, gsize len) +{ + if (len + _lwp_write_len > G_N_ELEMENTS (_lwp_write_buf)) { + if (! _lwp_flush (fd)) + return FALSE; + + if (len >= G_N_ELEMENTS (_lwp_write_buf)) + return __lwp_writen (fd, data, len); + } + + memcpy (_lwp_write_buf + _lwp_write_len, data, len); + _lwp_write_len += len; + return TRUE; +} + static gboolean _lwp_write_string (int fd, const char *str) { @@ -524,7 +565,7 @@ _lwp_write_events (const LWP_EventRecord *events, gushort n_events) pending_allocators = NULL; } - return close (fd) != -1; + return _lwp_close (fd); CLEAN_FD: close (fd); -- cgit v1.2.3