Improve sbi_console
v6 cover Improve sbi_console
v5 changes:
- PATCH12: fix printf("%%") missing USED_TBUF;
- add PATCH14, Fix invalid directive print
v4 changes:
- PATCH11: Simplify printc
- add PATCH13, handle special case
v3 changes:
- PATCH11: Simplify get info->type
- PATCH12: Change the way to keep USED_TBUF by and to reset it by use_tbuf
v2 changes:
- add patch 10-12
Xiang W (12):
lib: sbi: Fix how print gets flags
lib: sbi: print not fill '0' when left-aligned
lib: sbi: Add '+' flags for print
lib: sbi: Add ' ' '\'' flags for print
lib: sbi: implifying the parameters of printi
lib: sbi: print add 'o' type
lib: sbi: Fix printi
lib: sbi: Simplify prints
lib: sbi: sbi_console.c code re-layout
lib: sbi: Add print_info for sbi_console.c
lib: sbi: Fix timing of clearing tbuf
lib: sbi: Fix missing '\0' when buffer szie equal 1
lib/sbi/sbi_console.c | 413 ++++++++++++++++++++++-------------------- 1 file changed, 212 insertions(+), 201 deletions(-)
Signature · 1 line
2.39.2
PATCH v6 1/12 lib: sbi: Fix how print gets flags
The flags for print should be able to appear in any order. The previous code required the order to be fixed.
--- lib/sbi/sbi_console.c | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-)
lib/sbi/sbi_console.c +21 -12
@@ -236,6 +236,7 @@ static int printi(char **out, u32 *out_len, long long i, int b, int sg,static int char **out,u32 *out_len,const char *format,va_list args) { +bool flags_done;int width, flags, pc =0 ;char scr[2 ], *tout;bool use_tbuf = (!out) ? true : false;
@@ -262,25 +263,33 @@ static int print(char **out, u32 *out_len, const char *format, va_list args)if (*format =='%' ) { ++format; - width = flags =0 ;if (*format =='\0' )break ;if (*format =='%' ) goto literal;/* Get flags */ -if (*format =='-' ) { - ++format; - flags =PAD_RIGHT ; - } -if (*format =='#' ) { - ++format; - flags |=PAD_ALTERNATE ; - } -while (*format =='0' ) { - ++format; - flags |=PAD_ZERO ; + flags =0 ; + flags_done = false; +while (!flags_done) { +switch (*format) { +case '-' : + flags |=PAD_RIGHT ; +break ; +case '#' : + flags |=PAD_ALTERNATE ; +break ; +case '0' : + flags |=PAD_ZERO ; +break ; +default : + flags_done = true; +break ; + } +if (!flags_done) + ++format; }/* Get width */ + width =0 ;for (; *format >='0' && *format <='9' ; ++format) { width *=10 ; width += *format -'0' ;
Signature · 1 line
2.39.2
PATCH v6 1/12 lib: sbi: Fix how print gets flags
Quoted 22 lines · On Mon, Jun 12, 2023 at 1:40?PM Xiang W <[email protected]> wrote:
The flags for print should be able to appear in any order. The previous code required the order to be fixed.
Signed-off-by Xiang W <[email protected]>--- lib/sbi/sbi_console.c | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-)lib/sbi/sbi_console.c +1
-0@@ -236,6 +236,7 @@ static int printi(char **out, u32 *out_len, long long i, int b, int sg,static int char **out,u32 *out_len,const char *format,va_list args) { +bool flags_done;int width, flags, pc =0 ;char scr[2 ], *tout;bool use_tbuf = (!out) ? true : false;
lib/sbi/sbi_console.c static int print(char **out, u32 *out_len, const char *format, va_list args) · line 265
Quoted 4 lines
lib/sbi/sbi_console.c +0
-1@@ -262,25 +263,33 @@ static int print(char **out, u32 *out_len, const char *format, va_list args)if (*format =='%' ) { ++format; - width = flags =0 ;
Why do you need to split this line into two statements ?
This is a totally unrelated change.
Quoted 42 lines
lib/sbi/sbi_console.c +20
-12if (*format =='\0' )break ;if (*format =='%' ) goto literal;/* Get flags */ -if (*format =='-' ) { - ++format; - flags =PAD_RIGHT ; - } -if (*format =='#' ) { - ++format; - flags |=PAD_ALTERNATE ; - } -while (*format =='0' ) { - ++format; - flags |=PAD_ZERO ; + flags =0 ; + flags_done = false; +while (!flags_done) { +switch (*format) { +case '-' : + flags |=PAD_RIGHT ; +break ; +case '#' : + flags |=PAD_ALTERNATE ; +break ; +case '0' : + flags |=PAD_ZERO ; +break ; +default : + flags_done = true; +break ; + } +if (!flags_done) + ++format; }/* Get width */ + width =0 ;for (; *format >='0' && *format <='9' ; ++format) { width *=10 ; width += *format -'0' ; --
Quoted 2 lines
2.39.2
Regards,
Anup
PATCH v6 1/12 lib: sbi: Fix how print gets flags
? 2023-07-04???? 09:25 +0530?Anup Patel???
Quoted 24 lines · On Mon, Jun 12, 2023 at 1:40?PM Xiang W <[email protected]> wrote:
Quoted 23 lines
The flags for print should be able to appear in any order. The previous code required the order to be fixed.
Signed-off-by Xiang W <[email protected]>---
?lib/sbi/sbi_console.c | 33 +++++++++++++++++++++------------ ?1 file changed, 21 insertions(+), 12 deletions(-)lib/sbi/sbi_console.c +1
-0@@ -236,6 +236,7 @@ static int printi(char **out, u32 *out_len, long long i, int b, int sg, ?static int print(char **out, u32 *out_len, const char *format, va_list args) ?{ +?????? bool flags_done; ??????? int width, flags, pc = 0; ??????? char scr[2], *tout; ??????? bool use_tbuf = (!out) ? true : false;@@ -262,25 +263,33 @@ static int print(char **out, u32 *out_len, const char *format, va_list args)
Quoted 7 lines
lib/sbi/sbi_console.c static int print(char **out, u32 *out_len, const char *format, va_list args) · line 265
Quoted 3 lines
lib/sbi/sbi_console.c +0
-1??????????????? if (*format == '%') { ??????????????????????? ++format; -?????????????????????? width = flags = 0;Why do you need to split this line into two statements ?
This is a totally unrelated change.
Putting initialization and parsing together makes the code clearer
Regards,
Xiang W
Quoted 48 lines
Quoted 36 lines
lib/sbi/sbi_console.c +0
-0??????????????????????? if (*format == '\0') ??????????????????????????????? break; ??????????????????????? if (*format == '%') ??????????????????????????????? goto literal;??????????????????????? /* Get flags */ -?????????????????????? if (*format == '-') { -?????????????????????????????? ++format; -?????????????????????????????? flags =
PAD_RIGHT; -?????????????????????? }
-?????????????????????? if (*format == '#') { -?????????????????????????????? ++format; -?????????????????????????????? flags |=PAD_ALTERNATE; -?????????????????????? }
-?????????????????????? while (*format == '0') { -?????????????????????????????? ++format; -?????????????????????????????? flags |=PAD_ZERO; +?????????????????????? flags = 0; +??????????????????????flags_done= false; +?????????????????????? while (!flags_done) { +?????????????????????????????? switch (*format) { +?????????????????????????????? case '-':
+?????????????????????????????????????? flags |=PAD_RIGHT; +?????????????????????????????????????? break; +?????????????????????????????? case '#':
+?????????????????????????????????????? flags |=PAD_ALTERNATE; +?????????????????????????????????????? break; +?????????????????????????????? case '0':
+?????????????????????????????????????? flags |=PAD_ZERO; +?????????????????????????????????????? break; +?????????????????????????????? default:
+??????????????????????????????????????flags_done= true; +?????????????????????????????????????? break; +?????????????????????????????? } +?????????????????????????????? if (!flags_done) +?????????????????????????????????????? ++format;Quoted 9 lines
??????????????????????? }
??????????????????????? /* Get width */
+?????????????????????? width = 0;
??????????????????????? for (; *format >= '0' && *format <= '9'; ++format) { ??????????????????????????????? width *= 10; ??????????????????????????????? width += *format - '0';
--
2.39.2Regards,
Anup
PATCH v6 2/12 lib: sbi: print not fill '0' when left-aligned
In reply to Xiang WLeft alignment and padding '0' should not exist at the same time, this patch is not padded with 0 when left aligned.
--- lib/sbi/sbi_console.c | 2 ++ 1 file changed, 2 insertions(+)
lib/sbi/sbi_console.c +2 -0
@@ -288,6 +288,8 @@ static int print(char **out, u32 *out_len, const char *format, va_list args)if (!flags_done) ++format; } +if (flags &PAD_RIGHT ) + flags &= ~PAD_ZERO ;/* Get width */ width =0 ;for (; *format >='0' && *format <='9' ; ++format) {
Signature · 1 line
2.39.2
PATCH v6 2/12 lib: sbi: print not fill '0' when left-aligned
Quoted 3 lines · On Mon, Jun 12, 2023 at 1:40?PM Xiang W <[email protected]> wrote:
Left alignment and padding '0' should not exist at the same time, this patch is not padded with 0 when left aligned.
s/this patch is not padded with/this patch skips padding/
Quoted 20 lines
Signed-off-by Xiang W <[email protected]>--- lib/sbi/sbi_console.c | 2 ++ 1 file changed, 2 insertions(+)lib/sbi/sbi_console.c +2
-1@@ -288,6 +288,8 @@ static int print(char **out, u32 *out_len, const char *format, va_list args)if (!flags_done) ++format; } +if (flags &PAD_RIGHT ) + flags &= ~PAD_ZERO ;/* Get width */ width =0 ;for (; *format >='0' && *format <='9' ; ++format) { --
Quoted 2 lines
2.39.2
Otherwise it looks good to me.
Regards,
Anup
PATCH v6 2/12 lib: sbi: print not fill '0' when left-aligned
? 2023-07-04???? 09:34 +0530?Anup Patel???
Quoted 6 lines · On Mon, Jun 12, 2023 at 1:40?PM Xiang W <[email protected]> wrote:
Quoted 3 lines
Left alignment and padding '0' should not exist at the same time, this patch is not padded with 0 when left aligned.
s/this patch is not padded with/this patch skips padding/
Will be done in next patch.
Regards?
Xiang W
Quoted 30 lines
Quoted 17 lines
Signed-off-by Xiang W <[email protected]>---
?lib/sbi/sbi_console.c | 2 ++
?1 file changed, 2 insertions(+)lib/sbi/sbi_console.c +2
-0@@ -288,6 +288,8 @@ static int print(char **out, u32 *out_len, const char *format, va_list args) ??????????????????????????????? if (!flags_done) ??????????????????????????????????????? ++format; ??????????????????????? } +?????????????????????? if (flags & PAD_RIGHT) +?????????????????????????????? flags &= ~PAD_ZERO;Quoted 6 lines
??????????????????????? /* Get width */
??????????????????????? width = 0;
??????????????????????? for (; *format >= '0' && *format <= '9'; ++format) { --
2.39.2Otherwise it looks good to me.
Reviewed-by Anup Patel <[email protected]>Regards,
Anup
PATCH v6 3/12 lib: sbi: Add '+' flags for print
In reply to Xiang W--- lib/sbi/sbi_console.c | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-)
lib/sbi/sbi_console.c +16 -9
@@ -120,6 +120,7 @@ unsigned long sbi_ngets(char *str, unsigned long len)#define PAD_RIGHT 1#define PAD_ZERO 2#define PAD_ALTERNATE 4 +#define PAD_SIGN 8#define PRINT_BUF_LEN 64#define va_start (v, l) __builtin_va_start((v), l)
@@ -186,15 +187,18 @@ static int prints(char **out, u32 *out_len, const char *string, int width,static int printi (char **out,u32 *out_len,long long i,int b,int sg,int width,int flags,int letbase) { -char print_buf[PRINT_BUF_LEN ]; -char *s; -int neg =0 , pc =0 ; +char *s, sign, print_buf[PRINT_BUF_LEN ]; +int pc =0 ;u64 t;unsigned long long u = i; -if (sg && b ==10 && i <0 ) { - neg =1 ; - u = -i; +if (sg && b ==10 ) { +if ((flags &PAD_SIGN ) && i >0 ) + sign ='+' ; +if (i <0 ) { + sign ='-' ; + u = -i; + } } s = print_buf +PRINT_BUF_LEN -1 ;
@@ -221,13 +225,13 @@ static int printi(char **out, u32 *out_len, long long i, int b, int sg, *--s ='0' ; } -if (neg) { +if (sign) {if (width && (flags &PAD_ZERO )) { -printc (out, out_len,'-' ); +printc (out, out_len, sign); ++pc; --width; }else { - *--s ='-' ; + *--s = sign; } }
@@ -275,6 +279,9 @@ static int print(char **out, u32 *out_len, const char *format, va_list args)case '-' : flags |=PAD_RIGHT ;break ; +case '+' : + flags |=PAD_SIGN ; +break ;case '#' : flags |=PAD_ALTERNATE ;break ;
Signature · 1 line
2.39.2
PATCH v6 3/12 lib: sbi: Add '+' flags for print
On Mon, Jun 12, 2023 at 1:40?PM Xiang W <[email protected]> wrote:
No patch description ?
Quoted 71 lines
Signed-off-by Xiang W <[email protected]>--- lib/sbi/sbi_console.c | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-)lib/sbi/sbi_console.c +16
-10@@ -120,6 +120,7 @@ unsigned long sbi_ngets(char *str, unsigned long len)#define PAD_RIGHT 1#define PAD_ZERO 2#define PAD_ALTERNATE 4 +#define PAD_SIGN 8#define PRINT_BUF_LEN 64#define va_start (v, l) __builtin_va_start((v), l)@@ -186,15 +187,18 @@ static int prints(char **out, u32 *out_len, const char *string, int width,static int printi (char **out,u32 *out_len,long long i,int b,int sg,int width,int flags,int letbase) { -char print_buf[PRINT_BUF_LEN ]; -char *s; -int neg =0 , pc =0 ; +char *s, sign, print_buf[PRINT_BUF_LEN ]; +int pc =0 ;u64 t;unsigned long long u = i; -if (sg && b ==10 && i <0 ) { - neg =1 ; - u = -i; +if (sg && b ==10 ) { +if ((flags &PAD_SIGN ) && i >0 ) + sign ='+' ; +if (i <0 ) { + sign ='-' ; + u = -i; + } } s = print_buf +PRINT_BUF_LEN -1 ;@@ -221,13 +225,13 @@ static int printi(char **out, u32 *out_len, long long i, int b, int sg, *--s ='0' ; } -if (neg) { +if (sign) {if (width && (flags &PAD_ZERO )) { -printc (out, out_len,'-' ); +printc (out, out_len, sign); ++pc; --width; }else { - *--s ='-' ; + *--s = sign; } }@@ -275,6 +279,9 @@ static int print(char **out, u32 *out_len, const char *format, va_list args)case '-' : flags |=PAD_RIGHT ;break ; +case '+' : + flags |=PAD_SIGN ; +break ;case '#' : flags |=PAD_ALTERNATE ;break ; --
Quoted 2 lines
2.39.2
Otherwise, it looks good to me.
Regards,
Anup
PATCH v6 3/12 lib: sbi: Add '+' flags for print
? 2023-07-04???? 09:38 +0530?Anup Patel???
Quoted 3 lines · On Mon, Jun 12, 2023 at 1:40?PM Xiang W <[email protected]> wrote:
No patch description ?
Will be done in next patch.
Regards?
Xiang W
Quoted 81 lines
Quoted 65 lines
Signed-off-by Xiang W <[email protected]>---
?lib/sbi/sbi_console.c | 25 ++++++++++++++++--------- ?1 file changed, 16 insertions(+), 9 deletions(-)lib/sbi/sbi_console.c +0
-0@@ -120,6 +120,7 @@ unsigned long sbi_ngets(char *str, unsigned long len)?#define
PAD_RIGHT1
?#definePAD_ZERO2
?#definePAD_ALTERNATE4
+#definePAD_SIGN8
?#definePRINT_BUF_LEN64?#define
va_start(v, l)__builtin_va_start((v), l)lib/sbi/sbi_console.c +12
-9@@ -186,15 +187,18 @@ static int prints(char **out, u32 *out_len, const char *string, int width, ?static int printi(char **out, u32 *out_len, long long i, int b, int sg, ????????????????? int width, int flags, int letbase) ?{ -?????? char print_buf[PRINT_BUF_LEN]; -?????? char *s; -?????? int neg = 0, pc = 0; +?????? char *s, sign, print_buf[PRINT_BUF_LEN]; +?????? int pc = 0; ??????? u64 t; ??????? unsigned long long u = i; -?????? if (sg && b == 10 && i < 0) { -?????????????? neg = 1; -?????????????? u?? = -i; +?????? if (sg && b == 10) { +?????????????? if ((flags & PAD_SIGN) && i > 0) +?????????????????????? sign = '+'; +?????????????? if (i < 0) { +?????????????????????? sign = '-'; +?????????????????????? u = -i; +?????????????? } ??????? } ??????? s? = print_buf + PRINT_BUF_LEN - 1;@@ -221,13 +225,13 @@ static int printi(char **out, u32 *out_len, long long i, int b, int sg, ??????????????? *--s = '0'; ??????? } -?????? if (neg) { +?????? if (sign) { ??????????????? if (width && (flags & PAD_ZERO)) { -?????????????????????? printc(out, out_len, '-'); +?????????????????????? printc(out, out_len, sign); ??????????????????????? ++pc; ??????????????????????? --width; ??????????????? } else { -?????????????????????? *--s = '-'; +?????????????????????? *--s = sign; ??????????????? } ??????? }@@ -275,6 +279,9 @@ static int print(char **out, u32 *out_len, const char *format, va_list args)??????????????????????????????? case '-':
??????????????????????????????????????? flags |=PAD_RIGHT; ??????????????????????????????????????? break;Quoted 9 lines
+?????????????????????????????? case '+':
+?????????????????????????????????????? flags |=PAD_SIGN; +?????????????????????????????????????? break; ??????????????????????????????? case '#':
??????????????????????????????????????? flags |=PAD_ALTERNATE; ??????????????????????????????????????? break;
--
2.39.2Otherwise, it looks good to me.
Reviewed-by Anup Patel <[email protected]>Regards,
Anup
PATCH v6 4/12 lib: sbi: Add ' ' '\'' flags for print
In reply to Xiang W--- lib/sbi/sbi_console.c | 4 ++++ 1 file changed, 4 insertions(+)
lib/sbi/sbi_console.c +4 -0
@@ -288,6 +288,10 @@ static int print(char **out, u32 *out_len, const char *format, va_list args)case '0' : flags |=PAD_ZERO ;break ; +case ' ' : +case '\'' : +/* Ignored flags, do nothing */ +break ;default : flags_done = true;break ;
Signature · 1 line
2.39.2
PATCH v6 4/12 lib: sbi: Add ' ' '\'' flags for print
On Mon, Jun 12, 2023 at 1:41?PM Xiang W <[email protected]> wrote:
No patch description ?
Quoted 22 lines
Signed-off-by Xiang W <[email protected]>--- lib/sbi/sbi_console.c | 4 ++++ 1 file changed, 4 insertions(+)lib/sbi/sbi_console.c +4
-1@@ -288,6 +288,10 @@ static int print(char **out, u32 *out_len, const char *format, va_list args)case '0' : flags |=PAD_ZERO ;break ; +case ' ' : +case '\'' : +/* Ignored flags, do nothing */ +break ;default : flags_done = true;break ; --
Quoted 2 lines
2.39.2
Otherwise, it looks good to me.
Regards,
Anup
PATCH v6 4/12 lib: sbi: Add ' ' '\'' flags for print
? 2023-07-04???? 09:54 +0530?Anup Patel???
Quoted 3 lines · On Mon, Jun 12, 2023 at 1:41?PM Xiang W <[email protected]> wrote:
No patch description ?
Will be done in next patch.
Regards?
Xiang W
Quoted 32 lines
Quoted 16 lines
Signed-off-by Xiang W <[email protected]>---
?lib/sbi/sbi_console.c | 4 ++++
?1 file changed, 4 insertions(+)lib/sbi/sbi_console.c +0
-0@@ -288,6 +288,10 @@ static int print(char **out, u32 *out_len, const char *format, va_list args)??????????????????????????????? case '0':
??????????????????????????????????????? flags |=PAD_ZERO; ??????????????????????????????????????? break; +?????????????????????????????? case ' ':Quoted 9 lines
+?????????????????????????????? case '\'':
+?????????????????????????????????????? /* Ignored flags, do nothing */ +?????????????????????????????????????? break; ??????????????????????????????? default:
???????????????????????????????????????flags_done= true; ??????????????????????????????????????? break;
--
2.39.2Otherwise, it looks good to me.
Reviewed-by Anup Patel <[email protected]>Regards,
Anup
PATCH v6 5/12 lib: sbi: implifying the parameters of printi
In reply to Xiang WThe information of sg/b/letbase can be obtained by the type character, simplifying the parameter by passing the type directly.
--- lib/sbi/sbi_console.c | 127 +++++++++++++++++------------------------- 1 file changed, 50 insertions(+), 77 deletions(-)
lib/sbi/sbi_console.c +50 -77
@@ -184,15 +184,26 @@ static int prints(char **out, u32 *out_len, const char *string, int width,return pc; } -static int printi (char **out,u32 *out_len,long long i,int b,int sg, -int width,int flags,int letbase) +static int printi (char **out,u32 *out_len,long long i, +int width,int flags,int type) { -char *s, sign, print_buf[PRINT_BUF_LEN ];int pc =0 ; -u64 t; -unsigned long long u = i; +char *s, sign, letbase, print_buf[PRINT_BUF_LEN ]; +unsigned long long u, b, t; + + b =10 ; + letbase ='a' ; +if (type =='o' ) + b =8 ; +else if (type =='x' || type =='X' || type =='p' || type =='P' ) { + b =16 ; + letbase &= ~0x20 ; + letbase |= type &0x20 ; + } -if (sg && b ==10 ) { + u = i; + sign =0 ; +if (type =='i' || type =='d' ) {if ((flags &PAD_SIGN ) && i >0 ) sign ='+' ;if (i <0 ) {
@@ -242,9 +253,8 @@ static int print(char **out, u32 *out_len, const char *format, va_list args) {bool flags_done;int width, flags, pc =0 ; -char scr[2 ], *tout; +char type, scr[2 ], *tout;bool use_tbuf = (!out) ? true : false; -unsigned long long tmp;/* * The console_tbuf is protected by console_out_lock and
@@ -315,83 +325,46 @@ static int print(char **out, u32 *out_len, const char *format, va_list args) }if ((*format =='d' ) || (*format =='i' )) { pc +=printi (out, out_len,va_arg (args, int), -10 ,1 , width, flags,'0' ); -continue ; - } -if (*format =='x' ) { - pc +=printi (out, out_len, - va_arg(args, unsigned int), 16, 0, - width, flags,'a' ); + width, flags, *format);continue ; } -if (*format =='X' ) { - pc +=printi (out, out_len, - va_arg(args, unsigned int), 16, 0, - width, flags,'A' ); +if ((*format =='u' ) || (*format =='x' ) || (*format =='X' )) { + pc += printi(out, out_len, va_arg(args, unsigned int), + width, flags, *format);continue ; } -if (*format =='u' ) { - pc +=printi (out, out_len, - va_arg(args, unsigned int), 10, 0, - width, flags,'a' ); +if ((*format =='p' ) || (*format =='P' )) { + pc += printi(out, out_len, (uintptr_t)va_arg(args, void*), + width, flags, *format);continue ; } -if (*format =='p' ) { - pc +=printi (out, out_len, - va_arg(args, unsigned long), 16, 0, - width, flags,'a' ); -continue ; - } -if (*format =='P' ) { - pc +=printi (out, out_len, - va_arg(args, unsigned long), 16, 0, - width, flags,'A' ); -continue ; - } -if (*format =='l' && *(format +1 ) =='l' ) { - tmp = va_arg(args, unsigned long long); -if (*(format +2 ) =='u' ) { - format +=2 ; - pc +=printi (out, out_len, tmp,10 ,0 , - width, flags,'a' ); - }else if (*(format +2 ) =='x' ) { - format +=2 ; - pc +=printi (out, out_len, tmp,16 ,0 , - width, flags,'a' ); - }else if (*(format +2 ) =='X' ) { - format +=2 ; - pc +=printi (out, out_len, tmp,16 ,0 , - width, flags,'A' ); - }else { - format +=1 ; - pc +=printi (out, out_len, tmp,10 ,1 , - width, flags,'0' ); +if (*format =='l' ) { + type ='i' ; +if (format[1 ] =='l' ) { + ++format; +if ((format[1 ] =='u' ) + || (format[1 ] =='d' ) || (format[1 ] =='i' ) + || (format[1 ] =='x' ) || (format[1 ] =='X' )) { + ++format; + type = *format; + } + pc += printi(out, out_len, va_arg(args, long long), + width, flags, type); +continue ; } -continue ; - }else if (*format =='l' ) { -if (*(format +1 ) =='u' ) { - format +=1 ; - pc +=printi ( - out, out_len, - va_arg(args, unsigned long), 10, -0 , width, flags,'a' ); - }else if (*(format +1 ) =='x' ) { - format +=1 ; - pc +=printi ( - out, out_len, - va_arg(args, unsigned long), 16, -0 , width, flags,'a' ); - }else if (*(format +1 ) =='X' ) { - format +=1 ; - pc +=printi ( - out, out_len, - va_arg(args, unsigned long), 16, -0 , width, flags,'A' ); - }else { - pc +=printi (out, out_len, -va_arg (args, long),10 ,1 , - width, flags,'0' ); +if ((format[1 ] =='u' ) + || (format[1 ] =='d' ) || (format[1 ] =='i' ) + || (format[1 ] =='x' ) || (format[1 ] =='X' )) { + ++format; + type = *format; } +if ((type =='d' ) || (type =='i' )) + pc +=printi (out, out_len,va_arg (args, long), + width, flags, type); +else + pc += printi(out, out_len, va_arg(args, unsigned long), + width, flags, type); +continue ; }if (*format =='c' ) {/* char are converted to int then pushed on the stack */
Signature · 1 line
2.39.2
PATCH v6 5/12 lib: sbi: implifying the parameters of printi
Quoted 5 lines · On Mon, Jun 12, 2023 at 1:40?PM Xiang W <[email protected]> wrote:
The information of sg/b/letbase can be obtained by the type character, simplifying the parameter by passing the type directly.
Signed-off-by Xiang W <[email protected]>
Looks good to me.
Regards,
Anup
Quoted 171 lines
--- lib/sbi/sbi_console.c | 127 +++++++++++++++++------------------------- 1 file changed, 50 insertions(+), 77 deletions(-)lib/sbi/sbi_console.c +50
-78@@ -184,15 +184,26 @@ static int prints(char **out, u32 *out_len, const char *string, int width,return pc; } -static int printi (char **out,u32 *out_len,long long i,int b,int sg, -int width,int flags,int letbase) +static int printi (char **out,u32 *out_len,long long i, +int width,int flags,int type) { -char *s, sign, print_buf[PRINT_BUF_LEN ];int pc =0 ; -u64 t; -unsigned long long u = i; +char *s, sign, letbase, print_buf[PRINT_BUF_LEN ]; +unsigned long long u, b, t; + + b =10 ; + letbase ='a' ; +if (type =='o' ) + b =8 ; +else if (type =='x' || type =='X' || type =='p' || type =='P' ) { + b =16 ; + letbase &= ~0x20 ; + letbase |= type &0x20 ; + } -if (sg && b ==10 ) { + u = i; + sign =0 ; +if (type =='i' || type =='d' ) {if ((flags &PAD_SIGN ) && i >0 ) sign ='+' ;if (i <0 ) {@@ -242,9 +253,8 @@ static int print(char **out, u32 *out_len, const char *format, va_list args) {bool flags_done;int width, flags, pc =0 ; -char scr[2 ], *tout; +char type, scr[2 ], *tout;bool use_tbuf = (!out) ? true : false; -unsigned long long tmp;/* * The console_tbuf is protected by console_out_lock and @@ -315,83 +325,46 @@ static int print(char **out, u32 *out_len, const char *format, va_list args) }if ((*format =='d' ) || (*format =='i' )) { pc +=printi (out, out_len,va_arg (args, int), -10 ,1 , width, flags,'0' ); -continue ; - } -if (*format =='x' ) { - pc +=printi (out, out_len, - va_arg(args, unsigned int), 16, 0, - width, flags,'a' ); + width, flags, *format);continue ; } -if (*format =='X' ) { - pc +=printi (out, out_len, - va_arg(args, unsigned int), 16, 0, - width, flags,'A' ); +if ((*format =='u' ) || (*format =='x' ) || (*format =='X' )) { + pc += printi(out, out_len, va_arg(args, unsigned int), + width, flags, *format);continue ; } -if (*format =='u' ) { - pc +=printi (out, out_len, - va_arg(args, unsigned int), 10, 0, - width, flags,'a' ); +if ((*format =='p' ) || (*format =='P' )) { + pc += printi(out, out_len, (uintptr_t)va_arg(args, void*), + width, flags, *format);continue ; } -if (*format =='p' ) { - pc +=printi (out, out_len, - va_arg(args, unsigned long), 16, 0, - width, flags,'a' ); -continue ; - } -if (*format =='P' ) { - pc +=printi (out, out_len, - va_arg(args, unsigned long), 16, 0, - width, flags,'A' ); -continue ; - } -if (*format =='l' && *(format +1 ) =='l' ) { - tmp = va_arg(args, unsigned long long); -if (*(format +2 ) =='u' ) { - format +=2 ; - pc +=printi (out, out_len, tmp,10 ,0 , - width, flags,'a' ); - }else if (*(format +2 ) =='x' ) { - format +=2 ; - pc +=printi (out, out_len, tmp,16 ,0 , - width, flags,'a' ); - }else if (*(format +2 ) =='X' ) { - format +=2 ; - pc +=printi (out, out_len, tmp,16 ,0 , - width, flags,'A' ); - }else { - format +=1 ; - pc +=printi (out, out_len, tmp,10 ,1 , - width, flags,'0' ); +if (*format =='l' ) { + type ='i' ; +if (format[1 ] =='l' ) { + ++format; +if ((format[1 ] =='u' ) + || (format[1 ] =='d' ) || (format[1 ] =='i' ) + || (format[1 ] =='x' ) || (format[1 ] =='X' )) { + ++format; + type = *format; + } + pc += printi(out, out_len, va_arg(args, long long), + width, flags, type); +continue ; } -continue ; - }else if (*format =='l' ) { -if (*(format +1 ) =='u' ) { - format +=1 ; - pc +=printi ( - out, out_len, - va_arg(args, unsigned long), 10, -0 , width, flags,'a' ); - }else if (*(format +1 ) =='x' ) { - format +=1 ; - pc +=printi ( - out, out_len, - va_arg(args, unsigned long), 16, -0 , width, flags,'a' ); - }else if (*(format +1 ) =='X' ) { - format +=1 ; - pc +=printi ( - out, out_len, - va_arg(args, unsigned long), 16, -0 , width, flags,'A' ); - }else { - pc +=printi (out, out_len, -va_arg (args, long),10 ,1 , - width, flags,'0' ); +if ((format[1 ] =='u' ) + || (format[1 ] =='d' ) || (format[1 ] =='i' ) + || (format[1 ] =='x' ) || (format[1 ] =='X' )) { + ++format; + type = *format; } +if ((type =='d' ) || (type =='i' )) + pc +=printi (out, out_len,va_arg (args, long), + width, flags, type); +else + pc += printi(out, out_len, va_arg(args, unsigned long), + width, flags, type); +continue ; }if (*format =='c' ) {/* char are converted to int then pushed on the stack */ --2.39.2
PATCH v6 6/12 lib: sbi: print add 'o' type
In reply to Xiang W--- lib/sbi/sbi_console.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
lib/sbi/sbi_console.c +4 -3
@@ -328,7 +328,8 @@ static int print(char **out, u32 *out_len, const char *format, va_list args) width, flags, *format);continue ; } -if ((*format =='u' ) || (*format =='x' ) || (*format =='X' )) { +if ((*format =='u' ) || (*format =='o' ) + || (*format =='x' ) || (*format =='X' )) { pc += printi(out, out_len, va_arg(args, unsigned int), width, flags, *format);continue ;
@@ -342,7 +343,7 @@ static int print(char **out, u32 *out_len, const char *format, va_list args) type ='i' ;if (format[1 ] =='l' ) { ++format; -if ((format[1 ] =='u' ) +if ((format[1 ] =='u' ) || (format[1 ] =='o' ) || (format[1 ] =='d' ) || (format[1 ] =='i' ) || (format[1 ] =='x' ) || (format[1 ] =='X' )) { ++format;
@@ -352,7 +353,7 @@ static int print(char **out, u32 *out_len, const char *format, va_list args) width, flags, type);continue ; } -if ((format[1 ] =='u' ) +if ((format[1 ] =='u' ) || (format[1 ] =='o' ) || (format[1 ] =='d' ) || (format[1 ] =='i' ) || (format[1 ] =='x' ) || (format[1 ] =='X' )) { ++format;
Signature · 1 line
2.39.2
PATCH v6 6/12 lib: sbi: print add 'o' type
On Mon, Jun 12, 2023 at 1:40?PM Xiang W <[email protected]> wrote:
No patch description ?
Quoted 39 lines
Signed-off-by Xiang W <[email protected]>--- lib/sbi/sbi_console.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)lib/sbi/sbi_console.c +4
-4@@ -328,7 +328,8 @@ static int print(char **out, u32 *out_len, const char *format, va_list args) width, flags, *format);continue ; } -if ((*format =='u' ) || (*format =='x' ) || (*format =='X' )) { +if ((*format =='u' ) || (*format =='o' ) + || (*format =='x' ) || (*format =='X' )) { pc += printi(out, out_len, va_arg(args, unsigned int), width, flags, *format);continue ;@@ -342,7 +343,7 @@ static int print(char **out, u32 *out_len, const char *format, va_list args) type ='i' ;if (format[1 ] =='l' ) { ++format; -if ((format[1 ] =='u' ) +if ((format[1 ] =='u' ) || (format[1 ] =='o' ) || (format[1 ] =='d' ) || (format[1 ] =='i' ) || (format[1 ] =='x' ) || (format[1 ] =='X' )) { ++format;@@ -352,7 +353,7 @@ static int print(char **out, u32 *out_len, const char *format, va_list args) width, flags, type);continue ; } -if ((format[1 ] =='u' ) +if ((format[1 ] =='u' ) || (format[1 ] =='o' ) || (format[1 ] =='d' ) || (format[1 ] =='i' ) || (format[1 ] =='x' ) || (format[1 ] =='X' )) { ++format; --
Quoted 2 lines
2.39.2
Otherwise, it looks good to me.
Regards,
Anup
PATCH v6 6/12 lib: sbi: print add 'o' type
? 2023-07-04???? 10:24 +0530?Anup Patel???
Quoted 3 lines · On Mon, Jun 12, 2023 at 1:40?PM Xiang W <[email protected]> wrote:
No patch description ?
Will be done in next patch.
Regards?
Xiang W
Quoted 49 lines
Quoted 40 lines
Signed-off-by Xiang W <[email protected]>---
?lib/sbi/sbi_console.c | 7 ++++---
?1 file changed, 4 insertions(+), 3 deletions(-)lib/sbi/sbi_console.c +4
-4@@ -328,7 +328,8 @@ static int print(char **out, u32 *out_len, const char *format, va_list args) ???????????????????????????????????????????? width, flags, *format); ??????????????????????????????? continue; ??????????????????????? } -?????????????????????? if ((*format == 'u') || (*format == 'x') || (*format == 'X')) { +?????????????????????? if ((*format == 'u') || (*format == 'o') +??????????????????????????????????????? || (*format == 'x') || (*format == 'X')) { ??????????????????????????????? pc += printi(out, out_len, va_arg(args, unsigned int), ???????????????????????????????????????????? width, flags, *format); ??????????????????????????????? continue;@@ -342,7 +343,7 @@ static int print(char **out, u32 *out_len, const char *format, va_list args) ??????????????????????????????? type = 'i'; ??????????????????????????????? if (format[1] == 'l') { ??????????????????????????????????????? ++format; -?????????????????????????????????????? if ((format[1] == 'u') +?????????????????????????????????????? if ((format[1] == 'u') || (format[1] == 'o') ??????????????????????????????????????????????????????? || (format[1] == 'd') || (format[1] == 'i') ??????????????????????????????????????????????????????? || (format[1] == 'x') || (format[1] == 'X')) { ??????????????????????????????????????????????? ++format;@@ -352,7 +353,7 @@ static int print(char **out, u32 *out_len, const char *format, va_list args) ??????????????????????????????????????????????? width, flags, type); ??????????????????????????????????????? continue; ??????????????????????????????? } -?????????????????????????????? if ((format[1] == 'u') +?????????????????????????????? if ((format[1] == 'u') || (format[1] == 'o') ??????????????????????????????????????????????? || (format[1] == 'd') || (format[1] == 'i') ??????????????????????????????????????????????? || (format[1] == 'x') || (format[1] == 'X')) { ??????????????????????????????????????? ++format; --Quoted 2 lines
2.39.2
Otherwise, it looks good to me.
Reviewed-by Anup Patel <[email protected]>Regards,
Anup
PATCH v6 7/12 lib: sbi: Fix printi
In reply to Xiang WFix two bug:
Quoted 2 lines
printf("%#08x", 0x123); /* print 0000x123 */printf("%#x", 0); /* print0x0*/
--- lib/sbi/sbi_console.c | 36 +++++++++++++++++++++++------------- 1 file changed, 23 insertions(+), 13 deletions(-)
lib/sbi/sbi_console.c +23 -13
@@ -227,23 +227,33 @@ static int printi(char **out, u32 *out_len, long long i, } } -if (flags &PAD_ALTERNATE ) { -if ((b ==16 ) && (letbase =='A' )) { - *--s ='X' ; - }else if ((b ==16 ) && (letbase =='a' )) { - *--s ='x' ; - } - *--s ='0' ; - } - -if (sign) { -if (width && (flags &PAD_ZERO )) { +if (flags &PAD_ZERO ) { +if (sign) {printc (out, out_len, sign); ++pc; --width; - }else { - *--s = sign; } +if (i && (flags &PAD_ALTERNATE )) { +if (b ==16 || b ==8 ) { +printc (out, out_len,'0' ); + ++pc; + --width; + } +if (b ==16 ) { +printc (out, out_len,'x' -'a' + letbase); + ++pc; + --width; + } + } + }else { +if (i && (flags &PAD_ALTERNATE )) { +if (b ==16 ) + *--s ='x' -'a' + letbase; +if (b ==16 || b ==8 ) + *--s ='0' ; + } +if (sign) + *--s = sign; }return pc +prints (out, out_len, s, width, flags);
Signature · 1 line
2.39.2
PATCH v6 7/12 lib: sbi: Fix printi
Quoted 6 lines · On Mon, Jun 12, 2023 at 1:40?PM Xiang W <[email protected]> wrote:
Fix two bug:
Quoted 2 lines
printf("%#08x", 0x123); /* print 0000x123 */printf("%#x", 0); /* print0x0*/Signed-off-by Xiang W <[email protected]>
Looks good to me.
Regards,
Anup
Quoted 58 lines
--- lib/sbi/sbi_console.c | 36 +++++++++++++++++++++++------------- 1 file changed, 23 insertions(+), 13 deletions(-)lib/sbi/sbi_console.c +23
-14@@ -227,23 +227,33 @@ static int printi(char **out, u32 *out_len, long long i, } } -if (flags &PAD_ALTERNATE ) { -if ((b ==16 ) && (letbase =='A' )) { - *--s ='X' ; - }else if ((b ==16 ) && (letbase =='a' )) { - *--s ='x' ; - } - *--s ='0' ; - } - -if (sign) { -if (width && (flags &PAD_ZERO )) { +if (flags &PAD_ZERO ) { +if (sign) {printc (out, out_len, sign); ++pc; --width; - }else { - *--s = sign; } +if (i && (flags &PAD_ALTERNATE )) { +if (b ==16 || b ==8 ) { +printc (out, out_len,'0' ); + ++pc; + --width; + } +if (b ==16 ) { +printc (out, out_len,'x' -'a' + letbase); + ++pc; + --width; + } + } + }else { +if (i && (flags &PAD_ALTERNATE )) { +if (b ==16 ) + *--s ='x' -'a' + letbase; +if (b ==16 || b ==8 ) + *--s ='0' ; + } +if (sign) + *--s = sign; }return pc +prints (out, out_len, s, width, flags); --2.39.2
PATCH v6 8/12 lib: sbi: Simplify prints
In reply to Xiang WWhen doing width = width - strlen(string) in prints there is no need to consider the case that witdh may be less than 0. This is because the code to do filling needs to be executed under the condition that width > 0.
--- lib/sbi/sbi_console.c | 21 ++++----------------- 1 file changed, 4 insertions(+), 17 deletions(-)
lib/sbi/sbi_console.c +4 -17
@@ -151,24 +151,11 @@ static void printc(char **out, u32 *out_len, char ch)static int prints (char **out,u32 *out_len,const char *string,int width,int flags) { -int pc =0 ; -char padchar =' ' ; - -if (width >0 ) { -int len =0 ; -const char *ptr; -for (ptr = string; *ptr; ++ptr) - ++len; -if (len >= width) - width =0 ; -else - width -= len; -if (flags &PAD_ZERO ) - padchar ='0' ; - } +int pc =0 ; + width -=sbi_strlen (string);if (!(flags &PAD_RIGHT )) {for (; width >0 ; --width) { -printc (out, out_len, padchar); +printc (out, out_len, flags &PAD_ZERO ?'0' :' ' ); ++pc; } }
@@ -177,7 +164,7 @@ static int prints(char **out, u32 *out_len, const char *string, int width, ++pc; }for (; width >0 ; --width) { -printc (out, out_len, padchar); +printc (out, out_len,' ' ); ++pc; }
Signature · 1 line
2.39.2
PATCH v6 8/12 lib: sbi: Simplify prints
Quoted 44 lines · On Mon, Jun 12, 2023 at 1:40?PM Xiang W <[email protected]> wrote:
When doing width = width - strlen(string) in prints there is no need to consider the case that witdh may be less than 0. This is because the code to do filling needs to be executed under the condition that width > 0.
Signed-off-by Xiang W <[email protected]>--- lib/sbi/sbi_console.c | 21 ++++----------------- 1 file changed, 4 insertions(+), 17 deletions(-)lib/sbi/sbi_console.c +3
-16@@ -151,24 +151,11 @@ static void printc(char **out, u32 *out_len, char ch)static int prints (char **out,u32 *out_len,const char *string,int width,int flags) { -int pc =0 ; -char padchar =' ' ; - -if (width >0 ) { -int len =0 ; -const char *ptr; -for (ptr = string; *ptr; ++ptr) - ++len; -if (len >= width) - width =0 ; -else - width -= len; -if (flags &PAD_ZERO ) - padchar ='0' ; - } +int pc =0 ; + width -=sbi_strlen (string);if (!(flags &PAD_RIGHT )) {for (; width >0 ; --width) { -printc (out, out_len, padchar); +printc (out, out_len, flags &PAD_ZERO ?'0' :' ' ); ++pc; } }
lib/sbi/sbi_console.c static int prints(char **out, u32 *out_len, const char *string, int width, · line 167
Quoted 5 lines
lib/sbi/sbi_console.c +1
-1@@ -177,7 +164,7 @@ static int prints(char **out, u32 *out_len, const char *string, int width, ++pc; }for (; width >0 ; --width) { -printc (out, out_len, padchar); +printc (out, out_len,' ' );
The padchar should be
flags & PAD_ZERO ? '0' : ' '
Quoted 6 lines
lib/sbi/sbi_console.c +0
-1++pc; } --2.39.2
Regards,
Anup
PATCH v6 8/12 lib: sbi: Simplify prints
? 2023-07-04???? 11:24 +0530?Anup Patel???
Quoted 45 lines · On Mon, Jun 12, 2023 at 1:40?PM Xiang W <[email protected]> wrote:
Quoted 44 lines
When doing width = width - strlen(string) in prints there is no need to consider the case that witdh may be less than 0. This is because the code to do filling needs to be executed under the condition that width > 0.
Signed-off-by Xiang W <[email protected]>---
?lib/sbi/sbi_console.c | 21 ++++-----------------
?1 file changed, 4 insertions(+), 17 deletions(-)lib/sbi/sbi_console.c +3
-16@@ -151,24 +151,11 @@ static void printc(char **out, u32 *out_len, char ch) ?static int prints(char **out, u32 *out_len, const char *string, int width, ????????????????? int flags) ?{ -?????? int pc?????? = 0; -?????? char padchar = ' '; - -?????? if (width > 0) { -?????????????? int len = 0; -?????????????? const char *ptr; -?????????????? for (ptr = string; *ptr; ++ptr) -?????????????????????? ++len; -?????????????? if (len >= width) -?????????????????????? width = 0; -?????????????? else -?????????????????????? width -= len; -?????????????? if (flags & PAD_ZERO) -?????????????????????? padchar = '0'; -?????? } +?????? int pc = 0; +?????? width -= sbi_strlen(string); ??????? if (!(flags & PAD_RIGHT)) { ??????????????? for (; width > 0; --width) { -?????????????????????? printc(out, out_len, padchar); +?????????????????????? printc(out, out_len, flags & PAD_ZERO ? '0' : ' '); ??????????????????????? ++pc; ??????????????? } ??????? }@@ -177,7 +164,7 @@ static int prints(char **out, u32 *out_len, const char *string, int width,
Quoted 8 lines
lib/sbi/sbi_console.c static int prints(char **out, u32 *out_len, const char *string, int width, · line 167
Quoted 5 lines
lib/sbi/sbi_console.c +1
-1??????????????? ++pc; ??????? } ??????? for (; width > 0; --width) { -?????????????? printc(out, out_len, padchar); +?????????????? printc(out, out_len, ' ');The padchar should be
flags &PAD_ZERO? '0' : ' '
PATCH2 ensures that the right side not need pad 0
Regards,
Xiang W
Quoted 10 lines
Quoted 7 lines
lib/sbi/sbi_console.c +0
-1??????????????? ++pc; ??????? } --2.39.2
Regards,
Anup
PATCH v6 9/12 lib: sbi: sbi_console.c code re-layout
In reply to Xiang WMove the type definitions and macros in sbi_console.c to the begin of the file.
--- lib/sbi/sbi_console.c | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-)
lib/sbi/sbi_console.c +11 -11
@@ -14,8 +14,19 @@#include <sbi/sbi_scratch.h> #include <sbi/sbi_string.h> +#define PAD_RIGHT 1 +#define PAD_ZERO 2 +#define PAD_ALTERNATE 4 +#define PAD_SIGN 8 +#define PRINT_BUF_LEN 64#define CONSOLE_TBUF_MAX 256 +#define va_start (v, l) __builtin_va_start((v), l) +#define va_end __builtin_va_end +#define va_arg __builtin_va_arg + +typedef __builtin_va_list va_list ; +static const struct sbi_console_device *console_dev =NULL ;static char console_tbuf[CONSOLE_TBUF_MAX ];static u32 console_tbuf_len;
@@ -117,17 +128,6 @@ unsigned long sbi_ngets(char *str, unsigned long len)return i; } -#define PAD_RIGHT 1 -#define PAD_ZERO 2 -#define PAD_ALTERNATE 4 -#define PAD_SIGN 8 -#define PRINT_BUF_LEN 64 - -#define va_start (v, l) __builtin_va_start((v), l) -#define va_end __builtin_va_end -#define va_arg __builtin_va_arg -typedef __builtin_va_list va_list ; -static void printc (char **out,u32 *out_len,char ch) {if (!out) {
Signature · 1 line
2.39.2
PATCH v6 9/12 lib: sbi: sbi_console.c code re-layout
On Mon, Jun 12, 2023 at 1:40?PM Xiang W <[email protected]> wrote:
The patch subject is totally misleading.
Quoted 52 lines
Move the type definitions and macros in
sbi_console.cto the begin of the file.Signed-off-by Xiang W <[email protected]>--- lib/sbi/sbi_console.c | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-)lib/sbi/sbi_console.c +11
-12@@ -14,8 +14,19 @@#include <sbi/sbi_scratch.h> #include <sbi/sbi_string.h> +#define PAD_RIGHT 1 +#define PAD_ZERO 2 +#define PAD_ALTERNATE 4 +#define PAD_SIGN 8 +#define PRINT_BUF_LEN 64#define CONSOLE_TBUF_MAX 256 +#define va_start (v, l) __builtin_va_start((v), l) +#define va_end __builtin_va_end +#define va_arg __builtin_va_arg + +typedef __builtin_va_list va_list ; +static const struct sbi_console_device *console_dev =NULL ;static char console_tbuf[CONSOLE_TBUF_MAX ];static u32 console_tbuf_len;@@ -117,17 +128,6 @@ unsigned long sbi_ngets(char *str, unsigned long len)return i; } -#define PAD_RIGHT 1 -#define PAD_ZERO 2 -#define PAD_ALTERNATE 4 -#define PAD_SIGN 8 -#define PRINT_BUF_LEN 64 - -#define va_start (v, l) __builtin_va_start((v), l) -#define va_end __builtin_va_end -#define va_arg __builtin_va_arg -typedef __builtin_va_list va_list ; -static void printc (char **out,u32 *out_len,char ch) {if (!out) { --
Quoted 2 lines
2.39.2
Regards,
Anup
PATCH v6 9/12 lib: sbi: sbi_console.c code re-layout
? 2023-07-04???? 11:25 +0530?Anup Patel???
Quoted 3 lines · On Mon, Jun 12, 2023 at 1:40?PM Xiang W <[email protected]> wrote:
The patch subject is totally misleading.
This is in preparation for PATCH10
Regards,
Xiang W
Quoted 58 lines
Quoted 53 lines
Move the type definitions and macros in
sbi_console.cto the begin of the file.Signed-off-by Xiang W <[email protected]>---
?lib/sbi/sbi_console.c | 22 +++++++++++----------- ?1 file changed, 11 insertions(+), 11 deletions(-)lib/sbi/sbi_console.c +0
-0@@ -14,8 +14,19 @@?#include <
sbi/sbi_scratch.h>
?#include <sbi/sbi_string.h>lib/sbi/sbi_console.c +5
-0+#define PAD_RIGHT 1 +#define PAD_ZERO 2 +#define PAD_ALTERNATE 4 +#define PAD_SIGN 8 +#define PRINT_BUF_LEN 64?#define
CONSOLE_TBUF_MAX256lib/sbi/sbi_console.c +6
-0+#define va_start (v, l) __builtin_va_start((v), l) +#define va_end __builtin_va_end +#define va_arg __builtin_va_arg + +typedef __builtin_va_list va_list ; +?static const struct
sbi_console_device*console_dev= NULL; ?static char console_tbuf[CONSOLE_TBUF_MAX]; ?static u32console_tbuf_len;lib/sbi/sbi_console.c +0
-12@@ -117,17 +128,6 @@ unsigned long sbi_ngets(char *str, unsigned long len) ??????? return i; ?} -#define PAD_RIGHT 1 -#define PAD_ZERO 2 -#define PAD_ALTERNATE 4 -#define PAD_SIGN 8 -#define PRINT_BUF_LEN 64 - -#define va_start (v, l) __builtin_va_start((v), l) -#define va_end __builtin_va_end -#define va_arg __builtin_va_arg -typedef __builtin_va_list va_list ; - ?static void printc(char **out, u32 *out_len, char ch) ?{ ??????? if (!out) { --Quoted 2 lines
2.39.2
Regards,
Anup
PATCH v6 10/12 lib: sbi: Add print_info for sbi_console.c
In reply to Xiang WCreate a structure print_info for print, this structure simplifies parameter passing.
--- lib/sbi/sbi_console.c | 235 +++++++++++++++++++++--------------------- 1 file changed, 120 insertions(+), 115 deletions(-)
lib/sbi/sbi_console.c +120 -115
@@ -27,11 +27,22 @@typedef __builtin_va_list va_list ; +struct print_info { +char *out; +int len; +int pos; +int pc; +int flags; +int width; +int wcnt; +char type; +}; +static const struct sbi_console_device *console_dev =NULL ;static char console_tbuf[CONSOLE_TBUF_MAX ]; -static u32 console_tbuf_len;static spinlock_t console_out_lock =SPIN_LOCK_INITIALIZER ; +bool sbi_isprintable (char c) { if (((31 < c) && (c < 127)) || (c == '\f') || (c == '\r') ||
@@ -128,70 +139,63 @@ unsigned long sbi_ngets(char *str, unsigned long len)return i; } -static void printc (char **out,u32 *out_len,char ch) +static void printc (struct print_info *info,char ch) { -if (!out) { +/* output to the console */ +if (info->out ==NULL ) {sbi_putc (ch); + info->pc++;return ; } -/* -* The *printf entry point functions have enforced that (*out) can -* only be null when out_len is non-null and its value is zero. -*/ -if (!out_len || *out_len >1 ) { - *(*out)++ = ch; - **out ='\0' ; - } +/* unknown buffer length */ +if (info->len ==0 ) + goto append; -if (out_len && *out_len >0 ) - --(*out_len); +/* The buffer is long enough to add new characters */ +if (info->len - info->pos >1 ) + goto append; +return ; + +append: + info->out[info->pos++] = ch; + info->out[info->pos] ='\0' ; + info->pc++; } -static int prints (char **out,u32 *out_len,const char *string,int width, -int flags) +static void prints (struct print_info *info,const char *string) { -int pc =0 ; - width -=sbi_strlen (string); -if (!(flags &PAD_RIGHT )) { -for (; width >0 ; --width) { -printc (out, out_len, flags &PAD_ZERO ?'0' :' ' ); - ++pc; - } - } -for (; *string; ++string) { -printc (out, out_len, *string); - ++pc; - } -for (; width >0 ; --width) { -printc (out, out_len,' ' ); - ++pc; + info->wcnt +=sbi_strlen (string); +if (!(info->flags &PAD_RIGHT )) { +for (; info->wcnt < info->width; info->wcnt++) +printc (info, info->flags &PAD_ZERO ?'0' :' ' ); } - -return pc; +for (; *string; ++string) +printc (info, *string); +for (; info->wcnt < info->width; info->wcnt++) +printc (info,' ' ); } -static int printi (char **out,u32 *out_len,long long i, -int width,int flags,int type) +static void printi (struct print_info *info,long long i) { -int pc =0 ;char *s, sign, letbase, print_buf[PRINT_BUF_LEN ];unsigned long long u, b, t; b =10 ; letbase ='a' ; -if (type =='o' ) +if (info->type =='o' ) b =8 ; -else if (type =='x' || type =='X' || type =='p' || type =='P' ) { +else if (info->type =='x' || info->type =='X' + || info->type =='p' || info->type =='P' ) { b =16 ; letbase &= ~0x20 ; - letbase |= type &0x20 ; + letbase |= info->type &0x20 ; } u = i; sign =0 ; -if (type =='i' || type =='d' ) { -if ((flags &PAD_SIGN ) && i >0 ) +if (info->type =='i' || info->type =='d' ) { +if ((info->flags &PAD_SIGN ) && i >0 ) sign ='+' ;if (i <0 ) { sign ='-' ;
@@ -214,26 +218,23 @@ static int printi(char **out, u32 *out_len, long long i, } } -if (flags &PAD_ZERO ) { +if (info->flags &PAD_ZERO ) {if (sign) { -printc (out, out_len, sign); - ++pc; - --width; +printc (info, sign); + ++info->wcnt; } -if (i && (flags &PAD_ALTERNATE )) { +if (i && (info->flags &PAD_ALTERNATE )) {if (b ==16 || b ==8 ) { -printc (out, out_len,'0' ); - ++pc; - --width; +printc (info,'0' ); + ++info->wcnt; }if (b ==16 ) { -printc (out, out_len,'x' -'a' + letbase); - ++pc; - --width; +printc (info,'x' -'a' + letbase); + ++info->wcnt; } } }else { -if (i && (flags &PAD_ALTERNATE )) { +if (i && (info->flags &PAD_ALTERNATE )) {if (b ==16 ) *--s ='x' -'a' + letbase;if (b ==16 || b ==8 )
@@ -243,15 +244,16 @@ static int printi(char **out, u32 *out_len, long long i, *--s = sign; } -return pc +prints (out, out_len, s, width, flags); +prints (info, s); } -static int char **out,u32 *out_len,const char *format,va_list args) +static void struct print_info *info,const char *format,va_list args) {bool flags_done; -int width, flags, pc =0 ; -char type, scr[2 ], *tout; -bool use_tbuf = (!out) ? true : false; +char scr[2 ]; +bool use_tbuf = (!info->out) ? true : false; + info->pos =0 ; + info->pc =0 ;/* * The console_tbuf is protected by console_out_lock and
@@ -259,19 +261,15 @@ static int print(char **out, u32 *out_len, const char *format, va_list args)* when out == NULL. */ if (use_tbuf) { - console_tbuf_len =CONSOLE_TBUF_MAX ; - tout = console_tbuf; - out = &tout; - out_len = &console_tbuf_len; + info->out = console_tbuf; + info->len =CONSOLE_TBUF_MAX ; }for (; *format !=0 ; ++format) { -if (use_tbuf && !console_tbuf_len) { -nputs_all (console_tbuf,CONSOLE_TBUF_MAX ); - console_tbuf_len =CONSOLE_TBUF_MAX ; - tout = console_tbuf; - } - +if (use_tbuf && (info->len - info->pos <=1 )) { +nputs_all (info->out, info->pos); + info->pos =0 ; + }if (*format =='%' ) { ++format;if (*format =='\0' )
@@ -279,21 +277,21 @@ static int print(char **out, u32 *out_len, const char *format, va_list args)if (*format =='%' ) goto literal;/* Get flags */ - flags =0 ; + info->flags =0 ; flags_done = false;while (!flags_done) {switch (*format) {case '-' : - flags |=PAD_RIGHT ; + info->flags |=PAD_RIGHT ;break ;case '+' : - flags |=PAD_SIGN ; + info->flags |=PAD_SIGN ;break ;case '#' : - flags |=PAD_ALTERNATE ; + info->flags |=PAD_ALTERNATE ;break ;case '0' : - flags |=PAD_ZERO ; + info->flags |=PAD_ZERO ;break ;case ' ' :case '\'' :
@@ -306,153 +304,160 @@ static int print(char **out, u32 *out_len, const char *format, va_list args)if (!flags_done) ++format; } -if (flags &PAD_RIGHT ) - flags &= ~PAD_ZERO ; +if (info->flags &PAD_RIGHT ) + info->flags &= ~PAD_ZERO ;/* Get width */ - width =0 ; + info->width =0 ; + info->wcnt =0 ;for (; *format >='0' && *format <='9' ; ++format) { - width *=10 ; - width += *format -'0' ; + info->width *=10 ; + info->width += *format -'0' ; } + info->type = *format;if (*format =='s' ) { char *s = va_arg(args, char *); - pc +=prints (out, out_len, s ? s :"(null)" , - width, flags); +prints (info, s ? s :"(null)" );continue ; }if ((*format =='d' ) || (*format =='i' )) { - pc +=printi (out, out_len,va_arg (args, int), - width, flags, *format); +printi (info,va_arg (args, int));continue ; }if ((*format =='u' ) || (*format =='o' ) || (*format =='x' ) || (*format =='X' )) { - pc += printi(out, out_len, va_arg(args, unsigned int), - width, flags, *format); + printi(info, va_arg(args, unsigned int));continue ; }if ((*format =='p' ) || (*format =='P' )) { - pc += printi(out, out_len, (uintptr_t)va_arg(args, void*), - width, flags, *format); + printi(info, (uintptr_t)va_arg(args, void*));continue ; }if (*format =='l' ) { - type ='i' ; + info->type ='i' ;if (format[1 ] =='l' ) { ++format;if ((format[1 ] =='u' ) || (format[1 ] =='o' ) || (format[1 ] =='d' ) || (format[1 ] =='i' ) || (format[1 ] =='x' ) || (format[1 ] =='X' )) { ++format; - type = *format; + info->type = *format; } - pc += printi(out, out_len, va_arg(args, long long), - width, flags, type); + printi(info, va_arg(args, long long));continue ; }if ((format[1 ] =='u' ) || (format[1 ] =='o' ) || (format[1 ] =='d' ) || (format[1 ] =='i' ) || (format[1 ] =='x' ) || (format[1 ] =='X' )) { ++format; - type = *format; + info->type = *format; } -if ((type =='d' ) || (type =='i' )) - pc +=printi (out, out_len,va_arg (args, long), - width, flags, type); +if ((info->type =='d' ) || (info->type =='i' )) +printi (info,va_arg (args, long));else - pc += printi(out, out_len, va_arg(args, unsigned long), - width, flags, type); + printi(info, va_arg(args, unsigned long));continue ; }if (*format =='c' ) {/* char are converted to int then pushed on the stack */ scr[0 ] =va_arg (args, int); scr[1 ] ='\0' ; - pc +=prints (out, out_len, scr, width, flags); +prints (info, scr);continue ; } }else { literal: -printc (out, out_len, *format); - ++pc; +printc (info, *format); } } -if (use_tbuf && console_tbuf_len <CONSOLE_TBUF_MAX ) -nputs_all (console_tbuf,CONSOLE_TBUF_MAX - console_tbuf_len); - -return pc; +if (use_tbuf && info->pos >0 ) +nputs_all (info->out, info->pos); }int sbi_sprintf (char *out,const char *format, ...) {va_list args; -int retval; +struct print_info info; + info.out = out; + info.len =0 ; +if (unlikely (!out))sbi_panic ("sbi_sprintf called with NULL output string\n" );va_start (args, format); - retval =NULL , format, args); +va_end (args); -return retval; +return info.pc; }int sbi_snprintf (char *out,u32 out_sz,const char *format, ...) {va_list args; -int retval; +struct print_info info; + info.out = out; + info.len = out_sz; +if (unlikely (!out && out_sz !=0 ))sbi_panic ("sbi_snprintf called with NULL output string and " "output size is not zero\n" );va_start (args, format); - retval =va_end (args); -return retval; +return info.pc; }int sbi_printf (const char *format, ...) {va_list args; -int retval; +struct print_info info; + info.out =NULL ; + info.len =0 ; +spin_lock (&console_out_lock);va_start (args, format); - retval =NULL ,NULL , format, args); +va_end (args);spin_unlock (&console_out_lock); -return retval; +return info.pc; }int sbi_dprintf (const char *format, ...) {va_list args; -int retval =0 ;struct sbi_scratch *scratch =sbi_scratch_thishart_ptr (); +struct print_info info; + info.out =NULL ; + info.len =0 ; +va_start (args, format);if (scratch->options &SBI_SCRATCH_DEBUG_PRINTS ) {spin_lock (&console_out_lock); - retval =NULL ,NULL , format, args); +spin_unlock (&console_out_lock); }va_end (args); -return retval; +return info.pc; }void sbi_panic (const char *format, ...) {va_list args; +struct print_info info; + info.out =NULL ; + info.len =0 ;spin_lock (&console_out_lock);va_start (args, format); -NULL ,NULL , format, args); +va_end (args);spin_unlock (&console_out_lock);
Signature · 1 line
2.39.2
PATCH v6 10/12 lib: sbi: Add print_info for sbi_console.c
Quoted 3 lines · On Mon, Jun 12, 2023 at 1:40?PM Xiang W <[email protected]> wrote:
Create a structure
print_infofor print, this structure simplifies parameter passing.
I don't see any advantage of this new structure.
NACK to this patch.
Regards,
Anup
Quoted 455 lines
Signed-off-by Xiang W <[email protected]>--- lib/sbi/sbi_console.c | 235 +++++++++++++++++++++--------------------- 1 file changed, 120 insertions(+), 115 deletions(-)lib/sbi/sbi_console.c +120
-116@@ -27,11 +27,22 @@typedef __builtin_va_list va_list ; +struct print_info { +char *out; +int len; +int pos; +int pc; +int flags; +int width; +int wcnt; +char type; +}; +static const struct sbi_console_device *console_dev =NULL ;static char console_tbuf[CONSOLE_TBUF_MAX ]; -static u32 console_tbuf_len;static spinlock_t console_out_lock =SPIN_LOCK_INITIALIZER ; +bool sbi_isprintable (char c) { if (((31 < c) && (c < 127)) || (c == '\f') || (c == '\r') ||@@ -128,70 +139,63 @@ unsigned long sbi_ngets(char *str, unsigned long len)return i; } -static void printc (char **out,u32 *out_len,char ch) +static void printc (struct print_info *info,char ch) { -if (!out) { +/* output to the console */ +if (info->out ==NULL ) {sbi_putc (ch); + info->pc++;return ; } -/* -* The *printf entry point functions have enforced that (*out) can -* only be null when out_len is non-null and its value is zero. -*/ -if (!out_len || *out_len >1 ) { - *(*out)++ = ch; - **out ='\0' ; - } +/* unknown buffer length */ +if (info->len ==0 ) + goto append; -if (out_len && *out_len >0 ) - --(*out_len); +/* The buffer is long enough to add new characters */ +if (info->len - info->pos >1 ) + goto append; +return ; + +append: + info->out[info->pos++] = ch; + info->out[info->pos] ='\0' ; + info->pc++; } -static int prints (char **out,u32 *out_len,const char *string,int width, -int flags) +static void prints (struct print_info *info,const char *string) { -int pc =0 ; - width -=sbi_strlen (string); -if (!(flags &PAD_RIGHT )) { -for (; width >0 ; --width) { -printc (out, out_len, flags &PAD_ZERO ?'0' :' ' ); - ++pc; - } - } -for (; *string; ++string) { -printc (out, out_len, *string); - ++pc; - } -for (; width >0 ; --width) { -printc (out, out_len,' ' ); - ++pc; + info->wcnt +=sbi_strlen (string); +if (!(info->flags &PAD_RIGHT )) { +for (; info->wcnt < info->width; info->wcnt++) +printc (info, info->flags &PAD_ZERO ?'0' :' ' ); } - -return pc; +for (; *string; ++string) +printc (info, *string); +for (; info->wcnt < info->width; info->wcnt++) +printc (info,' ' ); } -static int printi (char **out,u32 *out_len,long long i, -int width,int flags,int type) +static void printi (struct print_info *info,long long i) { -int pc =0 ;char *s, sign, letbase, print_buf[PRINT_BUF_LEN ];unsigned long long u, b, t; b =10 ; letbase ='a' ; -if (type =='o' ) +if (info->type =='o' ) b =8 ; -else if (type =='x' || type =='X' || type =='p' || type =='P' ) { +else if (info->type =='x' || info->type =='X' + || info->type =='p' || info->type =='P' ) { b =16 ; letbase &= ~0x20 ; - letbase |= type &0x20 ; + letbase |= info->type &0x20 ; } u = i; sign =0 ; -if (type =='i' || type =='d' ) { -if ((flags &PAD_SIGN ) && i >0 ) +if (info->type =='i' || info->type =='d' ) { +if ((info->flags &PAD_SIGN ) && i >0 ) sign ='+' ;if (i <0 ) { sign ='-' ;@@ -214,26 +218,23 @@ static int printi(char **out, u32 *out_len, long long i, } } -if (flags &PAD_ZERO ) { +if (info->flags &PAD_ZERO ) {if (sign) { -printc (out, out_len, sign); - ++pc; - --width; +printc (info, sign); + ++info->wcnt; } -if (i && (flags &PAD_ALTERNATE )) { +if (i && (info->flags &PAD_ALTERNATE )) {if (b ==16 || b ==8 ) { -printc (out, out_len,'0' ); - ++pc; - --width; +printc (info,'0' ); + ++info->wcnt; }if (b ==16 ) { -printc (out, out_len,'x' -'a' + letbase); - ++pc; - --width; +printc (info,'x' -'a' + letbase); + ++info->wcnt; } } }else { -if (i && (flags &PAD_ALTERNATE )) { +if (i && (info->flags &PAD_ALTERNATE )) {if (b ==16 ) *--s ='x' -'a' + letbase;if (b ==16 || b ==8 )@@ -243,15 +244,16 @@ static int printi(char **out, u32 *out_len, long long i, *--s = sign; } -return pc +prints (out, out_len, s, width, flags); +prints (info, s); } -static int char **out,u32 *out_len,const char *format,va_list args) +static void struct print_info *info,const char *format,va_list args) {bool flags_done; -int width, flags, pc =0 ; -char type, scr[2 ], *tout; -bool use_tbuf = (!out) ? true : false; +char scr[2 ]; +bool use_tbuf = (!info->out) ? true : false; + info->pos =0 ; + info->pc =0 ;/* * The console_tbuf is protected by console_out_lock and @@ -259,19 +261,15 @@ static int print(char **out, u32 *out_len, const char *format, va_list args)* when out == NULL. */ if (use_tbuf) { - console_tbuf_len =CONSOLE_TBUF_MAX ; - tout = console_tbuf; - out = &tout; - out_len = &console_tbuf_len; + info->out = console_tbuf; + info->len =CONSOLE_TBUF_MAX ; }for (; *format !=0 ; ++format) { -if (use_tbuf && !console_tbuf_len) { -nputs_all (console_tbuf,CONSOLE_TBUF_MAX ); - console_tbuf_len =CONSOLE_TBUF_MAX ; - tout = console_tbuf; - } - +if (use_tbuf && (info->len - info->pos <=1 )) { +nputs_all (info->out, info->pos); + info->pos =0 ; + }if (*format =='%' ) { ++format;if (*format =='\0' )@@ -279,21 +277,21 @@ static int print(char **out, u32 *out_len, const char *format, va_list args)if (*format =='%' ) goto literal;/* Get flags */ - flags =0 ; + info->flags =0 ; flags_done = false;while (!flags_done) {switch (*format) {case '-' : - flags |=PAD_RIGHT ; + info->flags |=PAD_RIGHT ;break ;case '+' : - flags |=PAD_SIGN ; + info->flags |=PAD_SIGN ;break ;case '#' : - flags |=PAD_ALTERNATE ; + info->flags |=PAD_ALTERNATE ;break ;case '0' : - flags |=PAD_ZERO ; + info->flags |=PAD_ZERO ;break ;case ' ' :case '\'' :@@ -306,153 +304,160 @@ static int print(char **out, u32 *out_len, const char *format, va_list args)if (!flags_done) ++format; } -if (flags &PAD_RIGHT ) - flags &= ~PAD_ZERO ; +if (info->flags &PAD_RIGHT ) + info->flags &= ~PAD_ZERO ;/* Get width */ - width =0 ; + info->width =0 ; + info->wcnt =0 ;for (; *format >='0' && *format <='9' ; ++format) { - width *=10 ; - width += *format -'0' ; + info->width *=10 ; + info->width += *format -'0' ; } + info->type = *format;if (*format =='s' ) { char *s = va_arg(args, char *); - pc +=prints (out, out_len, s ? s :"(null)" , - width, flags); +prints (info, s ? s :"(null)" );continue ; }if ((*format =='d' ) || (*format =='i' )) { - pc +=printi (out, out_len,va_arg (args, int), - width, flags, *format); +printi (info,va_arg (args, int));continue ; }if ((*format =='u' ) || (*format =='o' ) || (*format =='x' ) || (*format =='X' )) { - pc += printi(out, out_len, va_arg(args, unsigned int), - width, flags, *format); + printi(info, va_arg(args, unsigned int));continue ; }if ((*format =='p' ) || (*format =='P' )) { - pc += printi(out, out_len, (uintptr_t)va_arg(args, void*), - width, flags, *format); + printi(info, (uintptr_t)va_arg(args, void*));continue ; }if (*format =='l' ) { - type ='i' ; + info->type ='i' ;if (format[1 ] =='l' ) { ++format;if ((format[1 ] =='u' ) || (format[1 ] =='o' ) || (format[1 ] =='d' ) || (format[1 ] =='i' ) || (format[1 ] =='x' ) || (format[1 ] =='X' )) { ++format; - type = *format; + info->type = *format; } - pc += printi(out, out_len, va_arg(args, long long), - width, flags, type); + printi(info, va_arg(args, long long));continue ; }if ((format[1 ] =='u' ) || (format[1 ] =='o' ) || (format[1 ] =='d' ) || (format[1 ] =='i' ) || (format[1 ] =='x' ) || (format[1 ] =='X' )) { ++format; - type = *format; + info->type = *format; } -if ((type =='d' ) || (type =='i' )) - pc +=printi (out, out_len,va_arg (args, long), - width, flags, type); +if ((info->type =='d' ) || (info->type =='i' )) +printi (info,va_arg (args, long));else - pc += printi(out, out_len, va_arg(args, unsigned long), - width, flags, type); + printi(info, va_arg(args, unsigned long));continue ; }if (*format =='c' ) {/* char are converted to int then pushed on the stack */ scr[0 ] =va_arg (args, int); scr[1 ] ='\0' ; - pc +=prints (out, out_len, scr, width, flags); +prints (info, scr);continue ; } }else { literal: -printc (out, out_len, *format); - ++pc; +printc (info, *format); } } -if (use_tbuf && console_tbuf_len <CONSOLE_TBUF_MAX ) -nputs_all (console_tbuf,CONSOLE_TBUF_MAX - console_tbuf_len); - -return pc; +if (use_tbuf && info->pos >0 ) +nputs_all (info->out, info->pos); }int sbi_sprintf (char *out,const char *format, ...) {va_list args; -int retval; +struct print_info info; + info.out = out; + info.len =0 ; +if (unlikely (!out))sbi_panic ("sbi_sprintf called with NULL output string\n" );va_start (args, format); - retval =NULL , format, args); +va_end (args); -return retval; +return info.pc; }int sbi_snprintf (char *out,u32 out_sz,const char *format, ...) {va_list args; -int retval; +struct print_info info; + info.out = out; + info.len = out_sz; +if (unlikely (!out && out_sz !=0 ))sbi_panic ("sbi_snprintf called with NULL output string and " "output size is not zero\n" );va_start (args, format); - retval =va_end (args); -return retval; +return info.pc; }int sbi_printf (const char *format, ...) {va_list args; -int retval; +struct print_info info; + info.out =NULL ; + info.len =0 ; +spin_lock (&console_out_lock);va_start (args, format); - retval =NULL ,NULL , format, args); +va_end (args);spin_unlock (&console_out_lock); -return retval; +return info.pc; }int sbi_dprintf (const char *format, ...) {va_list args; -int retval =0 ;struct sbi_scratch *scratch =sbi_scratch_thishart_ptr (); +struct print_info info; + info.out =NULL ; + info.len =0 ; +va_start (args, format);if (scratch->options &SBI_SCRATCH_DEBUG_PRINTS ) {spin_lock (&console_out_lock); - retval =NULL ,NULL , format, args); +spin_unlock (&console_out_lock); }va_end (args); -return retval; +return info.pc; }void sbi_panic (const char *format, ...) {va_list args; +struct print_info info; + info.out =NULL ; + info.len =0 ;spin_lock (&console_out_lock);va_start (args, format); -NULL ,NULL , format, args); +va_end (args);spin_unlock (&console_out_lock); --2.39.2
PATCH v6 10/12 lib: sbi: Add print_info for sbi_console.c
? 2023-07-04???? 11:27 +0530?Anup Patel???
Quoted 8 lines · On Mon, Jun 12, 2023 at 1:40?PM Xiang W <[email protected]> wrote:
Quoted 3 lines
Create a structure
print_infofor print, this structure simplifies parameter passing.I don't see any advantage of this new structure.
NACK to this patch.
printi is reduced from 6 to 2 parameters
prints is reduced from 5 to 2 parameters
processing of pc reduced from 21 to 8
This patch simplifies parameter passing. Because of this, the size of the code will also be reduced.
After applying the patch
size build/lib/sbi/sbi_console.o
text data bss dec hex filename 3329 0 272 3601 e11 build/lib/sbi/sbi_console.o
befor applying the patch
size build/lib/sbi/sbi_console.o
text data bss dec hex filename 3559 0 272 3831 ef7 build/lib/sbi/sbi_console.o
Regards,
Xiang W
Quoted 459 lines
Regards,
AnupQuoted 455 lines
Signed-off-by Xiang W <[email protected]>---
?lib/sbi/sbi_console.c | 235 +++++++++++++++++++++--------------------- ?1 file changed, 120 insertions(+), 115 deletions(-)lib/sbi/sbi_console.c +12
-1@@ -27,11 +27,22 @@ ?typedef __builtin_va_list va_list; +struct print_info { +?????? char *out; +?????? int len; +?????? int pos; +?????? int pc; +?????? int flags; +?????? int width; +?????? int wcnt; +?????? char type; +}; + ?static const struct sbi_console_device *console_dev = NULL; ?static char console_tbuf[CONSOLE_TBUF_MAX]; -static u32 console_tbuf_len; ?static spinlock_t console_out_lock??????????? = SPIN_LOCK_INITIALIZER; + ?bool sbi_isprintable(char c) ?{??????? if (((31 < c) && (c < 127)) || (c == '\f') || (c == '\r') ||
lib/sbi/sbi_console.c +47
-56@@ -128,70 +139,63 @@ unsigned long sbi_ngets(char *str, unsigned long len) ??????? return i; ?} -static void printc (char **out,u32 *out_len,char ch) +static void printc (struct print_info *info,char ch) ?{ -?????? if (!out) { +?????? /* output to the console */ +?????? if (info->out == NULL) { ??????????????? sbi_putc(ch); +?????????????? info->pc++; ??????????????? return; ??????? } -?????? /* -??????? * The *printf entry point functions have enforced that (*out) can -??????? * only be null when out_len is non-null and its value is zero. -??????? */ -?????? if (!out_len || *out_len > 1) { -?????????????? *(*out)++ = ch; -?????????????? **out = '\0'; -?????? } +?????? /* unknown buffer length */ +?????? if (info->len == 0) +?????????????? goto append; -?????? if (out_len && *out_len > 0) -?????????????? --(*out_len); +?????? /* The buffer is long enough to add new characters */ +?????? if (info->len - info->pos > 1) +?????????????? goto append; +?????? return; + +append: +?????? info->out[info->pos++] = ch; +?????? info->out[info->pos] = '\0'; +?????? info->pc++; ?} -static int prints (char **out,u32 *out_len,const char *string,int width, -???????????????? int flags) +static void prints (struct print_info *info,const char *string) ?{ -?????? int pc = 0; -?????? width -= sbi_strlen(string); -?????? if (!(flags & PAD_RIGHT)) { -?????????????? for (; width > 0; --width) { -?????????????????????? printc(out, out_len, flags & PAD_ZERO ? '0' : ' '); -?????????????????????? ++pc; -?????????????? } -?????? } -?????? for (; *string; ++string) { -?????????????? printc(out, out_len, *string); -?????????????? ++pc; -?????? } -?????? for (; width > 0; --width) { -?????????????? printc(out, out_len, ' '); -?????????????? ++pc; +?????? info->wcnt += sbi_strlen(string); +?????? if (!(info->flags & PAD_RIGHT)) { +?????????????? for (; info->wcnt < info->width; info->wcnt++) +?????????????????????? printc(info, info->flags & PAD_ZERO ? '0' : ' '); ??????? } - -?????? return pc; +?????? for (; *string; ++string) +?????????????? printc(info, *string); +?????? for (; info->wcnt < info->width; info->wcnt++) +?????????????? printc(info, ' '); ?} -static int printi (char **out,u32 *out_len,long long i, -???????????????? int width, int flags, int type) +static void printi (struct print_info *info,long long i) ?{ -?????? int pc = 0; ??????? char *s, sign, letbase, print_buf[PRINT_BUF_LEN]; ??????? unsigned long long u, b, t; ??????? b = 10; ??????? letbase = 'a'; -?????? if (type == 'o') +?????? if (info->type == 'o') ??????????????? b = 8; -?????? else if (type == 'x' || type == 'X' || type == 'p' || type == 'P') { +?????? else if (info->type == 'x' || info->type == 'X' +??????????????????????? || info->type == 'p' || info->type == 'P') { ??????????????? b = 16; ??????????????? letbase &= ~0x20; -?????????????? letbase |= type & 0x20; +?????????????? letbase |= info->type & 0x20; ??????? } ??????? u = i; ??????? sign = 0; -?????? if (type == 'i' || type == 'd') { -?????????????? if ((flags & PAD_SIGN) && i > 0) +?????? if (info->type == 'i' || info->type == 'd') { +?????????????? if ((info->flags & PAD_SIGN) && i > 0) ??????????????????????? sign = '+'; ??????????????? if (i < 0) { ??????????????????????? sign = '-';@@ -214,26 +218,23 @@ static int printi(char **out, u32 *out_len, long long i, ??????????????? } ??????? } -?????? if (flags & PAD_ZERO) { +?????? if (info->flags & PAD_ZERO) { ??????????????? if (sign) { -?????????????????????? printc(out, out_len, sign); -?????????????????????? ++pc; -?????????????????????? --width; +?????????????????????? printc(info, sign); +?????????????????????? ++info->wcnt; ??????????????? } -?????????????? if (i && (flags & PAD_ALTERNATE)) { +?????????????? if (i && (info->flags & PAD_ALTERNATE)) { ??????????????????????? if (b == 16 || b == 8) { -?????????????????????????????? printc(out, out_len, '0'); -?????????????????????????????? ++pc; -?????????????????????????????? --width; +?????????????????????????????? printc(info, '0'); +?????????????????????????????? ++info->wcnt; ??????????????????????? } ??????????????????????? if (b == 16) { -?????????????????????????????? printc(out, out_len, 'x' - 'a' + letbase); -?????????????????????????????? ++pc; -?????????????????????????????? --width; +?????????????????????????????? printc(info, 'x' - 'a' + letbase); +?????????????????????????????? ++info->wcnt; ??????????????????????? } ??????????????? } ??????? } else { -?????????????? if (i && (flags & PAD_ALTERNATE)) { +?????????????? if (i && (info->flags & PAD_ALTERNATE)) { ??????????????????????? if (b == 16) ??????????????????????????????? *--s = 'x' - 'a' + letbase; ??????????????????????? if (b == 16 || b == 8)@@ -243,15 +244,16 @@ static int printi(char **out, u32 *out_len, long long i, ??????????????????????? *--s = sign; ??????? } -?????? return pc + prints(out, out_len, s, width, flags); +?????? prints(info, s); ?} -static int char **out,u32 *out_len,const char *format,va_list args) +static void struct print_info *info,const char *format,va_list args) ?{ ??????? bool flags_done; -?????? int width, flags, pc = 0; -?????? char type, scr[2], *tout; -?????? bool use_tbuf = (!out) ? true : false; +?????? char scr[2]; +?????? bool use_tbuf = (!info->out) ? true : false; +?????? info->pos = 0; +?????? info->pc = 0;??????? /*
???????? * Theconsole_tbufis protected byconsole_out_lockandlib/sbi/sbi_console.c +0
-0@@ -259,19 +261,15 @@ static int print(char **out, u32 *out_len, const char *format, va_list args)???????? * when out == NULL.
???????? */
??????? if (use_tbuf) {
-??????????????console_tbuf_len=CONSOLE_TBUF_MAX; -?????????????? tout =console_tbuf; -?????????????? out = &tout;
-??????????????out_len=&console_tbuf_len; +??????????????info->out=console_tbuf; +??????????????info->len=CONSOLE_TBUF_MAX; ??????? }??????? for (; *format != 0; ++format) { -?????????????? if (
use_tbuf&& !console_tbuf_len) { -??????????????????????nputs_all(console_tbuf, CONSOLE_TBUF_MAX); -??????????????????????console_tbuf_len=CONSOLE_TBUF_MAX; -?????????????????????? tout =console_tbuf; -?????????????? }
-
+?????????????? if (use_tbuf&& (info->len-info->pos<= 1)) { +??????????????????????nputs_all(info->out, info->pos); +??????????????????????info->pos= 0;
+?????????????? }
??????????????? if (*format == '%') { ??????????????????????? ++format; ??????????????????????? if (*format == '\0')lib/sbi/sbi_console.c +0
-0@@ -279,21 +277,21 @@ static int print(char **out, u32 *out_len, const char *format, va_list args) ??????????????????????? if (*format == '%') ??????????????????????????????? goto literal;??????????????????????? /* Get flags */ -?????????????????????? flags = 0;
+??????????????????????info->flags= 0; ???????????????????????flags_done= false; ??????????????????????? while (!flags_done) { ??????????????????????????????? switch (*format) { ??????????????????????????????? case '-':
-?????????????????????????????????????? flags |=PAD_RIGHT; +??????????????????????????????????????info->flags|=PAD_RIGHT; ??????????????????????????????????????? break; ??????????????????????????????? case '+':
-?????????????????????????????????????? flags |=PAD_SIGN; +??????????????????????????????????????info->flags|=PAD_SIGN; ??????????????????????????????????????? break; ??????????????????????????????? case '#':
-?????????????????????????????????????? flags |=PAD_ALTERNATE; +??????????????????????????????????????info->flags|=PAD_ALTERNATE; ??????????????????????????????????????? break; ??????????????????????????????? case '0':
-?????????????????????????????????????? flags |=PAD_ZERO; +??????????????????????????????????????info->flags|=PAD_ZERO; ??????????????????????????????????????? break; ??????????????????????????????? case ' ':
??????????????????????????????? case '\'':lib/sbi/sbi_console.c +2
-2@@ -306,153 +304,160 @@ static int print(char **out, u32 *out_len, const char *format, va_list args) ??????????????????????????????? if (!flags_done) ??????????????????????????????????????? ++format; ??????????????????????? } -?????????????????????? if (flags & PAD_RIGHT) -?????????????????????????????? flags &= ~PAD_ZERO; +?????????????????????? if (info->flags & PAD_RIGHT) +?????????????????????????????? info->flags &= ~PAD_ZERO;??????????????????????? /* Get width */
-?????????????????????? width = 0;
+??????????????????????info->width= 0;
+??????????????????????info->wcnt= 0;
??????????????????????? for (; *format >= '0' && *format <= '9'; ++format) { -?????????????????????????????? width *= 10; -?????????????????????????????? width += *format - '0'; +??????????????????????????????info->width*= 10; +??????????????????????????????info->width+= *format - '0'; ??????????????????????? }
+??????????????????????info->type= *format;
??????????????????????? if (*format == 's') { ??????????????????????????????? char *s =va_arg(args, char *); -?????????????????????????????? pc +=prints(out, out_len, s ? s : "(null)", -??????????????????????????????????????????? width, flags); +??????????????????????????????prints(info, s ? s : "(null)"); ??????????????????????????????? continue;
??????????????????????? }
??????????????????????? if ((*format == 'd') || (*format == 'i')) { -?????????????????????????????? pc += printi(out,out_len,va_arg(args, int), -??????????????????????????????????????????? width, flags, *format); +??????????????????????????????printi(info, va_arg(args, int)); ??????????????????????????????? continue;
??????????????????????? }
??????????????????????? if ((*format == 'u') || (*format == 'o') ???????????????????????????????????????? || (*format == 'x') || (*format == 'X')) { -?????????????????????????????? pc += printi(out,out_len,va_arg(args, unsigned int), -??????????????????????????????????????????? width, flags, *format); +??????????????????????????????printi(info, va_arg(args, unsigned int)); ??????????????????????????????? continue;
??????????????????????? }
??????????????????????? if ((*format == 'p') || (*format == 'P')) { -?????????????????????????????? pc += printi(out,out_len, (uintptr_t)va_arg(args, void*), -??????????????????????????????????????????? width, flags, *format); +??????????????????????????????printi(info, (uintptr_t)va_arg(args, void*)); ??????????????????????????????? continue;
??????????????????????? }
??????????????????????? if (*format == 'l') { -?????????????????????????????? type = 'i';
+??????????????????????????????info->type= 'i'; ??????????????????????????????? if (format[1] == 'l') { ??????????????????????????????????????? ++format; ??????????????????????????????????????? if ((format[1] == 'u') || (format[1] == 'o')??????????????????????????????????????????????????????? || (format[1] == 'd') || (format[1] == 'i') ??????????????????????????????????????????????????????? || (format[1] == 'x') || (format[1] == 'X')) {??????????????????????????????????????????????? ++format; -?????????????????????????????????????????????? type = *format; +??????????????????????????????????????????????
info->type= *format; ??????????????????????????????????????? } -?????????????????????????????????????? pc += printi(out,out_len,va_arg(args, long long), -?????????????????????????????????????????????? width, flags, type); +??????????????????????????????????????printi(info, va_arg(args, long long)); ??????????????????????????????????????? continue; ??????????????????????????????? }
??????????????????????????????? if ((format[1] == 'u') || (format[1] == 'o')??????????????????????????????????????????????? || (format[1] == 'd') || (format[1] == 'i') ??????????????????????????????????????????????? || (format[1] == 'x') || (format[1] == 'X')) {??????????????????????????????????????? ++format; -?????????????????????????????????????? type = *format; +??????????????????????????????????????
info->type= *format; ??????????????????????????????? }
-?????????????????????????????? if ((type == 'd') || (type == 'i')) -?????????????????????????????????????? pc +=printi(out, out_len, va_arg(args, long), -??????????????????????????????????????????? width, flags, type); +?????????????????????????????? if ((info->type== 'd') || (info->type== 'i')) +??????????????????????????????????????printi(info, va_arg(args, long)); ??????????????????????????????? else
-?????????????????????????????????????? pc += printi(out,out_len,va_arg(args, unsigned long), -??????????????????????????????????????????? width, flags, type); +??????????????????????????????????????printi(info, va_arg(args, unsigned long)); ??????????????????????????????? continue;
??????????????????????? }
??????????????????????? if (*format == 'c') {
??????????????????????????????? /* char are converted to int then pushed on the stack */ ??????????????????????????????? scr[0] =va_arg(args, int); ??????????????????????????????? scr[1] = '\0';
-?????????????????????????????? pc +=prints(out, out_len, scr, width, flags); +??????????????????????????????prints(info, scr); ??????????????????????????????? continue;
??????????????????????? }
??????????????? } else {
?literal:
-??????????????????????printc(out, out_len, *format);
-?????????????????????? ++pc;
+??????????????????????printc(info, *format);
??????????????? }
??????? }-?????? if (
use_tbuf&&console_tbuf_len<CONSOLE_TBUF_MAX) -??????????????nputs_all(console_tbuf, CONSOLE_TBUF_MAX - console_tbuf_len); -
-?????? return pc;
+?????? if (use_tbuf&&info->pos> 0)
+??????????????nputs_all(info->out, info->pos);
?}?int
sbi_sprintf(char *out, const char *format, ...)?{
???????va_listargs;
-?????? int retval;
+?????? structprint_infoinfo;
+?????? info.out = out;
+?????? info.len = 0;
+??????? if (unlikely(!out))
???????????????sbi_panic("sbi_sprintf called with NULL output string\n");???????
va_start(args, format);
-?????? retval =print(&out, NULL, format, args);
+??????print(&info, format, args);
???????va_end(args);-?????? return retval;
+?????? return info.pc;
?}?int
sbi_snprintf(char *out, u32 out_sz, const char *format, ...)?{
???????va_listargs;
-?????? int retval;
+?????? structprint_infoinfo;
+?????? info.out = out;
+?????? info.len =out_sz;
+??????? if (
unlikely(!out && out_sz != 0))
??????????????? sbi_panic("sbi_snprintf called with NULL output string and " ????????????????????????? "output size is not zero\n");???????
va_start(args, format);
-?????? retval =print(&out, &out_sz, format, args); +??????print(&info, format, args);
???????va_end(args);-?????? return retval;
+?????? return info.pc;
?}?int
sbi_printf(const char *format, ...)
?{
???????va_listargs;
-?????? int retval;
+?????? structprint_infoinfo;
+?????? info.out = NULL;
+?????? info.len = 0;
+???????
spin_lock(&console_out_lock);
???????va_start(args, format);
-?????? retval =print(NULL, NULL, format, args);
+??????print(&info, format, args);
???????va_end(args);
???????spin_unlock(&console_out_lock);-?????? return retval;
+?????? return info.pc;
?}?int
sbi_dprintf(const char *format, ...)
?{
???????va_listargs;
-?????? int retval = 0;
??????? structsbi_scratch*scratch =sbi_scratch_thishart_ptr(); +?????? structprint_infoinfo;
+?????? info.out = NULL;
+?????? info.len = 0;
+???????
va_start(args, format);
??????? if (scratch->options&SBI_SCRATCH_DEBUG_PRINTS) { ???????????????spin_lock(&console_out_lock); -?????????????? retval =print(NULL, NULL, format, args); +??????????????print(&info, format, args); ???????????????spin_unlock(&console_out_lock); ??????? }
???????va_end(args);-?????? return retval;
+?????? return info.pc;
?}?void
sbi_panic(const char *format, ...)
?{
???????va_listargs;
+?????? structprint_infoinfo;
+?????? info.out = NULL;
+?????? info.len = 0;???????
spin_lock(&console_out_lock);
???????va_start(args, format);
-??????print(NULL, NULL, format, args);
+??????print(&info, format, args);
???????va_end(args);
???????spin_unlock(&console_out_lock);--
2.39.2
PATCH v6 10/12 lib: sbi: Add print_info for sbi_console.c
Quoted 19 lines · On Tue, Jul 4, 2023 at 3:19?PM Xiang W <[email protected]> wrote:
? 2023-07-04???? 11:27 +0530?Anup Patel???
Quoted 8 lines · On Mon, Jun 12, 2023 at 1:40?PM Xiang W <[email protected]> wrote:
Quoted 3 lines
Create a structure
print_infofor print, this structure simplifies parameter passing.I don't see any advantage of this new structure.
NACK to this patch.
printi is reduced from 6 to 2 parameters
prints is reduced from 5 to 2 parameters
processing of pc reduced from 21 to 8This patch simplifies parameter passing. Because of this, the size of the code will also be reduced.
After applying the patch
sizebuild/lib/sbi/sbi_console.o
Quoted 7 lines
text data bss dec hex filename 3329 0 272 3601 e11 build/lib/sbi/sbi_console.obefor applying the patch
sizebuild/lib/sbi/sbi_console.otext data bss dec hex filename 3559 0 272 3831 ef7 build/lib/sbi/sbi_console.o
Mixing parameters across functions into a struct is a NOGO from myside.
Still NACK.
Regards,
Anup
Quoted 463 lines
Regards,
Xiang WQuoted 459 lines
Regards,
AnupQuoted 455 lines
Signed-off-by Xiang W <[email protected]>--- lib/sbi/sbi_console.c | 235 +++++++++++++++++++++--------------------- 1 file changed, 120 insertions(+), 115 deletions(-)lib/sbi/sbi_console.c +120
-116@@ -27,11 +27,22 @@typedef __builtin_va_list va_list ; +struct print_info { +char *out; +int len; +int pos; +int pc; +int flags; +int width; +int wcnt; +char type; +}; +static const struct sbi_console_device *console_dev =NULL ;static char console_tbuf[CONSOLE_TBUF_MAX ]; -static u32 console_tbuf_len;static spinlock_t console_out_lock =SPIN_LOCK_INITIALIZER ; +bool sbi_isprintable (char c) { if (((31 < c) && (c < 127)) || (c == '\f') || (c == '\r') ||@@ -128,70 +139,63 @@ unsigned long sbi_ngets(char *str, unsigned long len)return i; } -static void printc (char **out,u32 *out_len,char ch) +static void printc (struct print_info *info,char ch) { -if (!out) { +/* output to the console */ +if (info->out ==NULL ) {sbi_putc (ch); + info->pc++;return ; } -/* -* The *printf entry point functions have enforced that (*out) can -* only be null when out_len is non-null and its value is zero. -*/ -if (!out_len || *out_len >1 ) { - *(*out)++ = ch; - **out ='\0' ; - } +/* unknown buffer length */ +if (info->len ==0 ) + goto append; -if (out_len && *out_len >0 ) - --(*out_len); +/* The buffer is long enough to add new characters */ +if (info->len - info->pos >1 ) + goto append; +return ; + +append: + info->out[info->pos++] = ch; + info->out[info->pos] ='\0' ; + info->pc++; } -static int prints (char **out,u32 *out_len,const char *string,int width, -int flags) +static void prints (struct print_info *info,const char *string) { -int pc =0 ; - width -=sbi_strlen (string); -if (!(flags &PAD_RIGHT )) { -for (; width >0 ; --width) { -printc (out, out_len, flags &PAD_ZERO ?'0' :' ' ); - ++pc; - } - } -for (; *string; ++string) { -printc (out, out_len, *string); - ++pc; - } -for (; width >0 ; --width) { -printc (out, out_len,' ' ); - ++pc; + info->wcnt +=sbi_strlen (string); +if (!(info->flags &PAD_RIGHT )) { +for (; info->wcnt < info->width; info->wcnt++) +printc (info, info->flags &PAD_ZERO ?'0' :' ' ); } - -return pc; +for (; *string; ++string) +printc (info, *string); +for (; info->wcnt < info->width; info->wcnt++) +printc (info,' ' ); } -static int printi (char **out,u32 *out_len,long long i, -int width,int flags,int type) +static void printi (struct print_info *info,long long i) { -int pc =0 ;char *s, sign, letbase, print_buf[PRINT_BUF_LEN ];unsigned long long u, b, t; b =10 ; letbase ='a' ; -if (type =='o' ) +if (info->type =='o' ) b =8 ; -else if (type =='x' || type =='X' || type =='p' || type =='P' ) { +else if (info->type =='x' || info->type =='X' + || info->type =='p' || info->type =='P' ) { b =16 ; letbase &= ~0x20 ; - letbase |= type &0x20 ; + letbase |= info->type &0x20 ; } u = i; sign =0 ; -if (type =='i' || type =='d' ) { -if ((flags &PAD_SIGN ) && i >0 ) +if (info->type =='i' || info->type =='d' ) { +if ((info->flags &PAD_SIGN ) && i >0 ) sign ='+' ;if (i <0 ) { sign ='-' ;@@ -214,26 +218,23 @@ static int printi(char **out, u32 *out_len, long long i, } } -if (flags &PAD_ZERO ) { +if (info->flags &PAD_ZERO ) {if (sign) { -printc (out, out_len, sign); - ++pc; - --width; +printc (info, sign); + ++info->wcnt; } -if (i && (flags &PAD_ALTERNATE )) { +if (i && (info->flags &PAD_ALTERNATE )) {if (b ==16 || b ==8 ) { -printc (out, out_len,'0' ); - ++pc; - --width; +printc (info,'0' ); + ++info->wcnt; }if (b ==16 ) { -printc (out, out_len,'x' -'a' + letbase); - ++pc; - --width; +printc (info,'x' -'a' + letbase); + ++info->wcnt; } } }else { -if (i && (flags &PAD_ALTERNATE )) { +if (i && (info->flags &PAD_ALTERNATE )) {if (b ==16 ) *--s ='x' -'a' + letbase;if (b ==16 || b ==8 )@@ -243,15 +244,16 @@ static int printi(char **out, u32 *out_len, long long i, *--s = sign; } -return pc +prints (out, out_len, s, width, flags); +prints (info, s); } -static int char **out,u32 *out_len,const char *format,va_list args) +static void struct print_info *info,const char *format,va_list args) {bool flags_done; -int width, flags, pc =0 ; -char type, scr[2 ], *tout; -bool use_tbuf = (!out) ? true : false; +char scr[2 ]; +bool use_tbuf = (!info->out) ? true : false; + info->pos =0 ; + info->pc =0 ;/* * The console_tbuf is protected by console_out_lock and @@ -259,19 +261,15 @@ static int print(char **out, u32 *out_len, const char *format, va_list args)* when out == NULL. */ if (use_tbuf) { - console_tbuf_len =CONSOLE_TBUF_MAX ; - tout = console_tbuf; - out = &tout; - out_len = &console_tbuf_len; + info->out = console_tbuf; + info->len =CONSOLE_TBUF_MAX ; }for (; *format !=0 ; ++format) { -if (use_tbuf && !console_tbuf_len) { -nputs_all (console_tbuf,CONSOLE_TBUF_MAX ); - console_tbuf_len =CONSOLE_TBUF_MAX ; - tout = console_tbuf; - } - +if (use_tbuf && (info->len - info->pos <=1 )) { +nputs_all (info->out, info->pos); + info->pos =0 ; + }if (*format =='%' ) { ++format;if (*format =='\0' )@@ -279,21 +277,21 @@ static int print(char **out, u32 *out_len, const char *format, va_list args)if (*format =='%' ) goto literal;/* Get flags */ - flags =0 ; + info->flags =0 ; flags_done = false;while (!flags_done) {switch (*format) {case '-' : - flags |=PAD_RIGHT ; + info->flags |=PAD_RIGHT ;break ;case '+' : - flags |=PAD_SIGN ; + info->flags |=PAD_SIGN ;break ;case '#' : - flags |=PAD_ALTERNATE ; + info->flags |=PAD_ALTERNATE ;break ;case '0' : - flags |=PAD_ZERO ; + info->flags |=PAD_ZERO ;break ;case ' ' :case '\'' :@@ -306,153 +304,160 @@ static int print(char **out, u32 *out_len, const char *format, va_list args)if (!flags_done) ++format; } -if (flags &PAD_RIGHT ) - flags &= ~PAD_ZERO ; +if (info->flags &PAD_RIGHT ) + info->flags &= ~PAD_ZERO ;/* Get width */ - width =0 ; + info->width =0 ; + info->wcnt =0 ;for (; *format >='0' && *format <='9' ; ++format) { - width *=10 ; - width += *format -'0' ; + info->width *=10 ; + info->width += *format -'0' ; } + info->type = *format;if (*format =='s' ) { char *s = va_arg(args, char *); - pc +=prints (out, out_len, s ? s :"(null)" , - width, flags); +prints (info, s ? s :"(null)" );continue ; }if ((*format =='d' ) || (*format =='i' )) { - pc +=printi (out, out_len,va_arg (args, int), - width, flags, *format); +printi (info,va_arg (args, int));continue ; }if ((*format =='u' ) || (*format =='o' ) || (*format =='x' ) || (*format =='X' )) { - pc += printi(out, out_len, va_arg(args, unsigned int), - width, flags, *format); + printi(info, va_arg(args, unsigned int));continue ; }if ((*format =='p' ) || (*format =='P' )) { - pc += printi(out, out_len, (uintptr_t)va_arg(args, void*), - width, flags, *format); + printi(info, (uintptr_t)va_arg(args, void*));continue ; }if (*format =='l' ) { - type ='i' ; + info->type ='i' ;if (format[1 ] =='l' ) { ++format;if ((format[1 ] =='u' ) || (format[1 ] =='o' ) || (format[1 ] =='d' ) || (format[1 ] =='i' ) || (format[1 ] =='x' ) || (format[1 ] =='X' )) { ++format; - type = *format; + info->type = *format; } - pc += printi(out, out_len, va_arg(args, long long), - width, flags, type); + printi(info, va_arg(args, long long));continue ; }if ((format[1 ] =='u' ) || (format[1 ] =='o' ) || (format[1 ] =='d' ) || (format[1 ] =='i' ) || (format[1 ] =='x' ) || (format[1 ] =='X' )) { ++format; - type = *format; + info->type = *format; } -if ((type =='d' ) || (type =='i' )) - pc +=printi (out, out_len,va_arg (args, long), - width, flags, type); +if ((info->type =='d' ) || (info->type =='i' )) +printi (info,va_arg (args, long));else - pc += printi(out, out_len, va_arg(args, unsigned long), - width, flags, type); + printi(info, va_arg(args, unsigned long));continue ; }if (*format =='c' ) {/* char are converted to int then pushed on the stack */ scr[0 ] =va_arg (args, int); scr[1 ] ='\0' ; - pc +=prints (out, out_len, scr, width, flags); +prints (info, scr);continue ; } }else { literal: -printc (out, out_len, *format); - ++pc; +printc (info, *format); } } -if (use_tbuf && console_tbuf_len <CONSOLE_TBUF_MAX ) -nputs_all (console_tbuf,CONSOLE_TBUF_MAX - console_tbuf_len); - -return pc; +if (use_tbuf && info->pos >0 ) +nputs_all (info->out, info->pos); }int sbi_sprintf (char *out,const char *format, ...) {va_list args; -int retval; +struct print_info info; + info.out = out; + info.len =0 ; +if (unlikely (!out))sbi_panic ("sbi_sprintf called with NULL output string\n" );va_start (args, format); - retval =NULL , format, args); +va_end (args); -return retval; +return info.pc; }int sbi_snprintf (char *out,u32 out_sz,const char *format, ...) {va_list args; -int retval; +struct print_info info; + info.out = out; + info.len = out_sz; +if (unlikely (!out && out_sz !=0 ))sbi_panic ("sbi_snprintf called with NULL output string and " "output size is not zero\n" );va_start (args, format); - retval =va_end (args); -return retval; +return info.pc; }int sbi_printf (const char *format, ...) {va_list args; -int retval; +struct print_info info; + info.out =NULL ; + info.len =0 ; +spin_lock (&console_out_lock);va_start (args, format); - retval =NULL ,NULL , format, args); +va_end (args);spin_unlock (&console_out_lock); -return retval; +return info.pc; }int sbi_dprintf (const char *format, ...) {va_list args; -int retval =0 ;struct sbi_scratch *scratch =sbi_scratch_thishart_ptr (); +struct print_info info; + info.out =NULL ; + info.len =0 ; +va_start (args, format);if (scratch->options &SBI_SCRATCH_DEBUG_PRINTS ) {spin_lock (&console_out_lock); - retval =NULL ,NULL , format, args); +spin_unlock (&console_out_lock); }va_end (args); -return retval; +return info.pc; }void sbi_panic (const char *format, ...) {va_list args; +struct print_info info; + info.out =NULL ; + info.len =0 ;spin_lock (&console_out_lock);va_start (args, format); -NULL ,NULL , format, args); +va_end (args);spin_unlock (&console_out_lock); --2.39.2
PATCH v6 11/12 lib: sbi: Fix timing of clearing tbuf
In reply to Xiang WA single scan of the format char may add multiple characters to the tbuf, causing a buffer overflow. You should check if tbuf is full in printc so that it does not cause a buffer overflow.
--- lib/sbi/sbi_console.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-)
lib/sbi/sbi_console.c +10 -5
@@ -18,6 +18,8 @@#define PAD_ZERO 2#define PAD_ALTERNATE 4#define PAD_SIGN 8 +#define USED_TBUF (1 << (8 * sizeof(int) - 1)) +#define PRINT_BUF_LEN 64#define CONSOLE_TBUF_MAX 256
@@ -161,6 +163,11 @@ append: info->out[info->pos++] = ch; info->out[info->pos] ='\0' ; info->pc++; + +if ((info->flags &USED_TBUF ) && (info->len - info->pos <=1 )) { +nputs_all (info->out, info->pos); + info->pos =0 ; + } }static void prints (struct print_info *info,const char *string)
@@ -266,10 +273,9 @@ static void print(struct print_info *info, const char *format, va_list args) }for (; *format !=0 ; ++format) { -if (use_tbuf && (info->len - info->pos <=1 )) { -nputs_all (info->out, info->pos); - info->pos =0 ; - } + info->flags =0 ; +if (use_tbuf) + info->flags |=USED_TBUF ;if (*format =='%' ) { ++format;if (*format =='\0' )
@@ -277,7 +283,6 @@ static void print(struct print_info *info, const char *format, va_list args)if (*format =='%' ) goto literal;/* Get flags */ - info->flags =0 ; flags_done = false;while (!flags_done) {switch (*format) {
Signature · 1 line
2.39.2
PATCH v6 12/12 lib: sbi: Fix missing '\0' when buffer szie equal 1
In reply to Xiang WFix special case: sbi_snprintf(out, 1, ...), The previous code will not fill the buffer with any char.
--- lib/sbi/sbi_console.c | 8 ++++++++ 1 file changed, 8 insertions(+)
lib/sbi/sbi_console.c +8 -0
@@ -157,6 +157,14 @@ static void printc(struct print_info *info, char ch)/* The buffer is long enough to add new characters */ if (info->len - info->pos >1 ) goto append; + +/* +* special case: sbi_snprintf(out, 1, ...); +* This case never jumps to append, so add '\0' here +*/ +if (info->pos ==0 ) + info->out[0 ] ='\0' ; +return ; append:
Signature · 1 line
2.39.2