aboutsummaryrefslogtreecommitdiff
path: root/8.6
diff options
context:
space:
mode:
authorsinanmohd <sinan@firemail.cc>2023-05-20 12:47:56 +0530
committersinanmohd <sinan@firemail.cc>2023-05-20 17:36:39 +0530
commit6023a709ed2fb5e9d7c91c713255e227e3804f78 (patch)
tree218c685b1b949cde2372c1bfece6f090d7f438c2 /8.6
parentf6901aab1fb07f8ee807afb7b76a3c69ff6a9370 (diff)
8.6: initial commit
Diffstat (limited to '8.6')
-rw-r--r--8.6/Makefile12
-rw-r--r--8.6/main.c14
-rw-r--r--8.6/malloc.c107
-rw-r--r--8.6/malloc.h5
4 files changed, 138 insertions, 0 deletions
diff --git a/8.6/Makefile b/8.6/Makefile
new file mode 100644
index 0000000..1201253
--- /dev/null
+++ b/8.6/Makefile
@@ -0,0 +1,12 @@
+OBJECTS = main.o malloc.o
+CC = gcc
+CFLAGS = -Wvla -Wall -Wextra -Wstrict-prototypes -Wmissing-prototypes -fsanitize=address
+
+main: $(OBJECTS)
+ $(CC) $(CFLAGS) -o main $(OBJECTS)
+
+main.o: malloc.h
+
+.PHONY: clean
+clean:
+ rm -f main $(OBJECTS)
diff --git a/8.6/main.c b/8.6/main.c
new file mode 100644
index 0000000..e1cecb6
--- /dev/null
+++ b/8.6/main.c
@@ -0,0 +1,14 @@
+#include <stdio.h>
+#include <string.h>
+#include "malloc.h"
+
+int main(void)
+{
+ char *s = my_calloc(sizeof(char), 11);
+
+ strcpy(s, "just werks");
+ printf("%s\n", s);
+ my_free(s);
+
+ return 0;
+}
diff --git a/8.6/malloc.c b/8.6/malloc.c
new file mode 100644
index 0000000..0834669
--- /dev/null
+++ b/8.6/malloc.c
@@ -0,0 +1,107 @@
+#include <unistd.h>
+#include "malloc.h"
+
+#define NALLOC 128
+
+typedef long long Align;
+
+typedef union header {
+ struct {
+ union header *ptr;
+ size_t size;
+ } s;
+
+ Align x;
+} Header;
+
+static Header base;
+static Header *freep = NULL;
+
+static Header *my_morecore(size_t nunits);
+
+void *my_calloc(size_t n ,size_t size)
+{
+ char *p, *temp;
+ size_t nb;
+
+ nb = n * size;
+ if ((p = temp = my_malloc(nb)) == NULL)
+ while (nb-- > 0)
+ *p = '\0';
+
+ return p;
+}
+
+void *my_malloc(size_t nbytes)
+{
+ Header *prevp, *p;
+ size_t nunits;
+
+ if ((prevp = freep) == NULL) {
+ base.s.ptr = prevp = freep = &base;
+ base.s.size = 0;
+ }
+
+ nunits = (nbytes + sizeof(Header) - 1) / sizeof(Header) + 1;
+ for (p = freep->s.ptr; ; prevp = p, p = p->s.ptr) {
+ if (p->s.size >= nunits) {
+ if (p->s.size == nunits) {
+ prevp->s.ptr = p->s.ptr;
+ } else {
+ p->s.size -= nunits;
+ p += p->s.size;
+ p->s.size = nunits;
+ }
+
+ freep = prevp;
+ return (void *)(p + 1);
+ }
+
+ if (p == freep)
+ if ((p = my_morecore(nunits)) == NULL)
+ return NULL;
+ }
+
+ return NULL;
+}
+
+static Header *my_morecore(size_t nunits)
+{
+ Header *p;
+
+ if (nunits < NALLOC)
+ nunits = NALLOC;
+
+ if ((p = sbrk(nunits * sizeof(Header))) == (Header *) -1)
+ return NULL;
+
+ p->s.size = nunits;
+ my_free (p + 1);
+ return freep;
+}
+
+void my_free(void *ap)
+{
+ Header *bp, *p;
+
+ bp = (Header *)ap - 1;
+ for (p = freep; !(bp > p && bp < p->s.ptr) ; p = p->s.ptr)
+ if (p >= p->s.ptr && (bp > p || bp < p->s.ptr))
+ break;
+
+ if (bp + bp->s.size == p->s.ptr) {
+ bp->s.size += p->s.ptr->s.size;
+ bp->s.ptr = p->s.ptr->s.ptr;
+ } else {
+ bp->s.ptr = p->s.ptr;
+ }
+
+ if (p + p->s.size == bp) {
+ p->s.size += bp->s.size;
+ p->s.ptr = bp->s.ptr;
+ } else {
+ p->s.ptr = bp;
+ }
+
+ freep = p;
+}
diff --git a/8.6/malloc.h b/8.6/malloc.h
new file mode 100644
index 0000000..ca317fa
--- /dev/null
+++ b/8.6/malloc.h
@@ -0,0 +1,5 @@
+#include <stddef.h>
+
+void *my_malloc(size_t nbytes);
+void *my_calloc(size_t n ,size_t size);
+void my_free(void *ap);