[master] 5921f91 Use realloc if possible
    Federico G. Schwindt 
    fgsch at lodoss.net
       
    Tue Jan  5 17:06:57 CET 2016
    
    
  
commit 5921f91a7bc115a0d7dced7bcd9838a03255008e
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