Implement a malloc

Implement a malloc-like memory allocation library



1. Declare an array of 25000 bytes.

2. You must implement a function that is like malloc(). Call it MyMalloc(). Its signature is similar to malloc(). You should also implement MyFree() which has a signature and functionality that are similar to free().

3. MyMalloc() allocates memory only from the previously mentioned array of 25000 bytes.

4. All the data structures that are required to manage the memory must also reside within the same array.

5. MyMalloc() and MyFree() must be in a file called mymalloc.c. You should also provide a suitable header file mymalloc.h.

6. Our test program contains the main() and it includes mymalloc.h. Do not include a main() in your mymalloc.c.

Sample Code in C:

#include <stdio.h>
#include <stddef.h>
#include "mymalloc.h"

struct node *freearray=(void*)array; // to point the first place in array[25000]

void *mymalloc(size_t bytes){ // get the value of bytes that pass from mymalloc function in main function
    struct node *current; 
    void *result; 
    
    if(!(freearray->size)){ 
    initialize();
    printf("Memory Allocated\n");
}
current=freearray; // assign the first place address of array[25000] to current
    
    while((((current->size)<bytes)||((current->free)==0))&&(current->link!=NULL)){
    current=current->link;
    printf("One node checked\n");
    }
    
    if((current->size)==bytes){
    current->free=0;
    result=(void*)(++current);
    printf("One node allocated\n");
    return result;
}
else if((current->size)>(bytes+sizeof(struct node))){ //array[25000] size > metadata+bytes then this function run
devide(current,bytes);
result=(void*)(++current); // point to the array after the metadata part
printf("One node allocated with a devide\n");
return result;
}
else{
result=NULL;
printf("Your memory is not enough\n");
return result;
}
}
void initialize(){ // runs only in first time because our array[25000] is still free
freearray->size=25000-sizeof(struct node); // metadata node
freearray->free=1;
freearray->link=NULL;
}

void devide(struct node* space, size_t size){
struct node *new=(void*)space+size+sizeof(struct node); // create a pointer "new" to store the new address of starting point of array
new->size=(space->size)-size-sizeof(struct node);
new->free=1;
new->link=space->link;
space->size=size;
space->free=0;
space->link=new;
}
void myfree(void* ptr){
if(((void*)array<=ptr)&&(ptr<=(void*)(array+25000))){ // check whether the ptr address is inside the array[25000]
struct node* current=ptr;
--current; // move the pointer to remove metadata part
current->free=1;
add(); 
}
else printf("Enter a valid pointer\n");
}
void add(){ // add two nearby nodes as one node
struct node *current;
current=freearray;
while((current->link)!=NULL){
if((current->free)&&(current->link->free)){
current->size+=(current->link->size)+sizeof(struct node);
current->link=current->link->link;
}
current=current->link;
}
}
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);
int *j = (int*)mymalloc(100*sizeof(int));
myfree(r);
int *k = (int*)mymalloc(500*sizeof(int));

int *s;
int *a=(int*)mymalloc(10000*sizeof(int));
myfree(s);

}

Sample Header File in C:

#include <stdio.h>
#include <stddef.h>

char array[25000]; // create a array of 25000 bytes

struct node{
size_t size;
int free; // to know a patticular node is free or full (if free=1 then node is free else it full)
struct node *link; // to store the address of next node
};

void initialize();
void devide(struct node *space, size_t size);
void *mymalloc(size_t bytes);
void add();
void myfree(void *ptr);




Comments

Popular posts from this blog

Programming Using GNU Octave

Library Problem

What Is A Gamma Ray Burst?