Use static buffers for mprintf and debug_printf, cut down on library dependencies
This commit is contained in:
@@ -1,28 +1,38 @@
|
||||
#include <malloc.h>
|
||||
#include <mprintf.h>
|
||||
#include <printf.h>
|
||||
#include <process_self.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdatomic.h>
|
||||
#include <streams.h>
|
||||
#include <string.h>
|
||||
#include <system.h>
|
||||
|
||||
static char mprintf_buffer[MPRINTF_BUF_MAX];
|
||||
static atomic_flag mprintf_buffer_lock1 = ATOMIC_FLAG_INIT;
|
||||
|
||||
static void mprintf_buffer_lock (void) {
|
||||
while (atomic_flag_test_and_set_explicit (&mprintf_buffer_lock1, memory_order_acquire)) {
|
||||
#if defined(__x86_64__)
|
||||
__asm__ volatile ("pause" ::: "memory");
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
static void mprintf_buffer_unlock (void) {
|
||||
atomic_flag_clear_explicit (&mprintf_buffer_lock1, memory_order_release);
|
||||
}
|
||||
|
||||
void mprintf (const char* fmt, ...) {
|
||||
va_list args;
|
||||
va_start (args, fmt);
|
||||
|
||||
char* buf = malloc (MPRINTF_BUF_MAX);
|
||||
mprintf_buffer_lock ();
|
||||
|
||||
if (buf == NULL) {
|
||||
va_end (args);
|
||||
return;
|
||||
}
|
||||
|
||||
memset (buf, 0, MPRINTF_BUF_MAX);
|
||||
int len = vsnprintf (buf, MPRINTF_BUF_MAX, fmt, args);
|
||||
int len = vsnprintf (mprintf_buffer, MPRINTF_BUF_MAX, fmt, args);
|
||||
|
||||
va_end (args);
|
||||
|
||||
stream_write (process_get_pgid (), STREAM_OUT, buf, len);
|
||||
free (buf);
|
||||
stream_write (process_get_pgid (), STREAM_OUT, mprintf_buffer, len);
|
||||
|
||||
mprintf_buffer_unlock ();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user