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

Commit 5d603a1

Browse files
authored
Merge pull request #177 from assembler-0/Development
C++
2 parents b3d695a + 9cacb45 commit 5d603a1

File tree

18 files changed

+1132
-1064
lines changed

18 files changed

+1132
-1064
lines changed

cmake/source.cmake

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ set(KERNEL_ETC_SOURCES
4343

4444
set(ATOMIC_IPC_SOURCES
4545
kernel/atomic/Atomics.c
46+
kernel/atomic/cpp/Spinlock.cpp
4647
kernel/ipc/Ipc.c
4748
)
4849

@@ -57,11 +58,13 @@ set(EXECF_SOURCES
5758
set(MM_SOURCES
5859
mm/PMem.c
5960
mm/MemOps.c
60-
mm/VMem.c
61+
mm/VMem.cpp
6162
mm/StackGuard.c
6263
mm/MemPool.c
6364
mm/trace/StackTrace.c
6465
mm/security/Cerberus.c
66+
mm/dynamic/cpp/BuddyAllocator.cpp
67+
mm/dynamic/cpp/new.cpp
6568
mm/dynamic/c/Magazine.c
6669
mm/PageFaultHandler.c
6770
)
@@ -180,7 +183,9 @@ include_directories(
180183
include
181184
include/Switch
182185
include/Vector
186+
kernel
183187
kernel/atomic
188+
kernel/atomic/cpp
184189
kernel/core
185190
kernel/etc
186191
kernel/execf
@@ -194,6 +199,7 @@ include_directories(
194199
mm/asm
195200
mm/dynamic
196201
mm/dynamic/c
202+
mm/dynamic/cpp
197203
mm/dynamic/rust
198204
mm/security
199205
mm/trace

drivers/virtio/VirtioBlk.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
#include <BlockDevice.h>
44
#include <Console.h>
55
#include <DriveNaming.h>
6-
#include <Format.h>
76
#include <PCI/PCI.h>
8-
#include <Spinlock.h>
97
#include <SpinlockRust.h>
108
#include <VMem.h>
119
#include <Virtio.h>

include/Io.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33

44
#include <stdint.h>
55

6+
#ifdef __cplusplus
7+
extern "C" {
8+
#endif
9+
610
static inline void outb(uint16_t port, uint8_t val) {
711
__asm__ volatile ("outb %0, %1" : : "a"(val), "Nd"(port));
812
}
@@ -72,5 +76,9 @@ void cpuid(uint32_t leaf, uint32_t* eax, uint32_t* ebx, uint32_t* ecx, uint32_t*
7276
uint64_t rdmsr(uint32_t msr);
7377
void wrmsr(uint32_t msr, uint64_t value);
7478

79+
#ifdef __cplusplus
80+
}
81+
#endif
82+
7583
#endif
7684

include/stdbool.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#ifndef VOIDFRAME_STDBOOL_H
22
#define VOIDFRAME_STDBOOL_H
3+
#ifndef __cplusplus
34
typedef int bool;
45
#define true 1
56
#define false 0
67
#endif
8+
#endif

kernel/atomic/Spinlock.h

Lines changed: 0 additions & 159 deletions
This file was deleted.

kernel/atomic/cpp/Spinlock.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#include <Spinlock.h>
2+
3+
#ifdef __cplusplus
4+
5+
Spinlock::Spinlock() : locked(false) {}
6+
7+
void Spinlock::lock() {
8+
while (__atomic_test_and_set(&locked, __ATOMIC_ACQUIRE)) {
9+
while (__atomic_load_n(&locked, __ATOMIC_RELAXED))
10+
__asm__ __volatile__("pause");
11+
}
12+
}
13+
14+
void Spinlock::unlock() {
15+
__atomic_clear(&locked, __ATOMIC_RELEASE);
16+
}
17+
18+
bool Spinlock::try_lock() {
19+
return !__atomic_test_and_set(&locked, __ATOMIC_ACQUIRE);
20+
}
21+
22+
SpinlockGuard::SpinlockGuard(Spinlock& lock) : lock(lock) {
23+
lock.lock();
24+
}
25+
26+
SpinlockGuard::~SpinlockGuard() {
27+
lock.unlock();
28+
}
29+
30+
#endif // __cplusplus
31+
32+
#ifdef __cplusplus
33+
extern "C" {
34+
#endif
35+
36+
void spinlock_lock(Spinlock* lock) {
37+
lock->lock();
38+
}
39+
40+
void spinlock_unlock(Spinlock* lock) {
41+
lock->unlock();
42+
}
43+
44+
bool spinlock_try_lock(Spinlock* lock) {
45+
return lock->try_lock();
46+
}
47+
48+
#ifdef __cplusplus
49+
}
50+
#endif

kernel/atomic/cpp/Spinlock.h

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#pragma once
2+
3+
#ifdef __cplusplus
4+
5+
class Spinlock {
6+
public:
7+
Spinlock();
8+
void lock();
9+
void unlock();
10+
bool try_lock();
11+
12+
private:
13+
volatile int locked;
14+
};
15+
16+
class SpinlockGuard {
17+
public:
18+
explicit SpinlockGuard(Spinlock& lock);
19+
~SpinlockGuard();
20+
SpinlockGuard(const SpinlockGuard&) = delete;
21+
SpinlockGuard& operator=(const SpinlockGuard&) = delete;
22+
SpinlockGuard(SpinlockGuard&&) = delete;
23+
SpinlockGuard& operator=(SpinlockGuard&&) = delete;
24+
private:
25+
Spinlock& lock;
26+
};
27+
28+
#endif // __cplusplus
29+
30+
#ifdef __cplusplus
31+
extern "C" {
32+
#endif
33+
34+
typedef Spinlock Spinlock;
35+
36+
void spinlock_lock(Spinlock* lock);
37+
void spinlock_unlock(Spinlock* lock);
38+
bool spinlock_try_lock(Spinlock* lock);
39+
40+
#ifdef __cplusplus
41+
}
42+
#endif

kernel/core/Panic.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ typedef enum {
2222
PANIC_ASSERTION = 0x0008
2323
} PanicCode;
2424

25+
#ifdef __cplusplus
26+
extern "C" {
27+
#endif
28+
2529
// --- Public Panic API ---
2630
void __attribute__((noreturn)) Panic(const char* message);
2731
void __attribute__((noreturn)) PanicWithCode(const char* message, uint64_t error_code);
@@ -48,5 +52,8 @@ PanicWithContext(msg, PANIC_GENERAL, __FUNCTION__, __FILE__, __LINE__)
4852
#define PANIC_CODE(msg, code) \
4953
PanicWithContext(msg, code, __FUNCTION__, __FILE__, __LINE__)
5054

55+
#ifdef __cplusplus
56+
}
57+
#endif
5158

5259
#endif // PANIC_H

mm/KernelHeap.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
#define MAGAZINE_MAX_SIZE 1024
1717

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

2121
// Helper function to wrap large VMem allocations with a header
2222
static inline void* LargeBlockAlloc(size_t size) {

mm/MemOps.h

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,22 @@
33

44
#include <stdint.h>
55

6-
void* FastMemset(void* restrict dest, int value, uint64_t size);
7-
void* FastMemcpy(void* restrict dest, const void* restrict src, uint64_t size);
8-
int FastMemcmp(const void* restrict ptr1, const void* restrict ptr2, uint64_t size);
9-
void FastZeroPage(void* restrict page);
6+
#ifdef __cplusplus
7+
extern "C" {
8+
#endif
9+
10+
void* FastMemset(void* dest, int value, uint64_t size);
11+
void* FastMemcpy(void* dest, const void* src, uint64_t size);
12+
int FastMemcmp(const void* ptr1, const void* ptr2, uint64_t size);
13+
void FastZeroPage(void* page);
1014

1115
// Wrapper for host compilers
12-
void* memset(void* restrict dest, int value, unsigned long size);
13-
void* memcpy(void* restrict dest, const void* restrict src, unsigned long size);
14-
int memcmp(const void* restrict s1, const void* restrict s2, unsigned long);
16+
void* memset(void* dest, int value, unsigned long size);
17+
void* memcpy(void* dest, const void* src, unsigned long size);
18+
int memcmp(const void* s1, const void* s2, unsigned long);
19+
20+
#ifdef __cplusplus
21+
}
22+
#endif
1523

1624
#endif

0 commit comments

Comments
 (0)