[patch] size optimization for mkswap.c

Bernhard Fischer rep.nop at aon.at
Thu Mar 16 05:29:49 PST 2006


Hi,

Attached two patches do
1) shrink mkswap a little bit
2) make v0 support optional.

   text    data     bss     dec     hex filename
   2093       8      28    2129     851 util-linux/mkswap.o.oorig
   2044       8      28    2080     820 util-linux/mkswap.o.shrink
   1698       4      28    1730     6c2 util-linux/mkswap.o.no-V0

Comments? Ok to apply?
-------------- next part --------------
--- bb.oorig/util-linux/mkswap.c	2006-03-16 14:16:58.000000000 +0100
+++ busybox/util-linux/mkswap.c	2006-03-16 12:22:22.000000000 +0100
@@ -213,10 +213,8 @@
 		}
 		page_ok(current_page++);
 	}
-	if (badpages == 1)
-		printf("one bad page\n");
-	else if (badpages > 1)
-		printf("%d bad pages\n", badpages);
+	if (badpages > 0)
+		printf("%d bad page%s\n", badpages, (badpages==1)?"":"s");
 }
 
 static long valid_offset(int fd, int offset)
@@ -279,31 +277,22 @@
 
 	init_signature_page();		/* get pagesize */
 
-	while (argc-- > 1) {
-		argv++;
-		if (argv[0][0] != '-') {
-			if (device_name) {
-				int blocks_per_page = pagesize / 1024;
-
-				PAGES = strtol(argv[0], &tmp, 0) / blocks_per_page;
-				if (*tmp)
-					bb_show_usage();
-			} else
-				device_name = argv[0];
-		} else {
-			switch (argv[0][1]) {
-			case 'c':
-				check = 1;
-				break;
-			case 'f':
-				force = 1;
-				break;
-			case 'v':
-				version = atoi(argv[0] + 2);
-				break;
-			default:
+	sz = bb_getopt_ulflags(argc, argv, "+cfv:", &tmp);
+	if (sz & BB_GETOPT_ERROR)
+		bb_show_usage();
+	version = atoi(tmp);
+	argv += optind;
+	argc -= optind;
+	while (argc--) {
+		if (device_name) {
+			int blocks_per_page = pagesize / 1024;
+
+			PAGES = strtol(argv[0], &tmp, 0) / blocks_per_page;
+			if (*tmp)
 				bb_show_usage();
-			}
+		} else {
+			device_name = argv[0];
+			argv++;
 		}
 	}
 	if (!device_name) {
-------------- next part --------------
--- bb.oorig/util-linux/Config.in	2006-03-13 18:21:11.000000000 +0100
+++ busybox/util-linux/Config.in	2006-03-16 13:04:13.000000000 +0100
@@ -288,6 +288,16 @@
 	  Once you have created swap space using 'mkswap' you need to enable
 	  the swap space using the 'swapon' utility.
 
+config CONFIG_FEATURE_MKSWAP_V0
+	bool "  version 0 support"
+	default n
+	depends on CONFIG_MKSWAP
+#	depends on CONFIG_MKSWAP && CONFIG_DEPRECATED
+	help
+	  Enable support for the old v0 style.
+	  If your kernel is older than 2.1.117, then v0 support is the
+	  only option.
+
 config CONFIG_MORE
 	bool "more"
 	default n
--- bb.oorig/util-linux/mkswap.c	2006-03-16 12:22:22.000000000 +0100
+++ busybox/util-linux/mkswap.c	2006-03-16 14:11:39.000000000 +0100
@@ -59,9 +59,13 @@
 static long PAGES = 0;
 static int check = 0;
 static int badpages = 0;
+#if ENABLE_FEATURE_MKSWAP_V0
 static int version = -1;
-
 #define MAKE_VERSION(p,q,r)	(65536*(p) + 256*(q) + (r))
+#else
+#define version 1
+#define MAKE_VERSION(p,q,r) 1
+#endif
 
 /*
  * The definition of the union swap_header uses the constant PAGE_SIZE.
@@ -74,7 +78,7 @@
 
 static struct swap_header_v1 {
 	char bootbits[1024];		/* Space for disklabel etc. */
-	unsigned int version;
+	unsigned int swap_version;
 	unsigned int last_page;
 	unsigned int nr_badpages;
 	unsigned int padding[125];
@@ -172,10 +176,9 @@
 	return (r & m) != 0;
 }
 
-
 static void page_ok(int page)
 {
-	if (version == 0)
+	if (ENABLE_FEATURE_MKSWAP_V0)
 		bit_set(signature_page, page);
 }
 
@@ -200,7 +203,7 @@
 	buffer = xmalloc(pagesize);
 	current_page = 0;
 	while (current_page < PAGES) {
-		if (!check) {
+		if (!check && version == 0) {
 			page_ok(current_page++);
 			continue;
 		}
@@ -280,7 +283,10 @@
 	sz = bb_getopt_ulflags(argc, argv, "+cfv:", &tmp);
 	if (sz & BB_GETOPT_ERROR)
 		bb_show_usage();
-	version = atoi(tmp);
+	USE_FEATURE_MKSWAP_V0(version = atoi(tmp);)
+	SKIP_FEATURE_MKSWAP_V0(if (00 && sz & 4)
+			bb_error_msg_and_die("No v0 support");)
+
 	argv += optind;
 	argc -= optind;
 	while (argc--) {
@@ -307,13 +313,14 @@
 				PAGES * (pagesize / 1024), sz * (pagesize / 1024));
 		return EXIT_FAILURE;
 	}
-
+#if ENABLE_FEATURE_MKSWAP_V0
 	if (version == -1) {
 		if (get_kernel_revision() < MAKE_VERSION(2, 1, 117))
 			version = 0;
 		else
 			version = 1;
 	}
+#endif
 	if (version != 0 && version != 1) {
 		bb_error_msg("error: unknown version %d", version);
 		bb_show_usage();
@@ -378,7 +385,7 @@
 	if (version == 0 && !bit_test_and_clear(signature_page, 0))
 		bb_error_msg_and_die("fatal: first page unreadable");
 	if (version == 1) {
-		p->version = version;
+		p->swap_version = version;
 		p->last_page = PAGES - 1;
 		p->nr_badpages = badpages;
 	}


More information about the busybox mailing list