From: John Tsiombikas Date: Thu, 22 Aug 2024 02:27:48 +0000 (+0300) Subject: debugging kfree X-Git-Url: http://git.mutantstargoat.com/user/nuclear/?a=commitdiff_plain;h=537c470ba4c0b28c924c961a7ad391400603cc32;p=eightysix debugging kfree --- diff --git a/kern/src/config.h b/kern/src/config.h index d46832b..45bf49f 100644 --- a/kern/src/config.h +++ b/kern/src/config.h @@ -4,6 +4,6 @@ #define VERSTR "v0.0" /* CONDEV: which device to use for the kernel console (DEV_VID or DEV_SER) */ -#define CONDEV DEV_VID +#define CONDEV DEV_SER #endif /* CONFIG_H_ */ diff --git a/kern/src/libc/malloc.c b/kern/src/libc/malloc.c index 223ba13..31bb294 100644 --- a/kern/src/libc/malloc.c +++ b/kern/src/libc/malloc.c @@ -49,7 +49,7 @@ void __far *kmalloc(unsigned long sz) pptr = pool; while(pptr) { if(fullsz <= pptr->len) { - newfree = pptr + fullsz; + newfree = (struct memrange __far*)((char __far*)pptr + fullsz); newfree->magic = pptr->magic; newfree->len = pptr->len - fullsz; newfree->next = pptr->next; @@ -166,13 +166,15 @@ void kfree(void __far *p) void dbg_print_kmalloc_pool(void) { struct memrange __far *m; + struct memrange __far *end; m = pool; while(m) { - printf("[%x:%x/%x] ", FP_SEG(m), FP_OFFS(m), m->len); + end = (struct memrange __far*)((char __far*)m + m->len); + printf("[%04x:%04x - ", FP_SEG(m), FP_OFFS(m)); + printf("%04x:%04x] (%x)\n", FP_SEG(end), FP_OFFS(end), m->len); m = m->next; } - putchar('\n'); } void dbg_print_kmalloc_stats(void) diff --git a/kern/src/libc/stdarg.h b/kern/src/libc/stdarg.h index bb589db..5a6cdb1 100644 --- a/kern/src/libc/stdarg.h +++ b/kern/src/libc/stdarg.h @@ -3,8 +3,8 @@ typedef char *va_list; -#define va_start(ap, last) ((ap) = (va_list)&(last)) -#define va_arg(ap, type) ((ap) += sizeof(type), *(type*)(ap)) +#define va_start(ap, last) ((ap) = (char*)&(last) + sizeof(last)) +#define va_arg(ap, type) ((ap) += sizeof(type), *(type*)((ap) - sizeof(type))) #define va_end(ap) #define va_copy(dest, src) ((dest) = (src)) diff --git a/kern/src/libc/stdio.c b/kern/src/libc/stdio.c index f34c1f7..3922681 100644 --- a/kern/src/libc/stdio.c +++ b/kern/src/libc/stdio.c @@ -24,7 +24,7 @@ static int intern_vprintf(struct outbuf *outbuf, const char *fmt, va_list ap); static void wrchar(struct outbuf *outbuf, int c); static int wrstr(struct outbuf *outbuf, struct format *fmt, const char *s); static int wrint(struct outbuf *outbuf, struct format *fmt, long val); -static int wruint(struct outbuf *outbuf, struct format *fmt, unsigned long val); +int wruint(struct outbuf *outbuf, struct format *fmt, unsigned long val); int printf(const char *fmt, ...) { @@ -101,6 +101,10 @@ static int intern_vprintf(struct outbuf *outbuf, const char *fmtstr, va_list ap) reset_format(&fmt); break; + case 'l': + fmt.flags |= FMT_LONG; + break; + case '0': if(fmt.fchar != '0') { fmt.fchar = '0'; @@ -200,9 +204,8 @@ static int wrint(struct outbuf *outbuf, struct format *fmt, long val) return wrstr(outbuf, fmt, ptr); } -static int wruint(struct outbuf *outbuf, struct format *fmt, unsigned long val) +int wruint(struct outbuf *outbuf, struct format *fmt, unsigned long val) { - static const char hexconv[] = "0123456789abcdef"; char buf[16], *ptr = buf + 15; buf[15] = 0; diff --git a/kern/src/main.c b/kern/src/main.c index 6a412d4..ab6f0f8 100644 --- a/kern/src/main.c +++ b/kern/src/main.c @@ -21,29 +21,35 @@ void kmain(void) printf("eightysix kernel %s\n", VERSTR); - printf("kmalloc free pool\n"); - dbg_print_kmalloc_pool(); - printf("kmalloc stats\n"); + printf("initial kmalloc stats\n"); dbg_print_kmalloc_stats(); + printf("initial free pool\n"); + dbg_print_kmalloc_pool(); void __far *ptr[32]; for(i=0; i<32; i++) { ptr[i] = kmalloc(256); + printf("ALLOC(256) - %x:%x\n", FP_SEG(ptr[i]), FP_OFFS(ptr[i])); } - for(i=0; i<20; i++) { + printf("\nallocated 32 256b blocks ... "); + dbg_print_kmalloc_stats(); + dbg_print_kmalloc_pool(); + + for(i=0; i<40; i++) { int idx = rand() & 0x1f; if(ptr[idx]) { + printf("FREE(%x:%x)\n", FP_SEG(ptr[idx]), FP_OFFS(ptr[idx])); kfree(ptr[idx]); ptr[idx] = 0; + dbg_print_kmalloc_pool(); } } - printf("kmalloc free pool\n"); - dbg_print_kmalloc_pool(); - printf("kmalloc stats\n"); + printf("\nafter freeing some... kmalloc free pool\n"); dbg_print_kmalloc_stats(); + dbg_print_kmalloc_pool(); init_intr(); }