aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--malloclab-handout/mm-implicit.cc19
-rw-r--r--malloclab-handout/util/mm-implicit.h15
2 files changed, 19 insertions, 15 deletions
diff --git a/malloclab-handout/mm-implicit.cc b/malloclab-handout/mm-implicit.cc
index ecc40a9..624af8c 100644
--- a/malloclab-handout/mm-implicit.cc
+++ b/malloclab-handout/mm-implicit.cc
@@ -24,8 +24,6 @@
using namespace std;
-void print_list();
-
/*********************************************************
* NOTE TO STUDENTS: Before you do anything else, please
* provide your team information in the following struct.
@@ -89,9 +87,7 @@ void *mm_malloc(size_t size)
loggerln("sbrk {}", newsize);
loggerln("max addr {}", offset((size_t)mem_heap_hi()));
- set_size(status, newsize);
- set_alloc(status, false);
- set_prev_alloc(status, prev_alloc); // TODO
+ status = stat_pack(newsize, false, prev_alloc);
*(size_t*)p = status;
}
@@ -118,9 +114,7 @@ void *mm_malloc(size_t size)
size_t oldsize = get_size(status);
- set_size(status, newsize);
- set_alloc(status, true);
- set_prev_alloc(status, prev_alloc); // TODO
+ status = stat_pack(newsize, true, prev_alloc);
*(size_t*)p = status;
void *ret = (void*)((char*)p + SIZE_T_SIZE);
@@ -130,10 +124,11 @@ void *mm_malloc(size_t size)
if (oldsize > newsize)
{
size_t free_status = 0;
- set_size(free_status, oldsize - newsize);
- set_alloc(free_status, false);
- set_prev_alloc(free_status, true); // becomes: ALLOC FREE
- // so definitely prev_alloc=true for FREE part
+
+ // becomes: ALLOC FREE
+ // so definitely prev_alloc=true for FREE part
+ free_status = stat_pack(oldsize - newsize, false, true);
+
size_t free_header_p = p+newsize;
*(size_t*)(free_header_p) = free_status;
sync_footer(free_header_p);
diff --git a/malloclab-handout/util/mm-implicit.h b/malloclab-handout/util/mm-implicit.h
index 820310c..64b2eaa 100644
--- a/malloclab-handout/util/mm-implicit.h
+++ b/malloclab-handout/util/mm-implicit.h
@@ -69,7 +69,7 @@ void loggerln( std::format_string<Args...> fmt, Args&&... args )
}
template< class... Args >
-void logger( std::format_string<Args...> fmt, Args&&... args )
+inline void logger( std::format_string<Args...> fmt, Args&&... args )
{
#ifdef OV4_VERBOSE
print(fmt, forward<Args>(args)...);
@@ -78,7 +78,7 @@ void logger( std::format_string<Args...> fmt, Args&&... args )
}
-void print_list()
+inline void print_list()
{
#ifdef OV4_VERBOSE
logger("print_list: ");
@@ -95,8 +95,17 @@ void print_list()
#endif
}
-void sync_footer(size_t header)
+inline void sync_footer(size_t header)
{
size_t footer = header + get_size(*(size_t*)header) - SIZE_T_SIZE;
*(size_t*)footer = *(size_t*)header;
+}
+
+inline size_t stat_pack(size_t size, bool alloc, bool prev_alloc)
+{
+ size_t status = 0;
+ set_size(status, size);
+ set_alloc(status, alloc);
+ set_prev_alloc(status, prev_alloc);
+ return status;
} \ No newline at end of file