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: 3794493 • Letter: I

Question

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

Please don't modify anything besides what I have bolded down

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

#include <iostream>

#include "MemoryManager.h"

using namespace std;

int main()

{

   MemoryManager heaper(50);

   cout << " heap initialized ";

   cout << " -------------BlockList start------------------ ";

   heaper.showBlockList();

   cout << "-------------BlockList end------------------ ";

   cout << "Doing first malloc: ";

   unsigned char * p1 = heaper.malloc(10);

   cout << "malloc done ";

   cout << " -------------BlockList start------------------ ";

   heaper.showBlockList();

   cout << "-------------BlockList end------------------ ";

   cout << "On to the second malloc ";

   unsigned char *p2 = heaper.malloc(20);

   cout << "malloc done ";

   cout << " -------------BlockList start------------------ ";

   heaper.showBlockList();

   cout << "-------------BlockList end------------------ ";

   cout << "Now let's ask 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 << "Next free the first pointer ";

   heaper.free(p1);

   cout << " -------------BlockList start------------------ ";

   heaper.showBlockList();

   cout << "-------------BlockList end------------------ ";

   cout << "Now do a malloc for a block too big for the initial open block ";

   p1 = heaper.malloc(15);

   cout << "malloc done ";

   cout << " -------------BlockList start------------------ ";

   heaper.showBlockList();

   cout << "-------------BlockList end------------------ ";

   cout << "Next free the most recently allocated pointer ";

   heaper.free(p1);

   cout << "Here is the block list ";

   cout << " -------------BlockList start------------------ ";

   heaper.showBlockList();

   cout << "-------------BlockList end------------------ ";

   cout << "Next free the middle pointer ";

   heaper.free(p2);

   cout << "Here is the block list ";

   cout << "-------------BlockList start------------------ ";

   heaper.showBlockList();

   cout << " -------------BlockList end------------------ ";

   cin.get();

   return 0;

}

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

   unsigned char * malloc(unsigned int request);

   void free(unsigned char * ptr2block);

   void showBlockList();

private:

   unsigned int memsize;

   unsigned char *baseptr;

   dlNode<blockdata>* firstBlock;

   void mergeForward(dlNode<blockdata> *p);

   void mergeBackward(dlNode<blockdata> *p);

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

};

#endif

MemoryManager.cpp

#include <cassert>

#include <iostream>

#include "dlUtils.h"

#include "MemoryManager.h"

#include "blockdata.h"

MemoryManager::MemoryManager(unsigned int memtotal): memsize(memtotal)

{

   baseptr = new unsigned char[memsize];

   blockdata originalBlock(memsize,true,baseptr);

   firstBlock = new dlNode<blockdata>(originalBlock,NULL,NULL);

}

void MemoryManager::showBlockList()

{

printDlList(firstBlock,"->");

}

void MemoryManager::splitBlock(dlNode<blockdata> *p, unsigned int chunksize)

{ // Put your code below

   int newSize = p->info.blocksize - chunksize;

   blockdata C(newSize, true, baseptr+chunksize);

   insertAfter(firstBlock, p, C );

   p->info.free = false;

   p->info.blocksize = chunksize;

     

}

unsigned char * MemoryManager::malloc(unsigned int request)

{ // Put your code below

   dlNode<blockdata> *B = firstBlock;

   while (B!= NULL)

   {

       if (B->info.free == true && B->info.blocksize >= request)

       {

           splitBlock(B, request);

           return B->info.blockptr;

           break;

       }

       B=B->next;

   }

   return NULL;

}

void MemoryManager::mergeForward(dlNode<blockdata> *p)

{ // Put your code below

   int newSize = p->info.blocksize + p->next->info.blocksize;

   p->info.blocksize = newSize;

   dlNode<blockdata> *temp = p->next;

   if (temp->next == NULL)

       p->next = NULL;

   else

   {

       p->next = temp->next;

       p->next->prev = p;

     

   }

       delete temp;

}

void MemoryManager::mergeBackward(dlNode<blockdata> *p)

{ // Put your code below

   int newSize = p->prev->info.blocksize + p->info.blocksize;

   p->info.blocksize = newSize;

p->info.blockptr = p->prev->info.blockptr;

   dlNode<blockdata> *temp = p->prev;

   if (temp->prev == NULL)

   {

       p->prev = NULL;

   firstBlock=p;

   }

   else

   {

       p->prev = temp->prev;

       p->prev->next = p;

   }

   delete temp;

}

void MemoryManager::free(unsigned char *ptr2block)

{ // Put your code below

   dlNode<blockdata> *curr=firstBlock;

   assert(curr != NULL);

   while (curr && curr->info.blockptr != ptr2block)

   {

     

       curr = curr->next;

   }

   assert(curr);

   if (curr->info.blockptr == ptr2block)

   {

       curr->info.free = true;

       if (curr->prev != NULL)

       {

           if (curr->prev->info.free == true && curr->info.free == true)

           {

               mergeBackward(curr);

           }

       }

       if (curr->next != NULL)

       {

           if (curr->info.free == true && curr->next->info.free == true)

           {

               mergeForward(curr);

           }

       };

   }

   else

       return;

         

       //}

}

blockdata.cpp

#include "dlUtils.h"

#include "blockdata.h"

#include <iostream>

using namespace std;

blockdata::blockdata(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;

}

blockdata.h

#ifndef _BLOCKDATA_

#define _BLOCKDATA_

#include <iostream>

using namespace std;

class blockdata {

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

public:

blockdata(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>

struct dlNode {

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>* first,const char *sep)

{

dlNode<T> *cursor = first;

while(cursor != NULL && cursor->next!= NULL) {

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

    cursor = cursor->next;

}

if (cursor != NULL)

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

}

template <class T>

void insertAsFirst(dlNode<T>* &first, T newval)

{

first = new dlNode<T>(newval,NULL,first);

first->next->prev = first;

}

template <class T>

void insertAfter(dlNode<T> *first, dlNode<T> *current, T newval)

{

assert(current != NULL);

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

current = current->next;

if (current->next != NULL)

    current->next->prev = current;

}

template <class T>

void insertBefore(dlNode<T>* &first, dlNode<T> *current, T newval)

{

assert(current != NULL);

if(current == first)

    insertAsFirst(first,newval);

else

      insertAfter(first,current->prev,newval);

}

template <class T>

void deleteNext(dlNode<T> *current)

{

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

dlNode<T> *hold = current->next;

current->next = hold->next;

if (current->next != NULL)

    current->next->prev = current;

delete hold;

}

template <class T>

void deletePrevious(dlNode<T> * &first,dlNode<T> *current)

{

assert(first != NULL && current != NULL && current->prev != NULL);

dlNode<T> *hold = current->prev;

current->prev = hold->prev;

if (current->prev != NULL)

     current->prev->next = current;

else

     first = current;

delete hold;

}

template <class T>

void deleteNode(dlNode<T>* &first, dlNode<T>* current)

{

assert(first != NULL && current != NULL);

dlNode<T> *hold = current;

if (current == first) {

    first = first->next;

    first->prev = NULL;

    current = first;

} else {

    current->prev->next = current->next;

    current->next->prev = current->prev;

    current = current->prev;

}

delete hold;

}

#endif

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