[master] ce9c09a Negative SIZE to debug.workspace_allocate() now means "leave this much free in workspace".

Poul-Henning Kamp phk at FreeBSD.org
Thu Mar 23 14:36:06 CET 2017


commit ce9c09a297a7b15f9db28411a5cb712113f803d9
Author: Poul-Henning Kamp <phk at FreeBSD.org>
Date:   Thu Mar 23 13:27:45 2017 +0000

    Negative SIZE to debug.workspace_allocate() now means
    "leave this much free in workspace".
    
    Add debug.typesize() which can tell us the C-sizeof() various types.

diff --git a/lib/libvmod_debug/vmod.vcc b/lib/libvmod_debug/vmod.vcc
index 4fe903f..b33d3af 100644
--- a/lib/libvmod_debug/vmod.vcc
+++ b/lib/libvmod_debug/vmod.vcc
@@ -167,6 +167,8 @@ $Function VOID workspace_allocate(ENUM { client, backend, session, thread },
 	  INT size)
 
 Allocate and zero out SIZE bytes from a workspace.
+If SIZE is negative, all but that many bytes are allocated from the workspace.
+(NB: Beware of the alignment imposed on workspace allocations.)
 
 $Function BOOL workspace_overflowed(ENUM { client, backend, session, thread })
 
@@ -203,3 +205,17 @@ Synchronize with a varnishtest shared barrier.
 $Function VOID test_probe(PROBE probe, PROBE same = 0)
 
 Only here to make sure probe definitions are passed properly.
+
+$Function INT typesize(STRING)
+
+Returns the size in bytes of a collection of C-datatypes.
+
+* 'p' = pointer
+* 'i' = int
+* 'd' = double
+* 'f' = float
+* 'l' = long
+* 's' = short
+* 'z' = size_t
+* 'o' = off_t
+* 'j' = intmax_t
diff --git a/lib/libvmod_debug/vmod_debug.c b/lib/libvmod_debug/vmod_debug.c
index fe07219..65bea1f 100644
--- a/lib/libvmod_debug/vmod_debug.c
+++ b/lib/libvmod_debug/vmod_debug.c
@@ -410,6 +410,10 @@ vmod_workspace_allocate(VRT_CTX, VCL_ENUM which, VCL_INT size)
 	WS_Assert(ws);
 	AZ(ws->r);
 
+	if (size < 0) {
+		size += WS_Reserve(ws, 0);
+		WS_Release(ws, 0);
+	}
 	s = WS_Alloc(ws, size);
 	if (!s)
 		return;
@@ -544,3 +548,27 @@ vmod_test_probe(VRT_CTX, VCL_PROBE probe, VCL_PROBE same)
 	CHECK_OBJ_ORNULL(same, VRT_BACKEND_PROBE_MAGIC);
 	AZ(same == NULL || probe == same);
 }
+
+VCL_INT
+vmod_typesize(VRT_CTX, VCL_STRING s)
+{
+	int i = 0;
+	const char *p;
+
+	(void)ctx;
+	for (p = s; *p; p++) {
+		switch (*p) {
+		case 'p':	i += sizeof(void *); break;
+		case 'i':	i += sizeof(int); break;
+		case 'd':	i += sizeof(double); break;
+		case 'f':	i += sizeof(float); break;
+		case 'l':	i += sizeof(long); break;
+		case 's':	i += sizeof(short); break;
+		case 'z':	i += sizeof(size_t); break;
+		case 'o':	i += sizeof(off_t); break;
+		case 'j':	i += sizeof(intmax_t); break;
+		default:	return(-1);
+		}
+	}
+	return (i);
+}



More information about the varnish-commit mailing list