Question

The goal is to simulate the C heap manager in C++ use -std=c++11 to compile ONLY...

The goal is to simulate the C heap manager in C++ use -std=c++11 to compile

ONLY EDIT THE MemoryManager.cpp file in the sections marked in bold please and thank you

• A runtime module used to allocate and de-allocate dynamic memory

. • The "heap" is a large "pool" of memory set aside by the runtime system

• The two main functions are

– malloc, used to satisfy a request for a specific number of consecutive blocks;

– free, used to make allocated blocks available f

//blockdata.cpp do not edit this file

#include "dlUtils.h"
#include "blockdata.h"
#include
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;
}

//blockdata.h do not edit this file

#ifndef _BLOCKDATA_

#define _BLOCKDATA_

#include

using namespace std;

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

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


#endif

//dlUtils.h do not edit this file

#ifndef __DLNODE__

#define __DLNODE__

#include
#include

template
class dlNode {
public:
T info;
dlNode *prev;
dlNode *next;
dlNode(T val, dlNode *p, dlNode *n):info(val),prev(p),next(n){};
};

template
void printDlList(dlNode* header,dlNode *trailer,const char *sep)
{
assert(header != NULL && trailer != NULL);
dlNode *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
void insertAfter(dlNode *trailer, dlNode *current, T newval)
{
assert(current != trailer);
current->next = new dlNode(newval,current,current->next);
current = current->next;
current->next->prev = current;
}

template
void deleteNode(dlNode* header, dlNode* trailer, dlNode* current)
{
assert(current!= header && current != trailer);
dlNode *hold = current;
current->prev->next = current->next;
current->next->prev = current->prev;
delete hold;
}

template
void deleteNext(dlNode* header, dlNode* trailer, dlNode *current)
{
assert(current != trailer && current->next != trailer);
deleteNode(header,trailer, current->next);
}

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

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

#endif

// MemoryManager.h do not edit this file

#ifndef __MM__

#define __MM__

#include
#include
#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* header;
dlNode* trailer;

void mergeForward(dlNode *p);
void mergeBackward(dlNode *p);
void splitBlock(dlNode *p,unsigned int chunksize);
};
  
   #endif

//MemoryManager.cpp must complete the commented in sections

#include

#include

#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(dummyBlock,nullptr,nullptr);
trailer = new dlNode(dummyBlock,nullptr,nullptr);
header->next = new dlNode(originalBlock,header,trailer);
trailer->prev = header->next;
}

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

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

void MemoryManager::splitBlock(dlNode *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 *p)
{
// Complete the code for this method

}

void MemoryManager::mergeBackward(dlNode *p)
{
// Complete the code for this method

}

void MemoryManager::free(unsigned char *ptr2block)
{
// Complete the code for this method

}

//testMemMgr.cpp

#include
#include "MemoryManager.h"
using namespace std;

int main()
{
MemoryManager heaper(50);
cout << "\nheap initialized\n";
cout << "\n-------------BlockList start------------------\n";
heaper.showBlockList();
cout << "-------------BlockList end------------------\n\n";
cout << "Doing first malloc:\n";
unsigned char * p1 = heaper.malloc(10);

cout << "malloc done\n";
cout << "\n-------------BlockList start------------------\n";
heaper.showBlockList();
cout << "-------------BlockList end------------------\n\n";

cout << "On to the second malloc\n";
unsigned char *p2 = heaper.malloc(20);
cout << "malloc done\n";
cout << "\n-------------BlockList start------------------\n";
heaper.showBlockList();
cout << "-------------BlockList end------------------\n\n";

cout << "Now let's ask for an un-allocatable block\n";
unsigned char *p8 = heaper.malloc(30);
if (p8 == 0)
cout << "Good. The call to malloc returned NULL\n";
else
cout << "Uh-oh. Call to malloc did not return NULL as it should have\n";

cout << "Next free the first pointer\n";
heaper.free(p1);
cout << "\n-------------BlockList start------------------\n";
heaper.showBlockList();
cout << "-------------BlockList end------------------\n\n";

cout << "Now do a malloc for a block too big for the initial open block\n";
p1 = heaper.malloc(15);
cout << "malloc done\n";
cout << "\n-------------BlockList start------------------\n";
heaper.showBlockList();
cout << "-------------BlockList end------------------\n\n";

cout << "Next free the most recently allocated pointer\n";
heaper.free(p1);
cout << "Here is the block list\n";
cout << "\n-------------BlockList start------------------\n";
heaper.showBlockList();
cout << "-------------BlockList end------------------\n\n";

cout << "Next free the middle pointer\n";
heaper.free(p2);
cout << "Here is the block list\n";
cout << "-------------BlockList start------------------\n";
heaper.showBlockList();
cout << "\n-------------BlockList end------------------\n\n";

return 0;
}

// Makefile

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

0 0
Add a comment Improve this question Transcribed image text
Answer #1

etse escntinue es memPoo

