public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH v1 1/1] lib/vsprintf: refactor duplicate code to xnumber()
@ 2015-12-28 18:18 Andy Shevchenko
  2015-12-28 18:25 ` Joe Perches
  0 siblings, 1 reply; 10+ messages in thread
From: Andy Shevchenko @ 2015-12-28 18:18 UTC (permalink / raw)
  To: Andrew Morton, Rasmus Villemoes, linux-kernel; +Cc: Andy Shevchenko

xnumber() is a special helper to print a fixed size type in a hex format with
'0x' prefix with padding and reduced size. In the module we have already
several copies of such code. Consolidate them under xnumber() helper.

There are couple of differences though.

It seems nobody cared about the output in case of CONFIG_KALLSYMS=n when
printing symbol address because the asked width is not enough to care either
prefix or last byte. Fixed here.

The %pNF specifier used to be allowed with a specific field width, though there
is neither any user of it nor mention in the documentation.

Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
---
 lib/vsprintf.c | 43 +++++++++++++++----------------------------
 1 file changed, 15 insertions(+), 28 deletions(-)

diff --git a/lib/vsprintf.c b/lib/vsprintf.c
index dcf5646..e971549 100644
--- a/lib/vsprintf.c
+++ b/lib/vsprintf.c
@@ -514,6 +514,16 @@ char *number(char *buf, char *end, unsigned long long num,
 	return buf;
 }
 
+static noinline_for_stack
+char *xnumber(char *buf, char *end, unsigned long long value, unsigned int type,
+	      struct printf_spec spec)
+{
+	spec.field_width = 2 + 2 * type;
+	spec.flags |= SPECIAL | SMALL | ZEROPAD;
+	spec.base = 16;
+	return number(buf, end, value, spec);
+}
+
 static void move_right(char *buf, char *end, unsigned len, unsigned spaces)
 {
 	size_t size;
@@ -649,11 +659,7 @@ char *symbol_string(char *buf, char *end, void *ptr,
 
 	return string(buf, end, sym, spec);
 #else
-	spec.field_width = 2 * sizeof(void *);
-	spec.flags |= SPECIAL | SMALL | ZEROPAD;
-	spec.base = 16;
-
-	return number(buf, end, value, spec);
+	return xnumber(buf, end, value, sizeof(void *), spec);
 #endif
 }
 
@@ -1318,36 +1324,20 @@ static
 char *netdev_feature_string(char *buf, char *end, const u8 *addr,
 		      struct printf_spec spec)
 {
-	spec.flags |= SPECIAL | SMALL | ZEROPAD;
-	if (spec.field_width == -1)
-		spec.field_width = 2 + 2 * sizeof(netdev_features_t);
-	spec.base = 16;
-
-	return number(buf, end, *(const netdev_features_t *)addr, spec);
+	return xnumber(buf, end, *(const netdev_features_t *)addr, sizeof(netdev_features_t), spec);
 }
 
 static noinline_for_stack
 char *address_val(char *buf, char *end, const void *addr,
 		  struct printf_spec spec, const char *fmt)
 {
-	unsigned long long num;
-
-	spec.flags |= SPECIAL | SMALL | ZEROPAD;
-	spec.base = 16;
-
 	switch (fmt[1]) {
 	case 'd':
-		num = *(const dma_addr_t *)addr;
-		spec.field_width = sizeof(dma_addr_t) * 2 + 2;
-		break;
+		return xnumber(buf, end, *(const dma_addr_t *)addr, sizeof(dma_addr_t), spec);
 	case 'p':
 	default:
-		num = *(const phys_addr_t *)addr;
-		spec.field_width = sizeof(phys_addr_t) * 2 + 2;
-		break;
+		return xnumber(buf, end, *(const phys_addr_t *)addr, sizeof(phys_addr_t), spec);
 	}
-
-	return number(buf, end, num, spec);
 }
 
 static noinline_for_stack
@@ -1366,10 +1356,7 @@ char *clock(char *buf, char *end, struct clk *clk, struct printf_spec spec,
 #ifdef CONFIG_COMMON_CLK
 		return string(buf, end, __clk_get_name(clk), spec);
 #else
-		spec.base = 16;
-		spec.field_width = sizeof(unsigned long) * 2 + 2;
-		spec.flags |= SPECIAL | SMALL | ZEROPAD;
-		return number(buf, end, (unsigned long)clk, spec);
+		return xnumber(buf, end, (unsigned long)clk, sizeof(unsigned long), spec);
 #endif
 	}
 }
-- 
2.6.4


^ permalink raw reply related	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2015-12-29 15:07 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-12-28 18:18 [PATCH v1 1/1] lib/vsprintf: refactor duplicate code to xnumber() Andy Shevchenko
2015-12-28 18:25 ` Joe Perches
2015-12-28 19:02   ` Andy Shevchenko
2015-12-29  0:18     ` Joe Perches
2015-12-28 21:42   ` Rasmus Villemoes
2015-12-28 22:20     ` Andy Shevchenko
2015-12-28 23:01       ` Rasmus Villemoes
2015-12-29 15:07       ` Andy Shevchenko
2015-12-28 22:20     ` Rasmus Villemoes
2015-12-28 22:29       ` Andy Shevchenko

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox