[PATCH 04/15] Add defines for ban header sizes and offsets, and use them

Martin Blix Grydeland martin at varnish-software.com
Wed Nov 7 12:32:07 CET 2012


---
 bin/varnishd/cache/cache_ban.c |   53 ++++++++++++++++++++++++++--------------
 1 file changed, 35 insertions(+), 18 deletions(-)

diff --git a/bin/varnishd/cache/cache_ban.c b/bin/varnishd/cache/cache_ban.c
index 7f02f90..f44d98d 100644
--- a/bin/varnishd/cache/cache_ban.c
+++ b/bin/varnishd/cache/cache_ban.c
@@ -41,7 +41,7 @@
  * Bans are compiled into bytestrings as follows:
  *	8 bytes	- double: timestamp		XXX: Byteorder ?
  *	4 bytes - be32: length
- *	1 byte - flags: 0x01: BAN_F_REQ
+ *	1 byte - flags: 0x01: BANHEAD_F_REQ  <-> BAN_F_REQ
  *	N tests
  * A test have this form:
  *	1 byte - arg (see ban_vars.h col 3 "BAN_ARG_XXX")
@@ -107,6 +107,17 @@ static bgthread_t ban_lurker;
 static int ban_shutdown = 0;
 
 /*--------------------------------------------------------------------
+ * BANHEAD data markers
+ */
+
+#define BANHEAD_F_REQ   (1 << 0)
+
+#define BANHEAD_O_TS	0
+#define BANHEAD_O_LEN	8
+#define BANHEAD_O_FLAGS 12
+#define BANHEAD_LEN	13
+
+/*--------------------------------------------------------------------
  * BAN string magic markers
  */
 
@@ -221,7 +232,7 @@ ban_len(const uint8_t *banspec)
 {
 	unsigned u;
 
-	u = vbe32dec(banspec + 8);
+	u = vbe32dec(banspec + BANHEAD_O_LEN);
 	return (u);
 }
 
@@ -396,15 +407,17 @@ BAN_Insert(struct ban *b)
 	ln = VSB_len(b->vsb);
 	assert(ln >= 0);
 
-	b->spec = malloc(ln + 13L);	/* XXX */
+	b->spec = malloc(ln + BANHEAD_LEN);
 	XXXAN(b->spec);
 
 	t0 = VTIM_real();
-	memcpy(b->spec, &t0, sizeof t0);
-	b->spec[12] = (b->flags & BAN_F_REQ) ? 1 : 0;
-	memcpy(b->spec + 13, VSB_data(b->vsb), ln);
-	ln += 13;
-	vbe32enc(b->spec + 8, ln);
+	memcpy(b->spec + BANHEAD_O_TS, &t0, sizeof t0);
+	b->spec[BANHEAD_O_FLAGS] = 0;
+	if (b->flags & BAN_F_REQ)
+		b->spec[BANHEAD_O_FLAGS] |= BANHEAD_F_REQ;
+	memcpy(b->spec + BANHEAD_LEN, VSB_data(b->vsb), ln);
+	ln += BANHEAD_LEN;
+	vbe32enc(b->spec + BANHEAD_O_LEN, ln);
 
 	VSB_delete(b->vsb);
 	b->vsb = NULL;
@@ -531,7 +544,7 @@ void
 BAN_Reload(const uint8_t *ban, unsigned len)
 {
 	struct ban *b, *b2;
-	int gone = 0;
+	int duplicate = 0;
 	double t0, t1, t2 = 9e99;
 
 	ASSERT_CLI();
@@ -552,11 +565,9 @@ BAN_Reload(const uint8_t *ban, unsigned len)
 		}
 		if (t1 < t0)
 			break;
-		if (!memcmp(b->spec + 8, ban + 8, len - 8)) {
-			gone |= BAN_F_GONE;
-			VSC_C_main->bans_dups++;
-			VSC_C_main->bans_gone++;
-		}
+		if (!memcmp(b->spec + BANHEAD_O_LEN, ban + BANHEAD_O_LEN,
+			    len - BANHEAD_O_LEN))
+			duplicate = 1;
 	}
 
 	VSC_C_main->bans++;
@@ -567,9 +578,14 @@ BAN_Reload(const uint8_t *ban, unsigned len)
 	b2->spec = malloc(len);
 	AN(b2->spec);
 	memcpy(b2->spec, ban, len);
-	b2->flags |= gone;
-	if (ban[12])
+	if (duplicate) {
+		VSC_C_main->bans_dups++;
+		b2->flags |= BAN_F_GONE;
+	}
+	if (ban[BANHEAD_O_FLAGS] & BANHEAD_F_REQ)
 		b2->flags |= BAN_F_REQ;
+	if (b2->flags & BAN_F_GONE)
+		VSC_C_main->bans_gone++;
 	if (b == NULL)
 		VTAILQ_INSERT_TAIL(&ban_head, b2, list);
 	else
@@ -579,7 +595,8 @@ BAN_Reload(const uint8_t *ban, unsigned len)
 	for (b = VTAILQ_NEXT(b2, list); b != NULL; b = VTAILQ_NEXT(b, list)) {
 		if (b->flags & BAN_F_GONE)
 			continue;
-		if (!memcmp(b->spec + 8, ban + 8, len - 8)) {
+		if (!memcmp(b->spec + BANHEAD_O_LEN, ban + BANHEAD_O_LEN,
+			    len - BANHEAD_O_LEN)) {
 			b->flags |= BAN_F_GONE;
 			VSC_C_main->bans_dups++;
 			VSC_C_main->bans_gone++;
@@ -1075,7 +1092,7 @@ ban_render(struct cli *cli, const uint8_t *bs)
 	const uint8_t *be;
 
 	be = bs + ban_len(bs);
-	bs += 13;
+	bs += BANHEAD_LEN;
 	while (bs < be) {
 		ban_iter(&bs, &bt);
 		switch (bt.arg1) {
-- 
1.7.9.5




More information about the varnish-dev mailing list