[6.0] 2081eeb6e Add WS_ReserveAll() to replace WS_Reserve(ws, 0)

Martin Blix Grydeland martin at varnish-software.com
Tue Feb 4 10:02:07 UTC 2020


commit 2081eeb6e4ae64f95e2c6b9abdac7ca8a99bc5d2
Author: Nils Goroll <nils.goroll at uplex.de>
Date:   Mon Apr 8 15:19:30 2019 +0200

    Add WS_ReserveAll() to replace WS_Reserve(ws, 0)
    
    ... to un-confuse the interface
    
    Notes on changes from WS_Reserve():
    
    * Removed the first WS_Assert because all we change is ws->r and we got
      a specific assert on it.
    
    * it follows from PAOK(ws->e) && PAOK(ws->f) in WS_Assert() that
      PAOK(ws->r) && PAOK(b), so we remove the PRNDDN()
    
    Ref: varnishcache/varnish-cache#2967
    
    (cherry picked from commit d001cdd2045aa484a968d09859cddf59dee372cb)

diff --git a/bin/varnishd/cache/cache.h b/bin/varnishd/cache/cache.h
index 6dbefa84e..4455277b4 100644
--- a/bin/varnishd/cache/cache.h
+++ b/bin/varnishd/cache/cache.h
@@ -762,6 +762,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_ReserveAll(struct ws *);
 unsigned WS_ReserveLumps(struct ws *ws, size_t sz);
 void WS_MarkOverflow(struct ws *ws);
 void WS_Release(struct ws *ws, unsigned bytes);
diff --git a/bin/varnishd/cache/cache_hash.c b/bin/varnishd/cache/cache_hash.c
index 0bd31f2ca..9f1558aa0 100644
--- a/bin/varnishd/cache/cache_hash.c
+++ b/bin/varnishd/cache/cache_hash.c
@@ -626,7 +626,7 @@ double grace, double keep)
 
 	CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
 	CHECK_OBJ_NOTNULL(oh, OBJHEAD_MAGIC);
-	ospc = WS_Reserve(wrk->aws, 0);
+	ospc = WS_ReserveAll(wrk->aws);
 	assert(ospc >= sizeof *ocp);
 	/*
 	 * Because of "soft" purges, there might be oc's in the list that has
diff --git a/bin/varnishd/cache/cache_http.c b/bin/varnishd/cache/cache_http.c
index 070ead2e6..78d96293b 100644
--- a/bin/varnishd/cache/cache_http.c
+++ b/bin/varnishd/cache/cache_http.c
@@ -374,7 +374,7 @@ http_CollectHdrSep(struct http *hp, const char *hdr, const char *sep)
 		}
 		if (b == NULL) {
 			/* Found second header, start our collection */
-			ml = WS_Reserve(hp->ws, 0);
+			ml = WS_ReserveAll(hp->ws);
 			b = hp->ws->f;
 			e = b + ml;
 			x = Tlen(hp->hd[f]);
@@ -1208,7 +1208,7 @@ http_PrintfHeader(struct http *to, const char *fmt, ...)
 	unsigned l, n;
 
 	CHECK_OBJ_NOTNULL(to, HTTP_MAGIC);
-	l = WS_Reserve(to->ws, 0);
+	l = WS_ReserveAll(to->ws);
 	va_start(ap, fmt);
 	n = vsnprintf(to->ws->f, l, fmt, ap);
 	va_end(ap);
diff --git a/bin/varnishd/cache/cache_session.c b/bin/varnishd/cache/cache_session.c
index a8a75b7e7..e18accc57 100644
--- a/bin/varnishd/cache/cache_session.c
+++ b/bin/varnishd/cache/cache_session.c
@@ -204,7 +204,7 @@ HTC_RxInit(struct http_conn *htc, struct ws *ws)
 
 	CHECK_OBJ_NOTNULL(htc, HTTP_CONN_MAGIC);
 	htc->ws = ws;
-	(void)WS_Reserve(htc->ws, 0);
+	(void)WS_ReserveAll(htc->ws);
 	htc->rxbuf_b = ws->f;
 	htc->rxbuf_e = ws->f;
 	if (htc->pipeline_b != NULL) {
diff --git a/bin/varnishd/cache/cache_vary.c b/bin/varnishd/cache/cache_vary.c
index 46d6930fb..4af92f69c 100644
--- a/bin/varnishd/cache/cache_vary.c
+++ b/bin/varnishd/cache/cache_vary.c
@@ -230,7 +230,7 @@ VRY_Prep(struct req *req)
 		AZ(req->vary_b);
 		AZ(req->vary_l);
 		AZ(req->vary_e);
-		(void)WS_Reserve(req->ws, 0);
+		(void)WS_ReserveAll(req->ws);
 	} else {
 		AN(req->ws->r);
 	}
