Skip to content

Commit

Permalink
karm-io: Simplified number formating code and introduced new format s…
Browse files Browse the repository at this point in the history
…pecifiers.
  • Loading branch information
sleepy-monax committed Mar 7, 2024
1 parent 3253115 commit 9cd8d90
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 55 deletions.
8 changes: 4 additions & 4 deletions src/kernel/loader/loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ void enterKernel(usize entry, usize payload, usize stack, usize vmm);
Res<> loadEntry(Entry const &entry) {
logInfo("loader: preparing payload...");
auto payloadMem = try$(Sys::mmap().read().size(kib(16)).mapMut());
logInfo("loader: payload at vaddr: 0x{x} paddr: 0x{x}", payloadMem.vaddr(), payloadMem.paddr());
logInfo("loader: payload at vaddr: {p} paddr: {p}", payloadMem.vaddr(), payloadMem.paddr());
Handover::Builder payload{payloadMem.mutBytes()};

payload.agent("opstart");
Expand All @@ -29,7 +29,7 @@ Res<> loadEntry(Entry const &entry) {
auto kernelMem = try$(Sys::mmap().map(kernelFile));
Elf::Image image{kernelMem.bytes()};
payload.add(Handover::FILE, 0, kernelMem.prange().as<USizeRange>());
logInfo("loader: kernel at vaddr: 0x{x} paddr: 0x{x}", kernelMem.vaddr(), kernelMem.paddr());
logInfo("loader: kernel at vaddr: {p} paddr: {p}", kernelMem.vaddr(), kernelMem.paddr());

if (not image.valid()) {
logError("loader: invalid kernel image");
Expand All @@ -39,7 +39,7 @@ Res<> loadEntry(Entry const &entry) {
logInfo("loader: setting up stack...");
auto stackMap = try$(Sys::mmap().stack().size(Hal::PAGE_SIZE * 16).mapMut());
payload.add(Handover::STACK, 0, stackMap.prange());
logInfo("loader: stack at vaddr: 0x{x} paddr: 0x{x}", stackMap.vaddr(), stackMap.paddr());
logInfo("loader: stack at vaddr: {p} paddr: {p}", stackMap.vaddr(), stackMap.paddr());

logInfo("loader: loading kernel image...");
for (auto prog : image.programs()) {
Expand All @@ -49,7 +49,7 @@ Res<> loadEntry(Entry const &entry) {

usize paddr = prog.vaddr() - Handover::KERNEL_BASE;
usize memsz = Hal::pageAlignUp(prog.memsz());
logInfo("loader: loading segment: paddr=0x{x}, vaddr=0x{x}, memsz=0x{x}, filesz=0x{x}", paddr, prog.vaddr(), memsz, prog.filez());
logInfo("loader: loading segment: paddr={p}, vaddr={p}, memsz={p}, filesz={p}", paddr, prog.vaddr(), memsz, prog.filez());

usize remaining = prog.memsz() - prog.filez();
memcpy((void *)paddr, prog.buf(), prog.filez());
Expand Down
25 changes: 13 additions & 12 deletions src/libs/karm-base/rc.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

#include "lock.h"
#include "opt.h"
#include "ref.h"

namespace Karm {

Expand All @@ -23,7 +22,7 @@ struct _Cell {
virtual void clear() = 0;
virtual Meta::Type<> inspect() = 0;

_Cell *collectAndRelease() {
void collectAndRelease() {
if (_strong == 0 and not _clear) {
clear();
_clear = true;
Expand All @@ -32,11 +31,9 @@ struct _Cell {
if (_strong == 0 and _weak == 0) {
_lock.release();
delete this;
return nullptr;
}

_lock.release();
return nullptr;
}

_Cell *refStrong() {
Expand All @@ -50,13 +47,13 @@ struct _Cell {
return this;
}

_Cell *derefStrong() {
void derefStrong() {
_lock.acquire();

_strong--;
if (_strong < 0)
panic("derefStrong() underflow");
return collectAndRelease();
collectAndRelease();
}

_Cell *refWeak() {
Expand All @@ -68,13 +65,13 @@ struct _Cell {
return this;
}

_Cell *derefWeak() {
void derefWeak() {
_lock.acquire();

_weak--;
if (_weak < 0)
panic("derefWeak() underflow");
return collectAndRelease();
collectAndRelease();
}

template <typename T>
Expand Down Expand Up @@ -127,8 +124,10 @@ struct Strong {
}

constexpr ~Strong() {
if (_cell)
_cell = _cell->derefStrong();
if (_cell) {
_cell->derefStrong();
_cell = nullptr;
}
}

constexpr Strong &operator=(Strong const &other) {
Expand Down Expand Up @@ -277,8 +276,10 @@ struct Weak {
}

constexpr ~Weak() {
if (_cell)
_cell = _cell->derefWeak();
if (_cell) {
_cell->derefWeak();
_cell = nullptr;
}
}

Opt<Strong<T>> upgrade() const {
Expand Down
121 changes: 82 additions & 39 deletions src/libs/karm-io/fmt.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <karm-base/reflect.h>
#include <karm-base/time.h>
#include <karm-base/tuple.h>
#include <karm-base/vec.h>
#include <karm-io/impls.h>
#include <karm-io/sscan.h>
#include <karm-io/traits.h>
Expand Down Expand Up @@ -279,29 +280,38 @@ struct Formatter<Cased<T>> {
/* --- Number Formatting ---------------------------------------------------- */

struct NumberFormater {
bool prefix;
bool isChar = false;
usize base = 10;
usize width = 0;
bool fillZero = false;
char fillChar = ' ';

void parse(Io::SScan &scan) {
if (scan.ended()) {
return;
}
Str formatPrefix() {
if (base == 16)
return "0x";

if (scan.peek() == '0') {
scan.next();
fillZero = true;
}
if (base == 8)
return "0o";

if (scan.ended()) {
return;
}
if (base == 2)
return "0b";

return "";
}

void parse(Io::SScan &scan) {
if (scan.skip('#'))
prefix = true;

if (scan.skip('0'))
fillChar = '0';

width = tryOr(scan.nextInt(), 0);

Rune c = scan.next();
if (scan.ended())
return;

Rune c = scan.next();
switch (c) {
case 'b':
base = 2;
Expand All @@ -320,8 +330,9 @@ struct NumberFormater {
break;

case 'p':
prefix = true;
base = 16;
fillZero = true;
fillChar = '0';
width = sizeof(usize) * 2;
break;

Expand All @@ -335,36 +346,30 @@ struct NumberFormater {
}

Res<usize> formatUnsigned(Io::TextWriter &writer, usize val) {
usize ogVal = val;
(void)ogVal;
auto digit = [](usize v) {
if (v < 10)
return '0' + v;
return 'a' + (v - 10);
};
usize i = 0;
Array<char, 128> buf;

InlineVec<char, 128> buf;

do {
buf[i++] = digit(val % base);
buf.pushBack(digit(val % base));
val /= base;
} while (val != 0 and i < buf.len());
} while (val != 0 and buf.len() < buf.cap());

if (width > 0 and width > i) {
usize n = width - i;
if (fillZero) {
for (usize j = 0; j < n; j++) {
buf[i++] = '0';
}
} else {
for (usize j = 0; j < n; j++) {
buf[i++] = ' ';
}
}
}
while (width > buf.len())
buf.pushBack(fillChar);

reverse(mutSub(buf));

reverse(mutSub(buf, 0, i));
return writer.writeStr({buf.buf(), i});
usize written = 0;
if (prefix)
written += try$(writer.writeStr(formatPrefix()));
written += try$(writer.writeStr(buf));

return Ok(written);
}

Res<usize> formatSigned(Io::TextWriter &writer, isize val) {
Expand All @@ -376,24 +381,62 @@ struct NumberFormater {
written += try$(formatUnsigned(writer, val));
return Ok(written);
}

Res<usize> formatRune(Io::TextWriter &writer, Rune val) {
if (val == '\'')
return writer.writeStr("\\'");

if (val == '\"')
return writer.writeStr("\\\"");

if (val == '\?')
return writer.writeStr("\\?");

if (val == '\\')
return writer.writeStr("\\\\");

if (val == '\a')
return writer.writeStr("\\a");

if (val == '\b')
return writer.writeStr("\\b");

if (val == '\f')
return writer.writeStr("\\f");

if (val == '\n')
return writer.writeStr("\\n");

if (val == '\r')
return writer.writeStr("\\r");

if (val == '\t')
return writer.writeStr("\\t");

if (val == '\v')
return writer.writeStr("\\v");

if (not isAsciiPrint(val))
return Io::format(writer, "\\u{x}", val);

return writer.writeRune(val);
}
};

template <typename T>
struct UnsignedFormatter : public NumberFormater {
Res<usize> format(Io::TextWriter &writer, T const &val) {
if (isChar)
return writer.writeRune(val);
return formatRune(writer, val);
return formatUnsigned(writer, val);
}
};

template <typename T>
struct SignedFormatter : public NumberFormater {
Res<usize> format(Io::TextWriter &writer, T const &val) {
if (isChar) {
if (isChar)
return writer.writeRune(val);
}

return formatSigned(writer, val);
}
};
Expand All @@ -415,7 +458,7 @@ struct Formatter<f64> {
if (fpart != 0.0) {
written += try$(writer.writeRune('.'));
formater.width = 6;
formater.fillZero = true;
formater.fillChar = '0';
fpart *= 1000000;
written += try$(formater.formatUnsigned(writer, (u64)fpart));
}
Expand Down
1 change: 1 addition & 0 deletions src/libs/karm-io/tests/test-fmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ test$(fmtUnsignedNumber) {
try$(testCase("1", 1u, "x"));
try$(testCase("7b", 123u, "x"));
try$(testCase("499602d2", 1234567890u, "x"));
try$(testCase("0x499602d2", 1234567890u, "#x"));

try$(testCase("586524", 586524, "04"));

Expand Down

0 comments on commit 9cd8d90

Please sign in to comment.