hv:Clear up printf related definition

In hypervisor, all the parameter and return value printf related are
unsigned int, this patch is used to fix the function definitions.

v1->v2:
  *Modify the return value of various functions, such as printf(),
   vprintf(), charout(), do_printf(), charmem, print_pow2(),
   print_decimal to void due to never used, no necessary to use,
   or has already returned by param.
  *Delete the impossible judgement of param->emit due to the type
   is unsigned.

Tracked-On: #861
Signed-off-by: Junjun Shan <junjun.shan@intel.com>
Acked-by: Eddie Dong <eddie.dong@intel.com>
This commit is contained in:
Junjun Shan 2018-09-17 17:42:03 +08:00 committed by Wang, Minxia
parent ed06b8a7ca
commit 7ce0e6a395
4 changed files with 60 additions and 124 deletions

View File

@ -6,15 +6,15 @@
#include <hypervisor.h> #include <hypervisor.h>
static int charout(int cmd, const char *s_arg, uint32_t sz_arg, void *hnd) static void charout(size_t cmd, const char *s_arg, uint32_t sz_arg, void *hnd)
{ {
const char *s = s_arg; const char *s = s_arg;
uint32_t sz = sz_arg; uint32_t sz = sz_arg;
/* pointer to an integer to store the number of characters */ /* pointer to an integer to store the number of characters */
int *nchars = (int *)hnd; size_t *nchars = (size_t *)hnd;
/* working pointer */ /* working pointer */
const char *p = s; const char *p = s;
int len; size_t len;
/* copy mode ? */ /* copy mode ? */
if (cmd == PRINT_CMD_COPY) { if (cmd == PRINT_CMD_COPY) {
@ -33,17 +33,15 @@ static int charout(int cmd, const char *s_arg, uint32_t sz_arg, void *hnd)
} }
} }
return *nchars;
} }
int vprintf(const char *fmt, va_list args) void vprintf(const char *fmt, va_list args)
{ {
/* struct to store all necessary parameters */ /* struct to store all necessary parameters */
struct print_param param; struct print_param param;
/* the result of this function */
int res = 0;
/* argument fo charout() */ /* argument fo charout() */
int nchars = 0; size_t nchars = 0;
/* initialize parameters */ /* initialize parameters */
(void)memset(&param, 0U, sizeof(param)); (void)memset(&param, 0U, sizeof(param));
@ -51,27 +49,19 @@ int vprintf(const char *fmt, va_list args)
param.data = &nchars; param.data = &nchars;
/* execute the printf() */ /* execute the printf() */
res = do_print(fmt, &param, args); do_print(fmt, &param, args);
/* done */
return res;
} }
int printf(const char *fmt, ...) void printf(const char *fmt, ...)
{ {
/* variable argument list needed for do_print() */ /* variable argument list needed for do_print() */
va_list args; va_list args;
/* the result of this function */
int res;
va_start(args, fmt); va_start(args, fmt);
/* execute the printf() */ /* execute the printf() */
res = vprintf(fmt, args); vprintf(fmt, args);
/* destroy parameter list */ /* destroy parameter list */
va_end(args); va_end(args);
/* done */
return res;
} }

View File

@ -54,7 +54,7 @@ void asm_assert(int32_t line, const char *file, const char *txt);
* number if an error occurred. * number if an error occurred.
*/ */
int printf(const char *fmt, ...); void printf(const char *fmt, ...);
/** The well known vprintf() function. /** The well known vprintf() function.
* *
@ -66,7 +66,7 @@ int printf(const char *fmt, ...);
* number if an error occurred. * number if an error occurred.
*/ */
int vprintf(const char *fmt, va_list args); void vprintf(const char *fmt, va_list args);
#else /* HV_DEBUG */ #else /* HV_DEBUG */
@ -85,14 +85,14 @@ static inline void print_logmsg_buffer(__unused uint16_t pcpu_id)
#define ASSERT(x, ...) do { } while (0) #define ASSERT(x, ...) do { } while (0)
static inline int printf(__unused const char *fmt, ...) static inline void printf(__unused const char *fmt, ...)
{ {
return 0;
} }
static inline int vprintf(__unused const char *fmt, __unused va_list args) static inline void vprintf(__unused const char *fmt, __unused va_list args)
{ {
return 0;
} }
#endif /* HV_DEBUG */ #endif /* HV_DEBUG */

View File

