malloc.c
#include<stdio.h>
#include<stddef.h>
#include "mymalloc.h"
void initialize(){
freeList->size=20000-sizeof(struct block);
freeList->free=1;
freeList->next=NULL;
}
void split(struct block *fitting_slot,size_t size){
struct block *new=(void*)((void*)fitting_slot+size+sizeof(struct block));
new->size=(fitting_slot->size)-size-sizeof(struct block);
new->free=1;
new->next=fitting_slot->next;
fitting_slot->size=size;
fitting_slot->free=0;
fitting_slot->next=new;
}
void *MyMalloc(size_t noOfBytes){
struct block *curr,*prev;
void *result;
if(!(freeList->size)){
initialize();
printf("Memory initialized\n");
}
curr=freeList;
while((((curr->size)<noOfBytes)||((curr->free)==0))&&(curr->next!=NULL)){
prev=curr;
curr=curr->next;
printf("One block checked\n");
}
if((curr->size)==noOfBytes){
curr->free=0;
result=(void*)(++curr);
printf("Exact fitting block allocated\n");
return result;
}
else if((curr->size)>(noOfBytes+sizeof(struct block))){
split(curr,noOfBytes);
result=(void*)(++curr);
printf("Fitting block allocated with a split\n");
return result;
}
else{
result=NULL;
printf("Sorry. No sufficient memory to allocate\n");
return result;
}
}
void merge(){
struct block *curr,*prev;
curr=freeList;
while((curr->next)!=NULL){
if((curr->free) && (curr->next->free)){
curr->size+=(curr->next->size)+sizeof(struct block);
curr->next=curr->next->next;
}
prev=curr;
curr=curr->next;
}
}
void MyFree(void* ptr){
if(((void*)memory<=ptr)&&(ptr<=(void*)(memory+20000))){
struct block* curr=ptr;
--curr;
curr->free=1;
merge();
}
else printf("Please provide a valid pointer allocated by MyMalloc\n");
}
malloc.h
#include<stdio.h>
#include<stddef.h>
char memory[20000];
struct block{
size_t size;
int free;
struct block *next;
};
struct block *freeList=(void*)memory;
void initialize();
void split(struct block *fitting_slot,size_t size);
void *MyMalloc(size_t noOfBytes);
void merge();
void MyFree(void* ptr);
Main.c
#include<stdio.h>
int main(){
int *p=(int)MyMalloc(100*sizeof(int));
char *q=(char)MyMalloc(250*sizeof(char));
int *r=(int)MyMalloc(1000*sizeof(int));
MyFree(p);
char *w=(char)MyMalloc(700);
MyFree(r);
int *k=(int)MyMalloc(500*sizeof(int));
printf("Allocation and deallocation is done successfully!");
}
How to do it using best fit strategy
ReplyDeleteIn initialize(), where you set the freeList size:
ReplyDeletefreeList->size=20000-sizeof(struct block);
I was wondering why you use the number "20000" to subtract the size of the struct block. Is there any reason for that? Thanks
I think it's because when you initialize the list you have an initial metablock and this metablock has a certain size in the memory.
DeleteYou have to take into account the size of the header aka "block"
DeleteVS Code doesn't seem to like this line:
ReplyDelete`struct block *new = (void*)((void*)fitting_slot + size + sizeof(struct block));`, but when I remove the cast of `fitting_slot` to a `void*`, it looks fine. Does the cast do anything important? It seems to just cause problems and I'm not sure what it is actually supposed to do.
Is it possible to change MyMalloc into MyAlloc implementation with a small tweak?
ReplyDeleteMy question is why are you ++curr when your return the result?
ReplyDeletewe return curr +1 because we want to return a pointer to the region after the current block. Since block is a pointer of type struct block, +1 increments the address by one sizeof(struct block).
Delete