Changeset 690ad20 in mainline for kernel/generic/src/console/console.c


Ignore:
Timestamp:
2025-04-17T15:29:16Z (5 days ago)
Author:
Jiří Zárevúcky <zarevucky.jiri@…>
Branches:
master
Children:
39e1b9a
Parents:
d5b37b6
git-author:
Jiří Zárevúcky <zarevucky.jiri@…> (2025-04-11 20:06:16)
git-committer:
Jiří Zárevúcky <zarevucky.jiri@…> (2025-04-17 15:29:16)
Message:

Convert kio buffer to bytes (part 1)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • kernel/generic/src/console/console.c

    rd5b37b6 r690ad20  
    4040#include <errno.h>
    4141#include <ipc/event.h>
     42#include <log.h>
    4243#include <panic.h>
    4344#include <preemption.h>
    4445#include <proc/task.h>
    45 #include <putchar.h>
    4646#include <stdatomic.h>
    4747#include <stdio.h>
    4848#include <stdlib.h>  /* malloc */
     49#include <str.h>
    4950#include <synch/mutex.h>
    5051#include <synch/spinlock.h>
     
    5354
    5455#define KIO_PAGES    8
    55 #define KIO_LENGTH   (KIO_PAGES * PAGE_SIZE / sizeof(char32_t))
     56#define KIO_LENGTH   (KIO_PAGES * PAGE_SIZE)
    5657
    5758/** Kernel log cyclic buffer */
    58 static char32_t kio[KIO_LENGTH];
     59static char kio[KIO_LENGTH];
    5960
    6061/** Kernel log initialized */
     
    7879/** Kernel log spinlock */
    7980IRQ_SPINLOCK_INITIALIZE(kio_lock);
     81
     82static IRQ_SPINLOCK_INITIALIZE(early_mbstate_lock);
     83static mbstate_t early_mbstate;
    8084
    8185static indev_t stdin_sink;
     
    245249        irq_spinlock_lock(&kio_lock, true);
    246250
     251        static mbstate_t mbstate;
     252
    247253        /* Print characters that weren't printed earlier */
    248254        while (kio_written != kio_processed) {
    249                 char32_t tmp = kio[kio_processed % KIO_LENGTH];
    250                 kio_processed++;
     255                size_t offset = kio_processed % KIO_LENGTH;
     256                size_t len = min(kio_written - kio_processed, KIO_LENGTH - offset);
     257                size_t bytes = 0;
     258
     259                char32_t ch = str_decode_r(&kio[offset], &bytes, len, U'�', &mbstate);
     260                assert(bytes <= 4);
     261                kio_processed += bytes;
    251262
    252263                /*
     
    256267                 */
    257268                irq_spinlock_unlock(&kio_lock, true);
    258                 stdout->op->write(stdout, tmp);
     269                stdout->op->write(stdout, ch);
    259270                irq_spinlock_lock(&kio_lock, true);
    260271        }
     
    263274}
    264275
    265 /** Put a character into the output buffer.
    266  *
    267  * The caller is required to hold kio_lock
    268  */
    269 void kio_push_char(const char32_t ch)
    270 {
    271         kio[kio_written % KIO_LENGTH] = ch;
    272         kio_written++;
    273 }
    274 
    275 void putuchar(const char32_t ch)
     276void kio_push_bytes(const char *s, size_t n)
     277{
     278        /* Skip the section we know we can't keep. */
     279        if (n > KIO_LENGTH) {
     280                size_t lost = n - KIO_LENGTH;
     281                kio_written += lost;
     282                s += lost;
     283                n -= lost;
     284        }
     285
     286        size_t offset = kio_written % KIO_LENGTH;
     287        if (offset + n > KIO_LENGTH) {
     288                size_t first = KIO_LENGTH - offset;
     289                size_t last = n - first;
     290                memcpy(kio + offset, s, first);
     291                memcpy(kio, s + first, last);
     292        } else {
     293                memcpy(kio + offset, s, n);
     294        }
     295
     296        kio_written += n;
     297}
     298
     299static void early_putstr(const char *s, size_t n)
     300{
     301        irq_spinlock_lock(&early_mbstate_lock, true);
     302
     303        size_t offset = 0;
     304        char32_t c;
     305
     306        while ((c = str_decode_r(s, &offset, n, U_SPECIAL, &early_mbstate)))
     307                early_putuchar(c);
     308
     309        irq_spinlock_unlock(&early_mbstate_lock, true);
     310}
     311
     312void putstr(const char *s, size_t n)
    276313{
    277314        bool ordy = ((stdout) && (stdout->op->write));
    278315
    279316        irq_spinlock_lock(&kio_lock, true);
    280         kio_push_char(ch);
     317        kio_push_bytes(s, n);
    281318        irq_spinlock_unlock(&kio_lock, true);
    282319
     
    295332                 * a no-op on certain hardware configurations.
    296333                 */
    297                 early_putuchar(ch);
    298         }
    299 
    300         /* Force notification on newline */
    301         if (ch == '\n')
     334                early_putstr(s, n);
     335        }
     336
     337        /* Force notification when containing a newline */
     338        if (memchr(s, '\n', n) != NULL)
    302339                kio_update(NULL);
    303340}
     
    336373                size_t first = KIO_LENGTH - offset;
    337374                size_t last = actual_read - first;
    338                 size_t first_bytes = first * sizeof(kio[0]);
    339                 size_t last_bytes = last * sizeof(kio[0]);
    340 
    341                 rc = copy_to_uspace(buf, &kio[offset], first_bytes);
     375
     376                rc = copy_to_uspace(buf, &kio[offset], first);
    342377                if (rc == EOK)
    343                         rc = copy_to_uspace(buf + first_bytes, &kio[0], last_bytes);
     378                        rc = copy_to_uspace(buf + first, &kio[0], last);
    344379        } else {
    345                 rc = copy_to_uspace(buf, &kio[offset], actual_read * sizeof(kio[0]));
     380                rc = copy_to_uspace(buf, &kio[offset], actual_read);
    346381        }
    347382
Note: See TracChangeset for help on using the changeset viewer.