diff --git a/core/alloc.c b/core/alloc.c index 5a96d28..4258a24 100644 --- a/core/alloc.c +++ b/core/alloc.c @@ -12,7 +12,7 @@ struct slabDesc *slubArray[SLUB_SIZE]; int initSlabDesc(struct slabDesc **desc, size_t size); -int formatPage(struct slabDesc *desc, size_t size); +static int formatPage(struct slabDesc *desc, size_t size); int initSlab(void) { @@ -44,7 +44,7 @@ int initSlabDesc(struct slabDesc **desc, size_t size) return formatPage((*desc), size); } -int formatPage(struct slabDesc *desc, size_t size) +static int formatPage(struct slabDesc *desc, size_t size) { char *cur = (char *)desc + sizeof(struct slabDesc); ulong i; diff --git a/core/assert.h b/core/assert.h new file mode 100644 index 0000000..dad126d --- /dev/null +++ b/core/assert.h @@ -0,0 +1,21 @@ +#pragma once +#include "stack.h" + +#define assert(p) do { \ + if (!(p)) { \ + printf("BUG at %s:%d assert(%s)\n", \ + __FILE__, __LINE__, #p); \ + printStackTrace(5); \ + while(1){} \ + } \ +} while (0) + +#define assertmsg(p, ...) do { \ + if (!(p)) { \ + printf("BUG at %s:%d assert(%s)\n", \ + __FILE__, __LINE__, #p); \ + printf(__VA_ARGS__); \ + printStackTrace(5); \ + while(1){} \ + } \ +} while (0) diff --git a/core/main.c b/core/main.c index 4d24459..15b7ca2 100644 --- a/core/main.c +++ b/core/main.c @@ -81,10 +81,10 @@ void kmain(unsigned long magic, unsigned long addr) printf("Setting up Serial link (115200)\n"); serialSetup(115200); + initSlab(); #ifdef RUN_TEST run_test(); #endif - initSlab(); int count = 0; while (1) { diff --git a/drivers/vga.h b/drivers/vga.h index 5c90649..c6e3d19 100644 --- a/drivers/vga.h +++ b/drivers/vga.h @@ -55,3 +55,6 @@ void cursorMove(int x, int y); #define pr_devel(fmt, ...) \ no_printf(pr_fmt(fmt), ##__VA_ARGS__) #endif + +#define pr_info(fmt, ...) \ + printf(pr_fmt(fmt), ##__VA_ARGS__) diff --git a/tests/test.c b/tests/test.c index be951c6..7e27989 100644 --- a/tests/test.c +++ b/tests/test.c @@ -1,5 +1,7 @@ -#include "list.h" +#include "alloc.h" +#include "assert.h" #include "klibc.h" +#include "list.h" #include "mem.h" #include "paging.h" #include "serial.h" @@ -24,21 +26,31 @@ void testPhymem(void) printf("%d pages allocated\n", allocCount); while ((page = list_pop_head(allocated_page_list)) != NULL) { - if (page->phy_addr != (ulong)freeCount) { - printf("Error page %d modified\n", (ulong)page); - } - if (unrefPhyPage((ulong)page) < 0) { - printf("Failed to free page %d\n", (ulong)page); - } + assertmsg(page->phy_addr == (ulong)freeCount, "page %d modified", page); + assertmsg(unrefPhyPage((ulong)page) >= 0, "Failed to free page %d\n", + (ulong)page); freeCount++; } printf("%d pages freed\n", freeCount); - if ((page = (struct mem_desc *)allocPhyPage()) == NULL) { - printf("Error ! Cannot allocate memory\n"); - } else { - unrefPhyPage((ulong)page); - } + assertmsg((page = (struct mem_desc *)allocPhyPage()) != NULL, "Cannot allocate memory\n"); + unrefPhyPage((ulong)page); +} + +static void testAlloc(void) +{ + assert(malloc(1)); + assert(malloc(2)); + assert(malloc(3)); + assert(malloc(4)); + void *malloc1 = malloc(sizeof(void *)); + void *malloc2 = malloc(sizeof(void *)); + assert((char *)malloc2 == ((char *)malloc1 + sizeof(void *))); + assert(malloc(1024)); + assert(malloc(1024)); + assert(malloc(1024)); + assert(malloc(1024)); + assert(malloc(1024)); } static void testPaging(void) @@ -52,10 +64,8 @@ static void testPaging(void) int freeCount = 0; while ((page = (struct mem_desc *)allocPhyPage()) != NULL) { - if (pageMap((vaddr_t)page, (paddr_t)page, PAGING_MEM_WRITE)){ - printf("Fail to map page %d\n", allocCount); - break; - } + assertmsg(pageMap((vaddr_t)page, (paddr_t)page, PAGING_MEM_WRITE) == 0, + "Fail to map page %d\n", allocCount); page->phy_addr = allocCount; allocCount++; list_add_tail(allocated_page_list, page); @@ -63,33 +73,30 @@ static void testPaging(void) printf("%d pages allocated\n", allocCount); while ((page = list_pop_head(allocated_page_list)) != NULL) { - if (page->phy_addr != (ulong)freeCount) { - printf("Error page %d modified\n", (ulong)page); - } - if (unrefPhyPage((ulong)page) < 0) { - printf("Failed to free page %d\n", (ulong)page); - } + assertmsg(page->phy_addr == (ulong)freeCount, "page modified"); + assertmsg(unrefPhyPage((ulong)page) >= 0, "Failed to free page %d\n", + (ulong)page); pageUnmap((vaddr_t)page); freeCount++; } printf("%d pages freed\n", freeCount); - if ((page = (struct mem_desc *)allocPhyPage()) == NULL) { - printf("Error ! Cannot allocate memory\n"); - } else { - unrefPhyPage((ulong)page); - } + assertmsg((page = (struct mem_desc *)allocPhyPage()) != NULL, "Cannot allocate memory\n"); + unrefPhyPage((ulong)page); } -static void test_backtrace_2(int a, int b){ - printStackTrace(a+b); +static void test_backtrace_2(int a, int b) +{ + printStackTrace(a + b); } -static void test_backtrace_1(int a){ +static void test_backtrace_1(int a) +{ test_backtrace_2(a, 3); } -void test_backtrace(){ +void test_backtrace() +{ test_backtrace_1(2); } @@ -102,6 +109,7 @@ void run_test(void) serialWrite('l'); serialWrite('l'); serialWrite('o'); + testAlloc(); printf("Testing backtrace\n"); test_backtrace(); }