117 lines
3.3 KiB
C
Executable File
117 lines
3.3 KiB
C
Executable File
/*
|
|
* 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 <stdint.h>
|
|
|
|
#include "mm.h"
|
|
#include "memlib.h"
|
|
|
|
team_t team = {
|
|
/* Team name */
|
|
"MA",
|
|
/* First member's full name */
|
|
"Marvin Aleksa",
|
|
/* First member's email address */
|
|
"marvin.aleksa@stud.uni-due.de",
|
|
/* Second member's full name (leave blank if none) */
|
|
"",
|
|
/* Second member's email address (leave blank if none) */
|
|
""
|
|
};
|
|
|
|
typedef uint32_t t4; // 4 byte wide unsigned data type
|
|
typedef uint64_t t8; // 8 byte wide unsigned data type
|
|
|
|
typedef struct {
|
|
uint32_t ln;
|
|
void *nx;
|
|
} Header;
|
|
|
|
/* single word (4) or double word (8) alignment */
|
|
#define AL 8 // word length to align to
|
|
#define PS 4096 // pagesize
|
|
#define BS AL*1 // target block size
|
|
|
|
/* rounds up to the nearest multiple of ALIGNMENT */
|
|
#define ALIGN(size) (((size) + (AL-1)) & ~0x7)
|
|
#define ALIGN_ADDR(addr) (void *)ALIGN((int)addr)
|
|
#define SIZE_T_SIZE (ALIGN(sizeof(t8)))
|
|
|
|
#define SET_ALL0(x) ((x) & ~1)
|
|
#define SET_ALL1(x) ((x) | 1)
|
|
#define GET_LENG(x) ((x) & ~1)
|
|
#define GET_ALLC(x) ((x) & 1)
|
|
|
|
void *heap = NULL;
|
|
|
|
/* mm_init - initialize the malloc package. */
|
|
int mm_init(void) {
|
|
// printf("init -> allocating initial heap at %i bytes\n", PS);
|
|
heap = mem_sbrk(PS);
|
|
// printf("init -> heap starts at %u + %i bytes\n", mem_heap_lo(), mem_heapsize());
|
|
// printf(" ends at %u\n", mem_heap_hi());
|
|
|
|
Header *first = heap;
|
|
Header *final = mem_heap_hi();
|
|
first->ln = SET_ALL1(16);
|
|
first->nx = final;
|
|
// printf("init -> first block at %u with ln %i and next %u\n", first, first->ln, first->nx);
|
|
final->ln = SET_ALL1(0);
|
|
final->nx = NULL;
|
|
// printf("init -> final block at %u with ln %i and next %u\n", final, final->ln, final->nx);
|
|
|
|
|
|
// printf("\n");
|
|
return 0;
|
|
}
|
|
|
|
/* 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) {
|
|
// printf("malloc -> %i requested, aligned to + overhead %i\n", size, ALIGN( size ) + 8);
|
|
|
|
int newsize = ALIGN(size + SIZE_T_SIZE);
|
|
void *p = mem_sbrk(newsize);
|
|
if (p == (void *)-1)
|
|
return NULL;
|
|
else {
|
|
*(size_t *)p = size;
|
|
return (void *)((char *)p + SIZE_T_SIZE);
|
|
}
|
|
// printf("\n");
|
|
}
|
|
|
|
/* mm_free - Freeing a block does nothing. */
|
|
void mm_free(void *ptr) {
|
|
|
|
}
|
|
|
|
/* 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;
|
|
}
|