[master] daf48542b shard: more signedness stir

Nils Goroll nils.goroll at uplex.de
Wed Jun 10 07:42:06 UTC 2020


commit daf48542b0ce50c7e167792a43b6126764d1fbf0
Author: Nils Goroll <nils.goroll at uplex.de>
Date:   Wed Jun 10 09:29:34 2020 +0200

    shard: more signedness stir

diff --git a/lib/libvmod_directors/shard_cfg.c b/lib/libvmod_directors/shard_cfg.c
index 174e6e67e..82c51fd16 100644
--- a/lib/libvmod_directors/shard_cfg.c
+++ b/lib/libvmod_directors/shard_cfg.c
@@ -242,7 +242,7 @@ static void
 shardcfg_hashcircle(struct sharddir *shardd)
 {
 	const struct shard_backend *backends, *b;
-	int h;
+	unsigned h;
 	uint32_t i, j, n_points, r, rmax;
 	const char *ident;
 	const int len = 12; // log10(UINT32_MAX) + 2;
@@ -394,7 +394,7 @@ shardcfg_backend_lookup(const struct backend_reconfig *re,
 static void
 shardcfg_backend_expand(const struct backend_reconfig *re)
 {
-	int min = re->hint;
+	unsigned min = re->hint;
 
 	CHECK_OBJ_NOTNULL(re->shardd, SHARDDIR_MAGIC);
 
@@ -427,6 +427,7 @@ shardcfg_backend_add(struct backend_reconfig *re,
 		assert(re->shardd->n_backend < re->shardd->l_backend);
 		i = re->shardd->n_backend;
 	} else {
+		assert(re->hole_i != UINT_MAX);
 		do {
 			if (!bb[re->hole_i].backend)
 				break;
@@ -445,7 +446,7 @@ shardcfg_backend_add(struct backend_reconfig *re,
 static void
 shardcfg_backend_clear(struct sharddir *shardd)
 {
-	int i;
+	unsigned i;
 	for (i = 0; i < shardd->n_backend; i++)
 		shardcfg_backend_free(&shardd->backend[i]);
 	shardd->n_backend = 0;
@@ -530,7 +531,7 @@ shardcfg_apply_change(VRT_CTX, struct sharddir *shardd,
 		.shardd = shardd,
 		.hint = shardd->n_backend,
 		.hole_n = 0,
-		.hole_i = INT_MAX
+		.hole_i = UINT_MAX
 	};
 
 	// XXX assert sharddir_locked(shardd)
@@ -656,7 +657,7 @@ shardcfg_reconfigure(VRT_CTX, struct vmod_priv *priv,
 void
 shardcfg_delete(const struct sharddir *shardd)
 {
-	int i;
+	unsigned i;
 
 	for (i = 0; i < shardd->n_backend; i++)
 		shardcfg_backend_free(&shardd->backend[i]);
@@ -687,7 +688,7 @@ shardcfg_set_rampup(struct sharddir *shardd, VCL_DURATION duration)
 }
 
 VCL_DURATION
-shardcfg_get_rampup(const struct sharddir *shardd, int host)
+shardcfg_get_rampup(const struct sharddir *shardd, unsigned host)
 {
 	VCL_DURATION r;
 
diff --git a/lib/libvmod_directors/shard_dir.c b/lib/libvmod_directors/shard_dir.c
index 1c5f7d193..b82364297 100644
--- a/lib/libvmod_directors/shard_dir.c
+++ b/lib/libvmod_directors/shard_dir.c
@@ -37,6 +37,7 @@
 #include <stdlib.h>
 #include <stdio.h>
 #include <time.h>
+#include <limits.h>
 
 #include "cache/cache.h"
 
@@ -47,7 +48,7 @@
 #include "shard_dir.h"
 
 struct shard_be_info {
-	int		hostid;
+	unsigned	hostid;
 	unsigned	healthy;
 	double		changed;	// when
 };
@@ -63,7 +64,7 @@ struct shard_state {
 	uint32_t		idx;
 
 	struct vbitmap		*picklist;
-	int			pickcount;
+	unsigned		pickcount;
 
 	struct shard_be_info	previous;
 	struct shard_be_info	last;
@@ -159,7 +160,7 @@ shard_next(struct shard_state *state, VCL_INT skip, VCL_BOOL healthy)
 				sbe = &state->last;
 			}
 			if (sbe == &state->last &&
-			    state->last.hostid != -1)
+			    state->last.hostid != UINT_MAX)
 				memcpy(&state->previous, &state->last,
 				    sizeof(state->previous));
 
@@ -262,9 +263,9 @@ init_state(struct shard_state *state,
 	state->idx = UINT32_MAX;
 	state->picklist = picklist;
 
-	/* healhy and changed only defined for hostid != -1 */
-	state->previous.hostid = -1;
-	state->last.hostid = -1;
+	/* healhy and changed only defined for valid hostids */
+	state->previous.hostid = UINT_MAX;
+	state->last.hostid = UINT_MAX;
 }
 
 /* basically same as vdir_any_healthy
@@ -327,7 +328,7 @@ sharddir_pick_be_locked(VRT_CTX, const struct sharddir *shardd, uint32_t key,
 	if (alt > 0) {
 		if (shard_next(state, alt - 1,
 		    healthy == VENUM(ALL) ? 1 : 0) == -1) {
-			if (state->previous.hostid != -1) {
+			if (state->previous.hostid != UINT_MAX) {
 				be = sharddir_backend(shardd,
 				    state->previous.hostid);
 				AN(be);
@@ -338,7 +339,7 @@ sharddir_pick_be_locked(VRT_CTX, const struct sharddir *shardd, uint32_t key,
 	}
 
 	if (shard_next(state, 0, healthy == VENUM(IGNORE) ? 0 : 1) == -1) {
-		if (state->previous.hostid != -1) {
+		if (state->previous.hostid != UINT_MAX) {
 			be = sharddir_backend(shardd, state->previous.hostid);
 			AN(be);
 			return (be);
@@ -358,8 +359,8 @@ sharddir_pick_be_locked(VRT_CTX, const struct sharddir *shardd, uint32_t key,
 		return (be);
 
 	assert(alt == 0);
-	assert(state->previous.hostid >= 0);
-	assert(state->last.hostid >= 0);
+	assert(state->previous.hostid != UINT_MAX);
+	assert(state->last.hostid != UINT_MAX);
 	assert(state->previous.hostid != state->last.hostid);
 	assert(be == sharddir_backend(shardd, state->previous.hostid));
 
diff --git a/lib/libvmod_directors/shard_dir.h b/lib/libvmod_directors/shard_dir.h
index 3de949943..67b709aac 100644
--- a/lib/libvmod_directors/shard_dir.h
+++ b/lib/libvmod_directors/shard_dir.h
@@ -74,9 +74,8 @@ struct sharddir {
 };
 
 static inline VCL_BACKEND
-sharddir_backend(const struct sharddir *shardd, int id)
+sharddir_backend(const struct sharddir *shardd, unsigned id)
 {
-	assert(id >= 0);
 	assert(id < shardd->n_backend);
 	return (shardd->backend[id].backend);
 }
@@ -115,4 +114,4 @@ VCL_BACKEND sharddir_pick_be(VRT_CTX, struct sharddir *, uint32_t, VCL_INT,
 
 /* in shard_cfg.c */
 void shardcfg_delete(const struct sharddir *shardd);
-VCL_DURATION shardcfg_get_rampup(const struct sharddir *shardd, int host);
+VCL_DURATION shardcfg_get_rampup(const struct sharddir *shardd, unsigned host);
diff --git a/lib/libvmod_directors/vmod_shard.c b/lib/libvmod_directors/vmod_shard.c
index 3f0444f4c..6e59044d6 100644
--- a/lib/libvmod_directors/vmod_shard.c
+++ b/lib/libvmod_directors/vmod_shard.c
@@ -707,9 +707,8 @@ vmod_shard_list(VRT_CTX, VCL_BACKEND dir, struct vsb *vsb, int pflag, int jflag)
 	VCL_DURATION rampup_d, d;
 	VCL_BACKEND be;
 	VCL_BOOL h;
-	unsigned nh = 0;
+	unsigned i, nh = 0;
 	double rampup_p;
-	int i;
 
 	CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
 	CHECK_OBJ_NOTNULL(dir, DIRECTOR_MAGIC);


More information about the varnish-commit mailing list