The goal of your next project is to simulate the C heap manager THIS NEEDS TO BE
ID: 3792809 • Letter: T
Question
The goal of your next project is to simulate the C heap manager
THIS NEEDS TO BE IMPLEMENTED IN C++. Thank you
•A runtime module used to allocate and de-allocate dynamic memory.
•The "heap" is a large "pool" of memory set aside by the runtime system
•The two main functions are –malloc, used to satisfy a request for a specific number of consecutive blocks;
–free, used to make allocated blocks available f
Our simulation uses –a large block of unsigned chars as our memory pool; and
–a doubly-linked list to keep track of allocated and available blocks of unsigned char.
–We will refer to the nodes of this list as blocknodes
•The info field of each node is of type blockdata
•An object of type blockdata has attributes–blocksize number of bytes in the block
–free a Boolean flag indicating the status of a block
–blockptr a pointer to the first byte of the block
The mallocalgorithm has an intparameter request
•requestis the size of the block to be allocated
•requestscans the list until it finds the first blocknodeBsuch that–B.free== true
–B.sizerequest
•If no such block is found, mallocreturns NULL (0)
If B.sizeis larger than request, the block is broken up into two blocks–The first block's size: request
–The second's size: B.size-request
•This requires that we insert a new blocknodeCafter B to reference the second block (which is free)
•Then, whether we split the block or not, we–set B.freeto false
–set B.sizeto request
–return the address B.bptr
To implement free(unsigned char *p)we must find the blocknodewhose bptrfield equals p
•This is done by traversing the blocknodelist
•If this fails, we terminate the program
•Otherwise we change the blocknode'sfreefield to true
•But we don't stop there
It should be clear that we want to maximize the size of the free blocks
•This means there should never be consecutive free blocks
•Whenever consecutive free blocks occur, they should be merged
•When we free a block, we need to check the previous and next blocks to see if they are free
•If so, we must merge the blocks into one big block
•This may involve the deletion of one or two blocknodes from our list
#ifndef __MM__
#define __MM__
#include <iostream>
#include <sstream>
#include "blockdata.h"
#include "dlUtils.h"
using namespace std;
class MemoryManager
{
public:
MemoryManager(unsigned int memsize);
~MemoryManager();
unsigned char * malloc(unsigned int request);
void free(unsigned char * ptr2block);
void showBlockList();
private:
unsigned int memsize;
unsigned char *baseptr;
dlNode<blockdata>* header;
dlNode<blockdata>* trailer;
void mergeForward(dlNode<blockdata> *p);
void mergeBackward(dlNode<blockdata> *p);
void splitBlock(dlNode<blockdata> *p,unsigned int chunksize);
};
#endif
#ifndef __MM__
#define __MM__
#include <iostream>
#include <sstream>
#include "blockdata.h"
#include "dlUtils.h"
using namespace std;
class MemoryManager
{
public:
MemoryManager(unsigned int memsize);
~MemoryManager();
unsigned char * malloc(unsigned int request);
void free(unsigned char * ptr2block);
void showBlockList();
private:
unsigned int memsize;
unsigned char *baseptr;
dlNode<blockdata>* header;
dlNode<blockdata>* trailer;
void mergeForward(dlNode<blockdata> *p);
void mergeBackward(dlNode<blockdata> *p);
void splitBlock(dlNode<blockdata> *p,unsigned int chunksize);
};
#endif
#ifndef __DLNODE__
#define __DLNODE__
#include <iostream>
#include <cassert>
template <class T>
class dlNode {
public:
T info;
dlNode<T> *prev;
dlNode<T> *next;
dlNode<T>(T val, dlNode<T> *p, dlNode<T> *n):info(val),prev(p),next(n){};
};
template <class T>
void printDlList(dlNode<T>* header,dlNode<T> *trailer,const char *sep)
{
assert(header != NULL && trailer != NULL);
dlNode<T> *cursor = header->next;
while(cursor->next != trailer) {
std::cout << cursor->info << sep;
cursor = cursor->next;
}
if (cursor->next = trailer)
std::cout << cursor->info << std::endl;
}
template <class T>
void insertAfter(dlNode<T> *trailer, dlNode<T> *current, T newval)
{
assert(current != trailer);
current->next = new dlNode<T>(newval,current,current->next);
current = current->next;
current->next->prev = current;
}
template <class T>
void deleteNode(dlNode<T>* header, dlNode<T>* trailer, dlNode<T>* current)
{
assert(current!= header && current != trailer);
dlNode<T> *hold = current;
current->prev->next = current->next;
current->next->prev = current->prev;
delete hold;
}
template <class T>
void deleteNext(dlNode<T>* header, dlNode<T>* trailer, dlNode<T> *current)
{
#include "dlUtils.h"
#include "blockdata.h"
#include <iostream>
using namespace std;
blockdata::blockdata(unsigned int s, bool f, unsigned char *p)
{
blocksize = s;
free = f;
blockptr = p;
}
ostream &operator << (ostream &out, const blockdata &B)
{
out << "[" << B.blocksize << ",";
if (B.free)
out << "free";
else
out << "allocated";
out << "]";
return out;
}
#include <cassert>
#include <iostream>
#include "dlUtils.h"
#include "MemoryManager.h"
MemoryManager::MemoryManager(unsigned int memtotal): memsize(memtotal)
{
baseptr = new unsigned char[memsize];
blockdata dummyBlock(0,false,0);
blockdata originalBlock(memsize,true,baseptr);
header = new dlNode<blockdata>(dummyBlock,nullptr,nullptr);
trailer = new dlNode<blockdata>(dummyBlock,nullptr,nullptr);
header->next = new dlNode<blockdata>(originalBlock,header,trailer);
trailer->prev = header->next;
}
MemoryManager::~MemoryManager()
{
delete [] baseptr;
clearList(header);
}
void MemoryManager::showBlockList()
{
printDlList(header,trailer,"->");
}
void MemoryManager::splitBlock(dlNode<blockdata> *p, unsigned int chunksize)
{
// Complete the code for this method
}
unsigned char * MemoryManager::malloc(unsigned int request)
{
// Complete the code for this method
}
void MemoryManager::mergeForward(dlNode<blockdata> *p)
{
// Complete the code for this method
}
void MemoryManager::mergeBackward(dlNode<blockdata> *p)
{
// Complete the code for this method
}
void MemoryManager::free(unsigned char *ptr2block)
{
// Complete the code for this method
}
#include <iostream>
#include "MemoryManager.h"
using namespace std;
int main()
{
MemoryManager heaper(50);
cout << " heap initialized ";
cout << " -------------BlockList start------------------ ";
heaper.showBlockList();
cout << "-------------BlockList end------------------ ";
// Next, carry out a number of calls to malloc and free and
// show the blocklist after each operation:
return 0;
}
Explanation / Answer
#include<iostream.h>
#include<conio.h>
void main()
{
int *ptr=new int(25);
float *pt1=new float(12.98);
char *ptr2=new char('f');
float *sum=new float;
clrscr();
cout<<"Integer value is "<<*ptr<<endl;
cout<<"Float value is "<<*pt1<<endl;
*sum=*ptr+*pt1;
cout<<"sum of int float"<<*sum<<endl;
cout<<"Char value is "<<*ptr2;
delete ptr;
getch();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.