From e6199ac2c504b48366c5947904761357c8d247b8 Mon Sep 17 00:00:00 2001 From: sbosse Date: Sun, 12 Jan 2025 19:30:24 +0100 Subject: [PATCH] Sun 12 Jan 19:29:58 CET 2025 --- kernel/kmalloc.c | 45 +++++++++++++++++++++------------------------ 1 file changed, 21 insertions(+), 24 deletions(-) diff --git a/kernel/kmalloc.c b/kernel/kmalloc.c index d18ccdb..8c7ff60 100644 --- a/kernel/kmalloc.c +++ b/kernel/kmalloc.c @@ -14,14 +14,7 @@ See the file LICENSE for details. #define KMALLOC_STATE_FREE 0xa1a1a1a1 #define KMALLOC_STATE_USED 0xbfbfbfbf -struct kmalloc_chunk { - int state; - int length; - struct kmalloc_chunk *next; - struct kmalloc_chunk *prev; -}; - -static struct kmalloc_chunk *head = 0; +static struct kmalloc_chunk *kmalloc_head = 0; /* Initialize the linked list by creating a single chunk at @@ -31,11 +24,11 @@ free and has no next or previous chunks. void kmalloc_init(char *start, int length) { - head = (struct kmalloc_chunk *) start; - head->state = KMALLOC_STATE_FREE; - head->length = length; - head->next = 0; - head->prev = 0; + kmalloc_head = (struct kmalloc_chunk *) start; + kmalloc_head->state = KMALLOC_STATE_FREE; + kmalloc_head->length = length; + kmalloc_head->next = 0; + kmalloc_head->prev = 0; } /* @@ -76,7 +69,7 @@ void *kmalloc(int length) // then add one more unit to accommodate the chunk header length += KUNIT; - struct kmalloc_chunk *c = head; + struct kmalloc_chunk *c = kmalloc_head; while(1) { if(!c) { @@ -149,7 +142,7 @@ void kmalloc_debug() printf("state ptr prev next length\n"); - for(c = head; c; c = c->next) { + for(c = kmalloc_head; c; c = c->next) { if(c->state == KMALLOC_STATE_FREE) { printf("F"); } else if(c->state == KMALLOC_STATE_USED) { @@ -177,13 +170,13 @@ static int kmalloc_test_single_alloc(void) { char *ptr = kmalloc(128); struct kmalloc_chunk *next = 0; - int res = (unsigned long) ptr == (unsigned long) head + sizeof(struct kmalloc_chunk); - res &= head->state == KMALLOC_STATE_USED; - res &= head->length == 128 + sizeof(struct kmalloc_chunk); - res &= (char *) head->next == (char *) KMALLOC_START + head->length; - next = head->next; + int res = (unsigned long) ptr == (unsigned long) kmalloc_head + sizeof(struct kmalloc_chunk); + res &= kmalloc_head->state == KMALLOC_STATE_USED; + res &= kmalloc_head->length == 128 + sizeof(struct kmalloc_chunk); + res &= (char *) kmalloc_head->next == (char *) KMALLOC_START + kmalloc_head->length; + next = kmalloc_head->next; res &= next->state == KMALLOC_STATE_FREE; - res &= next->length == KMALLOC_LENGTH - head->length; + res &= next->length == KMALLOC_LENGTH - kmalloc_head->length; return res; } @@ -193,9 +186,9 @@ static int kmalloc_test_single_alloc_and_free(void) char *ptr = kmalloc(128); int res; kfree(ptr); - res = head->state == KMALLOC_STATE_FREE; - res &= head->next == 0; - res &= head->length == KMALLOC_LENGTH; + res = kmalloc_head->state == KMALLOC_STATE_FREE; + res &= kmalloc_head->next == 0; + res &= kmalloc_head->length == KMALLOC_LENGTH; return res; } @@ -223,3 +216,7 @@ int kmalloc_test(void) } return 1; } + +#ifdef KMALLOC_EXT + #include "kmalloc_ext.c" +#endif