Simulation of a basic memory allocator and garbage collector using doubly linked list

#include<stdio.h>

#include<stdlib.h>

struct node

{

int data;

int avail;

struct node *llink,*rlink;

}*first=NULL,*last=NULL,*temp=NULL,*t=NULL;

void create()

{

temp=(struct node*)malloc(sizeof(struct node));

printf("Enter data:");

scanf("%d",&temp->data);

temp->avail=1;

temp->llink=NULL;

temp->rlink=NULL;

if(first==NULL)

{

first=temp;

last=temp;

}

else

{

last->rlink=temp;

temp->llink=last;

last=temp;

}

}

void display()

{

temp=first;

printf("NULL");

while(temp!=NULL)

{

printf(" %d:%d<---->",temp->data,temp->avail);

temp=temp->rlink;

}

printf(" NULL\n");

}

void allocate()

{

int size,newdata;

printf("Enter size\n");

scanf("%d",&size);

temp=first;

while(temp!=NULL)

{

if(temp->data>=size && temp->avail==1)

   break;

temp=temp->rlink;

}

if( temp==NULL)

  printf("cannot allocate\n");

else if (temp->data==size)

  temp->avail=0;

 else

   {newdata=temp->data-size;

    temp->data=size;

    temp->avail=0;

    t=(struct node*)malloc(sizeof(struct node));

    t->data=newdata;

    t->avail=1;

    t->llink=temp;

    t->rlink=temp->rlink;

    t->llink=temp;

    temp->rlink=t;

   }

}

void garbagecollect()

{

 temp=first;

 int pos[100],p=1,i=0,j,tp;

 char ch;

    while(temp!=NULL)

    {

     if(temp->avail==0)

      {pos[i]=p;i++;}

     p++; 

     temp=temp->rlink;

    }

    printf("list of allocated positions.... \n");

    tp=--i;

 for(i=tp;i>=0;i--)

  {printf("%d-->",pos[i]);}

 for(i=0;i<=tp;i++)

  {

    printf("want to free...%d\n..enter y if yes::",pos[i]);

    ch=getchar();

    scanf("%c",&ch);

   if(ch=='y')

    {

        temp=first;

    for(j=1;j<pos[i];j++)

    {

    temp=temp->rlink;

    }

    temp->avail=1;

    } // end of choice

}//end of for

}

void main()

{

int ch;

do

{

printf("\nMenu\n----\n1. Create AVAIL List\n2.Display\n3.Allocate\n4.garbagecollect\n5.Exit\n");

printf("\nEnter the choice:");

scanf("%d",&ch);

switch(ch)

{

case 1:

    create();

    display();

    break;

case 2:

    display();

    break;

case 3:

     allocate();

     break;

case 4:

     garbagecollect();

     break;

case 5:

    exit(0);

default:

    printf("There is no such operation:\n");

}

}

while(1);

}


Comments

  1. I thoroughly enjoyed reading this article. The content was informative, well-structured, and presented in a way that made complex ideas easy to understand. The practical insights and detailed explanations added significant value and kept the discussion engaging throughout. Students studying legal subjects often look for law assignment writing help when working on case analyses, legal research, and academic writing tasks. Thank you for sharing such useful knowledge and thoughtful perspectives. Keep up the excellent work, and I look forward to reading more insightful and educational posts in the future.

    ReplyDelete

Post a Comment

Popular posts from this blog

Data Structure Lab CSL 201 Manual KTU - Dr Binu V P-9847390760

Polynomial Addition using Arrays

Sparse Matrix -Representation,Addition ,Transpose