Avoids memcpy/free if the buffer was dynamic.

Comments? OK?
From dca09b371c40f8730396a79ae076f10ed7c6874e Mon Sep 17 00:00:00 2001
From: "Federico G. Schwindt" <[email protected]>
Date: Mon, 28 Dec 2015 16:41:31 +0000
Subject: [PATCH] Use realloc if possible

Avoids memcpy/free if the buffer was dynamic.
---
 lib/libvarnish/vsb.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)

diff --git a/lib/libvarnish/vsb.c b/lib/libvarnish/vsb.c
index 7b9380a..4d80b8a 100644
--- a/lib/libvarnish/vsb.c
+++ b/lib/libvarnish/vsb.c
@@ -142,14 +142,16 @@ VSB_extend(struct vsb *s, int addlen)
 	if (!VSB_CANEXTEND(s))
 		return (-1);
 	newsize = VSB_extendsize(s->s_size + addlen);
-	newbuf = SBMALLOC(newsize);
-	if (newbuf == NULL)
-		return (-1);
-	memcpy(newbuf, s->s_buf, s->s_size);
 	if (VSB_ISDYNAMIC(s))
-		SBFREE(s->s_buf);
+		newbuf = realloc(s->s_buf, newsize);
 	else
+		newbuf = SBMALLOC(newsize);
+	if (newbuf == NULL)
+		return (-1);
+	if (!VSB_ISDYNAMIC(s)) {
+		memcpy(newbuf, s->s_buf, s->s_size);
 		VSB_SETFLAG(s, VSB_DYNAMIC);
+	}
 	s->s_buf = newbuf;
 	s->s_size = newsize;
 	return (0);
-- 
2.6.4

_______________________________________________
varnish-dev mailing list
[email protected]
https://www.varnish-cache.org/lists/mailman/listinfo/varnish-dev

Reply via email to