Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 *current, T newval);

void insertBefore(dlNode *current, T newval);

void deleteNext(dlNode *q);

~dlList();

void deletePrevious(dlNode *q);

void deleteNode(dlNode *q);

void print(const char *sep);

dlNode *header;

dlNode *trailer;

};

template

bool dlList::empty()

{

return header->next == trailer;

}

template

dlList::~dlList()

{

while(header->next != trailer) {

dlNode *hold = header->next;

header->next = hold->next;

delete hold;

}

delete header;

delete trailer;

}

template

dlList:: dlList(T headerData, T trailerData)

{

header = new (std::nothrow) dlNode(headerData);

trailer = new(std::nothrow) dlNode(trailerData);

trailer->prev = header;

header->next = trailer;

}

template

void dlList::print(const char *sep)

{

assert(header != nullptr && header->next != nullptr);

if(header->next == trailer) {

std::cout << "Empty list" << std::endl;

return;

}

dlNode *cursor = header->next;

while(cursor->next != trailer) {

std::cout << cursor->info << sep;

cursor = cursor->next;

}

std::cout << cursor->info << std::endl;

}

template

void dlList::insertAfter(dlNode *current, T newval)

{

assert(current != trailer);

current->next = new dlNode(newval,current,current->next);

current = current->next;

current->next->prev = current;

}

template

void dlList::insertBefore(dlNode *current, T newval)

{

assert(current != header);

insertAfter(header,trailer,current->prev,newval);

}

template

void dlList::deleteNext(dlNode *current)

{

assert(current != trailer && current->next != trailer);

dlNode *hold = current->next;

current->next = hold->next;

if (current->next != nullptr)

current->next->prev = current;

delete hold;

}

template

void dlList::deletePrevious(dlNode *current)

{

assert(current != header && current->prev != header);

dlNode *hold = current->prev;

current->prev = hold->prev;

current->prev->next = current;

delete hold;

}

template

void dlList::deleteNode(dlNode* current)

{

assert(current != header && current != trailer);

dlNode *hold = current;

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 *prev;

dlNode *next;

dlNode(T val, dlNode *p = nullptr, dlNode *n = nullptr) : info(val),

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 blocklist;

unsigned int memsize;

unsigned char *baseptr;

void mergeForward(dlNode *p);

void mergeBackward(dlNode *p);

void splitBlock(dlNode *p,unsigned int chunksize);

};

#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 #include #include "MemoryManager.h" MemoryManager::MemoryManager(unsigned int memtotal) { memsize = memtotal; baseptr = new unsigned char[memsize]; blockdata originalBlock(memsize,baseptr,true); blocklist.insertAfter(blocklist.header,originalBlock); assert(!blocklist.empty()); } void MemoryManager::showBlockList() { // cout << " -------------BlockList start------------------ "; blocklist.print("->"); cout << endl; // cout << " -------------BlockList end------------------ "; } void MemoryManager::splitBlock(dlNode *p, unsigned int chunksize) { // Fill in the missing code and delete this comment } unsigned char * MemoryManager::malloc(unsigned int request) { // Fill in the missing code and delete this comment } void MemoryManager::mergeForward(dlNode *p) { // Fill in the missing code and delete this comment } void MemoryManager::mergeBackward(dlNode *p) { // Fill in the missing code and delete this comment } void MemoryManager::free(unsigned char *ptr2block) { // Fill in the missing code and delete this comment }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Beginning Databases With PostgreSQL From Novice To Professional

Authors: Richard Stones, Neil Matthew

2nd Edition

1590594789, 978-1590594780

More Books

Students also viewed these Databases questions

Question

How wide are Salary Structure Ranges?

Answered: 1 week ago