Use static buffers for mprintf and debug_printf, cut down on library dependencies
All checks were successful
Build ISO image / build-and-deploy (push) Successful in 3m22s
Build documentation / build-and-deploy (push) Successful in 2m48s

This commit is contained in:
2026-04-03 17:38:39 +02:00
parent 6dd7a7c654
commit eab4cae083
6 changed files with 45 additions and 26 deletions

View File

@@ -5,8 +5,8 @@ $(eval $(call add_lib,libprocess))
$(eval $(call add_lib,libstring))
$(eval $(call add_lib,libdebugconsole))
$(eval $(call add_lib,libaux))
$(eval $(call add_lib,libmalloc))
$(eval $(call add_lib,libkb))
$(eval $(call add_lib,libmalloc))
cflags += -DPRINTF_INCLUDE_CONFIG_H=1

View File

@@ -1,7 +1,6 @@
#include <debugconsole.h>
#include <kb.h>
#include <limits.h>
#include <malloc.h>
#include <process.h>
#include <stddef.h>
#include <stdint.h>

View File

@@ -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 ();
}

View File

@@ -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 ();
}

View File

@@ -2,7 +2,6 @@ include ../make/ufuncs.mk
$(eval $(call add_lib,libprocess))
$(eval $(call add_lib,libstring))
$(eval $(call add_lib,libmalloc))
$(eval $(call add_lib,libdebugconsole))
$(eval $(call add_lib,libaux))

View File

@@ -23,10 +23,11 @@ void app_main (void) {
char recv_buffer[1024];
for (;;) {
debug_printf ("Waiting...\n");
memset (recv_buffer, 0, sizeof (recv_buffer));
mail_receive (recv_buffer, sizeof (recv_buffer));
debug_printf ("%s\n", recv_buffer);
debug_printf ("Recv: %s\n", recv_buffer);
}
} else if (strcmp (commandbuf, "send") == 0) {
if (env_get (process_get_pgid (), "payload", (void*)payloadbuf, sizeof (payloadbuf)) != ST_OK) {