[master] 6440798 Introduce WS_ReserveLumps() to allocate an array of something.

Poul-Henning Kamp phk at FreeBSD.org
Wed Feb 15 12:54:04 CET 2017


commit 64407985441e1732cd9ee99f4300b872de71f3a0
Author: Poul-Henning Kamp <phk at FreeBSD.org>
Date:   Wed Feb 15 11:52:43 2017 +0000

    Introduce WS_ReserveLumps() to allocate an array of something.

diff --git a/bin/varnishd/cache/cache.h b/bin/varnishd/cache/cache.h
index 6bd88e4..d6a5544 100644
--- a/bin/varnishd/cache/cache.h
+++ b/bin/varnishd/cache/cache.h
@@ -1058,6 +1058,7 @@ void WRK_BgThread(pthread_t *thr, const char *name, bgthread_t *func,
 
 void WS_Init(struct ws *ws, const char *id, void *space, unsigned len);
 unsigned WS_Reserve(struct ws *ws, unsigned bytes);
+unsigned WS_ReserveLumps(struct ws *ws, size_t sz);
 void WS_MarkOverflow(struct ws *ws);
 void WS_Release(struct ws *ws, unsigned bytes);
 void WS_ReleaseP(struct ws *ws, char *ptr);
diff --git a/bin/varnishd/cache/cache_ws.c b/bin/varnishd/cache/cache_ws.c
index c4d64cb..b2f312e 100644
--- a/bin/varnishd/cache/cache_ws.c
+++ b/bin/varnishd/cache/cache_ws.c
@@ -241,6 +241,16 @@ WS_Reserve(struct ws *ws, unsigned bytes)
 	return (pdiff(ws->f, ws->r));
 }
 
+unsigned
+WS_ReserveLumps(struct ws *ws, size_t sz)
+{
+	unsigned u;
+
+	u = WS_Reserve(ws, 0);
+	u /= sz;
+	return (u);
+}
+
 void
 WS_Release(struct ws *ws, unsigned bytes)
 {
diff --git a/bin/varnishd/http1/cache_http1_line.c b/bin/varnishd/http1/cache_http1_line.c
index 756fe81..a1b7537 100644
--- a/bin/varnishd/http1/cache_http1_line.c
+++ b/bin/varnishd/http1/cache_http1_line.c
@@ -88,16 +88,15 @@ V1L_Reserve(struct worker *wrk, struct ws *ws, int *fd, struct vsl_log *vsl,
 	v1l->ws = ws;
 	v1l->res = res;
 
-	u = WS_Reserve(ws, 0);
-	u = PRNDDN(u);
-	u /= sizeof(struct iovec);
+	u = WS_ReserveLumps(ws, sizeof(struct iovec));
 	if (u == 0) {
 		WS_Release(ws, 0);
 		WS_MarkOverflow(ws);
 		return;
-	} else if (u > IOV_MAX)
+	}
+	if (u > IOV_MAX)
 		u = IOV_MAX;
-	v1l->iov = (void*)PRNDUP(ws->f);
+	v1l->iov = (void*)ws->f;
 	v1l->siov = u;
 	v1l->ciov = u;
 	v1l->werr = 0;
diff --git a/lib/libvmod_std/vmod_std_querysort.c b/lib/libvmod_std/vmod_std_querysort.c
index 024b42d..2fae3a1 100644
--- a/lib/libvmod_std/vmod_std_querysort.c
+++ b/lib/libvmod_std/vmod_std_querysort.c
@@ -56,6 +56,7 @@ vmod_querysort(VRT_CTX, VCL_STRING url)
 	char *p, *r;
 	const char **pp;
 	const char **pe;
+	unsigned u;
 	int np;
 	int i;
 
@@ -78,16 +79,14 @@ vmod_querysort(VRT_CTX, VCL_STRING url)
 	if (r == NULL)
 		return (url);
 
-	(void)WS_Reserve(ctx->ws, 0);
-	/* We trust cache_ws.c to align sensibly */
+	u = WS_ReserveLumps(ctx->ws, sizeof(const char **));
 	pp = (const char**)(void*)(ctx->ws->f);
-	pe = (const char**)(void*)(ctx->ws->e);
-
-	if (pp + 4 > pe) {
+	if (u < 4) {
 		WS_Release(ctx->ws, 0);
 		WS_MarkOverflow(ctx->ws);
 		return (url);
 	}
+	pe = pp + u;
 
 	/* Collect params as pointer pairs */
 	np = 0;



More information about the varnish-commit mailing list