[master] 519ead4a3 take in use vmin[_t] for existing code

Nils Goroll nils.goroll at uplex.de
Mon Nov 8 16:14:06 UTC 2021


commit 519ead4a3a019e28c1e5b7c8eab87261760dd07c
Author: Asad Sajjad Ahmed <asadsa at varnish-software.com>
Date:   Tue Sep 28 14:22:07 2021 +0200

    take in use vmin[_t] for existing code
    
    Signed-off-by: Asad Sajjad Ahmed <asadsa at varnish-software.com>

diff --git a/bin/varnishd/cache/cache_backend_probe.c b/bin/varnishd/cache/cache_backend_probe.c
index 8d50d64b2..97e652702 100644
--- a/bin/varnishd/cache/cache_backend_probe.c
+++ b/bin/varnishd/cache/cache_backend_probe.c
@@ -633,8 +633,7 @@ vbp_set_defaults(struct vbp_target *vt, const struct vrt_backend_probe *vp)
 	if (vt->initial == ~0U)
 		vt->initial = vt->threshold - 1;
 
-	if (vt->initial > vt->threshold)
-		vt->initial = vt->threshold;
+	vt->initial = vmin(vt->initial, vt->threshold);
 }
 
 /*--------------------------------------------------------------------
diff --git a/bin/varnishd/cache/cache_esi_deliver.c b/bin/varnishd/cache/cache_esi_deliver.c
index c6fc3b0b6..07af11e37 100644
--- a/bin/varnishd/cache/cache_esi_deliver.c
+++ b/bin/varnishd/cache/cache_esi_deliver.c
@@ -663,8 +663,7 @@ ved_gzgz_bytes(struct vdp_ctx *vdx, enum vdp_action act, void **priv,
 		/* The main body of the object */
 		dl = foo->last / 8 - foo->ll;
 		if (dl > 0) {
-			if (dl > len)
-				dl = len;
+			dl = vmin(dl, len);
 			if (ved_bytes(foo->ecx, act, pp, dl))
 				return (-1);
 			foo->ll += dl;
@@ -686,8 +685,7 @@ ved_gzgz_bytes(struct vdp_ctx *vdx, enum vdp_action act, void **priv,
 		/* Last block */
 		dl = foo->stop / 8 - foo->ll;
 		if (dl > 0) {
-			if (dl > len)
-				dl = len;
+			dl = vmin(dl, len);
 			if (ved_bytes(foo->ecx, act, pp, dl))
 				return (-1);
 			foo->ll += dl;
diff --git a/bin/varnishd/cache/cache_esi_fetch.c b/bin/varnishd/cache/cache_esi_fetch.c
index 0624fefe1..cee4eb57b 100644
--- a/bin/varnishd/cache/cache_esi_fetch.c
+++ b/bin/varnishd/cache/cache_esi_fetch.c
@@ -271,7 +271,6 @@ static enum vfp_status v_matchproto_(vfp_pull_f)
 vfp_esi_pull(struct vfp_ctx *vc, struct vfp_entry *vfe, void *p, ssize_t *lp)
 {
 	enum vfp_status vp;
-	ssize_t d;
 	struct vef_priv *vef;
 
 	CHECK_OBJ_NOTNULL(vc, VFP_CTX_MAGIC);
@@ -280,9 +279,7 @@ vfp_esi_pull(struct vfp_ctx *vc, struct vfp_entry *vfe, void *p, ssize_t *lp)
 	AN(p);
 	AN(lp);
 	if (DO_DEBUG(DBG_ESI_CHOP)) {
-		d = (VRND_RandomTestable() & 3) + 1;
-		if (d < *lp)
-			*lp = d;
+		*lp = vmin(*lp, (VRND_RandomTestable() & 3) + 1);
 	}
 	vp = VFP_Suck(vc, p, lp);
 	if (vp != VFP_ERROR && *lp > 0)
diff --git a/bin/varnishd/cache/cache_fetch.c b/bin/varnishd/cache/cache_fetch.c
index ea5553b3f..76a3a2422 100644
--- a/bin/varnishd/cache/cache_fetch.c
+++ b/bin/varnishd/cache/cache_fetch.c
@@ -110,8 +110,7 @@ vbf_allocobj(struct busyobj *bo, unsigned l)
 	 * on Transient storage.
 	 */
 
-	if (oc->ttl > cache_param->shortlived)
-		oc->ttl = cache_param->shortlived;
+	oc->ttl = vmin_t(float, oc->ttl, cache_param->shortlived);
 	oc->grace = 0.0;
 	oc->keep = 0.0;
 	return (STV_NewObject(bo->wrk, oc, stv_transient, l));
@@ -762,8 +761,7 @@ vbf_objiterator(void *priv, unsigned flush, const void *ptr, ssize_t len)
 		l = len;
 		if (VFP_GetStorage(bo->vfc, &l, &pd) != VFP_OK)
 			return (1);
-		if (len < l)
-			l = len;
+		l = vmin(l, len);
 		memcpy(pd, ps, l);
 		VFP_Extend(bo->vfc, l, l == len ? VFP_END : VFP_OK);
 		ps += l;
@@ -966,8 +964,7 @@ vbf_stp_error(struct worker *wrk, struct busyobj *bo)
 		l = ll;
 		if (VFP_GetStorage(bo->vfc, &l, &ptr) != VFP_OK)
 			break;
-		if (l > ll)
-			l = ll;
+		l = vmin(l, ll);
 		memcpy(ptr, VSB_data(synth_body) + o, l);
 		VFP_Extend(bo->vfc, l, l == ll ? VFP_END : VFP_OK);
 		ll -= l;
diff --git a/bin/varnishd/cache/cache_range.c b/bin/varnishd/cache/cache_range.c
index 402884e5f..e4ccbc7c6 100644
--- a/bin/varnishd/cache/cache_range.c
+++ b/bin/varnishd/cache/cache_range.c
@@ -85,9 +85,7 @@ vrg_range_bytes(struct vdp_ctx *vdx, enum vdp_action act, void **priv,
 			p += l;
 			len -= l;
 		}
-		l = vrg_priv->range_high - vrg_priv->range_off;
-		if (l > len)
-			l = len;
+		l = vmin(vrg_priv->range_high - vrg_priv->range_off, len);
 		vrg_priv->range_off += len;
 		if (vrg_priv->range_off >= vrg_priv->range_high)
 			act = VDP_END;
diff --git a/bin/varnishd/cache/cache_shmlog.c b/bin/varnishd/cache/cache_shmlog.c
index fbda72f0b..02ddbf922 100644
--- a/bin/varnishd/cache/cache_shmlog.c
+++ b/bin/varnishd/cache/cache_shmlog.c
@@ -80,9 +80,7 @@ strands_cat(char *buf, unsigned bufl, const struct strands *s)
 	for (i = 0; i < s->n && bufl > 0; i++) {
 		if (s->p[i] == NULL || *s->p[i] == '\0')
 			continue;
-		ll = strlen(s->p[i]);
-		if (ll > bufl)
-			ll = bufl;
+		ll = vmin_t(unsigned, strlen(s->p[i]), bufl);
 		memcpy(buf, s->p[i], ll);
 		l += ll;
 		buf += ll;
@@ -271,9 +269,8 @@ VSLv(enum VSL_tag_e tag, uint32_t vxid, const char *fmt, va_list ap)
 	if (strchr(fmt, '%') == NULL) {
 		vslr(tag, vxid, fmt, strlen(fmt) + 1);
 	} else {
-		n = vsnprintf(buf, mlen, fmt, ap);
-		if (n > mlen - 1)
-			n = mlen - 1;
+		/* XXX: should probably check return value of vsnprintf */
+		n = vmin_t(unsigned, vsnprintf(buf, mlen, fmt, ap), mlen - 1);
 		buf[n++] = '\0'; /* NUL-terminated */
 		vslr(tag, vxid, buf, n);
 	}
@@ -381,9 +378,7 @@ VSLbs(struct vsl_log *vsl, enum VSL_tag_e tag, const struct strands *s)
 	mlen = cache_param->vsl_reclen;
 
 	/* including NUL */
-	l = strands_len(s) + 1;
-	if (l > mlen)
-		l = mlen;
+	l = vmin(strands_len(s) + 1, mlen);
 
 	assert(vsl->wlp < vsl->wle);
 
@@ -508,8 +503,7 @@ VSLb_bin(struct vsl_log *vsl, enum VSL_tag_e tag, ssize_t len, const void *ptr)
 	mlen = cache_param->vsl_reclen;
 
 	/* Truncate */
-	if (len > mlen)
-		len = mlen;
+	len = vmin_t(ssize_t, len, mlen);
 
 	assert(vsl->wlp < vsl->wle);
 
diff --git a/bin/varnishd/cache/cache_ws_emu.c b/bin/varnishd/cache/cache_ws_emu.c
index 43ee87f85..f3d74150e 100644
--- a/bin/varnishd/cache/cache_ws_emu.c
+++ b/bin/varnishd/cache/cache_ws_emu.c
@@ -540,9 +540,7 @@ WS_Dump(const struct ws *ws, char where, size_t off, void *buf, size_t len)
 				break;
 			if (wa->ptr == NULL)
 				break;
-			l = wa->len;
-			if (l > len)
-				l = len;
+			l = vmin_t(size_t, wa->len, len);
 			memcpy(b, wa->ptr, l);
 			b += l;
 			len -= l;
diff --git a/bin/varnishd/hpack/vhp_decode.c b/bin/varnishd/hpack/vhp_decode.c
index b7c712212..01965d833 100644
--- a/bin/varnishd/hpack/vhp_decode.c
+++ b/bin/varnishd/hpack/vhp_decode.c
@@ -270,9 +270,7 @@ vhd_lookup(struct vhd_ctx *ctx, unsigned first)
 
 	assert(lu->l <= l);
 	p += l - lu->l;
-	l = lu->l;
-	if (l > ctx->out_e - ctx->out)
-		l = ctx->out_e - ctx->out;
+	l = vmin_t(size_t, lu->l, ctx->out_e - ctx->out);
 	memcpy(ctx->out, p, l);
 	ctx->out += l;
 	lu->l -= l;
diff --git a/bin/varnishd/hpack/vhp_gen_hufdec.c b/bin/varnishd/hpack/vhp_gen_hufdec.c
index ae2b812e4..cf18857cc 100644
--- a/bin/varnishd/hpack/vhp_gen_hufdec.c
+++ b/bin/varnishd/hpack/vhp_gen_hufdec.c
@@ -236,8 +236,7 @@ main(int argc, const char **argv)
 	for (u = 0; u < HUF_LEN; u++) {
 		if (maxlen < huf[u].blen)
 			maxlen = huf[u].blen;
-		if (minlen > huf[u].blen)
-			minlen = huf[u].blen;
+		minlen = vmin(minlen, huf[u].blen);
 	}
 
 	top = tbl_new(8);
diff --git a/bin/varnishd/http1/cache_http1_vfp.c b/bin/varnishd/http1/cache_http1_vfp.c
index 835db3599..06869a59a 100644
--- a/bin/varnishd/http1/cache_http1_vfp.c
+++ b/bin/varnishd/http1/cache_http1_vfp.c
@@ -65,8 +65,7 @@ v1f_read(const struct vfp_ctx *vc, struct http_conn *htc, void *d, ssize_t len)
 	if (htc->pipeline_b) {
 		l = htc->pipeline_e - htc->pipeline_b;
 		assert(l > 0);
-		if (l > len)
-			l = len;
+		l = vmin(l, len);
 		memcpy(p, htc->pipeline_b, l);
 		p += l;
 		len -= l;
@@ -210,8 +209,7 @@ v1f_pull_straight(struct vfp_ctx *vc, struct vfp_entry *vfe, void *p,
 
 	if (vfe->priv2 == 0) // XXX: Optimize Content-Len: 0 out earlier
 		return (VFP_END);
-	if (vfe->priv2 < l)
-		l = vfe->priv2;
+	l = vmin(l, vfe->priv2);
 	lr = v1f_read(vc, htc, p, l);
 	if (lr <= 0)
 		return (VFP_Error(vc, "straight insufficient bytes"));
diff --git a/bin/varnishd/http2/cache_http2_send.c b/bin/varnishd/http2/cache_http2_send.c
index 84c6e2c93..172bb35b2 100644
--- a/bin/varnishd/http2/cache_http2_send.c
+++ b/bin/varnishd/http2/cache_http2_send.c
@@ -212,17 +212,13 @@ H2_Send_Frame(struct worker *wrk, struct h2_sess *h2,
 static int64_t
 h2_win_limit(const struct h2_req *r2, const struct h2_sess *h2)
 {
-	int64_t m;
 
 	CHECK_OBJ_NOTNULL(r2, H2_REQ_MAGIC);
 	CHECK_OBJ_NOTNULL(h2, H2_SESS_MAGIC);
 	CHECK_OBJ_NOTNULL(h2->req0, H2_REQ_MAGIC);
 
 	Lck_AssertHeld(&h2->sess->mtx);
-	m = r2->t_window;
-	if (m > h2->req0->t_window)
-		m = h2->req0->t_window;
-	return (m);
+	return (vmin(r2->t_window, h2->req0->t_window));
 }
 
 static void
@@ -276,9 +272,7 @@ h2_do_window(struct worker *wrk, struct h2_req *r2,
 			(void)h2_cond_wait(h2->winupd_cond, h2, r2);
 
 		if (h2_errcheck(r2, h2) == 0) {
-			w = h2_win_limit(r2, h2);
-			if (w > wanted)
-				w = wanted;
+			w = vmin(h2_win_limit(r2, h2), wanted);
 			h2_win_charge(r2, h2, w);
 			assert (w > 0);
 		}
diff --git a/bin/varnishstat/varnishstat_curses.c b/bin/varnishstat/varnishstat_curses.c
index 6863d6278..0f9aa3652 100644
--- a/bin/varnishstat/varnishstat_curses.c
+++ b/bin/varnishstat/varnishstat_curses.c
@@ -177,8 +177,7 @@ update_position(void)
 			current = 0;
 		if (current > n_ptarray - 1)
 			current = n_ptarray - 1;
-		if (current < page_start)
-			page_start = current;
+		page_start = vmin(page_start, current);
 		if (current > page_start + (l_points - 1))
 			page_start = current - (l_points - 1);
 		if (page_start < 0)
diff --git a/bin/varnishtest/vtc_http.c b/bin/varnishtest/vtc_http.c
index 5eb5d6306..1cc4d8f24 100644
--- a/bin/varnishtest/vtc_http.c
+++ b/bin/varnishtest/vtc_http.c
@@ -1426,9 +1426,7 @@ cmd_http_chunkedlen(CMD_ARGS)
 
 		VSB_printf(hp->vsb, "%x%s", len, nl);
 		for (u = 0; u < len; u += v) {
-			v = len - u;
-			if (v > sizeof buf)
-				v = sizeof buf;
+			v = vmin_t(unsigned, len - u, sizeof buf);
 			VSB_bcat(hp->vsb, buf, v);
 		}
 		VSB_printf(hp->vsb, "%s", nl);
diff --git a/bin/varnishtop/varnishtop.c b/bin/varnishtop/varnishtop.c
index d8e2b6183..b8d600877 100644
--- a/bin/varnishtop/varnishtop.c
+++ b/bin/varnishtop/varnishtop.c
@@ -222,9 +222,7 @@ update(unsigned p)
 		tp2 = VRBT_NEXT(t_order, &h_order, tp);
 
 		if (++l < LINES) {
-			len = tp->clen;
-			if (len > COLS - 20)
-				len = COLS - 20;
+			len = vmin(tp->clen, COLS - 20);
 			AC(mvprintw(l, 0, "%9.2f %-*.*s %*.*s\n",
 				tp->count, maxfieldlen, maxfieldlen,
 				VSL_tags[tp->tag],
diff --git a/lib/libvarnish/vre.c b/lib/libvarnish/vre.c
index d994f13fe..096a4ba79 100644
--- a/lib/libvarnish/vre.c
+++ b/lib/libvarnish/vre.c
@@ -218,9 +218,7 @@ vre_capture(const vre_t *code, const char *subject, size_t length,
 		AN(count);
 		AN(*count);
 		ovector = pcre2_get_ovector_pointer(data);
-		nov = pcre2_get_ovector_count(data);
-		if (nov > *count)
-			nov = *count;
+		nov = vmin_t(size_t, pcre2_get_ovector_count(data), *count);
 		for (g = 0; g < nov; g++) {
 			b = ovector[2 * g];
 			e = ovector[2 * g + 1];
diff --git a/lib/libvarnish/vsb.c b/lib/libvarnish/vsb.c
index f7e9e46c3..dd9210201 100644
--- a/lib/libvarnish/vsb.c
+++ b/lib/libvarnish/vsb.c
@@ -383,9 +383,7 @@ VSB_vprintf(struct vsb *s, const char *fmt, va_list ap)
 	 * vsnprintf() returns the amount that would have been copied,
 	 * given sufficient space, so don't over-increment s_len.
 	 */
-	if (VSB_FREESPACE(s) < len)
-		len = VSB_FREESPACE(s);
-	s->s_len += len;
+	s->s_len += vmin_t(ssize_t, len, VSB_FREESPACE(s));
 	if (!VSB_HASROOM(s) && !VSB_CANEXTEND(s))
 		s->s_error = ENOMEM;
 
diff --git a/lib/libvarnishapi/vsl_dispatch.c b/lib/libvarnishapi/vsl_dispatch.c
index 4038273d0..5474d8ca9 100644
--- a/lib/libvarnishapi/vsl_dispatch.c
+++ b/lib/libvarnishapi/vsl_dispatch.c
@@ -933,8 +933,7 @@ vslq_ratelimit(struct VSLQ *vslq)
 	now = VTIM_mono();
 	delta = now - vslq->last_use;
 	vslq->credits += (delta / vslq->vsl->R_opt_p) * vslq->vsl->R_opt_l;
-	if (vslq->credits > vslq->vsl->R_opt_l)
-		vslq->credits = vslq->vsl->R_opt_l;
+	vslq->credits = vmin_t(double, vslq->credits, vslq->vsl->R_opt_l);
 	vslq->last_use = now;
 
 	if (vslq->credits < 1.0)
diff --git a/lib/libvcc/vcc_acl.c b/lib/libvcc/vcc_acl.c
index 1982db96a..d78288db9 100644
--- a/lib/libvcc/vcc_acl.c
+++ b/lib/libvcc/vcc_acl.c
@@ -105,9 +105,7 @@ vcl_acl_cmp(const struct acl_e *ae1, const struct acl_e *ae2)
 
 	p1 = ae1->data;
 	p2 = ae2->data;
-	m = ae1->mask;
-	if (ae2->mask < m)
-		m = ae2->mask;
+	m = vmin(ae1->mask, ae2->mask);
 	for (; m >= 8; m -= 8) {
 		CMP(*p1, *p2);
 		p1++;
diff --git a/vmod/vmod_directors_shard_cfg.c b/vmod/vmod_directors_shard_cfg.c
index d76c61c6b..4c10334ae 100644
--- a/vmod/vmod_directors_shard_cfg.c
+++ b/vmod/vmod_directors_shard_cfg.c
@@ -282,10 +282,7 @@ shardcfg_hashcircle(struct sharddir *shardd)
 	rmax = (UINT32_MAX - 1) / shardd->n_backend;
 	for (b = backends; b < backends + shardd->n_backend; b++) {
 		CHECK_OBJ_NOTNULL(b->backend, DIRECTOR_MAGIC);
-		r = b->replicas;
-		if (r > rmax)
-			r = rmax;
-		n_points += r;
+		n_points += vmin(b->replicas, rmax);
 	}
 
 	assert(n_points < UINT32_MAX);
@@ -301,9 +298,7 @@ shardcfg_hashcircle(struct sharddir *shardd)
 		AN(ident);
 		assert(ident[0] != '\0');
 
-		r = b->replicas;
-		if (r > rmax)
-			r = rmax;
+		r = vmin(b->replicas, rmax);
 
 		for (j = 0; j < r; j++) {
 			assert(snprintf(s, len, "%d", j) < len);
@@ -488,8 +483,7 @@ shardcfg_backend_del(struct backend_reconfig *re,
 		re->shardd->n_backend--;
 		if (i < re->shardd->n_backend + re->hole_n) {
 			(re->hole_n)++;
-			if (i < re->hole_i)
-				re->hole_i = i;
+			re->hole_i = vmin(re->hole_i, i);
 		}
 	}
 }


More information about the varnish-commit mailing list