From 6e39f583ae404567477ebec4a051e7a5103aefda Mon Sep 17 00:00:00 2001 From: phk Date: Wed, 26 Sep 2007 19:15:25 +0000 Subject: [PATCH] Use the new txt type more extensively, it does improve readability a bit. git-svn-id: svn+ssh://projects.linpro.no/svn/varnish/trunk@2046 d4fa192b-c00b-0410-8231-f00ffab90ce4 --- varnish-cache/bin/varnishd/cache.h | 26 +++++++++++++++-- varnish-cache/bin/varnishd/cache_http.c | 38 ++++++++++++------------- varnish-cache/bin/varnishd/shmlog.c | 34 +++++++++++----------- 3 files changed, 61 insertions(+), 37 deletions(-) diff --git a/varnish-cache/bin/varnishd/cache.h b/varnish-cache/bin/varnishd/cache.h index e20f2ac2..17f72839 100644 --- a/varnish-cache/bin/varnishd/cache.h +++ b/varnish-cache/bin/varnishd/cache.h @@ -509,9 +509,9 @@ void SES_Charge(struct sess *sp); void VSL_Init(void); #ifdef SHMLOGHEAD_MAGIC -void VSLR(enum shmlogtag tag, int id, const char *b, const char *e); +void VSLR(enum shmlogtag tag, int id, txt t); void VSL(enum shmlogtag tag, int id, const char *fmt, ...); -void WSLR(struct worker *w, enum shmlogtag tag, int id, const char *b, const char *e); +void WSLR(struct worker *w, enum shmlogtag tag, int id, txt t); void WSL(struct worker *w, enum shmlogtag tag, int id, const char *fmt, ...); void WSL_Flush(struct worker *w); #define INCOMPL() do { \ @@ -602,3 +602,25 @@ pdiff(const void *b, const void *e) return ((unsigned)((const unsigned char *)e - (const unsigned char *)b)); } + +static inline void +Tcheck(const txt t) +{ + + AN(t.b); + AN(t.e); + assert(t.b <= t.e); +} + +/* + * unsigned length of a txt + */ + +static inline unsigned +Tlen(const txt t) +{ + + Tcheck(t); + return + ((unsigned)(t.e - t.b)); +} diff --git a/varnish-cache/bin/varnishd/cache_http.c b/varnish-cache/bin/varnishd/cache_http.c index 6194f67f..98e803cd 100644 --- a/varnish-cache/bin/varnishd/cache_http.c +++ b/varnish-cache/bin/varnishd/cache_http.c @@ -92,7 +92,7 @@ static void WSLH(struct worker *w, enum httptag t, int fd, const struct http *hp, unsigned hdr) { - WSLR(w, http2shmlog(hp, t), fd, hp->hd[hdr].b, hp->hd[hdr].e); + WSLR(w, http2shmlog(hp, t), fd, hp->hd[hdr]); } /*--------------------------------------------------------------------*/ @@ -325,16 +325,16 @@ http_GetTail(struct http *hp, unsigned len, char **b, char **e) return (0); if (len == 0) - len = pdiff(hp->pl.b, hp->pl.e); + len = Tlen(hp->pl); if (hp->pl.b + len > hp->pl.e) - len = pdiff(hp->pl.b, hp->pl.e); + len = Tlen(hp->pl); if (len == 0) return (0); *b = hp->pl.b; *e = hp->pl.b + len; hp->pl.b += len; - assert(hp->pl.b <= hp->pl.e); + Tcheck(hp->pl); return (1); } @@ -350,7 +350,7 @@ http_Read(struct http *hp, int fd, void *p, unsigned len) u = 0; if (hp->pl.b < hp->pl.e) { - u = pdiff(hp->pl.b, hp->pl.e); + u = Tlen(hp->pl); if (u > len) u = len; memcpy(b, hp->pl.b, u); @@ -435,7 +435,7 @@ http_dissect_hdrs(struct worker *w, struct http *hp, int fd, char *p) hp->nhd++; } else { VSL_stats->losthdr++; - WSLR(w, SLT_LostHeader, fd, p, q); + WSL(w, SLT_LostHeader, fd, "%.*s", q - p, p); } } return (0); @@ -450,7 +450,7 @@ http_DissectRequest(struct worker *w, struct http *hp, int fd) CHECK_OBJ_NOTNULL(hp, HTTP_MAGIC); /* Assert a NUL at rx.e */ - assert(hp->rx.b < hp->rx.e); + Tcheck(hp->rx); hp->logtag = HTTP_Rx; for (p = hp->rx.b ; isspace(*p); p++) @@ -468,7 +468,7 @@ http_DissectRequest(struct worker *w, struct http *hp, int fd) while (isspace(*p) && *p != '\n') p++; if (*p == '\n') { - WSLR(w, SLT_HttpGarbage, fd, hp->rx.b, hp->rx.e); + WSLR(w, SLT_HttpGarbage, fd, hp->rx); return (400); } hp->hd[HTTP_HDR_URL].b = p; @@ -477,7 +477,7 @@ http_DissectRequest(struct worker *w, struct http *hp, int fd) hp->hd[HTTP_HDR_URL].e = p; WSLH(w, HTTP_T_URL, fd, hp, HTTP_HDR_URL); if (*p == '\n') { - WSLR(w, SLT_HttpGarbage, fd, hp->rx.b, hp->rx.e); + WSLR(w, SLT_HttpGarbage, fd, hp->rx); return (400); } *p++ = '\0'; @@ -486,7 +486,7 @@ http_DissectRequest(struct worker *w, struct http *hp, int fd) while (isspace(*p) && *p != '\n') p++; if (*p == '\n') { - WSLR(w, SLT_HttpGarbage, fd, hp->rx.b, hp->rx.e); + WSLR(w, SLT_HttpGarbage, fd, hp->rx); return (400); } hp->hd[HTTP_HDR_PROTO].b = p; @@ -499,7 +499,7 @@ http_DissectRequest(struct worker *w, struct http *hp, int fd) while (isspace(*p) && *p != '\n') p++; if (*p != '\n') { - WSLR(w, SLT_HttpGarbage, fd, hp->rx.b, hp->rx.e); + WSLR(w, SLT_HttpGarbage, fd, hp->rx); return (400); } *p++ = '\0'; @@ -516,14 +516,14 @@ http_DissectResponse(struct worker *w, struct http *hp, int fd) CHECK_OBJ_NOTNULL(hp, HTTP_MAGIC); /* Assert a NUL at rx.e */ - assert(hp->rx.b < hp->rx.e); + Tcheck(hp->rx); hp->logtag = HTTP_Rx; for (p = hp->rx.b ; isspace(*p); p++) continue; if (memcmp(p, "HTTP/1.", 7)) { - WSLR(w, SLT_HttpGarbage, fd, hp->rx.b, hp->rx.e); + WSLR(w, SLT_HttpGarbage, fd, hp->rx); return (400); } /* First, protocol */ @@ -615,7 +615,7 @@ http_RecvPrep(struct http *hp) hp->rx.b = hp->ws->f; hp->rx.e = hp->rx.b; if (hp->pl.b != NULL) { - l = pdiff(hp->pl.b, hp->pl.e); + l = Tlen(hp->pl); memmove(hp->rx.b, hp->pl.b, l); hp->rx.e = hp->rx.b + l; hp->pl.b = hp->pl.e = NULL; @@ -644,7 +644,7 @@ http_RecvSome(int fd, struct http *hp) l /= 2; /* Don't fill all of workspace with read-ahead */ if (l <= 1) { VSL(SLT_HttpError, fd, "Received too much"); - VSLR(SLT_HttpGarbage, fd, hp->rx.b, hp->rx.e); + VSLR(SLT_HttpGarbage, fd, hp->rx); hp->rx.b = hp->rx.e = NULL; WS_Release(hp->ws, 0); return (1); @@ -663,7 +663,7 @@ http_RecvSome(int fd, struct http *hp) VSL(SLT_HttpError, fd, "Received (only) %d bytes, errno %d", hp->rx.e - hp->rx.b, errno); - VSLR(SLT_Debug, fd, hp->rx.b, hp->rx.e); + VSLR(SLT_Debug, fd, hp->rx); } else if (errno == 0) VSL(SLT_HttpError, fd, "Received nothing"); else @@ -767,7 +767,7 @@ http_copyheader(struct worker *w, int fd, struct http *to, const struct http *fm to->nhd++; } else { VSL_stats->losthdr++; - WSLR(w, SLT_LostHeader, fd, fm->hd[n].b, fm->hd[n].e); + WSLR(w, SLT_LostHeader, fd, fm->hd[n]); } } @@ -855,7 +855,7 @@ http_CopyHome(struct worker *w, int fd, struct http *hp) WSLH(w, htt, fd, hp, u); continue; } - l = pdiff(hp->hd[u].b, hp->hd[u].e); + l = Tlen(hp->hd[u]); p = WS_Alloc(hp->ws, l + 1); if (p != NULL) { WSLH(w, htt, fd, hp, u); @@ -863,7 +863,7 @@ http_CopyHome(struct worker *w, int fd, struct http *hp) hp->hd[u].b = p; hp->hd[u].e = p + l; } else { - WSLR(w, SLT_LostHeader, fd, hp->hd[u].b, hp->hd[u].e); + WSLR(w, SLT_LostHeader, fd, hp->hd[u]); hp->hd[u].b = NULL; hp->hd[u].e = NULL; } diff --git a/varnish-cache/bin/varnishd/shmlog.c b/varnish-cache/bin/varnishd/shmlog.c index 089cd852..76724c39 100644 --- a/varnish-cache/bin/varnishd/shmlog.c +++ b/varnish-cache/bin/varnishd/shmlog.c @@ -83,20 +83,18 @@ vsl_wrap(void) /*--------------------------------------------------------------------*/ void -VSLR(enum shmlogtag tag, int id, const char *b, const char *e) +VSLR(enum shmlogtag tag, int id, txt t) { unsigned char *p; unsigned l; - assert(b != NULL); - if (e == NULL) - e = strchr(b, '\0'); + Tcheck(t); /* Truncate */ - l = pdiff(b, e); + l = Tlen(t); if (l > 255) { l = 255; - e = b + l; + t.e = t.b + l; } /* Only hold the lock while we find our space */ @@ -117,7 +115,7 @@ VSLR(enum shmlogtag tag, int id, const char *b, const char *e) p[1] = l & 0xff; p[2] = (id >> 8) & 0xff; p[3] = id & 0xff; - memcpy(p + 4, b, l); + memcpy(p + 4, t.b, l); p[4 + l] = '\0'; /* XXX: memory barrier */ p[0] = tag; @@ -131,12 +129,15 @@ VSL(enum shmlogtag tag, int id, const char *fmt, ...) va_list ap; unsigned char *p; unsigned n; + txt t; AN(fmt); va_start(ap, fmt); if (strchr(fmt, '%') == NULL) { - VSLR(tag, id, fmt, NULL); + t.b = (void*)(uintptr_t)fmt; + t.e = strchr(fmt, '\0'); + VSLR(tag, id, t); return; } @@ -200,20 +201,18 @@ WSL_Flush(struct worker *w) /*--------------------------------------------------------------------*/ void -WSLR(struct worker *w, enum shmlogtag tag, int id, const char *b, const char *e) +WSLR(struct worker *w, enum shmlogtag tag, int id, txt t) { unsigned char *p; unsigned l; - assert(b != NULL); - if (e == NULL) - e = strchr(b, '\0'); + Tcheck(t); /* Truncate */ - l = pdiff(b, e); + l = Tlen(t); if (l > 255) { l = 255; - e = b + l; + t.e = t.b + l; } assert(w->wlp < w->wle); @@ -229,7 +228,7 @@ WSLR(struct worker *w, enum shmlogtag tag, int id, const char *b, const char *e) p[1] = l & 0xff; p[2] = (id >> 8) & 0xff; p[3] = id & 0xff; - memcpy(p + 4, b, l); + memcpy(p + 4, t.b, l); p[4 + l] = '\0'; p[0] = tag; w->wlr++; @@ -243,12 +242,15 @@ WSL(struct worker *w, enum shmlogtag tag, int id, const char *fmt, ...) va_list ap; unsigned char *p; unsigned n; + txt t; AN(fmt); va_start(ap, fmt); if (strchr(fmt, '%') == NULL) { - WSLR(w, tag, id, fmt, NULL); + t.b = (void*)(uintptr_t)fmt; + t.e = strchr(fmt, '\0'); + WSLR(w, tag, id, t); return; } -- 2.39.5