Kernel: lol
This commit is contained in:
3
BAN/.gitignore
vendored
Normal file
3
BAN/.gitignore
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
*.a
|
||||
*.d
|
||||
*.o
|
||||
@@ -15,7 +15,7 @@ LIBDIR?=$(EXEC_PREFIX)/lib
|
||||
|
||||
CFLAGS:=$(CFLAGS) -D__is_ban -Iinclude -ffreestanding -Wall -Wextra
|
||||
CPPFLAGS:=$(CPPFLAGS)
|
||||
LIBK_CFLAGS:=$(CFLAGS) -D__is_bank
|
||||
LIBK_CFLAGS:=$(CFLAGS) -D__is_kernel
|
||||
LIBK_CPPFLAGS:=$(CPPFLAGS)
|
||||
|
||||
ARCHDIR=arch/$(HOSTARCH)
|
||||
@@ -41,17 +41,17 @@ $(HOSTEDOBJS) \
|
||||
|
||||
BANK_OBJS=$(FREEOBJS:.o=.bank.o)
|
||||
|
||||
BINARIES=bank.a
|
||||
BINARIES=libbank.a
|
||||
|
||||
.PHONY: all always clean install install-headers install-libs
|
||||
.SUFFIXES: .o .bank.o .cpp .S
|
||||
|
||||
all: $(BINARIES)
|
||||
|
||||
ban.a: always $(OBJS)
|
||||
libban.a: always $(OBJS)
|
||||
cd $(BUILDDIR) && $(AR) rcs $@ $(OBJS)
|
||||
|
||||
bank.a: always $(LIBK_OBJS)
|
||||
libbank.a: always $(LIBK_OBJS)
|
||||
cd $(BUILDDIR) && $(AR) rcs $@ $(LIBK_OBJS)
|
||||
|
||||
.cpp.o:
|
||||
|
||||
@@ -1 +0,0 @@
|
||||
!<arch>
|
||||
@@ -2,14 +2,16 @@
|
||||
|
||||
#include <BAN/Formatter.h>
|
||||
|
||||
#if defined(__is_bank)
|
||||
#include <string.h>
|
||||
|
||||
#if defined(__is_kernel)
|
||||
#include <kernel/panic.h>
|
||||
#define MUST(error) { decltype(error) e = error; if (e.HasError()) { Kernel::panic("{}", e.GetError()); } }
|
||||
#define MUST(error) ({ auto e = error; if (e.IsError()) Kernel::panic("{}", e.GetError()); })
|
||||
#else
|
||||
#error "NOT IMPLEMENTED"
|
||||
#endif
|
||||
|
||||
#define TRY(error) { decltype(error) e = error; if (e.HasError()) return e; }
|
||||
#define TRY(error) ({ auto e = error; if (e.IsError()) return e; e.Value(); })
|
||||
|
||||
|
||||
class Error
|
||||
@@ -59,6 +61,7 @@ public:
|
||||
|
||||
bool IsError() const { return m_error; }
|
||||
const Error& GetError() const { return *m_error; }
|
||||
void Value() { }
|
||||
|
||||
private:
|
||||
Error* m_error;
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
#pragma once
|
||||
|
||||
#include <BAN/Errors.h>
|
||||
#include <kernel/kmalloc.h>
|
||||
|
||||
#if defined(__is_bank)
|
||||
#if defined(__is_kernel)
|
||||
#include <kernel/kmalloc.h>
|
||||
#else
|
||||
#include <stdlib.h>
|
||||
@@ -21,12 +20,12 @@ namespace BAN
|
||||
class Queue
|
||||
{
|
||||
private:
|
||||
#if defined(__is_bank)
|
||||
using allocator = kmalloc;
|
||||
using deallocator = kfree;
|
||||
#if defined(__is_kernel)
|
||||
static constexpr auto& allocator = kmalloc;
|
||||
static constexpr auto& deallocator = kfree;
|
||||
#else
|
||||
using allocator = malloc;
|
||||
using deallocator = free;
|
||||
static constexpr auto& allocator = malloc;
|
||||
static constexpr auto& deallocator = free;
|
||||
#endif
|
||||
|
||||
|
||||
@@ -59,13 +58,13 @@ namespace BAN
|
||||
template<typename T>
|
||||
Queue<T>::~Queue()
|
||||
{
|
||||
deallocator(m_data);
|
||||
Queue<T>::deallocator(m_data);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
ErrorOr<void> Queue<T>::Push(const T& value)
|
||||
{
|
||||
VerifyCapacity(m_size + 1);
|
||||
TRY(VerifyCapacity(m_size + 1));
|
||||
m_data[m_size++] = value;
|
||||
return {};
|
||||
}
|
||||
@@ -110,16 +109,18 @@ namespace BAN
|
||||
if (m_capacity > size)
|
||||
return {};
|
||||
|
||||
size_type new_cap = MAX(m_capacity * 1.5f, m_capacity + 1) * sizeof(T);
|
||||
void* new_data = allocator(new_cap);
|
||||
size_type new_cap = MAX(m_capacity * 1.5f, m_capacity + 1);
|
||||
void* new_data = Queue<T>::allocator(new_cap * sizeof(T));
|
||||
if (new_data == nullptr)
|
||||
return Error { .message = "Queue: out of memory", .error_code = ErrorCode::OutOfMemory };
|
||||
return Error::FromString("Queue: out of memory");
|
||||
|
||||
memcpy(new_data, m_data, m_size * sizeof(T));
|
||||
deallocator(m_data);
|
||||
Queue<T>::deallocator(m_data);
|
||||
|
||||
m_data = (T*)new_data;
|
||||
m_capacity = new_cap;
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user