Wednesday, May 8, 2013

Simple Malloc

//Make file code


newmalloc:newmalloc.o
gcc -o newmalloc newmalloc.o


//Malloc Code(Save as newmalloc.c)

#include <stdio.h>
#include <stdlib.h>
#define Memory_Size 50000


char NewMemory[50000] ;//declare the size of the array



//implementing free


void NewFree(void *point){

sprintf(&NewMemory[49901],"%d",0) ;     //pointing the zeroth element of the array
do{

if(point==&NewMemory[atoi(&NewMemory[49901])]) { ///check wether the pointer memory address with the  array index address

NewMemory[atoi(&NewMemory[49901])]='y' ;
NewMemory[atoi(&NewMemory[49901])+1]='z' ;
sprintf(&NewMemory[49888],"%d",atoi(&NewMemory[49901]));
//putting the siz of area that wants to free at the end of the "y" character 
NewMemory[atoi(&NewMemory[49901])+7]=='z' ; 

do{

if (NewMemory[atoi(&NewMemory[49901])+1]=='x'){
//getting the memory siz to be free 
sprintf(&NewMemory[atoi(&NewMemory[49888])+1],"%d",(atoi(&NewMemory[49901])-atoi(&NewMemory[49888]))) ;

break;}


else {

sprintf(&NewMemory[49901],"%d",(atoi(&NewMemory[49901])+1)) ; 
}

}while((atoi(&NewMemory[49901]))<Memory_Size);

break;

}

sprintf(&NewMemory[49901],"%d",(atoi(&NewMemory[49901])+1)) ;
 
}while((atoi(&NewMemory[49901]))<Memory_Size);


sprintf(&NewMemory[49894],"%d",((atoi(&NewMemory[49894]))-(atoi(&NewMemory[49888])))) ;



}

void initialization(){


//puting x,y,z to the first three telements of the array to do the searching process of allocation and deallocation
NewMemory[0]='x'; 
NewMemory[1]='y';
NewMemory[2]='z';
//point to z to remember the end point of allocated area
NewMemory[49900]='z';
NewMemory[49893]='z';
sprintf(&NewMemory[49894] , "%d" , 0);//first insert 0 to the zeroth index of the array keep remember that location in 49900th memory index
sprintf(&NewMemory[49888] , "%d" , 0);
}

//implementing malloc



void *NewMalloc(size_t siz){//passes the size of memory to be allocated 

if (NewMemory[0]!='x'){

initialization();

}


if (siz> (49893 - atoi(&NewMemory[49894]))){
//check wether we have enough memory to allocate memory in our memory 
//if not return NULL
return NULL;
}
void allocation1(){
NewMemory[atoi(&NewMemory[49901])]='0' ;
NewMemory[atoi(&NewMemory[49901])+siz+1]='x' ;//again insert symbol x to identify the area to be allocated
NewMemory[atoi(&NewMemory[49901])+siz+2]='y' ;
NewMemory[atoi(&NewMemory[49901])+siz+3]='z' ;
NewMemory[atoi(&NewMemory[49901])+siz+9]='z' ;//insert the size to be freed inside the end six bytes after z symbol
sprintf(&NewMemory[atoi(&NewMemory[49901])+siz+3],"%d",(atoi(&NewMemory[atoi(&NewMemory[49901])+1])-(siz-3))) ;


}
void allocation2(){
NewMemory[atoi(&NewMemory[49901])]='0' ;
NewMemory[atoi(&NewMemory[49901])+siz]='x' ;
NewMemory[atoi(&NewMemory[49901])+siz+1]='y' ;
NewMemory[atoi(&NewMemory[49901])+siz+2]='z' ;
sprintf(&NewMemory[49894],"%d",atoi(&NewMemory[49998])+(siz+1)) ;

}

{
for(sprintf(&NewMemory[49901] , "%d",0);atoi(&NewMemory[49901])<Memory_Size;sprintf(&NewMemory[49901], "%d",(atoi(&NewMemory[49901])+1))){/////travelling trough the array to allocate memory


if(NewMemory[atoi(&NewMemory[49901])]=='y'){  //check whether the value of memorylocation 49901 equals to the aski value of y or not

if(NewMemory[atoi(&NewMemory[49901])+1]== 'z'){ 

if(NewMemory[atoi(&NewMemory[49901])+7] == 'z'){

if(atoi(&NewMemory[atoi(&NewMemory[49901])+1]) > (siz+3)){
allocation1();
break;
}
}

else{
allocation2();

break;
}
}
}
}

 
 //Finally allocate the memory in the above array by returning the relavent memory address

return(&NewMemory[atoi(&NewMemory[49901])]) ; 
}

    }