-
-
Notifications
You must be signed in to change notification settings - Fork 130
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
impl-wasm: Fix logger and initial implemtation of the heap
- Loading branch information
1 parent
97b1b75
commit dd8a239
Showing
10 changed files
with
162 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,63 @@ | ||
#include <ce-heap/libheap.h> | ||
#include <karm-base/bits.h> | ||
#include <karm-base/lock.h> | ||
#include <karm-logger/logger.h> | ||
|
||
#include "externs.h" | ||
#include "karm-base/align.h" | ||
|
||
// MARK: WASM Implementation --------------------------------------------------- | ||
|
||
|
||
static Lock _heapLock; | ||
static Heap _heapImpl = { | ||
.ctx = nullptr, | ||
.alloc = [](void *, usize size) -> void * { | ||
return embedAlloc(size); | ||
}, | ||
.free = [](void *, void *, usize) { | ||
// Yeet | ||
}, | ||
.log = [](void *, enum HeapLogType type, char const *msg, va_list) { | ||
if (type == HEAP_ERROR) | ||
logError("heap: {}", msg); | ||
}, | ||
.root = nullptr, | ||
.best = nullptr, | ||
}; | ||
static Heap *_ensureHeap() { | ||
static Bits _heapBase = [] { | ||
Bits bits(MutBytes{__heap_base, (usize)__heap_end - (usize)__heap_base}); | ||
bits.fill(false); | ||
return bits; | ||
}(); | ||
static Heap _heapImpl = { | ||
.ctx = nullptr, | ||
.alloc = [](void *, usize size) -> void * { | ||
size = alignUp(size, PAGE_SIZE); | ||
auto range = _heapBase.alloc(size / PAGE_SIZE, 0, false); | ||
return (void *)(range.unwrap("out-of-memory").start * PAGE_SIZE); | ||
}, | ||
.free = [](void *, void *ptr, usize size) { | ||
size = alignUp(size, PAGE_SIZE); | ||
_heapBase.set(BitsRange{(usize)ptr / PAGE_SIZE, size / PAGE_SIZE}, false); | ||
}, | ||
.log = [](void *, enum HeapLogType type, char const *msg, va_list) { | ||
if (type == HEAP_ERROR) | ||
logError("heap: {}", msg); | ||
}, | ||
.root = nullptr, | ||
.best = nullptr, | ||
}; | ||
|
||
return &_heapImpl; | ||
} | ||
|
||
// MARK: New/Delete Implementation --------------------------------------------- | ||
|
||
void *operator new(usize size) { | ||
LockScope scope(_heapLock); | ||
return heap_calloc(&_heapImpl, size, 2); | ||
return heap_calloc(_ensureHeap(), size, 2); | ||
} | ||
|
||
void *operator new[](usize size) { | ||
LockScope scope(_heapLock); | ||
return heap_calloc(&_heapImpl, size, 2); | ||
return heap_calloc(_ensureHeap(), size, 2); | ||
} | ||
|
||
void operator delete(void *ptr) { | ||
LockScope scope(_heapLock); | ||
heap_free(&_heapImpl, ptr); | ||
heap_free(_ensureHeap(), ptr); | ||
} | ||
|
||
void operator delete[](void *ptr) { | ||
LockScope scope(_heapLock); | ||
heap_free(&_heapImpl, ptr); | ||
heap_free(_ensureHeap(), ptr); | ||
} | ||
|
||
void operator delete(void *ptr, usize) { | ||
LockScope scope(_heapLock); | ||
heap_free(&_heapImpl, ptr); | ||
heap_free(_ensureHeap(), ptr); | ||
} | ||
|
||
void operator delete[](void *ptr, usize) { | ||
LockScope scope(_heapLock); | ||
heap_free(&_heapImpl, ptr); | ||
heap_free(_ensureHeap(), ptr); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,91 +1,120 @@ | ||
#include <karm-sys/_embed.h> | ||
#include <karm-base/res.h> | ||
#include <karm-base/func.h> | ||
#include <karm-base/res.h> | ||
#include <karm-base/time.h> | ||
|
||
#include "externs.h" | ||
#include <karm-sys/_embed.h> | ||
|
||
#include "externs.h" | ||
|
||
namespace Karm::Sys::_Embed { | ||
struct JSConsole : public Sys::Fd { | ||
enum Proto { | ||
Log, | ||
Error | ||
} _proto; | ||
|
||
JSConsole(Proto proto) : _proto(proto) {} | ||
|
||
Sys::Handle handle() const override { | ||
return Handle{(usize)0}; | ||
} | ||
Res<usize> read(MutBytes) override { | ||
notImplemented(); | ||
} | ||
|
||
Res<usize> write(Bytes bytes) override { | ||
switch (_proto) { | ||
case Log: | ||
embedConsoleLog(bytes._buf, bytes._len); | ||
break; | ||
case Error: | ||
embedConsoleError(bytes._buf, bytes._len); | ||
break; | ||
} | ||
|
||
return Ok(bytes._len); | ||
} | ||
struct JSConsole : public Sys::Fd { | ||
enum Proto { | ||
LOG, | ||
ERROR | ||
} _proto; | ||
|
||
Res<usize> seek(Io::Seek) override { | ||
notImplemented(); | ||
} | ||
|
||
Res<usize> flush() override { | ||
return Ok(0uz); | ||
} | ||
Io::BufferWriter _buf; | ||
JSConsole(Proto proto) : _proto(proto) {} | ||
|
||
Res<Strong<Fd>> dup() override { | ||
notImplemented(); | ||
} | ||
Sys::Handle handle() const override { | ||
return Handle{(usize)_proto}; | ||
} | ||
Res<usize> read(MutBytes) override { | ||
notImplemented(); | ||
} | ||
|
||
Res<_Accepted> accept() override { | ||
notImplemented(); | ||
} | ||
Res<usize> write(Bytes bytes) override { | ||
return _buf.write(bytes); | ||
} | ||
|
||
Res<Stat> stat() override { | ||
return Ok<Stat>(); | ||
} | ||
Res<usize> seek(Io::Seek) override { | ||
notImplemented(); | ||
} | ||
|
||
Res<_Sent> send(Bytes, Slice<Handle>, SocketAddr) override { | ||
notImplemented(); | ||
Res<usize> flush() override { | ||
switch (_proto) { | ||
case LOG: | ||
embedConsoleLog(_buf.bytes().buf(), _buf.bytes().len()); | ||
break; | ||
case ERROR: | ||
embedConsoleError(_buf.bytes().buf(), _buf.bytes().len()); | ||
break; | ||
} | ||
|
||
Res<_Received> recv(MutBytes, MutSlice<Handle>) override { | ||
notImplemented(); | ||
} | ||
return _buf.flush(); | ||
} | ||
|
||
Res<> pack(Io::PackEmit &) override { | ||
notImplemented(); | ||
} | ||
}; | ||
Res<Strong<Fd>> dup() override { | ||
notImplemented(); | ||
} | ||
|
||
TimeStamp now() { | ||
auto span = embedGetTimeStamp(); | ||
return TimeStamp::epoch() + TimeSpan::fromMSecs(span); | ||
Res<_Accepted> accept() override { | ||
notImplemented(); | ||
} | ||
|
||
Res<Strong<Sys::Fd>> createIn() { | ||
return Ok(makeStrong<Sys::NullFd>()); | ||
Res<Stat> stat() override { | ||
return Ok<Stat>(); | ||
} | ||
|
||
Res<Strong<Sys::Fd>> createOut() { | ||
return Ok(makeStrong<JSConsole>(JSConsole::Log)); | ||
Res<_Sent> send(Bytes, Slice<Handle>, SocketAddr) override { | ||
notImplemented(); | ||
} | ||
|
||
Res<Strong<Sys::Fd>> createErr() { | ||
return Ok(makeStrong<JSConsole>(JSConsole::Error)); | ||
Res<_Received> recv(MutBytes, MutSlice<Handle>) override { | ||
notImplemented(); | ||
} | ||
|
||
Res<Strong<Sys::Fd>> unpackFd(Io::PackScan &) { | ||
Res<> pack(Io::PackEmit &) override { | ||
notImplemented(); | ||
} | ||
}; | ||
|
||
TimeStamp now() { | ||
auto span = embedGetTimeStamp(); | ||
return TimeStamp::epoch() + TimeSpan::fromMSecs(span); | ||
} | ||
|
||
Res<Strong<Sys::Fd>> createIn() { | ||
return Ok(makeStrong<Sys::NullFd>()); | ||
} | ||
|
||
Res<Strong<Sys::Fd>> createOut() { | ||
return Ok(makeStrong<JSConsole>(JSConsole::LOG)); | ||
} | ||
|
||
Res<Strong<Sys::Fd>> createErr() { | ||
return Ok(makeStrong<JSConsole>(JSConsole::ERROR)); | ||
} | ||
|
||
Res<Strong<Sys::Fd>> unpackFd(Io::PackScan &) { | ||
notImplemented(); | ||
} | ||
|
||
// MARK: System Informations --------------------------------------------------- | ||
|
||
Res<> populate(Sys::SysInfo &) { | ||
return Ok(); | ||
} | ||
|
||
Res<> populate(Sys::MemInfo &mem) { | ||
mem.physicalTotal = (usize)__heap_end - (usize)__heap_base; | ||
mem.physicalUsed = -1; | ||
mem.swapTotal = 0; | ||
mem.swapUsed = 0; | ||
mem.virtualTotal = 0; | ||
mem.virtualUsed = 0; | ||
return Ok(); | ||
} | ||
|
||
Res<> populate(Vec<Sys::CpuInfo> &) { | ||
return Ok(); | ||
} | ||
|
||
Res<> populate(Sys::UserInfo &) { | ||
return Ok(); | ||
} | ||
|
||
Res<> populate(Vec<Sys::UserInfo> &) { | ||
return Ok(); | ||
} | ||
} // namespace Karm::Sys::_Embed |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.