summaryrefslogtreecommitdiff
path: root/libc/malloc2
diff options
context:
space:
mode:
Diffstat (limited to 'libc/malloc2')
-rw-r--r--libc/malloc2/Config1
-rw-r--r--libc/malloc2/Makefile23
-rw-r--r--libc/malloc2/README19
-rw-r--r--libc/malloc2/malloc.c127
-rw-r--r--libc/malloc2/malloc.h21
-rw-r--r--libc/malloc2/stack.c10
6 files changed, 201 insertions, 0 deletions
diff --git a/libc/malloc2/Config b/libc/malloc2/Config
new file mode 100644
index 0000000..11c476d
--- /dev/null
+++ b/libc/malloc2/Config
@@ -0,0 +1 @@
+malloc: Joel's malloc functions
diff --git a/libc/malloc2/Makefile b/libc/malloc2/Makefile
new file mode 100644
index 0000000..0396df2
--- /dev/null
+++ b/libc/malloc2/Makefile
@@ -0,0 +1,23 @@
+
+TOP=..
+include $(TOP)/Make.defs
+
+MOBJ=malloc.o stack.o
+
+OBJ=$(MOBJ)
+
+all: $(OBJ)
+
+libc.a: $(OBJ)
+ ar r ../$(LIBC) $(OBJ)
+ @touch libc.a
+
+clean:
+ rm -f $(OBJ) libc.a
+
+$(AOBJ): $(ASRC)
+ $(CC) $(CFLAGS) -c -DL_$* -o $@ $(ASRC)
+
+transfer:
+ -@rm ../include/malloc.h
+ cp -p malloc.h ../include/.
diff --git a/libc/malloc2/README b/libc/malloc2/README
new file mode 100644
index 0000000..ecf3fd2
--- /dev/null
+++ b/libc/malloc2/README
@@ -0,0 +1,19 @@
+This is just the malloc for libc. It is untested; it won't even compile
+right now. In particular, __malloc_init needs some bug fixing!
+
+Apparently, there is another malloc that Robert Debath wrote which probably
+works by now. However, I honestly think that my malloc may be just as
+good when it's finished.
+
+In about six months, you'll probably see something like this:
+
+
+Linux/less-than-32-bit installation program
+Do you want
+ 1. Chad Page's kernel
+ 2. Alan Cox's kernel
+Enter your chioce --> 2
+Do you want
+ 1. Robert Debath's malloc
+ 2. Joel Weber's malloc
+[more choices for compilers, filetools, etc]
diff --git a/libc/malloc2/malloc.c b/libc/malloc2/malloc.c
new file mode 100644
index 0000000..143d324
--- /dev/null
+++ b/libc/malloc2/malloc.c
@@ -0,0 +1,127 @@
+/* simplified linux malloc.h
+ Copyright (C) 1995 Joel N. Weber II
+
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License
+ as published by the Free Software Foundation; either version 2
+ of the License, or (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+#include <malloc.h>
+
+typedef struct __malloc_struct malloc_struct;
+
+typedef union __malloc_union
+{
+ char *c;
+ malloc_struct *m;
+} malloc_union;
+
+
+struct __malloc_struct
+{
+ unsigned char status;
+#define ALLOC 0x53
+#define FREE 0x55
+#define END_OF_WORLD 0x57
+ malloc_union next;
+} *malloc_start;
+
+extern int __STACKSIZE;
+
+/* WARNING: this init will only work if there is a hard limit on the
+ amount of RAM that can be allocated.
+ */
+
+#ifdef __AS386_16__
+#asm
+ loc 1 ! Make sure the pointer is in the correct segment
+auto_func: ! Label for bcc -M to work.
+ .word _malloc_init ! Pointer to the autorun function
+ .word no_op ! Space filler cause segs are padded to 4 bytes.
+ .text ! So the function after is also in the correct seg.
+#endasm
+#endif
+
+malloc_init()
+{
+ extern unsigned int sbrk();
+
+ unsigned int ptr, sz, count;
+
+ malloc_start = (malloc_struct*) ((sbrk(16)+1)&~1);
+ malloc_start->status = FREE;
+
+ count=254;
+ for(sz=16384; sz>64; )
+ {
+ ptr= sbrk(sz);
+ if( ptr == (unsigned)-1 ) sz>>=1;
+ else count+=sz;
+ }
+ if( __STACKSIZE > count || __STACKSIZE <= 0 ) __STACKSIZE = ((count>>1)&-2);
+ ptr = sbrk(-__STACKSIZE);
+
+ malloc_start->next.m = ((malloc_struct*)ptr) - 1;
+
+ malloc_start->next.m->status = END_OF_WORLD;
+}
+
+char *malloc(size)
+size_t size;
+{
+ register malloc_union tmp, tmp2;
+
+ /* Make sure we don't lose the alignment */
+ size = (size+sizeof(malloc_struct)-1)/sizeof(malloc_struct);
+
+ tmp.m = malloc_start;
+ while ( ( tmp.m->next.m - tmp.m - 2 ) < size
+ || ( tmp.m->status == ALLOC ))
+ tmp.m = tmp.m->next.m;
+
+ if (tmp.m->status == FREE){
+ tmp2.m = size + tmp.m + 1;
+ tmp2.m->status = FREE;
+ tmp2.m->next.c = tmp.m->next.c;
+ tmp.m->status = ALLOC;
+ tmp.m->next.c = tmp2.c;
+ }
+ else return 0;
+ tmp.m++;
+ return tmp.c;
+}
+
+__malloc_cleanup() /* finds consecutive free blocks and joins them */
+{
+ malloc_struct *tmp;
+
+ tmp = malloc_start;
+ while ((tmp->status != END_OF_WORLD)&&(tmp->next.m->status != END_OF_WORLD)){
+ if ((tmp->status==FREE)&&(tmp->next.m->status==FREE))
+ tmp->next.m = tmp->next.m->next.m;
+ else tmp = tmp->next.m;
+ }
+}
+
+free(what)
+char *what;
+{
+ malloc_union tmp;
+
+ tmp.c = what; tmp.m--;
+ if( tmp.m->status == ALLOC )
+ {
+ tmp.m->status = FREE;
+ __malloc_cleanup;
+ }
+}
diff --git a/libc/malloc2/malloc.h b/libc/malloc2/malloc.h
new file mode 100644
index 0000000..ffcb3a4
--- /dev/null
+++ b/libc/malloc2/malloc.h
@@ -0,0 +1,21 @@
+/* simplified linux malloc.h
+ Copyright (C) 1995 Joel N. Weber II
+
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License
+ as published by the Free Software Foundation; either version 2
+ of the License, or (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+char *malloc(size);
+void free(what);
+char *realloc(what, size);
diff --git a/libc/malloc2/stack.c b/libc/malloc2/stack.c
new file mode 100644
index 0000000..5f33e21
--- /dev/null
+++ b/libc/malloc2/stack.c
@@ -0,0 +1,10 @@
+
+/*
+ * Under Linux 8086 the stack and heap areas are at the top and bottom
+ * of the same area of memory, this version of malloc requires that the
+ * malloc area is of a fixed size this means that there must also be a
+ * specific amount of stack space reserved outside of this. The number
+ * of bytes to be reserved is specified below.
+ */
+
+int __STACKSIZE = 2048;