fix: malloc

This commit is contained in:
Starnakin 2024-11-28 11:20:45 +01:00
parent 295c513f2d
commit dc12949329
2 changed files with 25 additions and 11 deletions

View File

@ -55,6 +55,21 @@ static void set_mem_size(multiboot_info_t *mbd, uint32_t magic)
} }
} }
extern uint32_t boot_page_table1;
void get_kernel_page_used(void)
{
uint32_t *boot_pt = &boot_page_table1;
size_t i = 0;
size_t used_pages = 0;
for (; i < 1024; i++) {
if ((boot_pt[i] != 0) && (boot_pt[i] != (i << 12))) {
used_pages++;
}
}
kprintf("page used: %d/%d\n", used_pages, PT_SIZE);
}
void kernel_main(multiboot_info_t *mbd, uint32_t magic) void kernel_main(multiboot_info_t *mbd, uint32_t magic)
{ {
terminal_initialize(); terminal_initialize();
@ -66,28 +81,25 @@ void kernel_main(multiboot_info_t *mbd, uint32_t magic)
"I see no way to confuse an array of 256 seg:off pairs with a " "I see no way to confuse an array of 256 seg:off pairs with a "
"complex 8*unknown quantity -byte descriptor table. -- Troy " "complex 8*unknown quantity -byte descriptor table. -- Troy "
"Martin 03:50, 22 March 2009 (UTC)\n"); "Martin 03:50, 22 March 2009 (UTC)\n");
// PRINT_PTR(alloc_frame()); // PRINT_PTR(alloc_frame());
if (true) { if (false) {
void *start = alloc_pages(1); void *start = alloc_pages(1);
int *ptr; int *ptr;
uint32_t i; uint32_t i;
for (i = 1; (ptr = alloc_pages(PAGE_SIZE)); i++) { for (i = 1; (ptr = alloc_pages(PAGE_SIZE)); i++) {
memset(ptr, ~0, PAGE_SIZE); memset(ptr, i % 256, PAGE_SIZE);
*ptr = i; *ptr = i;
if (i == 4096 && false) if (i == 4096 && 0)
break; break;
} }
PRINT_UINT(i); // 32573 PRINT_UINT(i); // 32573
uint16_t bozo = 0;
for (uint32_t j = 1; j < i - 1; j++) { for (uint32_t j = 1; j < i - 1; j++) {
ptr = start + j * PAGE_SIZE; ptr = start + j * PAGE_SIZE;
kprintf("j=%d %p: %d\n", j, ptr, *ptr); for (uint32_t k = sizeof(*ptr);
if (*ptr != j) { k < PAGE_SIZE - sizeof(*ptr); k++)
bozo++; if (((uint8_t *)ptr)[k] != (j % 256))
if (bozo == 12) kprintf("cringe %u != %u\n",
break; ((uint8_t *)ptr)[k], j % 256);
}
} }
} else { } else {

View File

@ -5,6 +5,7 @@
#include "debug.h" #include "debug.h"
#include "kprintf.h" #include "kprintf.h"
#include "memory.h" #include "memory.h"
#include "string.h"
#include "utils.h" #include "utils.h"
static int16_t find_next_block(size_t nb_pages, uint16_t *pd_index_ptr, static int16_t find_next_block(size_t nb_pages, uint16_t *pd_index_ptr,
@ -52,6 +53,7 @@ void *alloc_pages(size_t size)
} }
page_table[i] = ((uint32_t)frame & PAGE_MASK) | INIT_FLAGS; page_table[i] = ((uint32_t)frame & PAGE_MASK) | INIT_FLAGS;
} }
memset((void *)GET_PAGE_ADDR(pd_index, index), 0, nb_pages * PAGE_SIZE);
return (void *)GET_PAGE_ADDR(pd_index, index); return (void *)GET_PAGE_ADDR(pd_index, index);
} }