r5770 - trunk/varnish-cache/bin/varnishd

phk at varnish-cache.org phk at varnish-cache.org
Wed Jan 19 13:59:00 CET 2011


Author: phk
Date: 2011-01-19 13:58:59 +0100 (Wed, 19 Jan 2011)
New Revision: 5770

Modified:
   trunk/varnish-cache/bin/varnishd/cache.h
   trunk/varnish-cache/bin/varnishd/cache_gzip.c
   trunk/varnish-cache/bin/varnishd/cache_response.c
Log:
Improve and clean up the VGZ allocation a bit.



Modified: trunk/varnish-cache/bin/varnishd/cache.h
===================================================================
--- trunk/varnish-cache/bin/varnishd/cache.h	2011-01-19 11:32:42 UTC (rev 5769)
+++ trunk/varnish-cache/bin/varnishd/cache.h	2011-01-19 12:58:59 UTC (rev 5770)
@@ -625,10 +625,11 @@
 /* cache_gzip.c */
 struct vgz;
 
-struct vgz *VGZ_NewUnzip(struct sess *sp, struct ws *tmp, struct ws *buf);
+struct vgz *VGZ_NewUnzip(const struct sess *sp, struct ws *tmp,
+    struct ws *buf_ws, void *buf, ssize_t bufl);
 int VGZ_Feed(struct vgz *, const void *, size_t len);
 int VGZ_Produce(struct vgz *, const void **, size_t *len);
-int VGZ_Destroy(struct vgz **);
+void VGZ_Destroy(struct vgz **);
 
 /* cache_http.c */
 unsigned HTTP_estimate(unsigned nhttp);

Modified: trunk/varnish-cache/bin/varnishd/cache_gzip.c
===================================================================
--- trunk/varnish-cache/bin/varnishd/cache_gzip.c	2011-01-19 11:32:42 UTC (rev 5769)
+++ trunk/varnish-cache/bin/varnishd/cache_gzip.c	2011-01-19 12:58:59 UTC (rev 5770)
@@ -65,6 +65,7 @@
  */
 
 #include "config.h"
+#include <stdio.h>
 
 #include "svnid.h"
 SVNID("$Id$")
@@ -77,11 +78,11 @@
 struct vgz {
 	unsigned		magic;
 #define VGZ_MAGIC		0x162df0cb
-	struct sess 		*sp;
 	struct ws		*tmp;
 	char			*tmp_snapshot;
 
-	struct ws		*buf;
+	struct ws		*buf_ws;
+	void			*buf;
 	size_t			bufsiz;
 
 	z_stream		vz;
@@ -96,7 +97,7 @@
 
 	CAST_OBJ_NOTNULL(vg, opaque, VGZ_MAGIC);
 
-	return(WS_Alloc(vg->tmp, items * size));
+	return (WS_Alloc(vg->tmp, items * size));
 }
 
 static void
@@ -108,39 +109,97 @@
 	(void)address;
 }
 
-/*--------------------------------------------------------------------*/
+/*--------------------------------------------------------------------
+ * Set up a gunzip instance
+ */
 
-struct vgz *
-VGZ_NewUnzip(struct sess *sp, struct ws *tmp, struct ws *buf)
+static struct vgz *
+vgz_alloc_vgz(struct ws *ws, struct ws *buf_ws, void *buf, ssize_t bufl)
 {
+	char *s;
 	struct vgz *vg;
-	char *s;
 
-	CHECK_OBJ_NOTNULL(sp, SESS_MAGIC);
-
-	WS_Assert(tmp);
-	WS_Assert(buf);
-
-	s = WS_Snapshot(tmp);
-	vg = (void*)WS_Alloc(tmp, sizeof *vg);
+	WS_Assert(ws);
+	s = WS_Snapshot(ws);
+	vg = (void*)WS_Alloc(ws, sizeof *vg);
 	AN(vg);
 	memset(vg, 0, sizeof *vg);
 	vg->magic = VGZ_MAGIC;
-	vg->sp = sp;
-	vg->tmp = tmp;
-	vg->buf = buf;
+	vg->tmp = ws;
 	vg->tmp_snapshot = s;
 
 	vg->vz.zalloc = vgz_alloc;
 	vg->vz.zfree = vgz_free;
 	vg->vz.opaque = vg;
 
-	vg->bufsiz = WS_Reserve(buf, 0);
+	assert(buf_ws == NULL || buf == NULL);
+	if (buf_ws != NULL) {
+		WS_Assert(buf_ws);
+		vg->buf_ws = buf_ws;
+		vg->bufsiz = WS_Reserve(buf_ws, 0);
+		vg->buf = buf_ws->f;
+	} else {
+		assert(bufl > 0);
+		vg->buf = buf;
+		vg->bufsiz = bufl;
+	}
 
+	return (vg);
+}
+
+struct vgz *
+VGZ_NewUnzip(const struct sess *sp, struct ws *tmp, struct ws *buf_ws,
+    void *buf, ssize_t bufl)
+{
+	struct vgz *vg;
+
+	CHECK_OBJ_NOTNULL(sp, SESS_MAGIC);
+	vg = vgz_alloc_vgz(tmp, buf_ws, buf, bufl);
+
+	/*
+	 * Max memory usage according to zonf.h:
+	 * 	mem_needed = "a few kb" + (1 << (windowBits))
+	 * Since we don't control windowBits, we have to assume
+	 * it is 15, so 34-35KB or so.
+	 */
 	assert(Z_OK == inflateInit2(&vg->vz, 31));
 	return (vg);
 }
 
