From eb47984a47e0baa5a95cd25cf6b4d745429d42e6 Mon Sep 17 00:00:00 2001 From: _ov4 Date: Sat, 31 Oct 2026 07:08:25 +0000 Subject: fix malloc implicit --- malloclab-handout/util/mm-implicit.h | 101 +++++++++++++++++++++++++++++++++ malloclab-handout/util/mm-naive-free.h | 95 ------------------------------- 2 files changed, 101 insertions(+), 95 deletions(-) create mode 100644 malloclab-handout/util/mm-implicit.h delete mode 100644 malloclab-handout/util/mm-naive-free.h (limited to 'malloclab-handout/util') diff --git a/malloclab-handout/util/mm-implicit.h b/malloclab-handout/util/mm-implicit.h new file mode 100644 index 0000000..74e0f86 --- /dev/null +++ b/malloclab-handout/util/mm-implicit.h @@ -0,0 +1,101 @@ +#pragma once + +#include + +#include "../mm.h" +#include "../memlib.h" + + +using namespace std; + +/* single word (4) or double word (8) alignment */ +#define ALIGNMENT 8 + +/* rounds up to the nearest multiple of ALIGNMENT */ +inline size_t align(size_t size) +{ + return (((size) + (ALIGNMENT-1)) & ~0x7); +} + +inline constexpr size_t SIZE_T_SIZE = 8; + +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_alloc(size_t &status, bool is_alloc) +{ + if (is_alloc) + status |= 0x1; + else + status &= 0xfffffffe; +} +inline void set_prev_alloc(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); +} +inline size_t offset(size_t p) +{ + return (size_t)((size_t)p-(size_t)mem_heap_lo()); +} +inline void next(size_t &p) +{ + p += get_size(*(size_t*)p); +} + +template< class... Args > +void loggerln( std::format_string fmt, Args&&... args ) +{ +#ifdef OV4_VERBOSE + println(fmt, forward(args)...); +#endif +} + +template< class... Args > +void logger( std::format_string fmt, Args&&... args ) +{ +#ifdef OV4_VERBOSE + print(fmt, forward(args)...); +#endif +} + + +void print_list() +{ +#ifdef OV4_VERBOSE + logger("print_list: "); + size_t p = (size_t)mem_heap_lo(); + while (true) + { + if (p >= (size_t)mem_heap_hi()) + break; + + 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 diff --git a/malloclab-handout/util/mm-naive-free.h b/malloclab-handout/util/mm-naive-free.h deleted file mode 100644 index 3aa06bd..0000000 --- a/malloclab-handout/util/mm-naive-free.h +++ /dev/null @@ -1,95 +0,0 @@ -#pragma once - -#include - -#include "../mm.h" -#include "../memlib.h" - - -using namespace std; - -/* single word (4) or double word (8) alignment */ -#define ALIGNMENT 8 - -/* rounds up to the nearest multiple of ALIGNMENT */ -inline size_t align(size_t size) -{ - return (((size) + (ALIGNMENT-1)) & ~0x7); -} - -inline constexpr size_t SIZE_T_SIZE = 8; - -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_alloc(size_t &status, bool is_alloc) -{ - if (is_alloc) - status |= 0x1; - else - status &= 0xfffffffe; -} -inline void set_prev_alloc(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); -} -inline size_t offset(size_t p) -{ - return (size_t)((size_t)p-(size_t)mem_heap_lo()); -} -inline void next(size_t &p) -{ - p += get_size(*(size_t*)p); -} - -template< class... Args > -void loggerln( std::format_string fmt, Args&&... args ) -{ -#ifdef OV4_VERBOSE - println(fmt, forward(args)...); -#endif -} - -template< class... Args > -void logger( std::format_string fmt, Args&&... args ) -{ -#ifdef OV4_VERBOSE - print(fmt, forward(args)...); -#endif -} - - -void print_list() -{ -#ifdef OV4_VERBOSE - logger("print_list: "); - size_t p = (size_t)mem_heap_lo(); - while (true) - { - if (p >= (size_t)mem_heap_hi()) - break; - - logger("{} ", offset(p)); - p += get_size(*(size_t*)p); - } - loggerln(""); -#endif -} \ No newline at end of file -- cgit v1.3.1-4-g156e