[master] 368197a And now it is VNUM's turn...

Poul-Henning Kamp phk at varnish-cache.org
Sun Oct 9 21:30:23 CEST 2011


commit 368197ad6241b398fd6a50d25385d4bf9ba2cc81
Author: Poul-Henning Kamp <phk at FreeBSD.org>
Date:   Sun Oct 9 19:06:34 2011 +0000

    And now it is VNUM's turn...

diff --git a/bin/varnishd/mgt_shmem.c b/bin/varnishd/mgt_shmem.c
index 38dc59a..e42487a 100644
--- a/bin/varnishd/mgt_shmem.c
+++ b/bin/varnishd/mgt_shmem.c
@@ -103,6 +103,7 @@
 #include "vmb.h"
 #include "vsm.h"
 #include "vav.h"
+#include "vnum.h"
 #include "flopen.h"
 
 #ifndef MAP_HASSEMAPHORE
@@ -244,7 +245,7 @@ mgt_SHM_Init(const char *l_arg)
 
 	/* Size of SHMLOG */
 	if (*ap != NULL && **ap != '\0') {
-		q = str2bytes(*ap, &s1, 0);
+		q = VNUM_2bytes(*ap, &s1, 0);
 		if (q != NULL)
 			ARGV_ERR("\t-l[1] ...:  %s\n", q);
 	} else {
@@ -255,7 +256,7 @@ mgt_SHM_Init(const char *l_arg)
 
 	/* Size of space for other stuff */
 	if (*ap != NULL && **ap != '\0') {
-		q = str2bytes(*ap, &s2, 0);
+		q = VNUM_2bytes(*ap, &s2, 0);
 		if (q != NULL)
 			ARGV_ERR("\t-l[2] ...:  %s\n", q);
 	} else {
diff --git a/bin/varnishd/stevedore_utils.c b/bin/varnishd/stevedore_utils.c
index af7fc7f..725a086 100644
--- a/bin/varnishd/stevedore_utils.c
+++ b/bin/varnishd/stevedore_utils.c
@@ -53,6 +53,7 @@
 
 #include "mgt.h"
 #include "stevedore.h"
+#include "vnum.h"
 
 #ifndef O_LARGEFILE
 #define O_LARGEFILE	0
@@ -197,7 +198,7 @@ STV_FileSize(int fd, const char *size, unsigned *granularity, const char *ctx)
 		l = st.st_size;
 	} else {
 		AN(size);
-		q = str2bytes(size, &l, fssize);
+		q = VNUM_2bytes(size, &l, fssize);
 
 		if (q != NULL)
 			ARGV_ERR("(%s) size \"%s\": %s\n", size, ctx, q);
diff --git a/bin/varnishd/storage_file.c b/bin/varnishd/storage_file.c
index c363f4e..25c9947 100644
--- a/bin/varnishd/storage_file.c
+++ b/bin/varnishd/storage_file.c
@@ -38,6 +38,7 @@
 #include "cache.h"
 
 #include "stevedore.h"
+#include "vnum.h"
 
 #ifndef MAP_NOCORE
 #define MAP_NOCORE 0 /* XXX Linux */
@@ -131,7 +132,7 @@ smf_init(struct stevedore *parent, int ac, char * const *av)
 		size = av[1];
 	if (ac > 2 && *av[2] != '\0') {
 
-		r = str2bytes(av[2], &page_size, 0);
+		r = VNUM_2bytes(av[2], &page_size, 0);
 		if (r != NULL)
 			ARGV_ERR("(-sfile) granularity \"%s\": %s\n", av[2], r);
 	}
diff --git a/bin/varnishd/storage_malloc.c b/bin/varnishd/storage_malloc.c
index 500e74c..bc5a565 100644
--- a/bin/varnishd/storage_malloc.c
+++ b/bin/varnishd/storage_malloc.c
@@ -37,6 +37,7 @@
 #include "cache.h"
 
 #include "stevedore.h"
+#include "vnum.h"
 
 struct sma_sc {
 	unsigned		magic;
@@ -216,7 +217,7 @@ sma_init(struct stevedore *parent, int ac, char * const *av)
 	if (ac == 0 || *av[0] == '\0')
 		 return;
 
-	e = str2bytes(av[0], &u, 0);
+	e = VNUM_2bytes(av[0], &u, 0);
 	if (e != NULL)
 		ARGV_ERR("(-smalloc) size \"%s\": %s\n", av[0], e);
 	if ((u != (uintmax_t)(size_t)u))
diff --git a/bin/varnishd/storage_umem.c b/bin/varnishd/storage_umem.c
index 6e28a94..197d0ac 100644
--- a/bin/varnishd/storage_umem.c
+++ b/bin/varnishd/storage_umem.c
@@ -140,7 +140,7 @@ smu_init(struct stevedore *parent, int ac, char * const *av)
 	if (ac == 0 || *av[0] == '\0')
 		 return;
 
-	e = str2bytes(av[0], &u, 0);
+	e = VNUM_2bytes(av[0], &u, 0);
 	if (e != NULL)
 		ARGV_ERR("(-sumem) size \"%s\": %s\n", av[0], e);
 	if ((u != (uintmax_t)(size_t)u))
diff --git a/include/Makefile.am b/include/Makefile.am
index d62cab3..e852c58 100644
--- a/include/Makefile.am
+++ b/include/Makefile.am
@@ -48,6 +48,7 @@ nobase_noinst_HEADERS = \
 	vlu.h \
 	vmb.h \
 	vmod_abi.h \
+	vnum.h \
 	vpf.h \
 	vqueue.h \
 	vre.h \
diff --git a/include/libvarnish.h b/include/libvarnish.h
index c0851c8..1450578 100644
--- a/include/libvarnish.h
+++ b/include/libvarnish.h
@@ -38,9 +38,6 @@
 
 struct vsb;
 
-/* from libvarnish/num.c */
-const char *str2bytes(const char *p, uintmax_t *r, uintmax_t rel);
-
 /* from libvarnish/version.c */
 void VCS_Message(const char *);
 
diff --git a/lib/libvarnish/Makefile.am b/lib/libvarnish/Makefile.am
index 3c045e6..e890904 100644
--- a/lib/libvarnish/Makefile.am
+++ b/lib/libvarnish/Makefile.am
@@ -13,7 +13,7 @@ libvarnish_la_SOURCES = \
 	cli_common.c \
 	cli_serve.c \
 	flopen.c \
-	num.c \
+	vnum.c \
 	vtim.c \
 	vtcp.c \
 	vct.c \
@@ -33,13 +33,13 @@ libvarnish_la_CFLAGS = -DVARNISH_STATE_DIR='"${VARNISH_STATE_DIR}"'
 libvarnish_la_LIBADD = ${RT_LIBS} ${NET_LIBS} ${LIBM} @PCRE_LIBS@
 
 if ENABLE_TESTS
-TESTS = num_c_test
+TESTS = vnum_c_test
 
 noinst_PROGRAMS = ${TESTS}
 
-num_c_test_SOURCES = num.c
-num_c_test_CFLAGS = -DNUM_C_TEST -include config.h
-num_c_test_LDADD = ${LIBM}
+vnum_c_test_SOURCES = vnum.c
+vnum_c_test_CFLAGS = -DNUM_C_TEST -include config.h
+vnum_c_test_LDADD = ${LIBM}
 
 test: ${TESTS}
 	@for test in ${TESTS} ; do ./$${test} ; done
diff --git a/lib/libvarnish/num.c b/lib/libvarnish/num.c
deleted file mode 100644
index 70255db..0000000
--- a/lib/libvarnish/num.c
+++ /dev/null
@@ -1,197 +0,0 @@
-/*-
- * Copyright (c) 2008-2009 Varnish Software AS
- * All rights reserved.
- *
- * Author: Poul-Henning Kamp <phk at phk.freebsd.dk>
- *
- * 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.
- *
- * Deal with numbers with data storage suffix scaling
- */
-
-#include "config.h"
-
-#include <math.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <stdint.h>
-
-#include "libvarnish.h"
-
-static const char err_miss_num[] = "Missing number";
-static const char err_invalid_num[] = "Invalid number";
-static const char err_abs_req[] = "Absolute number required";
-static const char err_invalid_suff[] = "Invalid suffix";
-
-const char *
-str2bytes(const char *p, uintmax_t *r, uintmax_t rel)
-{
-	double fval;
-	char *end;
-
-	if (p == NULL || *p == '\0')
-		return (err_miss_num);
-
-	fval = strtod(p, &end);
-	if (end == p || !isfinite(fval))
-		return (err_invalid_num);
-
-	if (*end == '\0') {
-		*r = (uintmax_t)fval;
-		return (NULL);
-	}
-
-	if (end[0] == '%' && end[1] == '\0') {
-		if (rel == 0)
-			return (err_abs_req);
-		fval *= rel / 100.0;
-	} else {
-		/* accept a space before the multiplier */
-		if (end[0] == ' ' && end[1] != '\0')
-			++end;
-
-		switch (end[0]) {
-		case 'k': case 'K':
-			fval *= (uintmax_t)1 << 10;
-			++end;
-			break;
-		case 'm': case 'M':
-			fval *= (uintmax_t)1 << 20;
-			++end;
-			break;
-		case 'g': case 'G':
-			fval *= (uintmax_t)1 << 30;
-			++end;
-			break;
-		case 't': case 'T':
-			fval *= (uintmax_t)1 << 40;
-			++end;
-			break;
-		case 'p': case 'P':
-			fval *= (uintmax_t)1 << 50;
-			++end;
-			break;
-		case 'e': case 'E':
-			fval *= (uintmax_t)1 << 60;
-			++end;
-			break;
-		default:
-			break;
-		}
-
-		/* [bB] is a generic suffix of no effect */
-		if (end[0] == 'b' || end[0] == 'B')
-			end++;
-
-		if (end[0] != '\0')
-			return (err_invalid_suff);
-	}
-
-	*r = (uintmax_t)round(fval);
-	return (NULL);
-}
-
-#ifdef NUM_C_TEST
-/* Compile with: "cc -o foo -DNUM_C_TEST -I../.. -I../../include num.c -lm" */
-#include "vas.h"
-#include <math.h>
-#include <stdio.h>
-#include <string.h>
-
-struct test_case {
-	const char *str;
-	uintmax_t rel;
-	uintmax_t val;
-	const char *err;
-} test_cases[] = {
-	{ "1",			(uintmax_t)0,	(uintmax_t)1 },
-	{ "1B",			(uintmax_t)0,	(uintmax_t)1<<0 },
-	{ "1 B",		(uintmax_t)0,	(uintmax_t)1<<0 },
-	{ "1.3B",		(uintmax_t)0,	(uintmax_t)1 },
-	{ "1.7B",		(uintmax_t)0,	(uintmax_t)2 },
-
-	{ "1024",		(uintmax_t)0,	(uintmax_t)1024 },
-	{ "1k",			(uintmax_t)0,	(uintmax_t)1<<10 },
-	{ "1kB",		(uintmax_t)0,	(uintmax_t)1<<10 },
-	{ "1.3kB",		(uintmax_t)0,	(uintmax_t)1331 },
-	{ "1.7kB",		(uintmax_t)0,	(uintmax_t)1741 },
-
-	{ "1048576",		(uintmax_t)0,	(uintmax_t)1048576 },
-	{ "1M",			(uintmax_t)0,	(uintmax_t)1<<20 },
-	{ "1MB",		(uintmax_t)0,	(uintmax_t)1<<20 },
-	{ "1.3MB",		(uintmax_t)0,	(uintmax_t)1363149 },
-	{ "1.7MB",		(uintmax_t)0,	(uintmax_t)1782579 },
-
-	{ "1073741824",		(uintmax_t)0,	(uintmax_t)1073741824 },
-	{ "1G",			(uintmax_t)0,	(uintmax_t)1<<30 },
-	{ "1GB",		(uintmax_t)0,	(uintmax_t)1<<30 },
-	{ "1.3GB",		(uintmax_t)0,	(uintmax_t)1395864371 },
-	{ "1.7GB",		(uintmax_t)0,	(uintmax_t)1825361101 },
-
-	{ "1099511627776",	(uintmax_t)0,	(uintmax_t)1099511627776ULL },
-	{ "1T",			(uintmax_t)0,	(uintmax_t)1<<40 },
-	{ "1TB",		(uintmax_t)0,	(uintmax_t)1<<40 },
-	{ "1.3TB",		(uintmax_t)0,	(uintmax_t)1429365116109ULL },
-	{ "1.7TB",		(uintmax_t)0,	(uintmax_t)1869169767219ULL },
-
-	{ "1%",			(uintmax_t)1024,	(uintmax_t)10 },
-	{ "2%",			(uintmax_t)1024,	(uintmax_t)20 },
-	{ "3%",			(uintmax_t)1024,	(uintmax_t)31 },
-
-	/* Check the error checks */
-	{ "",			0,	0,	err_miss_num },
-	{ "m",			0,	0,	err_invalid_num },
-	{ "4%",			0,	0,	err_abs_req },
-	{ "3*",			0,	0,	err_invalid_suff },
-
-	/* TODO: add more */
-
-	{ 0, 0, 0 },
-};
-
-int
-main(int argc, char *argv[])
-{
-	struct test_case *tc;
-	uintmax_t val;
-	int ec;
-	const char *e;
-
-	(void)argc;
-	for (ec = 0, tc = test_cases; tc->str; ++tc) {
-		e = str2bytes(tc->str, &val, tc->rel);
-		if (e != tc->err) {
-			printf("%s: str2bytes(\"%s\", %ju) (%s) != (%s)\n",
-			    *argv, tc->str, tc->rel, tc->err, e);
-			++ec;
-		} else if (e == NULL && val != tc->val) {
-			printf("%s: str2bytes(\"%s\", %ju) %ju != %ju (%s)\n",
-			    *argv, tc->str, tc->rel, val, tc->val, e);
-			++ec;
-		}
-	}
-	/* TODO: test invalid strings */
-	if (!ec)
-		printf("OK\n");
-	return (ec > 0);
-}
-#endif
diff --git a/lib/libvarnish/vnum.c b/lib/libvarnish/vnum.c
new file mode 100644
index 0000000..7940e9b
--- /dev/null
+++ b/lib/libvarnish/vnum.c
@@ -0,0 +1,197 @@
+/*-
+ * Copyright (c) 2008-2009 Varnish Software AS
+ * All rights reserved.
+ *
+ * Author: Poul-Henning Kamp <phk at phk.freebsd.dk>
+ *
+ * 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.
+ *
+ * Deal with numbers with data storage suffix scaling
+ */
+
+#include "config.h"
+
+#include <math.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdint.h>
+
+#include "vnum.h"
+
+static const char err_miss_num[] = "Missing number";
+static const char err_invalid_num[] = "Invalid number";
+static const char err_abs_req[] = "Absolute number required";
+static const char err_invalid_suff[] = "Invalid suffix";
+
+const char *
+VNUM_2bytes(const char *p, uintmax_t *r, uintmax_t rel)
+{
+	double fval;
+	char *end;
+
+	if (p == NULL || *p == '\0')
+		return (err_miss_num);
+
+	fval = strtod(p, &end);
+	if (end == p || !isfinite(fval))
+		return (err_invalid_num);
+
+	if (*end == '\0') {
+		*r = (uintmax_t)fval;
+		return (NULL);
+	}
+
+	if (end[0] == '%' && end[1] == '\0') {
+		if (rel == 0)
+			return (err_abs_req);
+		fval *= rel / 100.0;
+	} else {
+		/* accept a space before the multiplier */
+		if (end[0] == ' ' && end[1] != '\0')
+			++end;
+
+		switch (end[0]) {
+		case 'k': case 'K':
+			fval *= (uintmax_t)1 << 10;
+			++end;
+			break;
+		case 'm': case 'M':
+			fval *= (uintmax_t)1 << 20;
+			++end;
+			break;
+		case 'g': case 'G':
+			fval *= (uintmax_t)1 << 30;
+			++end;
+			break;
+		case 't': case 'T':
+			fval *= (uintmax_t)1 << 40;
+			++end;
+			break;
+		case 'p': case 'P':
+			fval *= (uintmax_t)1 << 50;
+			++end;
+			break;
+		case 'e': case 'E':
+			fval *= (uintmax_t)1 << 60;
+			++end;
+			break;
+		default:
+			break;
+		}
+
+		/* [bB] is a generic suffix of no effect */
+		if (end[0] == 'b' || end[0] == 'B')
+			end++;
+
+		if (end[0] != '\0')
+			return (err_invalid_suff);
+	}
+
+	*r = (uintmax_t)round(fval);
+	return (NULL);
+}
+
+#ifdef NUM_C_TEST
+/* Compile with: "cc -o foo -DNUM_C_TEST -I../.. -I../../include num.c -lm" */
+#include "vas.h"
+#include <math.h>
+#include <stdio.h>
+#include <string.h>
+
+struct test_case {
+	const char *str;
+	uintmax_t rel;
+	uintmax_t val;
+	const char *err;
+} test_cases[] = {
+	{ "1",			(uintmax_t)0,	(uintmax_t)1 },
+	{ "1B",			(uintmax_t)0,	(uintmax_t)1<<0 },
+	{ "1 B",		(uintmax_t)0,	(uintmax_t)1<<0 },
+	{ "1.3B",		(uintmax_t)0,	(uintmax_t)1 },
+	{ "1.7B",		(uintmax_t)0,	(uintmax_t)2 },
+
+	{ "1024",		(uintmax_t)0,	(uintmax_t)1024 },
+	{ "1k",			(uintmax_t)0,	(uintmax_t)1<<10 },
+	{ "1kB",		(uintmax_t)0,	(uintmax_t)1<<10 },
+	{ "1.3kB",		(uintmax_t)0,	(uintmax_t)1331 },
+	{ "1.7kB",		(uintmax_t)0,	(uintmax_t)1741 },
+
+	{ "1048576",		(uintmax_t)0,	(uintmax_t)1048576 },
+	{ "1M",			(uintmax_t)0,	(uintmax_t)1<<20 },
+	{ "1MB",		(uintmax_t)0,	(uintmax_t)1<<20 },
+	{ "1.3MB",		(uintmax_t)0,	(uintmax_t)1363149 },
+	{ "1.7MB",		(uintmax_t)0,	(uintmax_t)1782579 },
+
+	{ "1073741824",		(uintmax_t)0,	(uintmax_t)1073741824 },
+	{ "1G",			(uintmax_t)0,	(uintmax_t)1<<30 },
+	{ "1GB",		(uintmax_t)0,	(uintmax_t)1<<30 },
+	{ "1.3GB",		(uintmax_t)0,	(uintmax_t)1395864371 },
+	{ "1.7GB",		(uintmax_t)0,	(uintmax_t)1825361101 },
+
+	{ "1099511627776",	(uintmax_t)0,	(uintmax_t)1099511627776ULL },
+	{ "1T",			(uintmax_t)0,	(uintmax_t)1<<40 },
+	{ "1TB",		(uintmax_t)0,	(uintmax_t)1<<40 },
+	{ "1.3TB",		(uintmax_t)0,	(uintmax_t)1429365116109ULL },
+	{ "1.7TB",		(uintmax_t)0,	(uintmax_t)1869169767219ULL },
+
+	{ "1%",			(uintmax_t)1024,	(uintmax_t)10 },
+	{ "2%",			(uintmax_t)1024,	(uintmax_t)20 },
+	{ "3%",			(uintmax_t)1024,	(uintmax_t)31 },
+
+	/* Check the error checks */
+	{ "",			0,	0,	err_miss_num },
+	{ "m",			0,	0,	err_invalid_num },
+	{ "4%",			0,	0,	err_abs_req },
+	{ "3*",			0,	0,	err_invalid_suff },
+
+	/* TODO: add more */
+
+	{ 0, 0, 0 },
+};
+
+int
+main(int argc, char *argv[])
+{
+	struct test_case *tc;
+	uintmax_t val;
+	int ec;
+	const char *e;
+
+	(void)argc;
+	for (ec = 0, tc = test_cases; tc->str; ++tc) {
+		e = VNUM_2bytes(tc->str, &val, tc->rel);
+		if (e != tc->err) {
+			printf("%s: VNUM_2bytes(\"%s\", %ju) (%s) != (%s)\n",
+			    *argv, tc->str, tc->rel, tc->err, e);
+			++ec;
+		} else if (e == NULL && val != tc->val) {
+			printf("%s: VNUM_2bytes(\"%s\", %ju) %ju != %ju (%s)\n",
+			    *argv, tc->str, tc->rel, val, tc->val, e);
+			++ec;
+		}
+	}
+	/* TODO: test invalid strings */
+	if (!ec)
+		printf("OK\n");
+	return (ec > 0);
+}
+#endif



More information about the varnish-commit mailing list