[master] 651a249f4 Retire the WB mini-interface used by VMOD blob.

Geoff Simmons geoff at uplex.de
Wed Jul 18 14:19:07 UTC 2018


commit 651a249f4a78fe433bda6b0dc3883b370dc73242
Author: Geoff Simmons <geoff at uplex.de>
Date:   Wed Jul 18 16:16:21 2018 +0200

    Retire the WB mini-interface used by VMOD blob.
    
    This was a proof of concept for an alternative WS interface -- write
    incrementally to reserved workspace as an "open buffer".
    
    That may be generally useful for Varnish, but for now we return to
    the usual WS_* idiom.

diff --git a/lib/libvmod_blob/Makefile.am b/lib/libvmod_blob/Makefile.am
index 2695913a2..fafce9acd 100644
--- a/lib/libvmod_blob/Makefile.am
+++ b/lib/libvmod_blob/Makefile.am
@@ -9,8 +9,6 @@ libvmod_blob_la_SOURCES = \
 	hex.h \
 	hex.c \
 	url.c \
-	wb.h \
-	wb.c \
 	tbl_encodings.h \
 	tbl_case.h
 
diff --git a/lib/libvmod_blob/vmod_blob.c b/lib/libvmod_blob/vmod_blob.c
index a53d8c415..87591fb52 100644
--- a/lib/libvmod_blob/vmod_blob.c
+++ b/lib/libvmod_blob/vmod_blob.c
@@ -34,7 +34,6 @@
 
 #include "vcc_if.h"
 #include "vmod_blob.h"
