Skip to main content

The full code


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!");
}



Comments

  1. How to do it using best fit strategy

    ReplyDelete
  2. In initialize(), where you set the freeList size:

    freeList->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

    ReplyDelete
    Replies
    1. 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.

      Delete
    2. You have to take into account the size of the header aka "block"

      Delete
  3. VS Code doesn't seem to like this line:
    `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.

    ReplyDelete
  4. Is it possible to change MyMalloc into MyAlloc implementation with a small tweak?

    ReplyDelete
  5. My question is why are you ++curr when your return the result?

    ReplyDelete
    Replies
    1. we 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

Post a Comment

Popular posts from this blog

How to connect my database instance with elastic beanstalk instance in AWS?

If you have deployed your web application in Elastic Beanstalk in AWS and now you need to connect a database to this instance, and your database is actually residing in a different instance, how can you actually connect them? It's easy with Elastic Beanstalk. I will explain an example scenario that I used for connecting my elastic beanstalk web application with another instance containing my MongoDB database. By looking at this, you can customize as per your need. Don't worry. This is easy. :) The only things you need here are the details about the 1. Database name that you need to connect to. Ex:- "myDB" 2. Port at which the database instance is listening. EX:- In the case of MongoDB, the listening port is 27017 3. Host name of your database instance. EX:- Like localhost, in this case, it will be the Public DNS of your database instance 4. The password of your database if exists. First these details need to be set as environment variables in Elastic Be

How to import the Public Certificate of one WSO2 product to the trust store of another?

To demonstrate this point, I will use the 2 products WSO2 API Manager 2.1.0 (referred as APIM from here onwards) and WSO2 Enterprise Integrator 6.1.1 (referred as EI from here onwards). When using EI as the Business Process Server during configuration of Workflows in APIM, one step to perform is to import the public certificate of EI to the truststore of APIM [1]. So now let's see how this can be done. Step 1: Go to <EI_HOME>/repository/resources/security/ folder and execute the following keytool command. This command is used to export the public certificate of EI as a certificate file called wso2carbon.cer. Since the default keystore in EI is wso2carbon.jks, we have specified it as the keystore and the default alias is wso2carbon. Provide wso2carbon as the keystore password when prompted as it is the default password. After executing the above command from within the security folder in EI, you will see that a file with the name of wso2carbon.cer is created