Skip to content

Commit f506970

Browse files
committed
update kklibc mem
1 parent 3017694 commit f506970

File tree

5 files changed

+22
-28
lines changed

5 files changed

+22
-28
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
commit 3017694afaa724b1ba8d50dbb061b323436da875
2+
Author: Alexeev Bronislav <[email protected]>
3+
Date: Wed Sep 3 22:38:46 2025 +0700
4+
5+
update kklibc
6+
17
commit 8a852d0488bd393fbc6c654f9854cd8c4626c122
28
Author: Alexeev Bronislav <[email protected]>
39
Date: Mon Sep 1 02:57:42 2025 +0700

src/kernel/kernel/sysinfo.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ void detect_cpu(void) {
3030
void detect_memory() {
3131
// используем битовую карту фреймов из пейджинга для определения памяти
3232
sys_info.total_memory = nframes * PAGE_SIZE;
33-
sys_info.free_memory = 0;
34-
sys_info.used_memory = 0;
33+
meminfo_t info = get_meminfo();
3534

3635
// подсчет свободных фреймов
3736
for (u32 i = 0; i < nframes; i++) {
@@ -40,7 +39,9 @@ void detect_memory() {
4039
}
4140
}
4241

43-
sys_info.used_memory = sys_info.total_memory - sys_info.free_memory;
42+
sys_info.free_memory = info.total_free;
43+
sys_info.used_memory = info.total_used;
44+
4445
sys_info.kernel_memory = free_mem_addr - HEAP_START;
4546
sys_info.heap_size = heap_current_end - HEAP_START; // динамик размер кучи
4647
}

src/kernel/kernel/utils.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,12 @@ void sysinfo_command() {
9898
system_info_t* info = get_system_info();
9999

100100
printf("CPU: %s, %d core(s)\n", info->cpu_vendor, info->cpu_cores);
101-
printf("Memory: %d MB total, %d MB free, %d MB used\n",
102-
info->total_memory / (1024),
103-
info->free_memory / (1024),
104-
info->used_memory / (1024));
105-
printf("Kernel memory: %d KB\n", info->kernel_memory / 1024);
106-
printf("Heap size: %d KB", info->heap_size / 1024);
101+
printf("Memory: %d KB total, %d KB free, %d KB used\n",
102+
info->total_memory / KB,
103+
info->free_memory / KB,
104+
info->used_memory / KB);
105+
printf("Kernel memory: %d KB\n", info->kernel_memory / KB);
106+
printf("Heap size: %d KB", info->heap_size / KB);
107107
}
108108

109109
void info_command_shell(char** args) {

src/kernel/kklibc/mem.c

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,11 @@ void heap_init() {
3939
}
4040

4141
int expand_heap(u32 size) {
42-
// Выравниваем размер вверх до границы страницы
4342
u32 num_pages = (size + PAGE_SIZE - 1) / PAGE_SIZE;
4443
u32 expand_size = num_pages * PAGE_SIZE;
4544

4645
printf("Expanding heap by %d bytes (%d pages)\n", expand_size, num_pages);
4746

48-
// Мы будем расширять кучу начиная с адреса heap_current_end
4947
u32 virtual_address = heap_current_end;
5048

5149
for (u32 i = 0; i < num_pages; i++) {
@@ -56,31 +54,26 @@ int expand_heap(u32 size) {
5654
return 0;
5755
}
5856

59-
// Выделяем фрейм для страницы (с флагами ядра и записи)
6057
alloc_frame(page, 0, 1);
6158

6259
// Обновляем виртуальный адрес для следующей страницы
6360
virtual_address += PAGE_SIZE;
6461
}
6562

66-
// Теперь нам нужно добавить новую область памяти в список свободных блоков
6763
mem_block_t* new_block = (mem_block_t*)heap_current_end;
68-
new_block->size = expand_size - sizeof(mem_block_t); // Учитываем заголовок
64+
new_block->size = expand_size - sizeof(mem_block_t);
6965
new_block->is_free = 1;
70-
new_block->next = free_blocks; // Добавляем в начало списка
66+
new_block->next = free_blocks;
7167

72-
// Обновляем глобальный список свободных блоков
7368
free_blocks = new_block;
7469

75-
// Обновляем текущий конец кучи
7670
heap_current_end += expand_size;
7771

7872
printf("Heap expanded successfully. New end: %x\n", heap_current_end);
7973
return 1;
8074
}
8175

8276
void* kmalloc(u32 size) {
83-
// Выравниваем размер до границы макроса BLOCK_SIZE
8477
if (size % BLOCK_SIZE != 0) {
8578
size += BLOCK_SIZE - (size % BLOCK_SIZE);
8679
}
@@ -90,16 +83,10 @@ void* kmalloc(u32 size) {
9083

9184
while (current) {
9285
if (current->is_free && current->size >= size) {
93-
// Нашли подходящий свободный блок: размер текущего больше чем размер
94-
// выделяемой памяти плюс размер структуры блока памяти и плюс размер
95-
// самого блока
9686
if (current->size > size + sizeof(mem_block_t) + BLOCK_SIZE) {
97-
// Можем разделить блок
98-
mem_block_t* new_block =
99-
(mem_block_t*)((u32)current + sizeof(mem_block_t) + size); // поинтер на новый блок
100-
new_block->size = current->size - size
101-
- sizeof(mem_block_t); // размер текущего - выделяемый - размер структуры
102-
new_block->is_free = 1; // свободен
87+
mem_block_t* new_block = (mem_block_t*)((u32)current + sizeof(mem_block_t) + size);
88+
new_block->size = current->size - size - sizeof(mem_block_t);
89+
new_block->is_free = 1;
10390
new_block->next = current->next;
10491

10592
current->size = size;

src/kernel/kklibc/mem.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#include "ctypes.h"
1212

1313
#define HEAP_START 0x100000 // Начинаем кучу с 1 МБ (выше ядра)
14-
#define HEAP_SIZE 0x1000000 // Размер кучи: 1 МБ
14+
#define HEAP_SIZE 0x1000000 // Размер кучи: 16 МБ
1515
#define BLOCK_SIZE 16 // Минимальный размер блока
1616

1717
extern u32 heap_current_end;

0 commit comments

Comments
 (0)