@ -8,15 +8,15 @@
#define SPRINTF_H #define SPRINTF_H
/* Command for the emit function: copy string to output. */ /* Command for the emit function: copy string to output. */
#define PRINT_CMD_COPY 0x00000000 #define PRINT_CMD_COPY 0x00000000U
/* Command for the emit function: fill output with first character. */ /* Command for the emit function: fill output with first character. */
#define PRINT_CMD_FILL 0x00000001 #define PRINT_CMD_FILL 0x00000001U
/* Structure used to parse parameters and variables to subroutines. */ /* Structure used to parse parameters and variables to subroutines. */
struct print_param { struct print_param {
/* A pointer to the function that is used to emit characters. */ /* A pointer to the function that is used to emit characters. */
int (*emit)(int, const char *, uint32_t, void *); void (*emit)(size_t, const char *, uint32_t, void *);
/* An opaque pointer that is passed as third argument to the emit /* An opaque pointer that is passed as third argument to the emit
* function. * function.
*/ */
@ -42,7 +42,7 @@ struct print_param {
} vars; } vars;
}; };
int do_print(const char *fmt_arg, struct print_param *param, void do_print(const char *fmt_arg, struct print_param *param,
__builtin_va_list args); __builtin_va_list args);
/** The well known vsnprintf() function. /** The well known vsnprintf() function.
@ -56,7 +56,7 @@ int do_print(const char *fmt_arg, struct print_param *param,
* @return The number of bytes which would be written, even if the destination * @return The number of bytes which would be written, even if the destination
* is smaller. On error a negative number is returned. * is smaller. On error a negative number is returned.
*/ */
int vsnprintf(char *dst_arg, size_t sz_arg, const char *fmt, va_list args); size_t vsnprintf(char *dst_arg, size_t sz_arg, const char *fmt, va_list args);
/** The well known snprintf() function. /** The well known snprintf() function.
* *
@ -72,6 +72,6 @@ int vsnprintf(char *dst_arg, size_t sz_arg, const char *fmt, va_list args);
* @bug sz == 0 doesn't work * @bug sz == 0 doesn't work
*/ */
int snprintf(char *dest, int sz, const char *fmt, ...); size_t snprintf(char *dest, size_t sz, const char *fmt, ...);
#endif /* SPRINTF_H */ #endif /* SPRINTF_H */

View File

@ -185,7 +185,7 @@ static const char *get_length_modifier(const char *s_arg,
return s; return s;
} }
static int format_number(struct print_param *param) static void format_number(struct print_param *param)
{ {
/* contains the character used for padding */ /* contains the character used for padding */
char pad; char pad;
@ -193,8 +193,6 @@ static int format_number(struct print_param *param)
uint32_t width; uint32_t width;
/* number of characters to insert for width (w) and precision (p) */ /* number of characters to insert for width (w) and precision (p) */
uint32_t p = 0U, w = 0U; uint32_t p = 0U, w = 0U;
/* the result */
int res = 0;
/* initialize variables */ /* initialize variables */
width = param->vars.valuelen + param->vars.prefixlen; width = param->vars.valuelen + param->vars.prefixlen;
@ -225,11 +223,8 @@ static int format_number(struct print_param *param)
pad = '0'; pad = '0';
/* emit prefix, return early if an error occurred */ /* emit prefix, return early if an error occurred */
res = param->emit(PRINT_CMD_COPY, param->vars.prefix, param->emit(PRINT_CMD_COPY, param->vars.prefix,
param->vars.prefixlen, param->data); param->vars.prefixlen, param->data);
if ((param->vars.prefix != NULL) && (res < 0)) {
return res;
}
/* invalidate prefix */ /* invalidate prefix */
param->vars.prefix = NULL; param->vars.prefix = NULL;
@ -239,48 +234,31 @@ static int format_number(struct print_param *param)
/* fill the width with the padding character, return early if /* fill the width with the padding character, return early if
* an error occurred * an error occurred
*/ */
res = param->emit(PRINT_CMD_FILL, &pad, w, param->data); param->emit(PRINT_CMD_FILL, &pad, w, param->data);
if (res < 0) {
return res;
}
} }
/* emit prefix (if any), return early in case of an error */ /* emit prefix (if any), return early in case of an error */
res = param->emit(PRINT_CMD_COPY, param->vars.prefix, param->emit(PRINT_CMD_COPY, param->vars.prefix,
param->vars.prefixlen, param->data); param->vars.prefixlen, param->data);
if ((param->vars.prefix != NULL) && (res < 0)) {
return res;
}
/* insert additional 0's for precision, return early if an error /* insert additional 0's for precision, return early if an error
* occurred * occurred
*/ */
res = param->emit(PRINT_CMD_FILL, "0", p, param->data); param->emit(PRINT_CMD_FILL, "0", p, param->data);
if (res < 0) {
return res;
}
/* emit the pre-calculated result, return early in case of an error */ /* emit the pre-calculated result, return early in case of an error */
res = param->emit(PRINT_CMD_COPY, param->vars.value, param->emit(PRINT_CMD_COPY, param->vars.value,
param->vars.valuelen, param->data); param->vars.valuelen, param->data);
if (res < 0) {
return res;
}
/* handle left justification */ /* handle left justification */
if ((param->vars.flags & PRINT_FLAG_LEFT_JUSTIFY) != 0U) { if ((param->vars.flags & PRINT_FLAG_LEFT_JUSTIFY) != 0U) {
/* emit trailing blanks, return early in case of an error */ /* emit trailing blanks, return early in case of an error */
res = param->emit(PRINT_CMD_FILL, " ", w, param->data); param->emit(PRINT_CMD_FILL, " ", w, param->data);
if (res < 0) {
return res;
}
} }
/* done, return the last result */
return res;
} }
static int print_pow2(struct print_param *param, static void print_pow2(struct print_param *param,
uint64_t v_arg, uint32_t shift) uint64_t v_arg, uint32_t shift)
{ {
uint64_t v = v_arg; uint64_t v = v_arg;
@ -294,7 +272,6 @@ static int print_pow2(struct print_param *param,
const char (*digits)[HEX_DIGITS_LEN]; const char (*digits)[HEX_DIGITS_LEN];
/* mask to extract next character */ /* mask to extract next character */
uint64_t mask; uint64_t mask;
int ret;
/* calculate mask */ /* calculate mask */
mask = (1UL << shift) - 1UL; mask = (1UL << shift) - 1UL;
@ -330,15 +307,14 @@ static int print_pow2(struct print_param *param,
param->vars.value = pos; param->vars.value = pos;
param->vars.valuelen = (digitbuff + sizeof(digitbuff)) - pos; param->vars.valuelen = (digitbuff + sizeof(digitbuff)) - pos;
ret = format_number(param); format_number(param);
param->vars.value = NULL; param->vars.value = NULL;
param->vars.valuelen = 0U; param->vars.valuelen = 0U;
return ret;
} }
static int print_decimal(struct print_param *param, int64_t value) static void print_decimal(struct print_param *param, int64_t value)
{ {
/* max. required buffer for unsigned long long in decimal format */ /* max. required buffer for unsigned long long in decimal format */
char digitbuff[20]; char digitbuff[20];
@ -348,7 +324,6 @@ static int print_decimal(struct print_param *param, int64_t value)
union u_qword v; union u_qword v;
/* next value in 32/64 bit */ /* next value in 32/64 bit */
union u_qword nv; union u_qword nv;
int ret;
/* assume an unsigned 64 bit value */ /* assume an unsigned 64 bit value */
v.qword = ((uint64_t)value) & param->vars.mask; v.qword = ((uint64_t)value) & param->vars.mask;
@ -400,15 +375,14 @@ static int print_decimal(struct print_param *param, int64_t value)
param->vars.value = pos; param->vars.value = pos;
param->vars.valuelen = (digitbuff + sizeof(digitbuff)) - pos; param->vars.valuelen = (digitbuff + sizeof(digitbuff)) - pos;
ret = format_number(param); format_number(param);
param->vars.value = NULL; param->vars.value = NULL;
param->vars.valuelen = 0U; param->vars.valuelen = 0U;
return ret;
} }
static int print_string(struct print_param *param, const char *s) static void print_string(struct print_param *param, const char *s)
{ {
/* the length of the string (-1) if unknown */ /* the length of the string (-1) if unknown */
uint32_t len; uint32_t len;
@ -416,8 +390,6 @@ static int print_string(struct print_param *param, const char *s)
* width * width
*/ */
uint32_t w = 0U; uint32_t w = 0U;
/* the last result of the emit function */
int res;
len = strnlen_s(s, PRINT_STRING_MAX_LEN); len = strnlen_s(s, PRINT_STRING_MAX_LEN);
@ -437,36 +409,25 @@ static int print_string(struct print_param *param, const char *s)
* occurred * occurred
*/ */
if ((param->vars.flags & PRINT_FLAG_LEFT_JUSTIFY) == 0U) { if ((param->vars.flags & PRINT_FLAG_LEFT_JUSTIFY) == 0U) {
res = param->emit(PRINT_CMD_FILL, " ", w, param->data); param->emit(PRINT_CMD_FILL, " ", w, param->data);
if (res < 0) {
return res;
}
} }
res = param->emit(PRINT_CMD_COPY, s, len, param->data); param->emit(PRINT_CMD_COPY, s, len, param->data);
if (res < 0) {
return res;
}
/* emit additional characters on the right, return early if an error /* emit additional characters on the right, return early if an error
* occurred * occurred
*/ */
if ((param->vars.flags & PRINT_FLAG_LEFT_JUSTIFY) != 0U) { if ((param->vars.flags & PRINT_FLAG_LEFT_JUSTIFY) != 0U) {
res = param->emit(PRINT_CMD_FILL, " ", w, param->data); param->emit(PRINT_CMD_FILL, " ", w, param->data);
if (res < 0) {
return res;
}
} }
return res;
} }
int do_print(const char *fmt_arg, struct print_param *param, void do_print(const char *fmt_arg, struct print_param *param,
__builtin_va_list args) __builtin_va_list args)
{ {
const char *fmt = fmt_arg; const char *fmt = fmt_arg;
/* the result of this function */
int res = 0;
/* temp. storage for the next character */ /* temp. storage for the next character */
char ch; char ch;
/* temp. pointer to the start of an analysed character sequence */ /* temp. pointer to the start of an analysed character sequence */
@ -485,11 +446,8 @@ int do_print(const char *fmt_arg, struct print_param *param,
* pass all characters until the next '%' to the emit function. * pass all characters until the next '%' to the emit function.
* Return early if the function fails * Return early if the function fails
*/ */
res = param->emit(PRINT_CMD_COPY, start, fmt - start, param->emit(PRINT_CMD_COPY, start, fmt - start,
param->data); param->data);
if (res < 0) {
return res;
}
/* continue only if the '%' character was found */ /* continue only if the '%' character was found */
if (*fmt == '%') { if (*fmt == '%') {
@ -523,16 +481,16 @@ int do_print(const char *fmt_arg, struct print_param *param,
/* a single '%'? => print out a single '%' */ /* a single '%'? => print out a single '%' */
if (ch == '%') { if (ch == '%') {
res = param->emit(PRINT_CMD_COPY, &ch, 1U, param->emit(PRINT_CMD_COPY, &ch, 1U,
param->data); param->data);
} else if ((ch == 'd') || (ch == 'i')) { } else if ((ch == 'd') || (ch == 'i')) {
/* decimal number */ /* decimal number */
if ((param->vars.flags & if ((param->vars.flags &
PRINT_FLAG_LONG_LONG) != 0U) { PRINT_FLAG_LONG_LONG) != 0U) {
res = print_decimal(param, print_decimal(param,
__builtin_va_arg(args, long)); __builtin_va_arg(args, long));
} else { } else {
res = print_decimal(param, print_decimal(param,
__builtin_va_arg(args, int)); __builtin_va_arg(args, int));
} }
} }
@ -541,11 +499,11 @@ int do_print(const char *fmt_arg, struct print_param *param,
param->vars.flags |= PRINT_FLAG_UINT32; param->vars.flags |= PRINT_FLAG_UINT32;
if ((param->vars.flags & if ((param->vars.flags &
PRINT_FLAG_LONG_LONG) != 0U) { PRINT_FLAG_LONG_LONG) != 0U) {
res = print_decimal(param, print_decimal(param,
(int64_t)__builtin_va_arg(args, (int64_t)__builtin_va_arg(args,
uint64_t)); uint64_t));
} else { } else {
res = print_decimal(param, print_decimal(param,
(int64_t)__builtin_va_arg(args, (int64_t)__builtin_va_arg(args,
uint32_t)); uint32_t));
} }
@ -554,11 +512,11 @@ int do_print(const char *fmt_arg, struct print_param *param,
else if (ch == 'o') { else if (ch == 'o') {
if ((param->vars.flags & if ((param->vars.flags &
PRINT_FLAG_LONG_LONG) != 0U) { PRINT_FLAG_LONG_LONG) != 0U) {
res = print_pow2(param, print_pow2(param,
__builtin_va_arg(args, __builtin_va_arg(args,
uint64_t), 3U); uint64_t), 3U);
} else { } else {
res = print_pow2(param, print_pow2(param,
__builtin_va_arg(args, __builtin_va_arg(args,
uint32_t), 3U); uint32_t), 3U);
} }
@ -570,11 +528,11 @@ int do_print(const char *fmt_arg, struct print_param *param,
} }
if ((param->vars.flags & if ((param->vars.flags &
PRINT_FLAG_LONG_LONG) != 0U) { PRINT_FLAG_LONG_LONG) != 0U) {
res = print_pow2(param, print_pow2(param,
__builtin_va_arg(args, __builtin_va_arg(args,
uint64_t), 4U); uint64_t), 4U);
} else { } else {
res = print_pow2(param, print_pow2(param,
__builtin_va_arg(args, __builtin_va_arg(args,
uint32_t), 4U); uint32_t), 4U);
} }
@ -586,12 +544,12 @@ int do_print(const char *fmt_arg, struct print_param *param,
if (s == NULL) { if (s == NULL) {
s = "(null)"; s = "(null)";
} }
res = print_string(param, s); print_string(param, s);
} }
/* pointer argument */ /* pointer argument */
else if (ch == 'p') { else if (ch == 'p') {
param->vars.flags |= PRINT_FLAG_ALTERNATE_FORM; param->vars.flags |= PRINT_FLAG_ALTERNATE_FORM;
res = print_pow2(param, (uint64_t) print_pow2(param, (uint64_t)
__builtin_va_arg(args, void *), 4U); __builtin_va_arg(args, void *), 4U);
} }
/* single character argument */ /* single character argument */
@ -600,25 +558,19 @@ int do_print(const char *fmt_arg, struct print_param *param,
c[0] = __builtin_va_arg(args, int); c[0] = __builtin_va_arg(args, int);
c[1] = 0; c[1] = 0;
res = print_string(param, c); print_string(param, c);
} }
/* default: print the format specifier as it is */ /* default: print the format specifier as it is */
else { else {
res = param->emit(PRINT_CMD_COPY, start, param->emit(PRINT_CMD_COPY, start,
fmt - start, param->data); fmt - start, param->data);
} }
} }
/* return if an error occurred */
if (res < 0) {
return res;
}
} }
/* done. Return the result of the last emit function call */
return res;
} }
static int charmem(int cmd, const char *s_arg, uint32_t sz, void *hnd) static void charmem(size_t cmd, const char *s_arg, uint32_t sz, void *hnd)
{ {
const char *s = s_arg; const char *s = s_arg;
/* pointer to the snprint parameter list */ /* pointer to the snprint parameter list */
@ -650,18 +602,14 @@ static int charmem(int cmd, const char *s_arg, uint32_t sz, void *hnd)
(void)memset(p, (uint8_t)*s, n); (void)memset(p, (uint8_t)*s, n);
} }
return (int)n;
} }
int vsnprintf(char *dst_arg, size_t sz_arg, const char *fmt, va_list args) size_t vsnprintf(char *dst_arg, size_t sz_arg, const char *fmt, va_list args)
{ {
char *dst = dst_arg; char *dst = dst_arg;
uint32_t sz = sz_arg; uint32_t sz = sz_arg;
int res; size_t res = 0U;
if ((sz == 0U) || (dst == NULL)) {
return -1;
}
/* struct to store all necessary parameters */ /* struct to store all necessary parameters */
struct print_param param; struct print_param param;
@ -678,9 +626,7 @@ int vsnprintf(char *dst_arg, size_t sz_arg, const char *fmt, va_list args)
param.data = &snparam; param.data = &snparam;
/* execute the printf()*/ /* execute the printf()*/
if (do_print(fmt, &param, args) < 0) { do_print(fmt, &param, args);
return -1;
}
/* ensure the written string is NULL terminated */ /* ensure the written string is NULL terminated */
if (snparam.wrtn < sz) { if (snparam.wrtn < sz) {
@ -691,18 +637,18 @@ int vsnprintf(char *dst_arg, size_t sz_arg, const char *fmt, va_list args)
} }
/* return the number of chars which would be written */ /* return the number of chars which would be written */
res = (int)snparam.wrtn; res = snparam.wrtn;
/* done */ /* done */
return res; return res;
} }
int snprintf(char *dest, int sz, const char *fmt, ...) size_t snprintf(char *dest, size_t sz, const char *fmt, ...)
{ {
/* variable argument list needed for do_print() */ /* variable argument list needed for do_print() */
va_list args; va_list args;
/* the result of this function */ /* the result of this function */
int res; size_t res;
va_start(args, fmt); va_start(args, fmt);