SimpleKernel 1.17.0
Loading...
Searching...
No Matches
memory.cpp File Reference
#include <cpu_io.h>
#include <algorithm>
#include <bmalloc.hpp>
#include <cstddef>
#include "arch.h"
#include "basic_info.hpp"
#include "kernel.h"
#include "kernel_elf.hpp"
#include "kernel_log.hpp"
#include "virtual_memory.hpp"
Include dependency graph for memory.cpp:

Go to the source code of this file.

Functions

auto malloc (size_t size) -> void *
 
auto free (void *ptr) -> void
 
auto calloc (size_t num, size_t size) -> void *
 
auto realloc (void *ptr, size_t new_size) -> void *
 
auto aligned_alloc (size_t alignment, size_t size) -> void *
 
auto aligned_free (void *ptr) -> void
 
auto MemoryInit () -> void
 内存子系统初始化
 
auto MemoryInitSMP () -> void
 多核内存子系统初始化
 

Function Documentation

◆ aligned_alloc()

auto aligned_alloc ( size_t  alignment,
size_t  size 
) -> void*

Definition at line 58 of file memory.cpp.

58 {
59 if (allocator) {
60 return allocator->aligned_alloc(alignment, size);
61 }
62 return nullptr;
63}
Here is the caller graph for this function:

◆ aligned_free()

auto aligned_free ( void *  ptr) -> void

Definition at line 65 of file memory.cpp.

65 {
66 if (allocator) {
67 allocator->aligned_free(ptr);
68 }
69}
Here is the caller graph for this function:

◆ calloc()

auto calloc ( size_t  num,
size_t  size 
) -> void*

Definition at line 44 of file memory.cpp.

44 {
45 if (allocator) {
46 return allocator->calloc(num, size);
47 }
48 return nullptr;
49}

◆ free()

auto free ( void *  ptr) -> void

Definition at line 38 of file memory.cpp.

38 {
39 if (allocator) {
40 allocator->free(ptr);
41 }
42}
Here is the caller graph for this function:

◆ malloc()

auto malloc ( size_t  size)

Definition at line 31 of file memory.cpp.

31 {
32 if (allocator) {
33 return allocator->malloc(size);
34 }
35 return nullptr;
36}
Here is the caller graph for this function:

◆ MemoryInit()

auto MemoryInit ( ) -> void

内存子系统初始化

Definition at line 71 of file memory.cpp.

71 {
72 auto allocator_addr =
73 reinterpret_cast<void*>(cpu_io::virtual_memory::PageAlignUp(
74 BasicInfoSingleton::instance().elf_addr +
75 KernelElfSingleton::instance().GetElfSize()));
76 auto allocator_size = BasicInfoSingleton::instance().physical_memory_addr +
77 BasicInfoSingleton::instance().physical_memory_size -
78 reinterpret_cast<uint64_t>(allocator_addr);
79
80 klog::Info("bmalloc address: {:#x}, size: {:#X}",
81 static_cast<uint64_t>(reinterpret_cast<uintptr_t>(allocator_addr)),
82 static_cast<uint64_t>(allocator_size));
83
84 static bmalloc::Bmalloc<BmallocLogger> bmallocator(allocator_addr,
85 allocator_size);
86 allocator = &bmallocator;
87
88 // 初始化当前核心的虚拟内存
89 VirtualMemorySingleton::create();
90 VirtualMemorySingleton::instance().InitCurrentCore();
91
92 // 重新映射早期控制台地址(如果有的话)
93 if (SIMPLEKERNEL_EARLY_CONSOLE_BASE != 0) {
94 VirtualMemorySingleton::instance()
95 .MapMMIO(SIMPLEKERNEL_EARLY_CONSOLE_BASE,
97 .or_else([](Error err) -> Expected<void*> {
98 klog::Warn("Failed to remap early console MMIO: {}", err.message());
99 return std::unexpected(err);
100 });
101 }
102
103 klog::Info("Memory initialization completed");
104}
std::expected< T, Error > Expected
std::expected 别名模板
Definition expected.hpp:365
static constexpr size_t kPageSize
Definition cpu_io.h:63
auto PageAlignUp(uint64_t addr) -> uint64_t
Definition cpu_io.h:161
auto Info(etl::format_string< Args... > fmt, Args &&... args) -> void
以 INFO 级别记录日志
auto Warn(etl::format_string< Args... > fmt, Args &&... args) -> void
以 WARN 级别记录日志
错误类型,用于 std::expected
Definition expected.hpp:343
constexpr auto message() const -> const char *
Definition expected.hpp:358
Here is the call graph for this function:
Here is the caller graph for this function:

◆ MemoryInitSMP()

auto MemoryInitSMP ( ) -> void

多核内存子系统初始化

Definition at line 106 of file memory.cpp.

106 {
107 VirtualMemorySingleton::instance().InitCurrentCore();
108 klog::Info("SMP Memory initialization completed");
109}
Here is the call graph for this function:

◆ realloc()

auto realloc ( void *  ptr,
size_t  new_size 
) -> void*

Definition at line 51 of file memory.cpp.

51 {
52 if (allocator) {
53 return allocator->realloc(ptr, new_size);
54 }
55 return nullptr;
56}