various bugfixes for Fall 2022

This commit is contained in:
Godmar Back 2022-10-28 13:40:22 -04:00
parent 91b0f7b037
commit e4275ea995

View File

@ -13,10 +13,10 @@
* but note that this is a choice (my actual solution chooses * but note that this is a choice (my actual solution chooses
* to count everything in bytes instead.) * to count everything in bytes instead.)
* *
* You may use this code as a starting point for your implementation * You should use this code as a starting point for your
* if you want. * implementation.
* *
* Adapted for CS3214 Summer 2020 by gback * First adapted for CS3214 Summer 2020 by gback
*/ */
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
@ -142,6 +142,7 @@ int mm_init(void)
{ {
assert (offsetof(struct block, payload) == 4); assert (offsetof(struct block, payload) == 4);
assert (sizeof(struct boundary_tag) == 4); assert (sizeof(struct boundary_tag) == 4);
/* Create the initial empty heap */ /* Create the initial empty heap */
struct boundary_tag * initial = mem_sbrk(4 * sizeof(struct boundary_tag)); struct boundary_tag * initial = mem_sbrk(4 * sizeof(struct boundary_tag));
if (initial == (void *)-1) if (initial == (void *)-1)
@ -176,9 +177,12 @@ void *mm_malloc(size_t size)
return NULL; return NULL;
/* Adjust block size to include overhead and alignment reqs. */ /* Adjust block size to include overhead and alignment reqs. */
size += 2 * sizeof(struct boundary_tag); /* account for tags */ size_t bsize = align(size + 2 * sizeof(struct boundary_tag)); /* account for tags */
if (bsize < size)
return NULL; /* integer overflow */
/* Adjusted block size in words */ /* Adjusted block size in words */
size_t awords = max(MIN_BLOCK_SIZE_WORDS, align(size)/WSIZE); /* respect minimum size */ size_t awords = max(MIN_BLOCK_SIZE_WORDS, bsize/WSIZE); /* respect minimum size */
/* Search the free list for a fit */ /* Search the free list for a fit */
if ((bp = find_fit(awords)) != NULL) { if ((bp = find_fit(awords)) != NULL) {
@ -270,9 +274,9 @@ void *mm_realloc(void *ptr, size_t size)
/* Copy the old data. */ /* Copy the old data. */
struct block *oldblock = ptr - offsetof(struct block, payload); struct block *oldblock = ptr - offsetof(struct block, payload);
size_t oldsize = blk_size(oldblock) * WSIZE; size_t oldpayloadsize = blk_size(oldblock) * WSIZE - 2 * sizeof(struct boundary_tag);
if (size < oldsize) oldsize = size; if (size < oldpayloadsize) oldpayloadsize = size;
memcpy(newptr, ptr, oldsize); memcpy(newptr, ptr, oldpayloadsize);
/* Free the old block. */ /* Free the old block. */
mm_free(ptr); mm_free(ptr);
@ -349,7 +353,7 @@ team_t team = {
/* First member's full name */ /* First member's full name */
"Godmar Back", "Godmar Back",
"gback@cs.vt.edu", "gback@cs.vt.edu",
/* Second member's full name (leave blank if none) */ /* Second member's full name (leave as empty strings if none) */
"", "",
"", "",
}; };