-#include "wb.h"
 
 struct vmod_blob_blob {
 	unsigned magic;
@@ -357,11 +356,11 @@ vmod_decode(VRT_CTX, VCL_ENUM decs, VCL_INT length, const char *p, ...)
 {
 	enum encoding dec = parse_encoding(decs);
 	va_list ap;
-	struct wb_s wb;
 	struct vmod_priv *b;
 	char *buf;
 	uintptr_t snap;
 	ssize_t len;
+	unsigned space;
 
 	CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
 	AENC(dec);
@@ -373,33 +372,28 @@ vmod_decode(VRT_CTX, VCL_ENUM decs, VCL_INT length, const char *p, ...)
 		return NULL;
 	}
 
-	if (wb_create(ctx->ws, &wb) == NULL) {
-		WS_Reset(ctx->ws, snap);
-		ERRNOMEM(ctx, "cannot decode");
-		return NULL;
-	}
-	buf = wb_buf(&wb);
+	buf = WS_Front(ctx->ws);
+	space = WS_Reserve(ctx->ws, 0);
 
 	if (length <= 0)
 		length = -1;
 	va_start(ap, p);
 	errno = 0;
-	len = func[dec].decode(dec, buf, wb_space(&wb), length, p, ap);
+	len = func[dec].decode(dec, buf, space, length, p, ap);
 	va_end(ap);
 
 	if (len == -1) {
 		err_decode(ctx, p);
-		wb_reset(&wb);
+		WS_Release(ctx->ws, 0);
 		WS_Reset(ctx->ws, snap);
 		return NULL;
 	}
 	if (len == 0) {
-		wb_reset(&wb);
+		WS_Release(ctx->ws, 0);
 		WS_Reset(ctx->ws, snap);
 		return null_blob;
 	}
-	wb_advance(&wb, len);
-	WS_ReleaseP(ctx->ws, wb_buf(&wb));
+	WS_Release(ctx->ws, len);
 	b->priv = buf;
 	b->len = len;
 	b->free = NULL;
@@ -409,8 +403,10 @@ vmod_decode(VRT_CTX, VCL_ENUM decs, VCL_INT length, const char *p, ...)
 static VCL_STRING
 encode(VRT_CTX, enum encoding enc, enum case_e kase, VCL_BLOB b)
 {
-	struct wb_s wb;
 	ssize_t len;
+	char *buf;
+	uintptr_t snap;
+	unsigned space;
 
 	AENC(enc);
 
@@ -418,25 +414,25 @@ encode(VRT_CTX, enum encoding enc, enum case_e kase, VCL_BLOB b)
 		return NULL;
 
 	CHECK_OBJ_NOTNULL(ctx->ws, WS_MAGIC);
-	if (wb_create(ctx->ws, &wb) == NULL) {
-		ERRNOMEM(ctx, "cannot encode");
-		return NULL;
-	}
+	snap = WS_Snapshot(ctx->ws);
+	buf = WS_Front(ctx->ws);
+	space = WS_Reserve(ctx->ws, 0);
 
-	len = func[enc].encode(enc, kase,
-			       wb_buf(&wb), wb_space(&wb), b->priv, b->len);
+	len = func[enc].encode(enc, kase, buf, space, b->priv, b->len);
 
 	if (len == -1) {
 		ERRNOMEM(ctx, "cannot encode");
-		wb_reset(&wb);
+		WS_Release(ctx->ws, 0);
+		WS_Reset(ctx->ws, snap);
 		return NULL;
 	}
 	if (len == 0) {
-		wb_reset(&wb);
+		WS_Release(ctx->ws, 0);
+		WS_Reset(ctx->ws, snap);
 		return "";
 	}
-	wb_advance(&wb, len);
-	return wb_finish(&wb, NULL);
+	WS_Release(ctx->ws, len + 1);
+	return buf;
 }
 
 VCL_STRING v_matchproto_(td_blob_encode)
diff --git a/lib/libvmod_blob/wb.c b/lib/libvmod_blob/wb.c
deleted file mode 100644
index 1b54b3e72..000000000
--- a/lib/libvmod_blob/wb.c
+++ /dev/null
@@ -1,78 +0,0 @@
-/*-
- * Copyright 2015 UPLEX - Nils Goroll Systemoptimierung
- * All rights reserved.
- *
- * Authors: Nils Goroll <nils.goroll at uplex.de>
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- * 1. Redistributions of source code must retain the above copyright notice,
- *    this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright notice,
- *    this list of conditions and the following disclaimer in the documentation
- *    and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
- * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY
- * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
- * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
- * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
- * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
- * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- * write buffer: utility functions to append-write on a varnish workspace
- */
-
-#include "config.h"
-
-#include <string.h>
-
-#include "cache/cache.h"
-#include "wb.h"
-
-char *
-wb_create(struct ws *ws, struct wb_s *wb)
-{
-	if (WS_Reserve(ws, 0) == 0) {
-		WS_Release(ws, 0);
-		wb->w = NULL;
-		wb->ws = NULL;
-		return NULL;
-	}
-	wb->w = ws->f;
-	wb->ws = ws;
-
-	return wb->w;
-}
-
-void
-wb_reset(struct wb_s *wb)
-{
-	WS_Release(wb->ws, 0);
-	memset(wb, 0, sizeof(*wb));
-}
-
-/*
- * release varnish workspace
- *
- * return start of buffer
- */
-char *
-wb_finish(struct wb_s *wb, ssize_t *l)
-{
-	char *r = wb->ws->f;
-	assert(wb->ws->r - wb->w > 0);
-	if (l)
-		*l = wb_len(wb);
-
-	*wb->w = '\0';
-	wb->w++;
-
-	/* amount of space used */
-	WS_ReleaseP(wb->ws, wb->w);
-
-	return r;
-}
diff --git a/lib/libvmod_blob/wb.h b/lib/libvmod_blob/wb.h
deleted file mode 100644
index e2c820d50..000000000
--- a/lib/libvmod_blob/wb.h
+++ /dev/null
@@ -1,68 +0,0 @@
-/*-
- * Copyright 2015-2016 UPLEX - Nils Goroll Systemoptimierung
- * All rights reserved.
- *
- * Author: Nils Goroll <nils.goroll at uplex.de>
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- * 1. Redistributions of source code must retain the above copyright notice,
- *    this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright notice,
- *    this list of conditions and the following disclaimer in the documentation
- *    and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
- * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY
- * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
- * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
- * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
- * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
- * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- */
-
-struct wb_s {
-	struct ws	*ws; // varnish workspace
-	char		*w;  // current write position
-};
-
-/* return one byte less for the final zero byte */
-static inline const char*
-wb_end(const struct wb_s *wb) {
-	return wb->ws->r - 1;
-}
-
-/* return the write position */
-static inline char*
-wb_buf(const struct wb_s *wb) {
-	return wb->w;
-}
-
-/* return one byte less for the final zero byte */
-static inline ssize_t
-wb_space(const struct wb_s *wb) {
-	ssize_t f = wb->ws->r - wb->w;
-	assert(f > 0);
-	return f - 1;
-}
-
-static inline ssize_t
-wb_len(const struct wb_s *wb) {
-	ssize_t l = wb->w - wb->ws->f;
-	assert(l >= 0);
-	return l;
-}
-
-static inline void
-wb_advance(struct wb_s *wb, ssize_t l) {
-	wb->w += l;			// final byte
-	assert(wb->w < wb_end(wb));
-}
-
-char *wb_create(struct ws *ws, struct wb_s *wb);
-void wb_reset(struct wb_s *wb);
-char *wb_finish(struct wb_s *wb, ssize_t *l);


More information about the varnish-commit mailing list