Skip to content

Commit

Permalink
Небольшие оптимизации
Browse files Browse the repository at this point in the history
  • Loading branch information
0Nera committed Feb 1, 2024
1 parent e31c9db commit 933baa5
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 34 deletions.
6 changes: 5 additions & 1 deletion include/arch.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ typedef struct task {
uint64_t cpu_time;
uint64_t cpu_time_expired;
uint64_t id;
char *id_str;
void *stack;

struct task *last;
Expand Down Expand Up @@ -59,12 +60,15 @@ typedef void (*int_entry_t)(struct frame *state);

extern lock_t task_lock;
extern uint64_t task_f_init;
extern task_t *current_task;

void arch_init( );
void task_init( );
void task_after_init( );
void task_switch( );
uint64_t task_new_thread(void (*func)(void *));
uint64_t task_new_thread(void (*func)(void *), char *name);
void task_del_current( );
void task_del(uint64_t id);
void cpu_init( );
void pic_init( );
void pit_init( );
Expand Down
2 changes: 1 addition & 1 deletion include/sys.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ typedef struct {
sys_info_t *(*get_info)( );
module_info_t *(*get_module)(char *module_id);
module_info_t *(*mod_list_get)(uint64_t *count);
uint64_t (*new_thread)(void (*func)(void *));
uint64_t (*new_thread)(void (*func)(void *), char *name);
void (*delete_thread)( );
time_t (*get_time)( );
} __attribute__((packed)) env_t;
Expand Down
55 changes: 34 additions & 21 deletions kernel/mem.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ static volatile struct limine_hhdm_request hhdm_request = { .id = LIMINE_HHDM_RE
struct mem_entry {
struct mem_entry *next;
bool free;
size_t size;
uint64_t task_id;
uint64_t size;
uint8_t data[0];
};

Expand All @@ -47,6 +48,9 @@ static uint64_t highest = 0;
// Количество записей в карте памяти
static uint64_t mmmap_count = 0;

extern task_t *current_task;
extern uint64_t full_init;

static const char memory_types[8][82] = { "Доступно", "Зарезервировано", "ACPI, можно освободить",
"ACPI NVS", "Плохая память", "Загрузчик, можно освободить",
"Ядро и модули", "Буфер кадра" };
Expand All @@ -60,11 +64,11 @@ void mem_dump_memory( ) {

while (curr) {
if (curr->next) {
LOG("->0x%x | %u мегабайт | %s | 0x%x\n", &curr->data, (curr->size) / 1024 / 1024,
curr->free ? memory_types[0] : memory_types[1], curr->next);
LOG("->0x%x | %u мегабайт | %s | 0x%x | поток %u\n", &curr->data, (curr->size) / 1024 / 1024,
curr->free ? memory_types[0] : memory_types[1], curr->next, curr->task_id);
} else {
LOG("->0x%x | %u мегабайт | %s | Это последний блок\n", &curr->data, (curr->size) / 1024 / 1024,
curr->free ? memory_types[0] : memory_types[1]);
LOG("->0x%x | %u мегабайт | %s | поток %u | Это последний блок\n", &curr->data, (curr->size) / 1024 / 1024,
curr->free ? memory_types[0] : memory_types[1], curr->task_id);
}
curr = curr->next;
}
Expand Down Expand Up @@ -153,7 +157,12 @@ static void merge_blocks(mem_entry_t *start) {
mem_entry_t *block = start;
while (block->next && block->next->free) {
block->size += block->next->size + sizeof(mem_entry_t);
block->next = block->next->next;

if (block->next->next) {
block->next = block->next->next;
continue;
}
block->next = NULL;
}
}

Expand Down Expand Up @@ -196,33 +205,37 @@ static void *alloc_align(size_t size, size_t alignment) {
mem_entry_t *curr = first_node;

while (curr) {
if (curr->free) {
if (curr->free && curr->size >= (alignment + sizeof(mem_entry_t) + size)) {
void *addr = curr->data + alignment - 1;
addr -= (uintptr_t)addr % alignment + sizeof(mem_entry_t);
mem_entry_t *second = (mem_entry_t *)addr;
if (curr->size >= (second->data - curr->data + size)) {
mem_entry_t *third = (mem_entry_t *)(second->data + size);

third->size = curr->size - (third->data - curr->data);
third->next = curr->next;
third->free = 1;
mem_entry_t *third = (mem_entry_t *)(second->data + size);
tool_memset(third, 0, sizeof(mem_entry_t));

second->size = size;
second->next = third;
second->free = 0;
third->size = curr->size - (third->data - curr->data);
third->next = curr->next;
third->free = 1;

if (curr != second) {
curr->next = second;
curr->size = (uintptr_t)second - (uintptr_t)curr->data;
curr->free = 1;
}
second->size = size;
second->next = third;
second->free = 0;
second->task_id = 0;

return second->data;
if (full_init) { second->task_id = current_task->id; }

if (curr != second) {
curr->next = second;
curr->size = (uintptr_t)second - (uintptr_t)curr->data;
curr->free = 1;
}

return second->data;
}

curr = curr->next;
}

return NULL;
}

Expand Down
27 changes: 20 additions & 7 deletions kernel/start.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,16 @@
#include <version.h>

uint64_t full_init = 0;
uint64_t dum = 0;

void finally( ) {
LOG("Готово! Для выхода из симуляции удерживайте: ESCAPE\n");
mod_after_init( );
for (;;) { task_switch( ); }
for (;;) { asm volatile("hlt"); }
}

void dummy( ) {
LOG("Поток %u\n", dum++);
task_del(current_task->id);
}

// Точка входа
Expand All @@ -32,6 +37,8 @@ void _start( ) {
fb_init( );
log_init_mem( );
arch_init( );
pit_init( );
task_init( );
mod_init( );

LOG("\t\t\t\t *** Базовая Модульная Платформа Операционных Систем "
Expand All @@ -41,16 +48,22 @@ void _start( ) {
LOG("\t\t\t\t *** Дата сборки: %s %s ***\n", __DATE__, __TIME__);

time_t time = rtc_get_time( );
LOG("Время: %2u:%2u.%2u, %2u.%2u.%2u\n", time.hours, time.minutes, time.second, time.day, time.month, time.year);
LOG("Время: %u:%u.%u, %u.%u.%u\n", time.hours, time.minutes, time.second, time.day, time.month, time.year);

pit_init( );
task_init( );

task_new_thread(finally);
task_new_thread(finally, "fin");

full_init = 1;
task_f_init = 1;

task_after_init( );

for (uint64_t i = 0; i < 6; i++) {
char *buf = mem_alloc(32);
tool_strcpy(buf, "dum");
tool_uint_to_str(i, 10, buf + 3);
task_new_thread(dummy, buf);
}

asm volatile("sti");

for (;;) { asm volatile("hlt"); }
Expand Down
4 changes: 1 addition & 3 deletions modlib/lib/tool.c
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,7 @@ char *trstr(char *str, char sym) {
else
left = 0x00U;

char *res = alloc(size);
memcpy(res, str + left, size);
return res;
return str + left;
}

char *strdup(char *str) {
Expand Down
2 changes: 1 addition & 1 deletion modules/ps2/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ static int ru = 1;
static char c_char = '\0';
static key_event_t keyboard_buffer;

static void virt_exit( ) {
void virt_exit( ) {
fb_printf("Выход для Bochs\n");
outw(0xB004, 0x2000);

Expand Down

0 comments on commit 933baa5

Please sign in to comment.