aboutsummaryrefslogtreecommitdiff
path: root/malloclab-handout
diff options
context:
space:
mode:
Diffstat (limited to 'malloclab-handout')
-rw-r--r--malloclab-handout/Makefile2
-rw-r--r--malloclab-handout/mm-naive-free.cc100
2 files changed, 91 insertions, 11 deletions
diff --git a/malloclab-handout/Makefile b/malloclab-handout/Makefile
index 36c382d..ba906f6 100644
--- a/malloclab-handout/Makefile
+++ b/malloclab-handout/Makefile
@@ -8,7 +8,7 @@ HANDINDIR = /afs/cs.cmu.edu/academic/class/15213-f01/malloclab/handin
CC = gcc
CXX = g++
CFLAGS = -Wall -Og -m32
-CXXFLAGS = -Wall -Og -m32
+CXXFLAGS = -Wall -Og -m32 -std=c++23
OBJS = mdriver.o memlib.o fsecs.o fcyc.o clock.o ftimer.o
MM_TARGETS = $(basename $(wildcard mm*.cc))
diff --git a/malloclab-handout/mm-naive-free.cc b/malloclab-handout/mm-naive-free.cc
index a482a43..e81afe3 100644
--- a/malloclab-handout/mm-naive-free.cc
+++ b/malloclab-handout/mm-naive-free.cc
@@ -14,21 +14,24 @@
#include <assert.h>
#include <unistd.h>
#include <string.h>
+#include <print>
#include "mm.h"
#include "memlib.h"
+using namespace std;
+
/*********************************************************
* NOTE TO STUDENTS: Before you do anything else, please
* provide your team information in the following struct.
********************************************************/
team_t team = {
/* Team name */
- "ateam",
+ "ov4.org",
/* First member's full name */
- "Harry Bovik",
+ "_ov4",
/* First member's email address */
- "bovik@cs.cmu.edu",
+ "ov4@ov4.org",
/* Second member's full name (leave blank if none) */
"",
/* Second member's email address (leave blank if none) */
@@ -38,11 +41,51 @@ team_t team = {
/* single word (4) or double word (8) alignment */
#define ALIGNMENT 8
+constexpr bool OV4_VERBOSE = false;
+
/* rounds up to the nearest multiple of ALIGNMENT */
-#define ALIGN(size) (((size) + (ALIGNMENT-1)) & ~0x7)
+inline size_t align(size_t size)
+{
+ return (((size) + (ALIGNMENT-1)) & ~0x7);
+}
+constexpr size_t SIZE_T_SIZE = 8;
-#define SIZE_T_SIZE (ALIGN(sizeof(size_t)))
+inline bool is_alloc(size_t status)
+{
+ return (status&0x1);
+}
+inline bool is_prev_alloc(size_t status)
+{
+ return (status&0x2) >> 1;
+}
+inline void set_status(size_t &status, bool is_alloc)
+{
+ if (is_alloc)
+ status |= 0x1;
+ else
+ status &= 0xfffffffe;
+}
+inline void set_prev_status(size_t &status, bool is_prev_alloc)
+{
+ if (is_prev_alloc)
+ status |= 0x2;
+ else
+ status &= 0xfffffffd;
+}
+inline size_t get_size(size_t status)
+{
+ // ignore lowest 2 bits
+ return status&0xfffffffc;
+}
+inline void set_size(size_t &status, size_t size)
+{
+ status &= 0x3;
+ status |= (size & 0xfffffffc);
+}
+
+template< class... Args >
+void loggerln( std::format_string<Args...> fmt, Args&&... args ) { if constexpr (OV4_VERBOSE) println(fmt, args...); }
/*
* mm_init - initialize the malloc package.
@@ -52,19 +95,56 @@ int mm_init(void)
return 0;
}
+void mm_quit(int code, int line)
+{
+ loggerln("exit, line: {}", line);
+ exit(code);
+}
+
/*
* mm_malloc - Allocate a block by incrementing the brk pointer.
* Always allocate a block whose size is a multiple of the alignment.
*/
void *mm_malloc(size_t size)
{
- int newsize = ALIGN(size + SIZE_T_SIZE);
- void *p = mem_sbrk(newsize);
+ loggerln("----------");
+ size_t newsize = align(size + SIZE_T_SIZE);
+ loggerln("newsize: {}", newsize);
+ //void *p = mem_sbrk(newsize);
+ char *p = (char*)mem_heap_lo();
+ while (true)
+ {
+ size_t status = 0;
+ loggerln("offset: 0x{:x}", (size_t)p-(size_t)mem_heap_lo());
+
+ // out of heap
+ if (p >= mem_heap_hi())
+ {
+ loggerln("sbrk");
+ mem_sbrk(newsize);
+ set_size(status, newsize);
+ set_status(status, true);
+ *(size_t*)p = status;
+ void *ret = (void*)((char*)p + SIZE_T_SIZE);
+ loggerln("return: 0x{:x}" , (size_t)ret-(size_t)mem_heap_lo());
+ return ret;
+ }
+ status = *(size_t*)p;
+ if (is_alloc(status))
+ {
+ p += get_size(status);
+ continue;
+ }
+
+ mm_quit(-1, __LINE__);
+
+ }
+
+
if (p == (void *)-1)
- return NULL;
+ return NULL;
else {
- *(size_t *)p = size;
- return (void *)((char *)p + SIZE_T_SIZE);
+
}
}