aboutsummaryrefslogtreecommitdiff
path: root/malloclab-handout/mm-naive-free.cc
diff options
context:
space:
mode:
author_ov4 <ov4@ov4.org>2026-10-31 07:08:25 +0000
committer_ov4 <ov4@ov4.org>2026-10-31 07:08:25 +0000
commiteb47984a47e0baa5a95cd25cf6b4d745429d42e6 (patch)
treef732b25360f2f0785a220080f33bc59cea5ec8f8 /malloclab-handout/mm-naive-free.cc
parent135d3c409c00210526afd0d649e5b709d8f29a7d (diff)
fix malloc implicit
Diffstat (limited to 'malloclab-handout/mm-naive-free.cc')
-rw-r--r--malloclab-handout/mm-naive-free.cc195
1 files changed, 0 insertions, 195 deletions
diff --git a/malloclab-handout/mm-naive-free.cc b/malloclab-handout/mm-naive-free.cc
deleted file mode 100644
index f39361a..0000000
--- a/malloclab-handout/mm-naive-free.cc
+++ /dev/null
@@ -1,195 +0,0 @@
-/*
- * mm-naive.c - The fastest, least memory-efficient malloc package.
- *
- * In this naive approach, a block is allocated by simply incrementing
- * the brk pointer. A block is pure payload. There are no headers or
- * footers. Blocks are never coalesced or reused. Realloc is
- * implemented directly using mm_malloc and mm_free.
- *
- * NOTE TO STUDENTS: Replace this header comment with your own header
- * comment that gives a high level description of your solution.
- */
-#include <stdio.h>
-#include <stdlib.h>
-#include <assert.h>
-#include <unistd.h>
-#include <string.h>
-#include <print>
-#include <unistd.h>
-
-#include "util/mm-naive-free.h"
-
-#include "mm.h"
-#include "memlib.h"
-
-using namespace std;
-
-void print_list();
-
-/*********************************************************
- * NOTE TO STUDENTS: Before you do anything else, please
- * provide your team information in the following struct.
- ********************************************************/
-team_t team = {
- /* Team name */
- "ov4.org",
- /* First member's full name */
- "_ov4",
- /* First member's email address */
- "ov4@ov4.org",
- /* Second member's full name (leave blank if none) */
- "",
- /* Second member's email address (leave blank if none) */
- ""
-};
-
-
-/*
- * mm_init - initialize the malloc package.
- */
-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)
-{
- loggerln("---------- malloc ----------");
- print_list();
- size_t newsize = align(size + SIZE_T_SIZE);
- loggerln("newsize: {}", newsize);
- size_t p = (size_t)mem_heap_lo();
- bool prev_alloc = true;
-
- while (true)
- {
- //if (newsize == 4080) sleep(1);
- size_t status = 0;
- loggerln("offset: {}", (size_t)p-(size_t)mem_heap_lo());
-
- // out of heap
- if (p >= (size_t)mem_heap_hi())
- {
- // then increase heap
- // and set current pos to be free
- // so it can be alloc'd soon
- loggerln("sbrk");
- mem_sbrk(newsize);
- set_size(status, newsize);
- set_alloc(status, false);
- *(size_t*)p = status;
- }
- status = *(size_t*)p;
- if (is_alloc(status))
- {
- p += get_size(status);
- prev_alloc = true;
- continue;
- }
- prev_alloc = false;
-
- if (get_size(status) < newsize)
- {
- next(p);
- continue;
- }
-
- size_t oldsize = get_size(status);
-
- set_size(status, newsize);
- set_alloc(status, true);
- set_prev_alloc(status, prev_alloc);
- *(size_t*)p = status;
- void *ret = (void*)((char*)p + 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, false);
- *(size_t*)(p+newsize) = free_status;
- loggerln("split free: free begins {}", offset(p+newsize));
- }
-
- loggerln("return: {}" , (size_t)ret-(size_t)mem_heap_lo());
- return ret;
-
- }
-
-}
-
-/*
- * mm_free - Freeing a block does nothing.
- */
-void mm_free(void *ptr)
-{
- if (ptr == nullptr) return;
-
- loggerln("---------- free ----------");
-
- loggerln("argument: {}", (size_t)ptr-(size_t)mem_heap_lo());
-
- print_list();
-
- size_t header = (size_t)ptr-SIZE_T_SIZE;
- size_t next = header + get_size(*(size_t*)header);
-
- if (next > (size_t)mem_heap_hi()) next = 0;
-
- loggerln("header: {}", offset(header));
- loggerln("header size: {}", get_size(*(size_t*)header));
- loggerln("next: {}", next != 0 ? offset(next) : 0);
-
- set_alloc(*(size_t*)header, false);
-
- // if next is free
- if ((next != 0)
- && (!is_alloc(*(size_t*)next)))
- {
- size_t newsize = get_size(*(size_t*)header) + get_size(*(size_t*)next);
- set_size(*(size_t*)header, newsize);
- }
-}
-
-/*
- * mm_realloc - Implemented simply in terms of mm_malloc and mm_free
- */
-void *mm_realloc(void *ptr, size_t size)
-{
- void *oldptr = ptr;
- void *newptr;
- size_t copySize;
-
- newptr = mm_malloc(size);
- if (newptr == NULL)
- return NULL;
- copySize = *(size_t *)((char *)oldptr - SIZE_T_SIZE);
- if (size < copySize)
- copySize = size;
- memcpy(newptr, oldptr, copySize);
- mm_free(oldptr);
- return newptr;
-}
-
-
-
-
-
-
-
-
-
-
-
-