Add a comment
Know the answer?
Add Answer to:
The goal is to simulate the C heap manager in C++ use -std=c++11 to compile ONLY...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • For the LinkedList class, create a getter and setter for the private member 'name', constructing your...

    For the LinkedList class, create a getter and setter for the private member 'name', constructing your definitions based upon the following declarations respectively: std::string get_name() const; and void set_name(std::string); In the Main.cpp file, let's test your getter and setter for the LinkedLIst private member 'name'. In the main function, add the following lines of code: cout << ll.get_name() << endl; ll.make_test_list(); ll.set_name("My List"); cout << ll.get_name() << endl; Output should be: Test List My List Compile and run your code;...

  • In C++, for the provided template linked list class create a derived class of it which...

    In C++, for the provided template linked list class create a derived class of it which adds the functionality to it to find the high and low value of any given data stored in the list. The derived class must be a template. LinkedList.h #pragma once #include <iostream> using namespace std; template <class T> class ListNode {    public:        T data;        ListNode<T>* next;        ListNode(T data)        {            this->data = data;...

  • Please rewrite this function using recursive function #include using namespace std; struct Node { char ch;...

    Please rewrite this function using recursive function #include using namespace std; struct Node { char ch; Node* next; }; class LinkedList { Node* head; public: LinkedList(); ~LinkedList(); void add(char ch); bool find(char ch); bool del(char ch); friend std::ostream& operator<<(std::ostream& out, LinkedList& list); }; LinkedList::LinkedList() { head = NULL; } LinkedList::~LinkedList() { Node* cur = head, * tmp; while (cur != NULL) { tmp = cur->next; delete cur; cur = tmp; } } void LinkedList::add(char ch) { Node* cur = head,...

  • Can someone explain me AddAtHead,AddAtTail And AddInOrder functions #include <iostream> #include <fstream> #include <ctime> #include <cstdlib>...

    Can someone explain me AddAtHead,AddAtTail And AddInOrder functions #include <iostream> #include <fstream> #include <ctime> #include <cstdlib> using namespace std; template <class T> class DNode{ public: T data; DNode *next,*prev; DNode(){ next=prev=NULL;} DNode(T d, DNode *n=NULL, DNode *p=NULL){ data=d; next=n; prev=p;} }; template <class T> ostream& operator<<(ostream &out, const DNode<T> &n){ out<<n.data<<' '; return out; } template <class T> class HCDLL{ DNode<T> *head; public: HCDLL(){ head=new DNode<T>; head->next=head->prev=head;} void addAtHead(T d){ head->next=head->next->prev=new DNode<T>(d, head->next, head); } void addAtTail(T d){ head->prev=head->prev->next=new DNode<T>(d,...

  • // thanks for helping // C++ homework // The homework is to complete below in the...

    // thanks for helping // C++ homework // The homework is to complete below in the stack.h : // 1. the copy constructor // 2. the assignment operator // 3. the destructor // 4. Write a test program (mytest.cpp) to test copy and assignment // 5. Verify destructor by running the test program in Valgrind // This is the main.cpp #include <iostream> #include "stack.h" using namespace std; int main() { Stack<int> intStack; cout << "\nPush integers on stack and dump...

  • #include "name.h" #include "contact.h" using namespace std; class ContactList; typedef class Node* NodePtr; class Node {...

    #include "name.h" #include "contact.h" using namespace std; class ContactList; typedef class Node* NodePtr; class Node {     Contact item;     NodePtr next;     friend class ContactList; }; class ContactList { public:     ContactList(char* clfile) ;     ~ContactList();     void display       (ostream & output) const;     int   insert        (Contact record_to_insert);     int   insert        (ContactList contact_list);     int   remove        (Contact record_to_delete);     int   size          () const;     int   save          () const;     void find_by_lname (ostream & output, string lname) const;     void...

  • I need help with understanding dummy nodes in doubly-linked lists. Here is the code that I...

    I need help with understanding dummy nodes in doubly-linked lists. Here is the code that I have right now. ************city.h**************** #ifndef city_h #define city_h #include <string> using namespace std; class City{ public: City () { name = "N/A"; population = 0; } City (string nm, unsigned int pop){ name = nm; population = pop; } void setName (string name) { this -> name = name; } void setPopulation (unsigned int population){ this -> population = population; } string getName() const...

  • The goal is to reinforce the implementation of container class concepts in C++. Specifically, the goal...

    The goal is to reinforce the implementation of container class concepts in C++. Specifically, the goal is to create a static implementation of a set. Add the efficiency of each function to the documentation in the header file. Your program must compile. Use test_set.cpp as your test program. Set.h & Test_Set.cpp is code that is already given. All I need is for you add comments to Set.cpp describing each function. FILE: SET.H #ifndef _SET_H #define _SET_H #include <cstdlib> #include <iostream>...

  • This is for my c++ class and im stuck. Complete the Multiplex.cpp file with definitions for a fun...

    This is for my c++ class and im stuck. Complete the Multiplex.cpp file with definitions for a function and three overloaded operators. You don't need to change anything in the Multiplex.h file or the main.cpp, though if you want to change the names of the movies or concession stands set up in main, that's fine. Project Description: A Multiplex is a complex with multiple movie theater screens and a variety of concession stands. It includes two vectors: screenings holds pointers...

  • there show an error in sample.cpp file that more than one instance of overloaded function find...

    there show an error in sample.cpp file that more than one instance of overloaded function find matches the argument list. can you please fix it. and rewrite the program and debug. thanks. I also wrote error below on which line in sample.cpp. it shows on find. #include #include #include "node1.cpp" using namespace main_savitch_5; // node1.h #ifndef MAIN_SAVITCH_NODE1_H #define MAIN_SAVITCH_NODE1_H #include <string> namespace main_savitch_5 {    template<class item>    class node    {    public:        typedef item value_type;   ...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT