printf: add lock
Signed-off-by: Michael Shaw <m@michaelsshaw.com>
This commit is contained in:
parent
da4578d05b
commit
dda38aa265
1 changed files with 103 additions and 36 deletions
139
lib/printf.c
139
lib/printf.c
|
|
@ -40,6 +40,7 @@
|
||||||
|
|
||||||
#include <drivers/serial.h>
|
#include <drivers/serial.h>
|
||||||
#include <kernel/tty.h>
|
#include <kernel/tty.h>
|
||||||
|
#include <kernel/spinlock.h>
|
||||||
|
|
||||||
// define this globally (e.g. gcc -DPRINTF_INCLUDE_CONFIG_H ...) to include the
|
// define this globally (e.g. gcc -DPRINTF_INCLUDE_CONFIG_H ...) to include the
|
||||||
// printf_config.h header file
|
// printf_config.h header file
|
||||||
|
|
@ -154,7 +155,7 @@ static inline void _out_char(char character, void *buffer, size_t idx, size_t ma
|
||||||
if (character) {
|
if (character) {
|
||||||
if (tty_printf) {
|
if (tty_printf) {
|
||||||
tty_putchar(tty_printf, character);
|
tty_putchar(tty_printf, character);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -197,8 +198,15 @@ static unsigned int _atoi(const char **str)
|
||||||
}
|
}
|
||||||
|
|
||||||
// output the specified string in reverse, taking care of any zero-padding
|
// output the specified string in reverse, taking care of any zero-padding
|
||||||
static size_t _out_rev(out_fct_type out, char *buffer, size_t idx, size_t maxlen, const char *buf, size_t len, unsigned int width,
|
static size_t _out_rev(
|
||||||
unsigned int flags)
|
out_fct_type out,
|
||||||
|
char *buffer,
|
||||||
|
size_t idx,
|
||||||
|
size_t maxlen,
|
||||||
|
const char *buf,
|
||||||
|
size_t len,
|
||||||
|
unsigned int width,
|
||||||
|
unsigned int flags)
|
||||||
{
|
{
|
||||||
const size_t start_idx = idx;
|
const size_t start_idx = idx;
|
||||||
|
|
||||||
|
|
@ -225,8 +233,18 @@ static size_t _out_rev(out_fct_type out, char *buffer, size_t idx, size_t maxlen
|
||||||
}
|
}
|
||||||
|
|
||||||
// internal itoa format
|
// internal itoa format
|
||||||
static size_t _ntoa_format(out_fct_type out, char *buffer, size_t idx, size_t maxlen, char *buf, size_t len, bool negative,
|
static size_t _ntoa_format(
|
||||||
unsigned int base, unsigned int prec, unsigned int width, unsigned int flags)
|
out_fct_type out,
|
||||||
|
char *buffer,
|
||||||
|
size_t idx,
|
||||||
|
size_t maxlen,
|
||||||
|
char *buf,
|
||||||
|
size_t len,
|
||||||
|
bool negative,
|
||||||
|
unsigned int base,
|
||||||
|
unsigned int prec,
|
||||||
|
unsigned int width,
|
||||||
|
unsigned int flags)
|
||||||
{
|
{
|
||||||
// pad leading zeros
|
// pad leading zeros
|
||||||
if (!(flags & FLAGS_LEFT)) {
|
if (!(flags & FLAGS_LEFT)) {
|
||||||
|
|
@ -275,8 +293,17 @@ static size_t _ntoa_format(out_fct_type out, char *buffer, size_t idx, size_t ma
|
||||||
}
|
}
|
||||||
|
|
||||||
// internal itoa for 'long' type
|
// internal itoa for 'long' type
|
||||||
static size_t _ntoa_long(out_fct_type out, char *buffer, size_t idx, size_t maxlen, unsigned long value, bool negative,
|
static size_t _ntoa_long(
|
||||||
unsigned long base, unsigned int prec, unsigned int width, unsigned int flags)
|
out_fct_type out,
|
||||||
|
char *buffer,
|
||||||
|
size_t idx,
|
||||||
|
size_t maxlen,
|
||||||
|
unsigned long value,
|
||||||
|
bool negative,
|
||||||
|
unsigned long base,
|
||||||
|
unsigned int prec,
|
||||||
|
unsigned int width,
|
||||||
|
unsigned int flags)
|
||||||
{
|
{
|
||||||
char buf[PRINTF_NTOA_BUFFER_SIZE];
|
char buf[PRINTF_NTOA_BUFFER_SIZE];
|
||||||
size_t len = 0U;
|
size_t len = 0U;
|
||||||
|
|
@ -300,8 +327,17 @@ static size_t _ntoa_long(out_fct_type out, char *buffer, size_t idx, size_t maxl
|
||||||
|
|
||||||
// internal itoa for 'long long' type
|
// internal itoa for 'long long' type
|
||||||
#if defined(PRINTF_SUPPORT_LONG_LONG)
|
#if defined(PRINTF_SUPPORT_LONG_LONG)
|
||||||
static size_t _ntoa_long_long(out_fct_type out, char *buffer, size_t idx, size_t maxlen, unsigned long long value, bool negative,
|
static size_t _ntoa_long_long(
|
||||||
unsigned long long base, unsigned int prec, unsigned int width, unsigned int flags)
|
out_fct_type out,
|
||||||
|
char *buffer,
|
||||||
|
size_t idx,
|
||||||
|
size_t maxlen,
|
||||||
|
unsigned long long value,
|
||||||
|
bool negative,
|
||||||
|
unsigned long long base,
|
||||||
|
unsigned int prec,
|
||||||
|
unsigned int width,
|
||||||
|
unsigned int flags)
|
||||||
{
|
{
|
||||||
char buf[PRINTF_NTOA_BUFFER_SIZE];
|
char buf[PRINTF_NTOA_BUFFER_SIZE];
|
||||||
size_t len = 0U;
|
size_t len = 0U;
|
||||||
|
|
@ -328,20 +364,34 @@ static size_t _ntoa_long_long(out_fct_type out, char *buffer, size_t idx, size_t
|
||||||
|
|
||||||
#if defined(PRINTF_SUPPORT_EXPONENTIAL)
|
#if defined(PRINTF_SUPPORT_EXPONENTIAL)
|
||||||
// forward declaration so that _ftoa can switch to exp notation for values > PRINTF_MAX_FLOAT
|
// forward declaration so that _ftoa can switch to exp notation for values > PRINTF_MAX_FLOAT
|
||||||
static size_t _etoa(out_fct_type out, char *buffer, size_t idx, size_t maxlen, double value, unsigned int prec, unsigned int width,
|
static size_t
|
||||||
unsigned int flags);
|
_etoa(out_fct_type out,
|
||||||
|
char *buffer,
|
||||||
|
size_t idx,
|
||||||
|
size_t maxlen,
|
||||||
|
double value,
|
||||||
|
unsigned int prec,
|
||||||
|
unsigned int width,
|
||||||
|
unsigned int flags);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// internal ftoa for fixed decimal floating point
|
// internal ftoa for fixed decimal floating point
|
||||||
static size_t _ftoa(out_fct_type out, char *buffer, size_t idx, size_t maxlen, double value, unsigned int prec, unsigned int width,
|
static size_t
|
||||||
unsigned int flags)
|
_ftoa(out_fct_type out,
|
||||||
|
char *buffer,
|
||||||
|
size_t idx,
|
||||||
|
size_t maxlen,
|
||||||
|
double value,
|
||||||
|
unsigned int prec,
|
||||||
|
unsigned int width,
|
||||||
|
unsigned int flags)
|
||||||
{
|
{
|
||||||
char buf[PRINTF_FTOA_BUFFER_SIZE];
|
char buf[PRINTF_FTOA_BUFFER_SIZE];
|
||||||
size_t len = 0U;
|
size_t len = 0U;
|
||||||
double diff = 0.0;
|
double diff = 0.0;
|
||||||
|
|
||||||
// powers of 10
|
// powers of 10
|
||||||
static const double pow10[] = { 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000 };
|
static const double pow10[] = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000};
|
||||||
|
|
||||||
// test for special values
|
// test for special values
|
||||||
if (value != value)
|
if (value != value)
|
||||||
|
|
@ -349,8 +399,9 @@ static size_t _ftoa(out_fct_type out, char *buffer, size_t idx, size_t maxlen, d
|
||||||
if (value < -DBL_MAX)
|
if (value < -DBL_MAX)
|
||||||
return _out_rev(out, buffer, idx, maxlen, "fni-", 4, width, flags);
|
return _out_rev(out, buffer, idx, maxlen, "fni-", 4, width, flags);
|
||||||
if (value > DBL_MAX)
|
if (value > DBL_MAX)
|
||||||
return _out_rev(out, buffer, idx, maxlen, (flags & FLAGS_PLUS) ? "fni+" : "fni", (flags & FLAGS_PLUS) ? 4U : 3U,
|
return _out_rev(
|
||||||
width, flags);
|
out, buffer, idx, maxlen, (flags & FLAGS_PLUS) ? "fni+" : "fni", (flags & FLAGS_PLUS) ? 4U : 3U, width,
|
||||||
|
flags);
|
||||||
|
|
||||||
// test for very large values
|
// test for very large values
|
||||||
// standard printf behavior is to print EVERY whole number digit -- which could be 100s of characters overflowing your buffers == bad
|
// standard printf behavior is to print EVERY whole number digit -- which could be 100s of characters overflowing your buffers == bad
|
||||||
|
|
@ -457,8 +508,15 @@ static size_t _ftoa(out_fct_type out, char *buffer, size_t idx, size_t maxlen, d
|
||||||
|
|
||||||
#if defined(PRINTF_SUPPORT_EXPONENTIAL)
|
#if defined(PRINTF_SUPPORT_EXPONENTIAL)
|
||||||
// internal ftoa variant for exponential floating-point type, contributed by Martijn Jasperse <m.jasperse@gmail.com>
|
// internal ftoa variant for exponential floating-point type, contributed by Martijn Jasperse <m.jasperse@gmail.com>
|
||||||
static size_t _etoa(out_fct_type out, char *buffer, size_t idx, size_t maxlen, double value, unsigned int prec, unsigned int width,
|
static size_t
|
||||||
unsigned int flags)
|
_etoa(out_fct_type out,
|
||||||
|
char *buffer,
|
||||||
|
size_t idx,
|
||||||
|
size_t maxlen,
|
||||||
|
double value,
|
||||||
|
unsigned int prec,
|
||||||
|
unsigned int width,
|
||||||
|
unsigned int flags)
|
||||||
{
|
{
|
||||||
// check for NaN and special values
|
// check for NaN and special values
|
||||||
if ((value != value) || (value > DBL_MAX) || (value < -DBL_MAX)) {
|
if ((value != value) || (value > DBL_MAX) || (value < -DBL_MAX)) {
|
||||||
|
|
@ -553,8 +611,9 @@ static size_t _etoa(out_fct_type out, char *buffer, size_t idx, size_t maxlen, d
|
||||||
// output the exponential symbol
|
// output the exponential symbol
|
||||||
out((flags & FLAGS_UPPERCASE) ? 'E' : 'e', buffer, idx++, maxlen);
|
out((flags & FLAGS_UPPERCASE) ? 'E' : 'e', buffer, idx++, maxlen);
|
||||||
// output the exponent value
|
// output the exponent value
|
||||||
idx = _ntoa_long(out, buffer, idx, maxlen, (expval < 0) ? -expval : expval, expval < 0, 10, 0, minwidth - 1,
|
idx = _ntoa_long(
|
||||||
FLAGS_ZEROPAD | FLAGS_PLUS);
|
out, buffer, idx, maxlen, (expval < 0) ? -expval : expval, expval < 0, 10, 0, minwidth - 1,
|
||||||
|
FLAGS_ZEROPAD | FLAGS_PLUS);
|
||||||
// might need to right-pad spaces
|
// might need to right-pad spaces
|
||||||
if (flags & FLAGS_LEFT) {
|
if (flags & FLAGS_LEFT) {
|
||||||
while (idx - start_idx < width)
|
while (idx - start_idx < width)
|
||||||
|
|
@ -731,31 +790,35 @@ static int _vsnprintf(out_fct_type out, char *buffer, const size_t maxlen, const
|
||||||
if (flags & FLAGS_LONG_LONG) {
|
if (flags & FLAGS_LONG_LONG) {
|
||||||
#if defined(PRINTF_SUPPORT_LONG_LONG)
|
#if defined(PRINTF_SUPPORT_LONG_LONG)
|
||||||
const long long value = va_arg(va, long long);
|
const long long value = va_arg(va, long long);
|
||||||
idx = _ntoa_long_long(out, buffer, idx, maxlen,
|
idx = _ntoa_long_long(
|
||||||
(unsigned long long)(value > 0 ? value : 0 - value), value < 0, base,
|
out, buffer, idx, maxlen, (unsigned long long)(value > 0 ? value : 0 - value),
|
||||||
precision, width, flags);
|
value < 0, base, precision, width, flags);
|
||||||
#endif
|
#endif
|
||||||
} else if (flags & FLAGS_LONG) {
|
} else if (flags & FLAGS_LONG) {
|
||||||
const long value = va_arg(va, long);
|
const long value = va_arg(va, long);
|
||||||
idx = _ntoa_long(out, buffer, idx, maxlen, (unsigned long)(value > 0 ? value : 0 - value),
|
idx = _ntoa_long(
|
||||||
value < 0, base, precision, width, flags);
|
out, buffer, idx, maxlen, (unsigned long)(value > 0 ? value : 0 - value), value < 0,
|
||||||
|
base, precision, width, flags);
|
||||||
} else {
|
} else {
|
||||||
const int value = (flags & FLAGS_CHAR) ? (char)va_arg(va, int) :
|
const int value = (flags & FLAGS_CHAR) ? (char)va_arg(va, int) :
|
||||||
(flags & FLAGS_SHORT) ? (short int)va_arg(va, int) :
|
(flags & FLAGS_SHORT) ? (short int)va_arg(va, int) :
|
||||||
va_arg(va, int);
|
va_arg(va, int);
|
||||||
idx = _ntoa_long(out, buffer, idx, maxlen, (unsigned int)(value > 0 ? value : 0 - value),
|
idx = _ntoa_long(
|
||||||
value < 0, base, precision, width, flags);
|
out, buffer, idx, maxlen, (unsigned int)(value > 0 ? value : 0 - value), value < 0,
|
||||||
|
base, precision, width, flags);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// unsigned
|
// unsigned
|
||||||
if (flags & FLAGS_LONG_LONG) {
|
if (flags & FLAGS_LONG_LONG) {
|
||||||
#if defined(PRINTF_SUPPORT_LONG_LONG)
|
#if defined(PRINTF_SUPPORT_LONG_LONG)
|
||||||
idx = _ntoa_long_long(out, buffer, idx, maxlen, va_arg(va, unsigned long long), false, base,
|
idx = _ntoa_long_long(
|
||||||
precision, width, flags);
|
out, buffer, idx, maxlen, va_arg(va, unsigned long long), false, base, precision,
|
||||||
|
width, flags);
|
||||||
#endif
|
#endif
|
||||||
} else if (flags & FLAGS_LONG) {
|
} else if (flags & FLAGS_LONG) {
|
||||||
idx = _ntoa_long(out, buffer, idx, maxlen, va_arg(va, unsigned long), false, base,
|
idx = _ntoa_long(
|
||||||
precision, width, flags);
|
out, buffer, idx, maxlen, va_arg(va, unsigned long), false, base, precision, width,
|
||||||
|
flags);
|
||||||
} else {
|
} else {
|
||||||
const unsigned int value =
|
const unsigned int value =
|
||||||
(flags & FLAGS_CHAR) ? (unsigned char)va_arg(va, unsigned int) :
|
(flags & FLAGS_CHAR) ? (unsigned char)va_arg(va, unsigned int) :
|
||||||
|
|
@ -841,12 +904,13 @@ static int _vsnprintf(out_fct_type out, char *buffer, const size_t maxlen, const
|
||||||
#if defined(PRINTF_SUPPORT_LONG_LONG)
|
#if defined(PRINTF_SUPPORT_LONG_LONG)
|
||||||
const bool is_ll = sizeof(vaddr_t) == sizeof(long long);
|
const bool is_ll = sizeof(vaddr_t) == sizeof(long long);
|
||||||
if (is_ll) {
|
if (is_ll) {
|
||||||
idx = _ntoa_long_long(out, buffer, idx, maxlen, (vaddr_t)va_arg(va, void *), false, 16U,
|
idx = _ntoa_long_long(
|
||||||
precision, width, flags);
|
out, buffer, idx, maxlen, (vaddr_t)va_arg(va, void *), false, 16U, precision, width, flags);
|
||||||
} else {
|
} else {
|
||||||
#endif
|
#endif
|
||||||
idx = _ntoa_long(out, buffer, idx, maxlen, (unsigned long)((vaddr_t)va_arg(va, void *)), false,
|
idx = _ntoa_long(
|
||||||
16U, precision, width, flags);
|
out, buffer, idx, maxlen, (unsigned long)((vaddr_t)va_arg(va, void *)), false, 16U,
|
||||||
|
precision, width, flags);
|
||||||
#if defined(PRINTF_SUPPORT_LONG_LONG)
|
#if defined(PRINTF_SUPPORT_LONG_LONG)
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
@ -877,11 +941,14 @@ static int _vsnprintf(out_fct_type out, char *buffer, const size_t maxlen, const
|
||||||
|
|
||||||
int printf_(const char *format, ...)
|
int printf_(const char *format, ...)
|
||||||
{
|
{
|
||||||
|
static spinlock_t lock = 0;
|
||||||
|
spinlock_acquire(&lock);
|
||||||
va_list va;
|
va_list va;
|
||||||
va_start(va, format);
|
va_start(va, format);
|
||||||
char buffer[1];
|
char buffer[1];
|
||||||
const int ret = _vsnprintf(_out_char, buffer, (size_t)-1, format, va);
|
const int ret = _vsnprintf(_out_char, buffer, (size_t)-1, format, va);
|
||||||
va_end(va);
|
va_end(va);
|
||||||
|
spinlock_release(&lock);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -918,7 +985,7 @@ int fctprintf(void (*out)(char character, void *arg), void *arg, const char *for
|
||||||
{
|
{
|
||||||
va_list va;
|
va_list va;
|
||||||
va_start(va, format);
|
va_start(va, format);
|
||||||
const out_fct_wrap_type out_fct_wrap = { out, arg };
|
const out_fct_wrap_type out_fct_wrap = {out, arg};
|
||||||
const int ret = _vsnprintf(_out_fct, (char *)(vaddr_t)&out_fct_wrap, (size_t)-1, format, va);
|
const int ret = _vsnprintf(_out_fct, (char *)(vaddr_t)&out_fct_wrap, (size_t)-1, format, va);
|
||||||
va_end(va);
|
va_end(va);
|
||||||
return ret;
|
return ret;
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue