From d1c5d264b0ca443fb0af191d818d1861efb44e41 Mon Sep 17 00:00:00 2001
From: Dmitry Simonenko <pmwkaa@gmail.com>
Date: Fri, 11 Jan 2013 18:24:51 +0400
Subject: [PATCH] connector-c-lite: added tp_req and tp_reqbuf functions.

---
 connector/c/include/tp.h | 21 +++++++++++++++++----
 test/connector_c/tp.c    |  5 ++---
 2 files changed, 19 insertions(+), 7 deletions(-)

diff --git a/connector/c/include/tp.h b/connector/c/include/tp.h
index a73e2b5aca..d19745de4a 100644
--- a/connector/c/include/tp.h
+++ b/connector/c/include/tp.h
@@ -481,8 +481,20 @@ tp_sz(struct tp *p, char *sz) {
 	return tp_field(p, sz, strlen(sz));
 }
 
-static ssize_t
-tp_required(struct tp *p) {
+static inline ssize_t
+tp_reqbuf(char *buf, size_t size) {
+	if (tp_unlikely(size < sizeof(struct tp_h)))
+		return sizeof(struct tp_h) - size;
+	register int sz =
+		((struct tp_h*)buf)->len + sizeof(struct tp_h);
+	return (tp_likely(size < sz)) ?
+	                  sz - size : size - sz;
+}
+
+static inline ssize_t
+tp_req(struct tp *p) {
+	return tp_reqbuf(p->s, tp_used(p));
+	/*
 	register size_t used = tp_used(p);
 	if (tp_unlikely(used < sizeof(struct tp_h)))
 		return sizeof(struct tp_h) - used;
@@ -490,6 +502,7 @@ tp_required(struct tp *p) {
 	register struct tp_h *h = (struct tp_h*)p->s;
 	return (tp_likely(used < h->len)) ?
 	                  h->len - used : used - h->len;
+					  */
 }
 
 static inline size_t
@@ -512,7 +525,7 @@ tp_replyerror(struct tp *p) {
 
 static inline int
 tp_replyerrorlen(struct tp *p) {
-	return tp_unfetched(p) + tp_required(p);
+	return tp_unfetched(p) + tp_req(p);
 }
 
 static inline uint32_t
@@ -532,7 +545,7 @@ tp_replyop(struct tp *p) {
 
 tp_function_unused static ssize_t
 tp_reply(struct tp *p) {
-	if (tp_unlikely(tp_required(p) > 0))
+	if (tp_unlikely(tp_req(p) > 0))
 		return -1;
 	p->c = p->s;
 	p->h = tp_fetch(p, sizeof(struct tp_h));
diff --git a/test/connector_c/tp.c b/test/connector_c/tp.c
index 7a2db8bf58..65438e16de 100644
--- a/test/connector_c/tp.c
+++ b/test/connector_c/tp.c
@@ -3,8 +3,7 @@
 #include <tp.h>
 
 static void reply_print(struct tp *rep) {
-	char *tu;
-	while ((tu = tp_next(rep))) {
+	while (tp_next(rep)) {
 		printf("tuple fields: %d\n", tp_tuplecount(rep));
 		printf("tuple size: %d\n", tp_tuplesize(rep));
 		printf("[");
@@ -24,7 +23,7 @@ static int reply(void) {
 	tp_init(&rep, NULL, 0, tp_reallocator_noloss, NULL);
 
 	while (1) {
-		ssize_t to_read = tp_required(&rep);
+		ssize_t to_read = tp_req(&rep);
 		printf("to_read: %zu\n", to_read);
 		if (to_read <= 0)
 			break;
-- 
GitLab