Question
The goal of this program is to simulate the C heap manager which is used to allocate and deallocate dynamic memory. Below are codes that
The goal of this program is to simulate the C heap manager which is used to allocate and deallocate dynamic memory.
Below are codes that I have completed. Use to complete the code for MemoryManager.cpp. The only one needed to be editted is MemoryManager.cpp.
Blockdata.cpp
#include "blockdata.h"
#include
using namespace std;
blockdata::blockdata(unsigned int s, unsigned char *p, bool f)
{
blocksize = s;
blockptr = p;
free = f;
}
ostream &operator << (ostream &out, const blockdata &B)
{
out << "[" << B.blocksize << ",";
if (B.free)
out << "free";
else
out << "allocated";
out << "]";
return out;
}
Blockdata.h
#ifndef _BLOCKDATA_
#define _BLOCKDATA_
#include
using namespace std;
struct blockdata {
friend ostream& operator << (ostream&, const blockdata &);
int blocksize;
bool free;
unsigned char *blockptr;
blockdata(unsigned int s = 0, unsigned char * p= nullptr, bool f = false);
};
#endif
dlList.h
#ifndef __DLLIST__
#define __DLLIST__
#include
#include
#include "dlNode.h"
template
class dlList {
public:
dlList(T headerData = T(), T trailerData = T() );
bool empty();
void insertAfter(dlNode
void insertBefore(dlNode
void deleteNext(dlNode
~dlList();
void deletePrevious(dlNode
void deleteNode(dlNode
void print(const char *sep);
dlNode
dlNode
};
template
bool dlList
{
return header->next == trailer;
}
template
dlList
{
while(header->next != trailer) {
dlNode
header->next = hold->next;
delete hold;
}
delete header;
delete trailer;
}
template
dlList
{
header = new (std::nothrow) dlNode
trailer = new(std::nothrow) dlNode
trailer->prev = header;
header->next = trailer;
}
template
void dlList
{
assert(header != nullptr && header->next != nullptr);
if(header->next == trailer) {
std::cout << "Empty list" << std::endl;
return;
}
dlNode
while(cursor->next != trailer) {
std::cout << cursor->info << sep;
cursor = cursor->next;
}
std::cout << cursor->info << std::endl;
}
template
void dlList
{
assert(current != trailer);
current->next = new dlNode
current = current->next;
current->next->prev = current;
}
template
void dlList
{
assert(current != header);
insertAfter(header,trailer,current->prev,newval);
}
template
void dlList
{
assert(current != trailer && current->next != trailer);
dlNode
current->next = hold->next;
if (current->next != nullptr)
current->next->prev = current;
delete hold;
}
template
void dlList
{
assert(current != header && current->prev != header);
dlNode
current->prev = hold->prev;
current->prev->next = current;
delete hold;
}
template
void dlList
{
assert(current != header && current != trailer);
dlNode
current->prev->next = current->next;
current->next->prev = current->prev;
delete hold;
}
#endif
dlNode.h
#ifndef __DLNODE__
#define __DLNODE__
#include
#include
template
class dlNode {
public:
T info;
dlNode
dlNode
dlNode
prev(p), next(n) {}
};
#endif
MemoryManager.h
#ifndef __MM__
#define __MM__
#include
#include
#include "blockdata.h"
#include "dlList.h"
using namespace std;
class MemoryManager
{
public:
MemoryManager(unsigned int memtotal);
unsigned char * malloc(unsigned int request);
void free(unsigned char * ptr2block);
void showBlockList();
private:
dlList
unsigned int memsize;
unsigned char *baseptr;
void mergeForward(dlNode
void mergeBackward(dlNode
void splitBlock(dlNode
};
#endif
testMemMgr.cpp
#include
#include "MemoryManager.h"
using namespace std;
int main()
{
MemoryManager heaper(50);
cout << "After initializing" << endl;
heaper.showBlockList();
unsigned char * p1 = heaper.malloc(10);
cout << "After the first malloc ";
heaper.showBlockList();
unsigned char *p2 = heaper.malloc(20);
cout << "After the second malloc ";
heaper.showBlockList();
cout << "Asking for an un-allocatable block ";
unsigned char *p8 = heaper.malloc(30);
if (p8 == 0)
cout << "Good. The call to malloc returned NULL ";
else
cout << "Uh-oh. Call to malloc did not return NULL as it should have ";
cout << "Free the second block ";
heaper.free(p1);
heaper.showBlockList();
cout << "Allocate a block too big for the initial open block ";
p1 = heaper.malloc(15);
// cout << "malloc done ";
heaper.showBlockList();
cout << "Free the most recently allocated pointer ";
heaper.free(p1);
heaper.showBlockList();
cout << "Free the middle block ";
heaper.free(p2);
heaper.showBlockList();
return 0;
}
CODE TO COMPLETE
MemoryManager.cpp
#include
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started