aboutsummaryrefslogtreecommitdiff
path: root/malloclab-handout/memlib.c
diff options
context:
space:
mode:
author_ov4 <ov4@ov4.org>2026-10-23 12:41:14 +0000
committer_ov4 <ov4@ov4.org>2026-10-23 12:41:14 +0000
commit9cdeda34930cba606725ce1497b72e006e1b1c54 (patch)
treeddfb3b7a50966655ac935b15b62c598e7a8892ef /malloclab-handout/memlib.c
parent3526782dad25bc7caaf5bf49c0010c5e917b11f1 (diff)
init malloc lab
Diffstat (limited to 'malloclab-handout/memlib.c')
-rw-r--r--malloclab-handout/memlib.c101
1 files changed, 101 insertions, 0 deletions
diff --git a/malloclab-handout/memlib.c b/malloclab-handout/memlib.c
new file mode 100644
index 0000000..82cd4f7
--- /dev/null
+++ b/malloclab-handout/memlib.c
@@ -0,0 +1,101 @@
+/*
+ * memlib.c - a module that simulates the memory system. Needed because it
+ * allows us to interleave calls from the student's malloc package
+ * with the system's malloc package in libc.
+ */
+#include <stdio.h>
+#include <stdlib.h>
+#include <assert.h>
+#include <unistd.h>
+#include <sys/mman.h>
+#include <string.h>
+#include <errno.h>
+
+#include "memlib.h"
+#include "config.h"
+
+/* private variables */
+static char *mem_start_brk; /* points to first byte of heap */
+static char *mem_brk; /* points to last byte of heap */
+static char *mem_max_addr; /* largest legal heap address */
+
+/*
+ * mem_init - initialize the memory system model
+ */
+void mem_init(void)
+{
+ /* allocate the storage we will use to model the available VM */
+ if ((mem_start_brk = (char *)malloc(MAX_HEAP)) == NULL) {
+ fprintf(stderr, "mem_init_vm: malloc error\n");
+ exit(1);
+ }
+
+ mem_max_addr = mem_start_brk + MAX_HEAP; /* max legal heap address */
+ mem_brk = mem_start_brk; /* heap is empty initially */
+}
+
+/*
+ * mem_deinit - free the storage used by the memory system model
+ */
+void mem_deinit(void)
+{
+ free(mem_start_brk);
+}
+
+/*
+ * mem_reset_brk - reset the simulated brk pointer to make an empty heap
+ */
+void mem_reset_brk()
+{
+ mem_brk = mem_start_brk;
+}
+
+/*
+ * mem_sbrk - simple model of the sbrk function. Extends the heap
+ * by incr bytes and returns the start address of the new area. In
+ * this model, the heap cannot be shrunk.
+ */
+void *mem_sbrk(int incr)
+{
+ char *old_brk = mem_brk;
+
+ if ( (incr < 0) || ((mem_brk + incr) > mem_max_addr)) {
+ errno = ENOMEM;
+ fprintf(stderr, "ERROR: mem_sbrk failed. Ran out of memory...\n");
+ return (void *)-1;
+ }
+ mem_brk += incr;
+ return (void *)old_brk;
+}
+
+/*
+ * mem_heap_lo - return address of the first heap byte
+ */
+void *mem_heap_lo()
+{
+ return (void *)mem_start_brk;
+}
+
+/*
+ * mem_heap_hi - return address of last heap byte
+ */
+void *mem_heap_hi()
+{
+ return (void *)(mem_brk - 1);
+}
+
+/*
+ * mem_heapsize() - returns the heap size in bytes
+ */
+size_t mem_heapsize()
+{
+ return (size_t)(mem_brk - mem_start_brk);
+}
+
+/*
+ * mem_pagesize() - returns the page size of the system
+ */
+size_t mem_pagesize()
+{
+ return (size_t)getpagesize();
+}