From e4d888010e5fcd92b6aa13993a5de6b3c6402dcf Mon Sep 17 00:00:00 2001
From: Konstantin Shulgin <konstantin.shulgin@gmail.com>
Date: Thu, 26 May 2011 12:18:16 +0400
Subject: [PATCH] Bug#773058:

tbuf: tbuf_ltrim function was added.
admin: admin_dispatch routine was fixed. fiber read buff managment was changed
       from peek to trim.
---
 core/admin.c   |  3 +--
 core/admin.rl  |  3 +--
 core/tbuf.c    | 13 +++++++++++++
 include/tbuf.h |  1 +
 4 files changed, 16 insertions(+), 4 deletions(-)

diff --git a/core/admin.c b/core/admin.c
index 3e5ddfe721..5f4e196e06 100644
--- a/core/admin.c
+++ b/core/admin.c
@@ -1452,8 +1452,7 @@ case 107:
 #line 199 "core/admin.rl"
 
 
-	fiber->rbuf->len -= (void *)pe - (void *)fiber->rbuf->data;
-	fiber->rbuf->data = pe;
+	tbuf_ltrim(fiber->rbuf, (void *)pe - (void *)fiber->rbuf->data);
 
 	if (p != pe) {
 		start(out);
diff --git a/core/admin.rl b/core/admin.rl
index bed4f22a8d..5352e2b5b2 100644
--- a/core/admin.rl
+++ b/core/admin.rl
@@ -198,8 +198,7 @@ admin_dispatch(void)
 		write exec;
 	}%%
 
-	fiber->rbuf->len -= (void *)pe - (void *)fiber->rbuf->data;
-	fiber->rbuf->data = pe;
+	tbuf_ltrim(fiber->rbuf, (void *)pe - (void *)fiber->rbuf->data);
 
 	if (p != pe) {
 		start(out);
diff --git a/core/tbuf.c b/core/tbuf.c
index 52a2dc48da..bb9fb13696 100644
--- a/core/tbuf.c
+++ b/core/tbuf.c
@@ -124,6 +124,19 @@ tbuf_peek(struct tbuf *b, size_t count)
 	return NULL;
 }
 
+void *
+tbuf_ltrim(struct tbuf *b, size_t count)
+{
+	void *p = NULL;
+
+	tbuf_assert(b);
+	if (count <= b->len) {
+		p = memmove(b->data, b->data + count, b->len - count);
+		b->len -= count;
+	}
+	return p;
+}
+
 size_t
 tbuf_reserve(struct tbuf *b, size_t count)
 {
diff --git a/include/tbuf.h b/include/tbuf.h
index 49c72f301e..e45d44fc09 100644
--- a/include/tbuf.h
+++ b/include/tbuf.h
@@ -63,6 +63,7 @@ struct tbuf *tbuf_split(struct tbuf *e, size_t at);
 size_t tbuf_reserve(struct tbuf *b, size_t count);
 void tbuf_reset(struct tbuf *b);
 void *tbuf_peek(struct tbuf *b, size_t count);
+void *tbuf_ltrim(struct tbuf *b, size_t count);
 
 void tbuf_append_field(struct tbuf *b, void *f);
 void tbuf_vprintf(struct tbuf *b, const char *format, va_list ap)
-- 
GitLab