Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I have completed all the codes except for one, I would love if anyone could help

ID: 3792730 • Letter: I

Question

I have completed all the codes except for one, I would love if anyone could help me out:

here is what I came up with (completed codes)

//MemoryManager.h
#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

-------------------------

//MemoryManager.h
#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

------------------------------------

//testMemMgr.cpp
#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;
}

------------------------------------------------

//blockdata.h
#ifndef _BLOCKDATA_
#define _BLOCKDATA_

#include <iostream>

using namespace std;

class blockdata {
friend ostream& operator << (ostream&, const blockdata &);

public:
blockdata(unsigned int s, bool f, unsigned char *p);
int blocksize;
bool free;
unsigned char *blockptr;  
};


#endif

------------------------------------------

//dlUtils.h
#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)
{
assert(current != trailer && current->next != trailer);
deleteNode(header,trailer, current->next);
}

template <class T>
void deletePrevious(dlNode<T> * header,dlNode<T> * trailer,dlNode<T> *current)
{
   assert(current != header && current->prev != header);
   deleteNode(header, trailer,current->prev);
}

template <class T>
void clearList(dlNode<T> *p)
{
dlNode<T> *hold = p;
while(p != NULL) {
p = p->next;
delete hold;
hold = p;
}
}

#endif

----------------------------------------

//blockdata.cpp
#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;
}

------------------

TO BE COMPLETED

//MemoryManager.cpp
#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

}

Explanation / Answer

Given below are the modified files to answer the question. Output is shown at the end. Please do rate the answer if it helped. Thank you very much.

dlUtils.h

//dlUtils.h
#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 != trailer) {
std::cout << cursor->info << sep;
cursor = cursor->next;
}   
}

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);
}

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)
{
assert(current != trailer && current->next != trailer);
deleteNode(header,trailer, current->next);
}

template <class T>
void deletePrevious(dlNode<T> * header,dlNode<T> * trailer,dlNode<T> *current)
{
assert(current != header && current->prev != header);
deleteNode(header, trailer,current->prev);
}

template <class T>
void clearList(dlNode<T> *p)
{
dlNode<T> *hold = p;
while(p != NULL) {
p = p->next;
delete hold;
hold = p;
}
}

#endif

MemoryManager.cpp

//MemoryManager.cpp
#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,header,nullptr);
header->next = trailer;
insertAfter(trailer, header, originalBlock);
}

MemoryManager::~MemoryManager()
{
delete [] baseptr;
clearList(header);
}

void MemoryManager::showBlockList()
{
printDlList(header,trailer,"->");
cout << endl;
}

//spilts the current free block into 2 blocks- 1st one is of chunksize and the next holds the
//remaining left from original size after taking away chunksize. The next block is marked as
//free as well. Only one new block is created while the original block is now marked with chunksize
void MemoryManager::splitBlock(dlNode<blockdata> *p, unsigned int chunksize)
{
blockdata newblock(p->info.blocksize - chunksize, true, p->info.blockptr + chunksize);
p->info.blocksize = chunksize;
insertAfter(trailer, p, newblock);
  
}

unsigned char * MemoryManager::malloc(unsigned int request)
{
dlNode<blockdata>* current = header->next;
while(current != trailer)
{
//searches the 1st free block enough to satisfy the request
if(current->info.free && current->info.blocksize >= request)
{
splitBlock(current, request);
current->info.free = false;
return current->info.blockptr;
}
current = current->next;
}
return nullptr;
}

//merges this free block with previous block if previous block was free. Deallocates dlnode of this
//block
void MemoryManager::mergeForward(dlNode<blockdata> *p)
{
if(p->next->info.free)
{
p->info.blocksize += p->next->info.blocksize;
deleteNext(header, trailer, p);
}
  
}
//merges this free block with next block if next block is free. Deallocates the dlnode of next
//block
void MemoryManager::mergeBackward(dlNode<blockdata> *p)
{
if(p->prev->info.free)
{
p->prev->info.blocksize += p->info.blocksize;
deleteNode(header, trailer, p);
}
}

void MemoryManager::free(unsigned char *ptr2block)
{
dlNode<blockdata>* current = header->next;
while(current != trailer)
{
//search for the block matching the pointer and make sure it was allocated
if(current->info.blockptr == ptr2block && current->info.free == false)
{
current->info.free = true;
mergeForward(current);
mergeBackward(current);
return;
}
current = current->next;
}
  
}

testMemMgr.cpp

//testMemMgr.cpp
#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:
cout << "malloc b1[5]" << endl;
unsigned char *b1 = heaper.malloc(5);
heaper.showBlockList();
  
  
cout << "malloc b2[3]" << endl;
unsigned char *b2 = heaper.malloc(3);
heaper.showBlockList();
  
cout << "free b1[5]" << endl;
heaper.free(b1);
heaper.showBlockList();
  
cout << " free b2[3]" << endl;
heaper.free(b2);
heaper.showBlockList();

cout << " malloc b1[15]" << endl;
b1 = heaper.malloc(15);
heaper.showBlockList();
  
cout << " malloc b2[20]" << endl;
b2 = heaper.malloc(20);
heaper.showBlockList();
  
cout << " malloc b3[10]" << endl;
unsigned char *b3 = heaper.malloc(10);
heaper.showBlockList();
  
  
cout << " malloc b4[20] ... not enough memory" << endl;
unsigned char *b4 = heaper.malloc(20);
heaper.showBlockList();
assert(b4 == nullptr);

  
  
return 0;
}

output


heap initialized

-------------BlockList start------------------
[50,free]->
-------------BlockList end------------------

malloc b1[5]
[5,allocated]->[45,free]->
malloc b2[3]
[5,allocated]->[3,allocated]->[42,free]->
free b1[5]
[5,free]->[3,allocated]->[42,free]->

free b2[3]
[50,free]->

malloc b1[15]
[15,allocated]->[35,free]->

malloc b2[20]
[15,allocated]->[20,allocated]->[15,free]->

malloc b3[10]
[15,allocated]->[20,allocated]->[10,allocated]->[5,free]->

malloc b4[20] ... not enough memory
[15,allocated]->[20,allocated]->[10,allocated]->[5,free]->

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote