[PATCH] vsprintf: Sample use of struct vsp_buf

From: Petr Mladek
Date: Tue Sep 07 2021 - 05:31:22 EST


This is just partial replacement of [buf,end] couple with
struct vsp_buf.

The intention is to see how the code would look like. It does
not compile.
---
lib/vsprintf.c | 85 +++++++++++++++++++++-----------------------------
1 file changed, 35 insertions(+), 50 deletions(-)

diff --git a/lib/vsprintf.c b/lib/vsprintf.c
index 3bcb7be03f93..963c9212141d 100644
--- a/lib/vsprintf.c
+++ b/lib/vsprintf.c
@@ -446,8 +446,9 @@ static_assert(sizeof(struct printf_spec) == 8);
#define PRECISION_MAX ((1 << 15) - 1)

static noinline_for_stack
-char *number(char *buf, char *end, unsigned long long num,
- struct printf_spec spec)
+strcut vsp_buf *number(struct vsp_buf *vsp_buf,
+ unsigned long long num,
+ struct printf_spec spec)
{
/* put_dec requires 2-byte alignment of the buffer. */
char tmp[3 * sizeof(num)] __aligned(2);
@@ -506,68 +507,52 @@ char *number(char *buf, char *end, unsigned long long num,
/* printing 100 using %2d gives "100", not "00" */
if (i > precision)
precision = i;
+
/* leading space padding */
field_width -= precision;
if (!(spec.flags & (ZEROPAD | LEFT))) {
- while (--field_width >= 0) {
- if (buf < end)
- *buf = ' ';
- ++buf;
- }
+ while (--field_width >= 0)
+ vsp_putc(vsp_buf, ' ');
}
+
/* sign */
- if (sign) {
- if (buf < end)
- *buf = sign;
- ++buf;
- }
+ if (sign)
+ vsp_putc(vsp_buf, sign);
+
/* "0x" / "0" prefix */
if (need_pfx) {
- if (spec.base == 16 || !is_zero) {
- if (buf < end)
- *buf = '0';
- ++buf;
- }
+ if (spec.base == 16 || !is_zero)
+ vsp_putc(vps_buf, '0');
if (spec.base == 16) {
- if (buf < end)
- *buf = ('X' | locase);
- ++buf;
- }
+ vsp_putc(vps_buf, 'X' | locase);
}
+
/* zero or space padding */
if (!(spec.flags & LEFT)) {
char c = ' ' + (spec.flags & ZEROPAD);

- while (--field_width >= 0) {
- if (buf < end)
- *buf = c;
- ++buf;
- }
+ while (--field_width >= 0)
+ vsp_putc(vps_buf, c);
}
+
/* hmm even more zero padding? */
- while (i <= --precision) {
- if (buf < end)
- *buf = '0';
- ++buf;
- }
+ while (i <= --precision)
+ vsp_putc(vps_buf, '0');
+
/* actual digits of result */
- while (--i >= 0) {
- if (buf < end)
- *buf = tmp[i];
- ++buf;
- }
+ while (--i >= 0)
+ vsp_putc(vps_buf, tmp[i]);
+
/* trailing space padding */
- while (--field_width >= 0) {
- if (buf < end)
- *buf = ' ';
- ++buf;
- }
+ while (--field_width >= 0)
+ vsp_putc(vps_buf, ' ');

return buf;
}

static noinline_for_stack
-char *special_hex_number(char *buf, char *end, unsigned long long num, int size)
+ struct vsp_buf* *special_hex_number(struct vsp_buf *vsp_buf,
+ unsigned long long num, int size)
{
struct printf_spec spec;

@@ -577,7 +562,7 @@ char *special_hex_number(char *buf, char *end, unsigned long long num, int size)
spec.base = 16;
spec.precision = -1;

- return number(buf, end, num, spec);
+ return number(vsp_buf, num, spec);
}

static void move_right(char *buf, char *end, unsigned len, unsigned spaces)
@@ -646,14 +631,14 @@ static char *string_nocheck(char *buf, char *end, const char *s,
return widen_string(buf, len, end, spec);
}

-static char *err_ptr(char *buf, char *end, void *ptr,
+static struct vsp_buf *err_ptr(struct vsp_buf *vsp_buf, void *ptr,
struct printf_spec spec)
{
int err = PTR_ERR(ptr);
const char *sym = errname(err);

if (sym)
- return string_nocheck(buf, end, sym, spec);
+ return string_nocheck(vsp_buf, sym, spec);

/*
* Somebody passed ERR_PTR(-1234) or some other non-existing
@@ -662,7 +647,7 @@ static char *err_ptr(char *buf, char *end, void *ptr,
*/
spec.flags |= SIGN;
spec.base = 10;
- return number(buf, end, err, spec);
+ return number(vsp_buf, err, spec);
}

/* Be careful: error messages must fit into the given buffer. */
@@ -720,9 +705,9 @@ char *string(char *buf, char *end, const char *s,
return string_nocheck(buf, end, s, spec);
}

-static char *pointer_string(char *buf, char *end,
- const void *ptr,
- struct printf_spec spec)
+static vsp_buf *pointer_string(struct vsp_buf *vsp_buf,
+ const void *ptr,
+ struct printf_spec spec)
{
spec.base = 16;
spec.flags |= SMALL;
@@ -731,7 +716,7 @@ static char *pointer_string(char *buf, char *end,
spec.flags |= ZEROPAD;
}

- return number(buf, end, (unsigned long int)ptr, spec);
+ return number(vsp_buf, (unsigned long int)ptr, spec);
}

/* Make pointers available for printing early in the boot sequence. */
--
2.26.2