+static struct vgz *
+VGZ_NewGzip(const struct sess *sp, struct ws *tmp, struct ws *buf_ws,
+    void *buf, ssize_t bufl)
+{
+	struct vgz *vg;
+	int i;
+
+	CHECK_OBJ_NOTNULL(sp, SESS_MAGIC);
+	vg = vgz_alloc_vgz(tmp, buf_ws, buf, bufl);
+
+	/*
+	 * From zconf.h:
+	 *
+	 * 	mem_needed = "a few kb" 
+	 *		+ (1 << (windowBits+2))
+	 *		+  (1 << (memLevel+9))
+	 *
+	 * windowBits [8..15] (-> 1K..128K)
+	 * memLevel [1..9] (-> 1K->256K)
+	 *
+	 * XXX: They probably needs to be params...
+	 */
+	i = deflateInit2(&vg->vz,
+	    0,				/* Level */
+	    Z_DEFLATED,			/* Method */
+	    16 + 8,			/* Window bits (16=gzip + 15) */
+	    1,				/* memLevel */
+	    Z_DEFAULT_STRATEGY);
+	if (i != Z_OK)
+		printf("deflateInit2() = %d\n", i);
+	assert(Z_OK == i);
+	return (vg);
+}
+
 /*--------------------------------------------------------------------*/
 
 int
@@ -167,12 +226,12 @@
 
 	*pptr = NULL;
 	*plen = 0;
-	vg->vz.next_out = (void*)vg->buf->f;
+	vg->vz.next_out = vg->buf;
 	vg->vz.avail_out = vg->bufsiz;
 
 	i = inflate(&vg->vz, 0);
 	if (i == Z_OK || i == Z_STREAM_END) {
-		*pptr = vg->buf->f;
+		*pptr = vg->buf;
 		*plen = vg->bufsiz - vg->vz.avail_out;
 	}
 	if (i == Z_OK)
@@ -186,15 +245,15 @@
 
 /*--------------------------------------------------------------------*/
 
-int
+void
 VGZ_Destroy(struct vgz **vg)
 {
 
 	CHECK_OBJ_NOTNULL(*vg, VGZ_MAGIC);
-	WS_Release((*vg)->buf, 0);
+	if ((*vg)->buf_ws != NULL)
+		WS_Release((*vg)->buf_ws, 0);
 	WS_Reset((*vg)->tmp, (*vg)->tmp_snapshot);
 	*vg = NULL;
-	return (0);
 }
 
 /*--------------------------------------------------------------------
@@ -207,7 +266,7 @@
 vfp_gunzip_begin(struct sess *sp, size_t estimate)
 {
 	(void)estimate;
-	sp->wrk->vfp_private = VGZ_NewUnzip(sp, sp->ws, sp->wrk->ws);
+	sp->wrk->vfp_private = VGZ_NewUnzip(sp, sp->ws, sp->wrk->ws, NULL, 0);
 }
 
 static int __match_proto__()
@@ -237,10 +296,10 @@
 			l = vg->bufsiz;
 			if (l > bytes)
 				l = bytes;
-			w = HTC_Read(htc, vg->buf->f, l);
+			w = HTC_Read(htc, vg->buf, l);
 			if (w <= 0)
 				return (w);
-			vg->vz.next_in = (void*)vg->buf->f;
+			vg->vz.next_in = vg->buf;
 			vg->vz.avail_in = w;
 			bytes -= w;
 		}
@@ -304,16 +363,7 @@
 	struct vgz *vg;
 	(void)estimate;
 
-	vg = VGZ_NewUnzip(sp, sp->ws, sp->wrk->ws);
-	/* XXX: hack */
-	memset(&vg->vz, 0, sizeof vg->vz);
-	assert(Z_OK == deflateInit2(&vg->vz,
-	    0,
-	    Z_DEFLATED,
-	    31,
-	    9,
-	    Z_DEFAULT_STRATEGY));
-
+	vg = VGZ_NewGzip(sp, sp->ws, sp->wrk->ws, NULL, 0);
 	sp->wrk->vfp_private = vg;
 }
 
@@ -344,10 +394,10 @@
 			l = vg->bufsiz;
 			if (l > bytes)
 				l = bytes;
-			w = HTC_Read(htc, vg->buf->f, l);
+			w = HTC_Read(htc, vg->buf, l);
 			if (w <= 0)
 				return (w);
-			vg->vz.next_in = (void*)vg->buf->f;
+			vg->vz.next_in = vg->buf;
 			vg->vz.avail_in = w;
 			bytes -= w;
 		}

Modified: trunk/varnish-cache/bin/varnishd/cache_response.c
===================================================================
--- trunk/varnish-cache/bin/varnishd/cache_response.c	2011-01-19 11:32:42 UTC (rev 5769)
+++ trunk/varnish-cache/bin/varnishd/cache_response.c	2011-01-19 12:58:59 UTC (rev 5770)
@@ -251,7 +251,7 @@
 
 	CHECK_OBJ_NOTNULL(sp, SESS_MAGIC);
 
-	vg = VGZ_NewUnzip(sp, sp->ws, sp->wrk->ws);
+	vg = VGZ_NewUnzip(sp, sp->ws, sp->wrk->ws, NULL, 0);
 	AN(vg);
 
 	VTAILQ_FOREACH(st, &sp->obj->store, list) {




More information about the varnish-commit mailing list