diff options
Diffstat (limited to 'malloclab-handout')
| -rw-r--r-- | malloclab-handout/Makefile | 14 | ||||
| -rw-r--r-- | malloclab-handout/mm-implicit.cc (renamed from malloclab-handout/mm-naive-free.cc) | 64 | ||||
| -rw-r--r-- | malloclab-handout/util/mm-implicit.h (renamed from malloclab-handout/util/mm-naive-free.h) | 8 |
3 files changed, 67 insertions, 19 deletions
diff --git a/malloclab-handout/Makefile b/malloclab-handout/Makefile index ba906f6..288a361 100644 --- a/malloclab-handout/Makefile +++ b/malloclab-handout/Makefile @@ -7,8 +7,8 @@ HANDINDIR = /afs/cs.cmu.edu/academic/class/15213-f01/malloclab/handin CC = gcc CXX = g++ -CFLAGS = -Wall -Og -m32 -CXXFLAGS = -Wall -Og -m32 -std=c++23 +CFLAGS = -Wall -Og -g -m32 +CXXFLAGS = -Wall -Og -g -m32 -std=c++23 OBJS = mdriver.o memlib.o fsecs.o fcyc.o clock.o ftimer.o MM_TARGETS = $(basename $(wildcard mm*.cc)) @@ -16,6 +16,12 @@ MM_TARGETS = $(basename $(wildcard mm*.cc)) MAKEFLAGS += --no-builtin-rules .SUFFIXES: +VERBOSE ?= 0 + +ifeq ($(VERBOSE),1) +VERBOSE_FLAG += -DOV4_VERBOSE +endif + .PHONY: $(MM_TARGETS) $(MM_TARGETS): %: %.out @@ -23,10 +29,10 @@ $(MM_TARGETS): %: %.out $(CXX) $(CXXFLAGS) -o $@ $^ %.o: %.cc mm.h memlib.h - $(CXX) $(CXXFLAGS) -c -o $@ $< + $(CXX) $(VERBOSE_FLAG) $(CXXFLAGS) -c -o $@ $< %.o: %.c - $(CC) $(CFLAGS) -c -o $@ $< + $(CC) $(VERBOSE_FLAG) $(CFLAGS) -c -o $@ $< mdriver.o: mdriver.c fsecs.h fcyc.h clock.h memlib.h config.h mm.h memlib.o: memlib.c memlib.h diff --git a/malloclab-handout/mm-naive-free.cc b/malloclab-handout/mm-implicit.cc index f39361a..f38032d 100644 --- a/malloclab-handout/mm-naive-free.cc +++ b/malloclab-handout/mm-implicit.cc @@ -17,7 +17,7 @@ #include <print> #include <unistd.h> -#include "util/mm-naive-free.h" +#include "util/mm-implicit.h" #include "mm.h" #include "memlib.h" @@ -65,6 +65,7 @@ void mm_quit(int code, int line) void *mm_malloc(size_t size) { loggerln("---------- malloc ----------"); + loggerln("argument: {}", size); print_list(); size_t newsize = align(size + SIZE_T_SIZE); loggerln("newsize: {}", newsize); @@ -83,10 +84,14 @@ void *mm_malloc(size_t size) // then increase heap // and set current pos to be free // so it can be alloc'd soon - loggerln("sbrk"); + loggerln("before: max addr {}", offset((size_t)mem_heap_hi())); mem_sbrk(newsize); + loggerln("sbrk {}", newsize); + loggerln("max addr {}", offset((size_t)mem_heap_hi())); + set_size(status, newsize); set_alloc(status, false); + set_prev_alloc(status, true); // TODO *(size_t*)p = status; } status = *(size_t*)p; @@ -108,18 +113,23 @@ void *mm_malloc(size_t size) set_size(status, newsize); set_alloc(status, true); - set_prev_alloc(status, prev_alloc); + set_prev_alloc(status, true); // TODO *(size_t*)p = status; void *ret = (void*)((char*)p + SIZE_T_SIZE); + + // if we don't use all of free space + // split free block into 2 parts if (oldsize > newsize) { size_t free_status = 0; - set_size(free_status, oldsize-newsize); + set_size(free_status, oldsize - newsize); set_alloc(free_status, false); - set_prev_alloc(free_status, false); - *(size_t*)(p+newsize) = free_status; - loggerln("split free: free begins {}", offset(p+newsize)); + set_prev_alloc(free_status, true); + size_t free_header_p = p+newsize; + *(size_t*)(free_header_p) = free_status; + sync_footer(free_header_p); + loggerln("split free: free begins {}", offset(free_header_p)); } loggerln("return: {}" , (size_t)ret-(size_t)mem_heap_lo()); @@ -143,22 +153,48 @@ void mm_free(void *ptr) print_list(); size_t header = (size_t)ptr-SIZE_T_SIZE; - size_t next = header + get_size(*(size_t*)header); + size_t next_header = header + get_size(*(size_t*)header); - if (next > (size_t)mem_heap_hi()) next = 0; + if (next_header > (size_t)mem_heap_hi()) next_header = 0; loggerln("header: {}", offset(header)); loggerln("header size: {}", get_size(*(size_t*)header)); - loggerln("next: {}", next != 0 ? offset(next) : 0); + loggerln("next: {}", next_header != 0 ? offset(next_header) : 0); + // free current block set_alloc(*(size_t*)header, false); + sync_footer(header); - // if next is free - if ((next != 0) - && (!is_alloc(*(size_t*)next))) + // if next_header is free + if ((next_header != 0) + && (!is_alloc(*(size_t*)next_header))) { - size_t newsize = get_size(*(size_t*)header) + get_size(*(size_t*)next); + size_t newsize = get_size(*(size_t*)header) + get_size(*(size_t*)next_header); + loggerln("next free: newsize {}", newsize); set_size(*(size_t*)header, newsize); + sync_footer(header); + } + + // if prev exists + // and is free + if (!is_prev_alloc(*(size_t*)header) + && header != (size_t)mem_heap_lo()) + { + + loggerln("handling prev free"); + + size_t prev_footer = header - SIZE_T_SIZE; + size_t prev_header = header - get_size(*(size_t*)prev_footer); + + loggerln("prev_footer: {}", offset(prev_footer)); + loggerln("prev footer size: {}", get_size(*(size_t*)prev_footer)); + + loggerln("ok 0"); + size_t newsize = get_size(*(size_t*)prev_header) + get_size(*(size_t*)header); + loggerln("ok1"); + set_size(*(size_t*)prev_header, newsize); + loggerln("ok2"); + sync_footer(prev_header); } } diff --git a/malloclab-handout/util/mm-naive-free.h b/malloclab-handout/util/mm-implicit.h index 3aa06bd..74e0f86 100644 --- a/malloclab-handout/util/mm-naive-free.h +++ b/malloclab-handout/util/mm-implicit.h @@ -87,9 +87,15 @@ void print_list() if (p >= (size_t)mem_heap_hi()) break; - logger("{} ", offset(p)); + logger("{}({},{}) ", offset(p), is_alloc(*(size_t*)p) ? "alloc" : "free", get_size(*(size_t*)p)); p += get_size(*(size_t*)p); } loggerln(""); #endif +} + +void sync_footer(size_t header) +{ + size_t footer = header + get_size(*(size_t*)header) - SIZE_T_SIZE; + *(size_t*)footer = *(size_t*)header; }
\ No newline at end of file |
