libc: malloc: fix heap_expand being called with a size measured in pages rather than bytes

This commit is contained in:
2026-05-31 17:23:12 +01:00
parent 1b7c3b1b0d
commit 2dc68a33ab
+17 -11
View File
@@ -1,5 +1,10 @@
#include "liballoc.h" #include "liballoc.h"
#include <magenta/config.h>
#include <magenta/types.h>
#include <stdio.h>
#include <string.h>
/** Durand's Amazing Super Duper Memory functions. */ /** Durand's Amazing Super Duper Memory functions. */
#define VERSION "1.1" #define VERSION "1.1"
@@ -206,6 +211,8 @@ static struct liballoc_major *allocate_new_page(heap_t *heap, unsigned int size)
return NULL; // uh oh, we ran out of memory. return NULL; // uh oh, we ran out of memory.
} }
memset(maj, 0x0, sizeof *maj);
maj->prev = NULL; maj->prev = NULL;
maj->next = NULL; maj->next = NULL;
maj->pages = st; maj->pages = st;
@@ -455,13 +462,11 @@ void *PREFIX(malloc)(heap_t *heap, size_t req_size)
if (diff if (diff
>= (size + sizeof(struct liballoc_minor))) { >= (size + sizeof(struct liballoc_minor))) {
// yay.... // yay....
min->next min->next = (struct liballoc_minor
= (struct liballoc_minor *)((uintptr_t)min
*)((uintptr_t)min + sizeof(
+ sizeof( struct liballoc_minor)
struct + min->size);
liballoc_minor)
+ min->size);
min->next->prev = min; min->next->prev = min;
min = min->next; min = min->next;
min->next = NULL; min->next = NULL;
@@ -506,8 +511,7 @@ void *PREFIX(malloc)(heap_t *heap, size_t req_size)
new_min = (struct liballoc_minor new_min = (struct liballoc_minor
*)((uintptr_t)min *)((uintptr_t)min
+ sizeof( + sizeof(
struct struct liballoc_minor)
liballoc_minor)
+ min->size); + min->size);
new_min->magic = LIBALLOC_MAGIC; new_min->magic = LIBALLOC_MAGIC;
@@ -822,9 +826,11 @@ int liballoc_unlock(heap_t *heap)
return 0; return 0;
} }
void *liballoc_alloc(heap_t *heap, size_t sz) void *liballoc_alloc(heap_t *heap, size_t nr_pages)
{ {
return heap_expand(heap, sz); size_t page_size = 0;
kern_config_get(KERN_CFG_PAGE_SIZE, &page_size, sizeof page_size);
return heap_expand(heap, nr_pages * page_size);
} }
int liballoc_free(heap_t *heap, void *p, size_t sz) int liballoc_free(heap_t *heap, void *p, size_t sz)