Compiler Design - Modify Code: Memory Management project Given FirstFit, please
ID: 3913147 • Letter: C
Question
Compiler Design - Modify Code:
Memory Management project
Given FirstFit, please finish BestFit and WorstFit ?Follow the form of FirstFit!?
Do not provide different code.
-------------------------------------------------------------------------------------
//FirstFit.hpp
#ifndef FIRSTFIT_HPP
#define FIRSTFIT_HPP
#include "Policy.hpp"
class FirstFit : public Policy
{
public:
void* onMalloc(int size);
void onFree(void* address);
};
#endif // !FIRSTFIT_HPP
-------------------------------------------------------------------------------------
//FirstFit.cpp
#include "stdafx.h"
#include "FirstFit.hpp"
#include "MemoryStructure.hpp"
#include "ScheduleProcessor.hpp"
#include "defines.hpp"
#include <thread>
int defragmentPrepareEnd(int target_size);
int defragmentPrepareFront(int target_size);
void* FirstFit::onMalloc(int size)
{
MemoryStructure* ms = MemoryStructure::getInstance();
while(true)
{
void* prev_end = 0;
for(int i = 0; i < ms->getAllocationListSize(); i++)
{
Allocation alloc = ms->getAllocation(i);
if((int)alloc.addr - (int)prev_end >= size)
{
ASSERT(ms->allocate(prev_end, size));
return prev_end;
}
prev_end = (void*)((int)alloc.addr + (int)alloc.size);
}
if(MAX_MEMORY_CAP - (int)prev_end >= size)
{
ASSERT(ms->allocate(prev_end, size));
return prev_end;
}
if(!defragmentPrepareEnd(size)) defragmentPrepareFront(size);
}
}
void FirstFit::onFree(void* address)
{
MemoryStructure* ms = MemoryStructure::getInstance();
for(int i = 0; i < ms->getAllocationListSize(); i++)
{
Allocation alloc = ms->getAllocation(i);
if(alloc.addr == address)
{
ASSERT(ms->deallocate(address));
return;
}
}
printf("DE Error : allocation not found! (%d, %p) ", address, address);
exit(1);
}
int defragmentPrepareEnd(int target_size)
{
printf("*** Fast defragmentation!! Target Size : %d ", target_size);
std::this_thread::sleep_for(std::chrono::milliseconds(300));
MemoryStructure* ms = MemoryStructure::getInstance();
int last_cap = MAX_MEMORY_CAP;
// Retry until threshold
for(int search_length = ms->getAllocationListSize(); search_length; search_length--)
{
// Move while moving allocation succeeds
for(bool succeeded = true; succeeded;)
{
succeeded = false;
Allocation alloc_from = ms->getAllocation(search_length - 1);
void* prev_end = 0;
// Search for the space to move on
for(int i = 0; i < search_length; i++)
{
Allocation alloc_front_to = ms->getAllocation(i);
// Found the space
if((int)alloc_front_to.addr - (int)prev_end >= alloc_from.size)
{
ScheduleProcessor::getInstance()->notifyAddressChange(alloc_from.addr, prev_end);
// Move allocation
ASSERT(ms->migrate(alloc_from.addr, prev_end));
succeeded = true;
break;
}
prev_end = (void*)((int)alloc_front_to.addr + alloc_front_to.size);
}
// Check if the space is enough
Allocation last_alloc = ms->getAllocation(search_length - 1);
if(last_cap - (int)last_alloc.addr - last_alloc.size >= target_size)
{
printf("good! %d at %d ", search_length - 1, last_alloc.addr);
return true;
}
}
last_cap = (int)ms->getAllocation(search_length - 1).addr;
}
return false;
}
int defragmentPrepareFront(int target_size)
{
printf("*** Fast defragmentation failed. Slow defragmentation.... ");
std::this_thread::sleep_for(std::chrono::milliseconds(300));
MemoryStructure* ms = MemoryStructure::getInstance();
void* prev_end = 0;
for(int i = 0; i < ms->getAllocationListSize(); i++)
{
Allocation alloc_from = ms->getAllocation(i);
int rem_space = (int)alloc_from.addr - (int)prev_end;
if(rem_space > 0)
{
if(rem_space >= target_size) return 1;
ScheduleProcessor::getInstance()->notifyAddressChange(alloc_from.addr, prev_end);
// Move allocation
ASSERT(ms->migrate(alloc_from.addr, prev_end));
}
prev_end = (void*)((int)prev_end + alloc_from.size);
}
return 0;
}
-------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------
//BestFit.hpp
#ifndef BESTFIT_HPP
#define BESTFIT_HPP
#include "Policy.hpp"
class BestFit : public Policy
{
public:
void* onMalloc(int size);
void onFree(void* address);
};
#endif // !BESTFIT_HPP
-------------------------------------------------------------------------------------
//BestFit.cpp
#include "stdafx.h"
#include "BestFit.hpp"
#include "MemoryStructure.hpp"
#include "ScheduleProcessor.hpp"
#include "defines.hpp"
void* BestFit::onMalloc(int size)
{
// Here to be implemented
return (void*)0x60000000;
}
void BestFit::onFree(void* address)
{
// Here to be implemented
}
-------------------------------------------------------------------------------------
//WorstFit .hpp
#ifndef WORSTFIT_HPP
#define WORSTFIT_HPP
#include "Policy.hpp"
class WorstFit : public Policy
{
public:
void* onMalloc(int size);
void onFree(void* address);
};
#endif // !WORSTFIT_HPP
-------------------------------------------------------------------------------------
//WorstFit.cpp
#include "stdafx.h"
#include "WorstFit.hpp"
#include "MemoryStructure.hpp"
#include "ScheduleProcessor.hpp"
#include "defines.hpp"
void* WorstFit::onMalloc(int size)
{
// Here to be implemented
return (void*)0x60000000;
}
void WorstFit::onFree(void* address)
{
// Here to be implemented
}
Explanation / Answer
BestFit:
#include<stdio.h>
#include<conio.h>
#define max 25
void main()
{
int frag[max],b[max],f[max],i,j,nb,nf,temp,lowest=10000;
static int bf[max],ff[max];
clrscr();
printf(" Enter the number of blocks:");
scanf("%d",&nb);
printf("Enter the number of files:");
scanf("%d",&nf);
printf(" Enter the size of the blocks:- ");
for(i=1;i<=nb;i++)
{
printf("Block %d:",i);
scanf("%d",&b[i]);
}
printf("Enter the size of the files :- ");
for(i=1;i<=nf;i++)
{
printf("File %d:",i);
scanf("%d",&f[i]);
}
for(i=1;i<=nf;i++)
{
for(j=1;j<=nb;j++)
{
if(bf[j]!=1)
{
temp=b[j]-f[i];
if(temp>=0)
if(lowest>temp)
{
ff[i]=j;
lowest=temp;
}
}
}
frag[i]=lowest;
bf[ff[i]]=1;
lowest=10000;
}
printf(" File No File Size Block No Block Size Fragment");
for(i=1;i<=nf && ff[i]!=0;i++)
printf(" %d %d %d %d %d",i,f[i],ff[i],b[ff[i]],frag[i]);
getch();
}
Worst Fit:
#include<stdio.h>
#include<conio.h>
#define max 25
void main()
{
int frag[max],b[max],f[max],i,j,nb,nf,temp;
static int bf[max],ff[max];
clrscr();
printf(" Memory Management Scheme - First Fit");
printf(" Enter the number of blocks:");
scanf("%d",&nb);
printf("Enter the number of files:");
scanf("%d",&nf);
printf(" Enter the size of the blocks:- ");
for(i=1;i<=nb;i++)
{
printf("Block %d:",i);
scanf("%d",&b[i]);
}
printf("Enter the size of the files :- ");
for(i=1;i<=nf;i++)
{
printf("File %d:",i);
scanf("%d",&f[i]);
}
for(i=1;i<=nf;i++)
{
for(j=1;j<=nb;j++)
{
if(bf[j]!=1)
{
temp=b[j]-f[i];
if(temp>=0)
{
ff[i]=j;
break;
}
}
}
frag[i]=temp;
bf[ff[i]]=1;
}
printf(" File_no: File_size : Block_no: Block_size: Fragement");
for(i=1;i<=nf;i++)
printf(" %d %d %d %d %d",i,f[i],ff[i],b[ff[i]],frag[i]);
getch();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.