wm: teppich

Download patch

ref: 0db68816a181050ffee307935f9a95884d0d2f8a
parent: 18740d02a89c964f7815a913c5396217a403f0af
author: mkf <mkf@cloud9p.org>
date: Tue Dec 5 16:23:23 EST 2023

move malloc and free into libc.h

--- a/Makefile
+++ b/Makefile
@@ -50,6 +50,9 @@
 	libc/scanf.o\
 	libc/itoa.o\
 	libc/adler32.o\
+	libc/malloc.o\
+	libc/free.o\
+	libc/panic.o\
 	
 K = \
 	pc/boot.o\
--- a/include/libc.h
+++ b/include/libc.h
@@ -36,3 +36,10 @@
 
 /* hash */
 long adler32(char *data);
+
+/* memory */
+void* malloc(uint16 size);
+void free(void *mem);
+
+/* sys */
+void panic(void);
--- /dev/null
+++ b/libc/malloc.c
@@ -1,0 +1,56 @@
+#include <u.h>
+#include <libc.h>
+#include <mem.h>
+#include <err.h>
+
+static int
+_malloc(uint16 size)
+{
+	int i, j;
+	uint16 fmem; /* number of free segments */
+
+	if(size > MEM_MAX * BLOCKSIZE)
+		return NO_MEM;
+
+	i = 0;
+	fmem = 0;
+	while(i < MEM_MAX * (1024 / BLOCKSIZE))
+	{
+		if(memap[i] == FREE)
+		{
+			fmem++;
+			if(fmem == size+1) /* one bit for padding */
+			{
+	
+				/* we are done here, give address */
+				for(j = i ; j > i - size; j--)
+					memap[j] = TAKEN;
+			 	return i - size;
+			}
+		}
+		else
+			fmem = 0; /* we need continuous(?) memory */
+		i++;
+	}	
+
+	/* where is your memory now? go ahead, do panic. */
+	panic();
+	return NO_MEM;
+}
+
+void*
+malloc(uint16 size)
+{
+	int t;
+	
+	if(size == 0)
+		return nil;
+
+	t = _malloc(size);
+
+	if(t == NO_MEM)
+	/* LEST THAT HAPPEN, THOU SHALL NOT BE HERE */
+		return nil;
+
+	return (void*)(t * ( BLOCKSIZE ) + MEM_BEG);
+}
--- a/pc/mem.c
+++ b/pc/mem.c
@@ -1,71 +1,2 @@
-#include <u.h>
-#include <libc.h>
-#include <mem.h>
-#include <err.h>
 
-/* does first fit, returns index of that part of memory in memap */
-int
-_malloc(uint16 size)
-{
-	int i, j;
-	uint16 fmem; /* number of free segments */
 
-	if(size > MEM_MAX * BLOCKSIZE)
-		return NO_MEM;
-
-	i = 0;
-	fmem = 0;
-	while(i < MEM_MAX * (1024 / BLOCKSIZE))
-	{
-		if(memap[i] == FREE)
-		{
-			fmem++;
-			if(fmem == size+1) /* one bit for padding */
-			{
-	
-				/* we are done here, give address */
-				for(j = i ; j > i - size; j--)
-					memap[j] = TAKEN;
-			 	return i - size;
-			}
-		}
-		else
-		{
-			fmem = 0; /* we need contigus(?) memory */
-		}
-		i++;
-	}	
-
-	
-	return NO_MEM;
-}
-
-void*
-malloc(uint16 size)
-{
-	int t;
-	
-	if(size == 0)
-		return nil;
-
-	t = _malloc(size);
-	if(t == NO_MEM)
-		return nil;
-
-	return (void*)(t * ( BLOCKSIZE ) + MEM_BEG);
-}
-
-int
-free(void *mem)
-{
-	int i;
-
-	i = (((int)(mem) - MEM_BEG ) / BLOCKSIZE)+1;
-	while(memap[i])
-	{
-		memap[i] = 0;
-		*(char*)mem++ = '\0';
-		i++;
-	}
-	return OK;
-}