[6.0] 534e045e0 Deprecate WS_Reserve() and replace it with WS_ReserveSize()

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


commit 534e045e040fc213f72aa38a85806f152e3ef5e0
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 4455277b4..9e47c7215 100644
--- a/bin/varnishd/cache/cache.h
+++ b/bin/varnishd/cache/cache.h
@@ -761,7 +761,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 e18accc57..8c9325515 100644
--- a/bin/varnishd/cache/cache_session.c
+++ b/bin/varnishd/cache/cache_session.c
@@ -113,7 +113,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;
@@ -417,7 +417,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;
@@ -455,7 +455,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 14bbfd4fd..fa3fa366a 100644
--- a/bin/varnishd/cache/cache_wrk.c
+++ b/bin/varnishd/cache/cache_wrk.c
@@ -239,7 +239,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