Use static buffers for mprintf and debug_printf, cut down on library dependencies
This commit is contained in:
@@ -1,10 +1,25 @@
|
||||
#include <debugconsole.h>
|
||||
#include <devices.h>
|
||||
#include <malloc.h>
|
||||
#include <printf.h>
|
||||
#include <stdatomic.h>
|
||||
#include <stddef.h>
|
||||
#include <system.h>
|
||||
|
||||
static char debug_printf_buffer[DEBUG_PRINTF_MAX];
|
||||
static atomic_flag debug_printf_buffer_lock = ATOMIC_FLAG_INIT;
|
||||
|
||||
static void debugconsole_printf_buffer_lock (void) {
|
||||
while (atomic_flag_test_and_set_explicit (&debug_printf_buffer_lock, memory_order_acquire)) {
|
||||
#if defined(__x86_64__)
|
||||
__asm__ volatile ("pause" ::: "memory");
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
static void debugconsole_printf_buffer_unlock (void) {
|
||||
atomic_flag_clear_explicit (&debug_printf_buffer_lock, memory_order_release);
|
||||
}
|
||||
|
||||
int debugconsole_print (const char* string, size_t len) {
|
||||
return device_do ("debugconsole", DEBUGCONSOLE_PUTSTR, (void*)string, (void*)&len, NULL, NULL);
|
||||
}
|
||||
@@ -13,18 +28,13 @@ void debug_printf (const char* fmt, ...) {
|
||||
va_list args;
|
||||
va_start (args, fmt);
|
||||
|
||||
char* buf = malloc (DEBUG_PRINTF_MAX);
|
||||
debugconsole_printf_buffer_lock ();
|
||||
|
||||
if (buf == NULL) {
|
||||
va_end (args);
|
||||
return;
|
||||
}
|
||||
|
||||
buf[0] = '\0';
|
||||
int len = vsnprintf (buf, DEBUG_PRINTF_MAX, fmt, args);
|
||||
int len = vsnprintf (debug_printf_buffer, DEBUG_PRINTF_MAX, fmt, args);
|
||||
|
||||
va_end (args);
|
||||
|
||||
debugconsole_print (buf, len);
|
||||
free (buf);
|
||||
debugconsole_print (debug_printf_buffer, len);
|
||||
|
||||
debugconsole_printf_buffer_unlock ();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user