[6.2] 68de849b1 Deprecate WS_Reserve() and replace it with WS_ReserveSize()

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


commit 68de849b1e874403e9d1eb0145c10c6b5d35f541
Author: Nils Goroll <nils.goroll at uplex.de>
Date:   Mon Apr 8 16:26:07 2019 +0200

    Deprecate WS_Reserve() and replace it with WS_ReserveSize()
    
    WS_ReserveSize() does not leave the workspace reserved when the
    reservation fails, so WS_Release() must be called for retval > 0 only.
    
    Besides the debug string, it is identical to WS_Reserve() except for
    
            assert(bytes > 0);
    
    Follow-up varnishcache/varnish-cache#2967
    
    Note: The WS_Reserve() function has not been deprecated as that can
    potentially create problems for the build process of VMODs.
    
    (cherry picked from commit 4e33359772a3d751b2ef4b5c4b40259f1bcd6903)

diff --git a/bin/varnishd/cache/cache.h b/bin/varnishd/cache/cache.h
index cc2fc993d..c67dca563 100644
--- a/bin/varnishd/cache/cache.h
+++ b/bin/varnishd/cache/cache.h
@@ -764,7 +764,10 @@ void WRK_BgThread(pthread_t *thr, const char *name, bgthread_t *func,
 /* cache_ws.c */
 
 void WS_Init(struct ws *ws, const char *id, void *space, unsigned len);
+
+/* WS_Reserve(): Use WS_ReserveSize() or WS_ReserveAll() */
 unsigned WS_Reserve(struct ws *ws, unsigned bytes);
+unsigned WS_ReserveSize(struct ws *, unsigned);
 unsigned WS_ReserveAll(struct ws *);
 unsigned WS_ReserveLumps(struct ws *ws, size_t sz);
 void WS_MarkOverflow(struct ws *ws);
diff --git a/bin/varnishd/cache/cache_session.c b/bin/varnishd/cache/cache_session.c
index eac4773fc..8c9e2d7c9 100644
--- a/bin/varnishd/cache/cache_session.c
+++ b/bin/varnishd/cache/cache_session.c
@@ -112,7 +112,7 @@ ses_reserve_attr(struct sess *sp, enum sess_attr a, void **dst, int sz)
 	assert(a < SA_LAST);
 	assert(sz >= 0);
 	AN(dst);
-	o = WS_Reserve(sp->ws, sz);
+	o = WS_ReserveSize(sp->ws, sz);
 	assert(o >= sz);
 	*dst = sp->ws->f;
 	o = sp->ws->f - sp->ws->s;
@@ -415,7 +415,7 @@ ses_handle(struct waited *wp, enum wait_event ev, vtim_real now)
 	case WAITER_ACTION:
 		pp = sp->pool;
 		CHECK_OBJ_NOTNULL(pp, POOL_MAGIC);
-		assert(sizeof *tp <= WS_Reserve(sp->ws, sizeof *tp));
+		assert(sizeof *tp <= WS_ReserveSize(sp->ws, sizeof *tp));
 		tp = (void*)sp->ws->f;
 		tp->func = xp->unwait;
 		tp->priv = sp;
@@ -453,7 +453,7 @@ SES_Wait(struct sess *sp, const struct transport *xp)
 	/*
 	 * put struct waited on the workspace
 	 */
-	if (WS_Reserve(sp->ws, sizeof(struct waited))
+	if (WS_ReserveSize(sp->ws, sizeof(struct waited))
 	    < sizeof(struct waited)) {
 		SES_Delete(sp, SC_OVERLOAD, NAN);
 		return;
diff --git a/bin/varnishd/cache/cache_wrk.c b/bin/varnishd/cache/cache_wrk.c
index a0fc4eacb..c6a37b862 100644
--- a/bin/varnishd/cache/cache_wrk.c
+++ b/bin/varnishd/cache/cache_wrk.c
@@ -238,7 +238,7 @@ Pool_Task_Arg(struct worker *wrk, enum task_prio prio, task_func_t *func,
 		retval = 0;
 	}
 	AZ(wrk2->task.func);
-	assert(arg_len <= WS_Reserve(wrk2->aws, arg_len));
+	assert(arg_len <= WS_ReserveSize(wrk2->aws, arg_len));
 	memcpy(wrk2->aws->f, arg, arg_len);
 	wrk2->task.func = func;
 	wrk2->task.priv = wrk2->aws->f;
diff --git a/bin/varnishd/cache/cache_ws.c b/bin/varnishd/cache/cache_ws.c
index e0a9b1d48..7b8df6d23 100644
--- a/bin/varnishd/cache/cache_ws.c
+++ b/bin/varnishd/cache/cache_ws.c
@@ -247,9 +247,34 @@ WS_ReserveAll(struct ws *ws)
 }
 
 /*
- * bytes == 0 argument is deprecated - use WS_ReserveAll
- *
- * XXX rename to WS_ReserveSize and macro-wrap WS_Reserve to emit #warn ?
+ * WS_Release() must be called for retval > 0 only
+ */
+unsigned
+WS_ReserveSize(struct ws *ws, unsigned bytes)
+{
+	unsigned b2;
+
+	WS_Assert(ws);
+	assert(ws->r == NULL);
+	assert(bytes > 0);
+
+	b2 = PRNDDN(ws->e - ws->f);
+	if (bytes < b2)
+		b2 = PRNDUP(bytes);
+
+	if (ws->f + b2 > ws->e) {
+		WS_MarkOverflow(ws);
+		return (0);
+	}
+	ws->r = ws->f + b2;
+	DSL(DBG_WORKSPACE, 0, "WS_ReserveSize(%p, %u/%u) = %u",
+	    ws, b2, bytes, pdiff(ws->f, ws->r));
+	WS_Assert(ws);
+	return (pdiff(ws->f, ws->r));
+}
+
+/*
+ * XXX remove for 2020-03-15 release
  */
 unsigned
 WS_Reserve(struct ws *ws, unsigned bytes)
diff --git a/lib/libvmod_proxy/vmod_proxy.c b/lib/libvmod_proxy/vmod_proxy.c
index fa7b9617e..58f2db295 100644
--- a/lib/libvmod_proxy/vmod_proxy.c
+++ b/lib/libvmod_proxy/vmod_proxy.c
@@ -105,7 +105,7 @@ tlv_string(VRT_CTX, int tlv)
 
 	if (VPX_tlv(ctx->req, tlv, (void **)&dst, &len))
 		return (NULL);
-	if (!WS_Reserve(ctx->ws, len+1))
+	if (!WS_ReserveSize(ctx->ws, len+1))
 		return (NULL);
 	d = ctx->ws->f;
 	memcpy(d, dst, len);


More information about the varnish-commit mailing list