diff --git a/bin/varnishd/cache/cache_vrt.c b/bin/varnishd/cache/cache_vrt.c
index 4b49e1d3a..343481346 100644
--- a/bin/varnishd/cache/cache_vrt.c
+++ b/bin/varnishd/cache/cache_vrt.c
@@ -271,7 +271,7 @@ VRT_String(struct ws *ws, const char *h, const char *p, va_list ap)
 	unsigned u, x;
 	va_list aq;
 
-	u = WS_Reserve(ws, 0);
+	u = WS_ReserveAll(ws);
 	e = b = ws->f;
 	e += u;
 
@@ -395,7 +395,7 @@ VRT_StrandsWS(struct ws *ws, const char *h, VCL_STRANDS s)
 	int i;
 
 	AN(s);
-	u = WS_Reserve(ws, 0);
+	u = WS_ReserveAll(ws);
 
 	for (i = 0; i < s->n; i++)
 		if (s->p[i] != NULL && *s->p[i] != '\0') {
@@ -578,7 +578,7 @@ VRT_IP_string(VRT_CTX, VCL_IP ip)
 	CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
 	if (ip == NULL)
 		return (NULL);
-	len = WS_Reserve(ctx->ws, 0);
+	len = WS_ReserveAll(ctx->ws);
 	if (len == 0) {
 		WS_Release(ctx->ws, 0);
 		return (NULL);
diff --git a/bin/varnishd/cache/cache_vrt_re.c b/bin/varnishd/cache/cache_vrt_re.c
index 20a0cf6fc..449f07126 100644
--- a/bin/varnishd/cache/cache_vrt_re.c
+++ b/bin/varnishd/cache/cache_vrt_re.c
@@ -127,7 +127,7 @@ VRT_regsub(VRT_CTX, int all, const char *str, void *re,
 		return(str);
 	}
 
-	u = WS_Reserve(ctx->ws, 0);
+	u = WS_ReserveAll(ctx->ws);
 	res_e = res_b = b0 = ctx->ws->f;
 	res_e += u;
 
diff --git a/bin/varnishd/cache/cache_ws.c b/bin/varnishd/cache/cache_ws.c
index 3ec5ec017..e0a9b1d48 100644
--- a/bin/varnishd/cache/cache_ws.c
+++ b/bin/varnishd/cache/cache_ws.c
@@ -202,7 +202,7 @@ WS_Printf(struct ws *ws, const char *fmt, ...)
 	va_list ap;
 	char *p;
 
-	u = WS_Reserve(ws, 0);
+	u = WS_ReserveAll(ws);
 	p = ws->f;
 	va_start(ap, fmt);
 	v = vsnprintf(p, u, fmt, ap);
@@ -227,6 +227,30 @@ WS_Snapshot(struct ws *ws)
 	return (ws->f == ws->s ? 0 : (uintptr_t)ws->f);
 }
 
+/*
+ * WS_Release() must be called in all cases
+ */
+unsigned
+WS_ReserveAll(struct ws *ws)
+{
+	unsigned b;
+
+	assert(ws->r == NULL);
+
+	ws->r = ws->e;
+	b = pdiff(ws->f, ws->r);
+
+	WS_Assert(ws);
+	DSL(DBG_WORKSPACE, 0, "WS_ReserveAll(%p) = %u", ws, b);
+
+	return (b);
+}
+
+/*
+ * bytes == 0 argument is deprecated - use WS_ReserveAll
+ *
+ * XXX rename to WS_ReserveSize and macro-wrap WS_Reserve to emit #warn ?
+ */
 unsigned
 WS_Reserve(struct ws *ws, unsigned bytes)
 {
@@ -253,11 +277,7 @@ WS_Reserve(struct ws *ws, unsigned bytes)
 unsigned
 WS_ReserveLumps(struct ws *ws, size_t sz)
 {
-	unsigned u;
-
-	u = WS_Reserve(ws, 0);
-	u /= sz;
-	return (u);
+	return (WS_ReserveAll(ws) / sz);
 }
 
 void
diff --git a/bin/varnishd/http2/cache_http2_deliver.c b/bin/varnishd/http2/cache_http2_deliver.c
index 46f7654d9..94018edd7 100644
--- a/bin/varnishd/http2/cache_http2_deliver.c
+++ b/bin/varnishd/http2/cache_http2_deliver.c
@@ -229,7 +229,7 @@ h2_build_headers(struct vsb *resp, struct req *req)
 	uint8_t buf[6];
 	ssize_t sz, sz1;
 
-	l = WS_Reserve(req->ws, 0);
+	l = WS_ReserveAll(req->ws);
 	AN(VSB_new(resp, req->ws->f, l, VSB_FIXEDLEN));
 	if (l < 10) {
 		WS_Release(req->ws, 0);
diff --git a/bin/varnishd/http2/cache_http2_hpack.c b/bin/varnishd/http2/cache_http2_hpack.c
index f2ef11cf7..d43262916 100644
--- a/bin/varnishd/http2/cache_http2_hpack.c
+++ b/bin/varnishd/http2/cache_http2_hpack.c
@@ -175,7 +175,7 @@ h2h_decode_init(const struct h2_sess *h2)
 	d = h2->decode;
 	INIT_OBJ(d, H2H_DECODE_MAGIC);
 	VHD_Init(d->vhd);
-	d->out_l = WS_Reserve(h2->new_req->http->ws, 0);
+	d->out_l = WS_ReserveAll(h2->new_req->http->ws);
 	/*
 	 * Can't do any work without any buffer
 	 * space. Require non-zero size.
diff --git a/lib/libvmod_blob/vmod_blob.c b/lib/libvmod_blob/vmod_blob.c
index dfb03cafa..0deb37f08 100644
--- a/lib/libvmod_blob/vmod_blob.c
+++ b/lib/libvmod_blob/vmod_blob.c
@@ -348,7 +348,7 @@ vmod_decode(VRT_CTX, VCL_ENUM decs, VCL_INT length, VCL_STRANDS strings)
 	}
 
 	buf = WS_Front(ctx->ws);
-	space = WS_Reserve(ctx->ws, 0);
+	space = WS_ReserveAll(ctx->ws);
 
 	if (length <= 0)
 		length = -1;
@@ -389,7 +389,7 @@ encode(VRT_CTX, enum encoding enc, enum case_e kase, VCL_BLOB b)
 	CHECK_OBJ_NOTNULL(ctx->ws, WS_MAGIC);
 	snap = WS_Snapshot(ctx->ws);
 	buf = WS_Front(ctx->ws);
-	space = WS_Reserve(ctx->ws, 0);
+	space = WS_ReserveAll(ctx->ws);
 
 	len = func[enc].encode(enc, kase, buf, space, b->priv, b->len);
 
diff --git a/lib/libvmod_std/vmod_std.c b/lib/libvmod_std/vmod_std.c
index ef801f11b..f36f4bc1c 100644
--- a/lib/libvmod_std/vmod_std.c
+++ b/lib/libvmod_std/vmod_std.c
@@ -74,7 +74,7 @@ vmod_updown(VRT_CTX, int up, const char *s, va_list ap)
 	const char *p;
 
 	CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
-	u = WS_Reserve(ctx->ws, 0);
+	u = WS_ReserveAll(ctx->ws);
 	e = b = ctx->ws->f;
 	e += u;
 	p = s;
diff --git a/lib/libvmod_vtc/vmod_vtc.c b/lib/libvmod_vtc/vmod_vtc.c
index 197fe81f0..18374e8a5 100644
--- a/lib/libvmod_vtc/vmod_vtc.c
+++ b/lib/libvmod_vtc/vmod_vtc.c
@@ -156,7 +156,7 @@ vmod_workspace_alloc(VRT_CTX, VCL_ENUM which, VCL_INT size)
 	WS_Assert(ws);
 
 	if (size < 0) {
-		size += WS_Reserve(ws, 0);
+		size += WS_ReserveAll(ws);
 		WS_Release(ws, 0);
 	}
 	if (size <= 0) {
@@ -183,7 +183,7 @@ vmod_workspace_free(VRT_CTX, VCL_ENUM which)
 		return(-1);
 	WS_Assert(ws);
 
-	u = WS_Reserve(ws, 0);
+	u = WS_ReserveAll(ws);
 	WS_Release(ws, 0);
 	return (u);
 }


More information about the varnish-commit mailing list