From 342f39b74809c5bff290c50cc256cd5c609c5804 Mon Sep 17 00:00:00 2001 From: kamkow1 Date: Fri, 3 Apr 2026 17:38:39 +0200 Subject: [PATCH] Use static buffers for mprintf and debug_printf, cut down on library dependencies --- .gitea/workflows/build_iso.yaml | 22 ++++++--------------- .gitea/workflows/docs.yaml | 21 ++++++-------------- Dockerfile.deploy | 24 +++++++++++++++++++++++ Dockerfile => Dockerfile.localdev | 0 aux/docker_devel.sh | 2 +- init/Makefile | 2 +- init/init.c | 1 - libaux/mprintf.c | 32 ++++++++++++++++++++----------- libdebugconsole/debugconsole.c | 32 ++++++++++++++++++++----------- mailtest/Makefile | 1 - mailtest/mailtest.c | 3 ++- 11 files changed, 82 insertions(+), 58 deletions(-) create mode 100644 Dockerfile.deploy rename Dockerfile => Dockerfile.localdev (100%) diff --git a/.gitea/workflows/build_iso.yaml b/.gitea/workflows/build_iso.yaml index 5ec37f6..7872aef 100644 --- a/.gitea/workflows/build_iso.yaml +++ b/.gitea/workflows/build_iso.yaml @@ -7,26 +7,16 @@ on: jobs: build-and-deploy: - runs-on: ubuntu-22.04 + runs-on: mop3-latest + container: + image: git.kamkow1lair.pl/kamkow1/mop3/mop3-deploy:latest + credentials: + username: kamkow1 + password: ${{ secrets.GITEA_TOKEN }} steps: - name: Check out repository uses: actions/checkout@v4 - - name: Add LLVM APT repo - run: | - wget https://apt.llvm.org/llvm.sh - chmod +x llvm.sh - sudo ./llvm.sh 21 - - - name: Install software - run: | - sudo apt-add-repository universe - sudo apt-get update - sudo apt-get install -y make rsync clang-21 clang-tools-21 bear llvm-21 lld xorriso - sudo ln -sf /usr/bin/clang-21 /usr/bin/clang - sudo ln -sf /usr/bin/clang-doc-21 /usr/bin/clang-doc - sudo ln -sf /usr/bin/llvm-ar-21 /usr/bin/llvm-ar - - name: Build release run: ./aux/devel.sh diff --git a/.gitea/workflows/docs.yaml b/.gitea/workflows/docs.yaml index afb0246..c8440fa 100644 --- a/.gitea/workflows/docs.yaml +++ b/.gitea/workflows/docs.yaml @@ -7,25 +7,16 @@ on: jobs: build-and-deploy: - runs-on: ubuntu-22.04 + runs-on: mop3-latest + container: + image: git.kamkow1lair.pl/kamkow1/mop3/mop3-deploy:latest + credentials: + username: kamkow1 + password: ${{ secrets.GITEA_TOKEN }} steps: - name: Check out repository uses: actions/checkout@v4 - - name: Add LLVM APT repo - run: | - wget https://apt.llvm.org/llvm.sh - chmod +x llvm.sh - sudo ./llvm.sh 21 - - - name: Install software - run: | - sudo apt-get update - sudo apt-get install -y make rsync clang-21 clang-tools-21 bear llvm-21 - sudo ln -sf /usr/bin/clang-21 /usr/bin/clang - sudo ln -sf /usr/bin/clang-doc-21 /usr/bin/clang-doc - sudo ln -sf /usr/bin/llvm-ar-21 /usr/bin/llvm-ar - - name: Set up python3 uses: actions/setup-python@v5 with: diff --git a/Dockerfile.deploy b/Dockerfile.deploy new file mode 100644 index 0000000..af61288 --- /dev/null +++ b/Dockerfile.deploy @@ -0,0 +1,24 @@ +FROM ubuntu:22.04 + +ENV DEBIAN_FRONTEND=noninteractive + +RUN apt-get update + +RUN apt-get install -y software-properties-common + +RUN apt-add-repository universe && apt-get update + +RUN apt-get install -y wget + +RUN wget https://apt.llvm.org/llvm.sh -O /llvm.sh && chmod +x /llvm.sh && /llvm.sh 21 + +RUN wget https://deb.nodesource.com/setup_20.x -O /setup_node.sh && chmod +x /setup_node.sh && /setup_node.sh + +RUN apt-get install -y make rsync clang-21 clang-tools-21 llvm-21 bear lld xorriso \ + zip gnupg openssh-client nodejs git build-essential + +RUN ln -sf /usr/bin/clang-21 /usr/bin/clang && \ + ln -sf /usr/bin/clang-doc-21 /usr/bin/clang-doc && \ + ln -sf /usr/bin/llvm-ar-21 /usr/bin/llvm-ar + +CMD ["/bin/bash"] diff --git a/Dockerfile b/Dockerfile.localdev similarity index 100% rename from Dockerfile rename to Dockerfile.localdev diff --git a/aux/docker_devel.sh b/aux/docker_devel.sh index 3f710c5..1e6a3a6 100755 --- a/aux/docker_devel.sh +++ b/aux/docker_devel.sh @@ -1,3 +1,3 @@ #!/bin/sh -docker build --progress=plain --build-arg bt="$1" --output . . +docker build -f Dockerfile.localdev --progress=plain --build-arg bt="$1" --output . . diff --git a/init/Makefile b/init/Makefile index 8394a4f..bb5406a 100644 --- a/init/Makefile +++ b/init/Makefile @@ -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 diff --git a/init/init.c b/init/init.c index 0a56203..416fdfc 100644 --- a/init/init.c +++ b/init/init.c @@ -1,7 +1,6 @@ #include #include #include -#include #include #include #include diff --git a/libaux/mprintf.c b/libaux/mprintf.c index 6614d88..b918a93 100644 --- a/libaux/mprintf.c +++ b/libaux/mprintf.c @@ -1,28 +1,38 @@ -#include #include #include #include #include +#include #include #include #include +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 (); } diff --git a/libdebugconsole/debugconsole.c b/libdebugconsole/debugconsole.c index a0a4823..009ded2 100644 --- a/libdebugconsole/debugconsole.c +++ b/libdebugconsole/debugconsole.c @@ -1,10 +1,25 @@ #include #include -#include #include +#include #include #include +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 (); } diff --git a/mailtest/Makefile b/mailtest/Makefile index e55f26f..41f3967 100644 --- a/mailtest/Makefile +++ b/mailtest/Makefile @@ -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)) diff --git a/mailtest/mailtest.c b/mailtest/mailtest.c index 42a67c3..b19f786 100644 --- a/mailtest/mailtest.c +++ b/mailtest/mailtest.c @@ -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) {