Skip to content
This repository was archived by the owner on Jan 4, 2026. It is now read-only.
Merged

C++ #177

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion cmake/source.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ set(KERNEL_ETC_SOURCES

set(ATOMIC_IPC_SOURCES
kernel/atomic/Atomics.c
kernel/atomic/cpp/Spinlock.cpp
kernel/ipc/Ipc.c
)

Expand All @@ -57,11 +58,13 @@ set(EXECF_SOURCES
set(MM_SOURCES
mm/PMem.c
mm/MemOps.c
mm/VMem.c
mm/VMem.cpp
mm/StackGuard.c
mm/MemPool.c
mm/trace/StackTrace.c
mm/security/Cerberus.c
mm/dynamic/cpp/BuddyAllocator.cpp
mm/dynamic/cpp/new.cpp
mm/dynamic/c/Magazine.c
mm/PageFaultHandler.c
)
Expand Down Expand Up @@ -180,7 +183,9 @@ include_directories(
include
include/Switch
include/Vector
kernel
kernel/atomic
kernel/atomic/cpp
kernel/core
kernel/etc
kernel/execf
Expand All @@ -194,6 +199,7 @@ include_directories(
mm/asm
mm/dynamic
mm/dynamic/c
mm/dynamic/cpp
mm/dynamic/rust
mm/security
mm/trace
Expand Down
2 changes: 0 additions & 2 deletions drivers/virtio/VirtioBlk.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
#include <BlockDevice.h>
#include <Console.h>
#include <DriveNaming.h>
#include <Format.h>
#include <PCI/PCI.h>
#include <Spinlock.h>
#include <SpinlockRust.h>
#include <VMem.h>
#include <Virtio.h>
Expand Down
8 changes: 8 additions & 0 deletions include/Io.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@

#include <stdint.h>

#ifdef __cplusplus
extern "C" {
#endif

static inline void outb(uint16_t port, uint8_t val) {
__asm__ volatile ("outb %0, %1" : : "a"(val), "Nd"(port));
}
Expand Down Expand Up @@ -72,5 +76,9 @@ void cpuid(uint32_t leaf, uint32_t* eax, uint32_t* ebx, uint32_t* ecx, uint32_t*
uint64_t rdmsr(uint32_t msr);
void wrmsr(uint32_t msr, uint64_t value);

#ifdef __cplusplus
}
#endif

#endif

2 changes: 2 additions & 0 deletions include/stdbool.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#ifndef VOIDFRAME_STDBOOL_H
#define VOIDFRAME_STDBOOL_H
#ifndef __cplusplus
typedef int bool;
#define true 1
#define false 0
#endif
#endif
159 changes: 0 additions & 159 deletions kernel/atomic/Spinlock.h

This file was deleted.

50 changes: 50 additions & 0 deletions kernel/atomic/cpp/Spinlock.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include <Spinlock.h>

#ifdef __cplusplus

Spinlock::Spinlock() : locked(false) {}

void Spinlock::lock() {
while (__atomic_test_and_set(&locked, __ATOMIC_ACQUIRE)) {
while (__atomic_load_n(&locked, __ATOMIC_RELAXED))
__asm__ __volatile__("pause");
}
}

void Spinlock::unlock() {
__atomic_clear(&locked, __ATOMIC_RELEASE);
}

bool Spinlock::try_lock() {
return !__atomic_test_and_set(&locked, __ATOMIC_ACQUIRE);
}

SpinlockGuard::SpinlockGuard(Spinlock& lock) : lock(lock) {
lock.lock();
}

SpinlockGuard::~SpinlockGuard() {
lock.unlock();
}

#endif // __cplusplus

#ifdef __cplusplus
extern "C" {
#endif

void spinlock_lock(Spinlock* lock) {
lock->lock();
}

void spinlock_unlock(Spinlock* lock) {
lock->unlock();
}

bool spinlock_try_lock(Spinlock* lock) {
return lock->try_lock();
}

#ifdef __cplusplus
}
#endif
42 changes: 42 additions & 0 deletions kernel/atomic/cpp/Spinlock.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#pragma once

#ifdef __cplusplus

class Spinlock {
public:
Spinlock();
void lock();
void unlock();
bool try_lock();

private:
volatile int locked;
};

class SpinlockGuard {
public:
explicit SpinlockGuard(Spinlock& lock);
~SpinlockGuard();
SpinlockGuard(const SpinlockGuard&) = delete;
SpinlockGuard& operator=(const SpinlockGuard&) = delete;
SpinlockGuard(SpinlockGuard&&) = delete;
SpinlockGuard& operator=(SpinlockGuard&&) = delete;
private:
Spinlock& lock;
};

#endif // __cplusplus

#ifdef __cplusplus
extern "C" {
#endif

typedef Spinlock Spinlock;

void spinlock_lock(Spinlock* lock);
void spinlock_unlock(Spinlock* lock);
bool spinlock_try_lock(Spinlock* lock);

#ifdef __cplusplus
}
#endif
7 changes: 7 additions & 0 deletions kernel/core/Panic.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ typedef enum {
PANIC_ASSERTION = 0x0008
} PanicCode;

#ifdef __cplusplus
extern "C" {
#endif

// --- Public Panic API ---
void __attribute__((noreturn)) Panic(const char* message);
void __attribute__((noreturn)) PanicWithCode(const char* message, uint64_t error_code);
Expand All @@ -48,5 +52,8 @@ PanicWithContext(msg, PANIC_GENERAL, __FUNCTION__, __FILE__, __LINE__)
#define PANIC_CODE(msg, code) \
PanicWithContext(msg, code, __FUNCTION__, __FILE__, __LINE__)

#ifdef __cplusplus
}
#endif

#endif // PANIC_H
2 changes: 1 addition & 1 deletion mm/KernelHeap.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#define MAGAZINE_MAX_SIZE 1024

// Tier 2: Rust Allocator for general-purpose medium-sized allocations
#define RUST_MAX_SIZE (128 * 1024)
#define RUST_MAX_SIZE (64 * 1024)

// Helper function to wrap large VMem allocations with a header
static inline void* LargeBlockAlloc(size_t size) {
Expand Down
22 changes: 15 additions & 7 deletions mm/MemOps.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,22 @@

#include <stdint.h>

void* FastMemset(void* restrict dest, int value, uint64_t size);
void* FastMemcpy(void* restrict dest, const void* restrict src, uint64_t size);
int FastMemcmp(const void* restrict ptr1, const void* restrict ptr2, uint64_t size);
void FastZeroPage(void* restrict page);
#ifdef __cplusplus
extern "C" {
#endif

void* FastMemset(void* dest, int value, uint64_t size);
void* FastMemcpy(void* dest, const void* src, uint64_t size);
int FastMemcmp(const void* ptr1, const void* ptr2, uint64_t size);
void FastZeroPage(void* page);

// Wrapper for host compilers
void* memset(void* restrict dest, int value, unsigned long size);
void* memcpy(void* restrict dest, const void* restrict src, unsigned long size);
int memcmp(const void* restrict s1, const void* restrict s2, unsigned long);
void* memset(void* dest, int value, unsigned long size);
void* memcpy(void* dest, const void* src, unsigned long size);
int memcmp(const void* s1, const void* s2, unsigned long);

#ifdef __cplusplus
}
#endif

#endif
Loading