[4.1] b42feb8 Use realloc if possible

Lasse Karstensen lkarsten at varnish-software.com
Thu Jan 14 15:15:10 CET 2016


commit b42feb8029bb93f6125d53acd04c71eda45fa385
Author: Federico G. Schwindt <fgsch at lodoss.net>
Date:   Mon Dec 28 16:41:31 2015 +0000

    Use realloc if possible
    
    Avoids memcpy/free if the buffer was dynamic.

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);



More information about the varnish-commit mailing list