malloc/calloc/free and minimal allocator testing master
authorJohn Tsiombikas <nuclear@member.fsf.org>
Tue, 2 Feb 2021 19:21:54 +0000 (21:21 +0200)
committerJohn Tsiombikas <nuclear@member.fsf.org>
Tue, 2 Feb 2021 19:21:54 +0000 (21:21 +0200)
src/amiga/libc/malloc.c [new file with mode: 0644]
src/amiga/libc/stdlib.h
src/amiga/main.c
src/amiga/mem.c

diff --git a/src/amiga/libc/malloc.c b/src/amiga/libc/malloc.c
new file mode 100644 (file)
index 0000000..104d7bc
--- /dev/null
@@ -0,0 +1,21 @@
+#include <stdlib.h>
+#include <string.h>
+#include "mem.h"
+
+void *malloc(size_t sz)
+{
+       return mem_alloc(sz, 0);
+}
+
+void *calloc(size_t n, size_t sz)
+{
+       void *p = mem_alloc(n * sz, 0);
+       if(!p) return 0;
+       memset(p, 0, n * sz);
+       return p;
+}
+
+void free(void *p)
+{
+       mem_free(p);
+}
index ea0a6dc..b353041 100644 (file)
@@ -13,4 +13,8 @@ long strtol(const char *str, char **endp, int base);
 void itoa(int val, char *buf, int base);
 void utoa(unsigned int val, char *buf, int base);
 
+void *malloc(size_t sz);
+void *calloc(size_t n, size_t sz);
+void free(void *p);
+
 #endif /* AMIGA_LIBC_STDLIB_H_ */
index 0be7275..c1749cc 100644 (file)
@@ -1,3 +1,5 @@
+#include <stdio.h>
+#include <stdlib.h>
 #include <string.h>
 #include "amigalib.h"
 #include "hwregs.h"
index 6b43a9e..b5556df 100644 (file)
@@ -217,7 +217,7 @@ static void add_range(int pidx, struct memrange *mr)
        struct memrange *prev, *next, dummy;
 
        printf("DBG adding free range: %06lx - %06lx to pool %d\n", (unsigned long)mr,
-                       (unsigned long)(mr->start + mr->size - (unsigned char*)mr), pidx);
+                       (unsigned long)(mr->start + mr->size), pidx);
 
        dummy.next = pool[pidx];
        prev = &dummy;
@@ -245,3 +245,15 @@ static void add_range(int pidx, struct memrange *mr)
 
        pool[pidx] = dummy.next;
 }
+
+void dbg_memprint(void)
+{
+       struct memrange *mr = pool[POOL_SLOW];
+
+       printf("MEM");
+       while(mr) {
+               printf(" [%lx - %lx]", (unsigned long)mr, (unsigned long)(mr->start + mr->size));
+               mr = mr->next;
+       }
+       putchar('\n');
+}