Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Heap_manager/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
final: main.o mm.o heap.o
cc main.o mm.o heap.o -o final
main.o: main.c
cc -c main.c
mm.o: mm.h mm.c
cc -c mm.c
heap.o: heap.h heap.c
cc -c heap.c
clean:
rm *.o final
Binary file added Heap_manager/final
Binary file not shown.
146 changes: 146 additions & 0 deletions Heap_manager/heap.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
#include <stdio.h>
#include "heap.h"

#define max(a, b) (a) > b ? a : b

void heapifyup(heap *h)
{
int i = h->rear;
int parent = (i - 1) / 2;
while (h->arr[i]->size > h->arr[parent]->size)
{
meta_block *temp = h->arr[i];
h->arr[i] = h->arr[parent];
h->arr[parent] = temp;
i = parent;
parent = (i - 1) / 2;
}
return;
}

void heapifydown(heap *h)
{
int i = 0;
while (1)
{
int left = i * 2 + 1;
int right = i * 2 + 2;
int maxi = h->arr[i]->size;
if (left <= h->rear)
maxi = max(maxi, h->arr[left]->size);
if (right <= h->rear)
maxi = max(maxi, h->arr[right]->size);
if (maxi == h->arr[i]->size)
return;
if (left <= h->rear && maxi == h->arr[left]->size)
{
meta_block *temp = h->arr[i];
h->arr[i] = h->arr[left];
h->arr[left] = temp;
i = left;
}
else
{
meta_block *temp = h->arr[i];
h->arr[i] = h->arr[right];
h->arr[right] = temp;
i = right;
}
}
}

void remove_heap(heap *h)
{
if (h->rear == -1)
return;
meta_block *ans = h->arr[0];
h->arr[0] = h->arr[h->rear];
h->rear--;
if (h->rear != -1)
heapifydown(h);
}

void insert_heap(heap *h, meta_block *mb)
{
if (h->rear == h->size - 1)
return;
h->arr[++h->rear] = mb;
heapifyup(h);
return;
}

meta_block *top_heap(heap *h)
{
if (h->rear == -1)
return NULL;
return h->arr[0];
}

int findindex(heap *h, meta_block *mb)
{
for (int i = 0; i <= h->rear; i++)
{
if (h->arr[i] == mb)
return i;
}
return -1;
}

void goup(heap *h, int i)
{
int parent = (i - 1) / 2;
while (h->arr[parent]->size < h->arr[i]->size)
{
meta_block *temp = h->arr[i];
h->arr[i] = h->arr[parent];
h->arr[parent] = temp;
i = parent;
parent = (i - 1) / 2;
}
return;
}

void godown(heap *h, int i)
{
while (1)
{
int left = i * 2 + 1;
int right = i * 2 + 2;
int maxi = h->arr[i]->size;
if (left <= h->rear)
maxi = max(maxi, h->arr[left]->size);
if (right <= h->rear)
maxi = max(maxi, h->arr[right]->size);
if (maxi == h->arr[i]->size)
return;
if (left <= h->rear && maxi == h->arr[left]->size)
{
meta_block *temp = h->arr[i];
h->arr[i] = h->arr[left];
h->arr[left] = temp;
i = left;
}
else
{
meta_block *temp = h->arr[i];
h->arr[i] = h->arr[right];
h->arr[right] = temp;
i = right;
}
}
}

void findandremove(heap *h, meta_block *mb)
{
int index = findindex(h, mb);
if (index == -1)
return;
h->arr[index] = h->arr[h->rear];
h->rear--;
int parent = (index - 1) / 2;
if (h->arr[parent]->size < h->arr[index]->size)
goup(h, index);
else
godown(h, index);
return;
}
25 changes: 25 additions & 0 deletions Heap_manager/heap.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#ifndef heaph
#define heaph
#include "mm.h"

// heap structure
typedef struct heap
{
int size;
int rear;
meta_block *arr[0];
} heap;

// insert in heap
void insert_heap(heap *h, meta_block *mb);

// finding top element from heap
meta_block *top_heap(heap *h);

// remove top element from heap
void remove_heap(heap *h);

// deleting some intermidiate element from heap
void findandremove(heap *h, meta_block *mb);

#endif
Binary file added Heap_manager/heap.o
Binary file not shown.
80 changes: 80 additions & 0 deletions Heap_manager/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#include <stdio.h>
#include <string.h>
#include "mm.h"

typedef struct student // size 16 kb
{
char name[10];
int id;
} student;

int main()
{
mm_init();
// printf("%d",sizeof(student));




// For malloc Testing
// student* st1=Malloc(sizeof(student)*1);
// st1->id = 12;
// strcpy(st1->name, "siddhesh");
// printf("%d %s\n", st1->id, st1->name);
// student* st2=Malloc(sizeof(student)*2);
// student* st3=Malloc(sizeof(student)*3);
// student* st4=Malloc(sizeof(student)*4);
// student* st5=Malloc(sizeof(student)*5);
// student* st6=Malloc(sizeof(student)*6);
// Free(st1);
// Free(st3);
// Free(st5);
// printheap();




// // for Free Testing
// student* st1=Malloc(sizeof(student)*1);
// student* st2=Malloc(sizeof(student)*2);
// student* st3=Malloc(sizeof(student)*3);
// student* st4=Malloc(sizeof(student)*1);
// // student* st5=Malloc(sizeof(student)*5);
// // student* st6=Malloc(sizeof(student)*6);
// Free(st2);
// Free(st1);
// // Free(st3);
// // Free(st5);
// // student* st7=Malloc(4496);
// printheap();





// // for Calloc testing
// student *st1 = Calloc(1,sizeof(student));;
// student *st2 = Calloc(2,sizeof(student));
// printf("%d %s\n",st1->id,st1->name);
// st1->id = 12;
// strcpy(st1->name, "siddhesh");
// printf("%d %s\n", st1->id, st1->name);




// For Realloc
// student* st1=Malloc(sizeof(student)*10);
// st1->id = 12;
// strcpy(st1->name, "siddhesh");
// printf("%d %s\n", st1->id, st1->name);
// printheap();
// st1=Realloc(st1,sizeof(student)*2);
// printf("%d %s\n", st1->id, st1->name);
// printheap();
// st1=Realloc(st1,sizeof(student)*5);
// printf("%d %s\n", st1->id, st1->name);
// printheap();

return 0;
}
Binary file added Heap_manager/main.o
Binary file not shown.
Loading