Add a libc skeleton with a primitive malloc.
This commit is contained in:
parent
eb04242a59
commit
dcc05f2741
|
@ -1 +1,2 @@
|
|||
add_subdirectory(libc)
|
||||
add_subdirectory(mammoth)
|
||||
|
|
|
@ -0,0 +1,15 @@
|
|||
|
||||
add_library(libc STATIC
|
||||
src/malloc.cpp
|
||||
)
|
||||
|
||||
target_include_directories(libc
|
||||
PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}
|
||||
PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)
|
||||
|
||||
target_link_libraries(libc
|
||||
zion_lib
|
||||
)
|
||||
|
||||
set_target_properties(libc PROPERTIES
|
||||
COMPILE_FLAGS "${CMAKE_CXX_FLAGS} -ffreestanding -mgeneral-regs-only")
|
|
@ -0,0 +1,5 @@
|
|||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
typedef uint64_t size_t;
|
|
@ -0,0 +1,5 @@
|
|||
#pragma once
|
||||
|
||||
#include "stddef.h"
|
||||
|
||||
void* malloc(size_t size);
|
|
@ -0,0 +1,44 @@
|
|||
#include <zcall.h>
|
||||
|
||||
#include "stdlib.h"
|
||||
|
||||
namespace {
|
||||
class NaiveAllocator {
|
||||
public:
|
||||
constexpr static uint64_t kSize = 0x4000;
|
||||
NaiveAllocator() {}
|
||||
bool is_init() { return next_addr_ != 0; }
|
||||
void Init() {
|
||||
uint64_t vmmo_cap;
|
||||
uint64_t err = ZMemoryObjectCreate(kSize, &vmmo_cap);
|
||||
if (err != 0) {
|
||||
ZProcessExit(err);
|
||||
}
|
||||
err = ZAddressSpaceMap(Z_INIT_VMAS_SELF, 0, vmmo_cap, &next_addr_);
|
||||
max_addr_ = next_addr_ + kSize;
|
||||
}
|
||||
|
||||
void* Allocate(size_t size) {
|
||||
uint64_t addr = next_addr_;
|
||||
next_addr_ += size;
|
||||
if (next_addr_ >= max_addr_) {
|
||||
return 0;
|
||||
}
|
||||
return reinterpret_cast<void*>(addr);
|
||||
}
|
||||
|
||||
private:
|
||||
uint64_t next_addr_ = 0;
|
||||
uint64_t max_addr_ = 0;
|
||||
};
|
||||
|
||||
NaiveAllocator gAlloc;
|
||||
|
||||
} // namespace
|
||||
|
||||
void* malloc(size_t size) {
|
||||
if (!gAlloc.is_init()) {
|
||||
gAlloc.Init();
|
||||
}
|
||||
return gAlloc.Allocate(size);
|
||||
}
|
|
@ -20,6 +20,7 @@ add_executable(test2
|
|||
test2.cpp)
|
||||
|
||||
target_link_libraries(test2
|
||||
libc
|
||||
mammoth_lib)
|
||||
|
||||
set_target_properties(test2
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
#include <mammoth/channel.h>
|
||||
#include <mammoth/debug.h>
|
||||
#include <mammoth/thread.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
void thread_entry(void* a) {
|
||||
dbgln("In thread");
|
||||
|
@ -18,7 +19,7 @@ int main(uint64_t bootstrap_cap) {
|
|||
Thread t2(thread_entry, b);
|
||||
|
||||
uint64_t size = 10;
|
||||
char buff[10];
|
||||
char* buff = (char*)malloc(size);
|
||||
Channel c1;
|
||||
c1.adopt_cap(bootstrap_cap);
|
||||
check(c1.ReadStr(buff, &size));
|
||||
|
|
Loading…
Reference in New Issue