Browse Source

bytes_cpy: avoid malloc and memcpy when size is zero

Ang Iongchun 12 years ago
parent
commit
55af8f09ab
1 changed files with 7 additions and 2 deletions
  1. 7 2
      util.h

+ 7 - 2
util.h

@@ -179,10 +179,15 @@ void bytes_cat(bytes_t *b, const bytes_t *cat)
 static inline
 void bytes_cpy(bytes_t *dst, const bytes_t *src)
 {
-	dst->allocsz = src->allocsz;
 	dst->sz = src->sz;
+	if (!dst->sz) {
+		dst->allocsz = 0;
+		dst->buf = NULL;
+		return;
+	}
+	dst->allocsz = src->allocsz;
 	size_t half;
-	while (dst->allocsz > 0 && dst->sz <= (half = dst->allocsz / 2))
+	while (dst->sz <= (half = dst->allocsz / 2))
 		dst->allocsz = half;
 	dst->buf = malloc(dst->allocsz);
 	memcpy(dst->buf, src->buf, dst->sz);