I have completed all the codes except for one I would love i
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;
}
------------------
makefile.txt
main: MemoryManager.o blockdata.o testMemMgr.o
g++ -std=c++11 blockdata.o MemoryManager.o testMemMgr.o -o main
testMemMgr.o: testMemMgr.cpp
g++ -std=c++11 -c testMemMgr.cpp
blockdata.o: blockdata.h blockdata.cpp dlUtils.h
g++ -std=c++11 -c blockdata.cpp
MemoryManager.o: MemoryManager.cpp MemoryManager.h
g++ -std=c++11 -c MemoryManager.cpp
clean:
rm *.o
----------------------------
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
}
Solution
void MemoryManager::free(unsigned char *ptr2block)
{
splitBlock(dlNode<blockdata> *ptr2block, unsigned int chunksize);
for(int i =0;i<chunksize ; i++)
{
current = ptr2block;
deleteNode(dlNode<T>* header, dlNode<T>* trailer, dlNode<T>* current);
}
unsigned char * MemoryManager::malloc(unsigned int request)
{
char* pointer_to_memory = new char[request];
return pointer_to_memory;
}





