Commit 508bad1e authored by Bhavesh Yadav's avatar Bhavesh Yadav

Implemented mm_alloc, mm_free and mm_realloc

parent 01b4e45a
/*
* mm-naive.c - The fastest, least memory-efficient malloc package.
* mm.c.
*
* 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.
* In this approach, a block is allocated by first searching through
* the list of free blocks, if found.
* Also if the required size of block is less than half the size of
* free block, the free block is split into two blocks. One is allocated
* to the user and other is marked as free.
* If there is no free block available of the required size then memory
* is allocated by incrementing the brk pointer
* Every block has size information as meta data.
* When a block is freed it is added to the list of free blocks.
* While reallocating a block if the new size is smaller than the original
* size of the block then the block is split as in the case of malloc from
* free block.
*/
#include <stdio.h>
#include <stdlib.h>
......@@ -24,16 +29,24 @@
********************************************************/
team_t team = {
/* Team name */
"TIGERS",
"H4-393",
/* First member's full name */
"UMESH",
"Bhaveshkumar Yadav",
/* First member's email address */
"ub@cse.iitb.ac.in",
"bhaveshy@iitb.ac.in",
/* Second member's full name (leave blank if none) */
"",
"Shreyansh Jain",
/* Second member's email address (leave blank if none) */
""
"shreyansh@cse.iitb.ac.in"
};
struct mm_block
{
unsigned short isUsed;
size_t size;
};
struct mm_block *freeList[100];
unsigned int freeListSize = 0;
/* single word (4) or double word (8) alignment */
#define ALIGNMENT 8
......@@ -42,28 +55,66 @@ team_t team = {
#define ALIGN(size) (((size) + (ALIGNMENT-1)) & ~0x7)
#define SIZE_T_SIZE (ALIGN(sizeof(size_t)))
#define SIZE_T_SIZE (ALIGN(sizeof(struct mm_block)))
/*
* mm_init - initialize the malloc package.
*/
int mm_init(void)
{
freeListSize = 0;
return 0;
}
void* freeBlockAvailable(size_t size) {
void* ptr;
size_t extraSize;
struct mm_block *newPtr;
for(int i = 0; i<freeListSize; i++) {
if(freeList[i]->size >= size) {
freeList[i]->isUsed = 1;
ptr = (void *)((char *)freeList[i] + SIZE_T_SIZE);
extraSize = freeList[i]->size - size;
if(2*extraSize >= freeList[i]->size) {
freeList[i]->size = size;
struct mm_block newBlock;
newBlock.size = extraSize;
newBlock.isUsed = 0;
newPtr = freeList[i] + ALIGN(freeList[i]->size + SIZE_T_SIZE);
*newPtr = newBlock;
freeList[i] = newPtr;
}else {
freeList[i] = freeList[freeListSize-1];
freeListSize--;
}
return ptr;
}
}
return NULL;
}
void mm_addToFreeList(struct mm_block *ptr) {
freeList[freeListSize] = ptr;
freeListSize++;
}
/*
* 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)
{
{
void *freePtr = freeBlockAvailable(size);
if(freePtr != NULL)
return freePtr;
int newsize = ALIGN(size + SIZE_T_SIZE);
void *p = mem_sbrk(newsize);
if (p == (void *)-1)
return NULL;
else {
*(size_t *)p = size;
struct mm_block temp;
temp.isUsed = 1;
temp.size = size;
*(struct mm_block *)p = temp;
return (void *)((char *)p + SIZE_T_SIZE);
}
}
......@@ -72,22 +123,40 @@ void *mm_malloc(size_t size)
* mm_free - Freeing a block does nothing.
*/
void mm_free(void *ptr)
{
{ struct mm_block *newPtr;
newPtr = (struct mm_block *)ptr;
newPtr = newPtr - 1;
newPtr->isUsed = 0;
mm_addToFreeList(newPtr);
}
/*
* mm_realloc - Implemented simply in terms of mm_malloc and mm_free
*/
void *mm_realloc(void *ptr, size_t size)
{
{
void *oldptr = ptr;
struct mm_block *blockPtr = (struct mm_block*)((char *)oldptr - SIZE_T_SIZE);
void *newptr;
size_t copySize;
if(blockPtr->size >= size) {
size_t extraSize = blockPtr->size - size;
/* */
if(2*extraSize >= blockPtr->size) {
blockPtr->size = size;
struct mm_block newBlock;
newBlock.size = extraSize;
newBlock.isUsed = 0;
newptr = blockPtr + ALIGN(blockPtr->size + SIZE_T_SIZE);
*(struct mm_block *)newptr = newBlock;
freeList[freeListSize++] = (struct mm_block *)newptr;
return oldptr;
}
}
newptr = mm_malloc(size);
if (newptr == NULL)
return NULL;
copySize = *(size_t *)((char *)oldptr - SIZE_T_SIZE);
copySize = blockPtr->size;
if (size < copySize)
copySize = size;
memcpy(newptr, oldptr, copySize);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment