Question: Memory Manager for C++ // What I need help with is creating the functions in the MemoryManager.cpp file at the bottom Background: Computer languages often
Memory Manager for C++
// What I need help with is creating the functions in the MemoryManager.cpp file at the bottom
Background:
Computer languages often require a way to dynamically allocate memory. This means that memory is acquired while the program is running. This is in contrast to static memory allocation such as declaring a variable, an instance of an object or an array of some data type.
For example, these statements in C++ require no dynamic memory allocation:
int x; // creates one integer storage space at compile time
char array[20]; // creates 20 char storage spaces at compile time
Compare the above to these statements which create memory locations dynamically after the program starts running
Int *dptr = new int[3]; // creates 3 integers, returned an integer pointer back to dptr
Card *myCard = new Card(A, H); // creates a Card the Ace of Hearts, returns a Card pointer to myCard.
Notice that the operator new is smart enough to return an integer pointer if an integer or group of integers is requested and return a Card pointer if a Card object is requested.
Your assignment is to create a Memory Manager which will allow the user to allocate and release memory. Your program also needs to keep track of how much memory is available, how much has been used, where the next available memory area is, etc.
Solution Requirements
---------------------
Your solution should be capable of managing a random number of allocations and deallocations,
All allocations will come from the free memory. You will start with 64K free memory. Each time an allocation is requested, the free memory will decrease. When there is no more free memory available, your solution will call the onOutOfMemory() function.
You should keep track of the InUse memory so you dont accidentally re-allocate it. The InUse memory is defined as the memory that is in use it has been allocated but not yet deallocated. When it is deallocated, it is no longer InUse.
When your solution deallocates memory, it is removed from the InUse memory. You need to keep track of the deallocated memory.
You are not required to re-allocate deallocated memory. Even though a real memory manager would be expected to do this, it adds complexity to the solution and is not required for this assignment. However you are welcome to try it if you are up to the challenge.
You should provide implementations of the functions within MemoryManager.cpp.
!! DO NOT MODIFY MemoryManager.h !!
You can define additional functions within MemoryManager.cpp as necessary, but do not change the MemoryManager interface.
Memory Restrictions
-------------------
- No memory dynamically allocated during program execution (new, malloc, etc.).
- ALL data must fit inside MM_pool[ ]
- No other static or global variables
Error Handling
--------------
If you are unable to satisfy a request due to lack of memory your code should call the provided failure function (which will not return):
namespace MemoryManager
{
void onOutOfMemory();
}
HexDump
-------------
Many operating systems provide a utility called hexdump (or some other name with the same functionality.) A hexdump program allows you to examine the memory contents. I am providing a similar program called memView which allows you to see the contents of memory. This is a good debugging tool.
Memory is nothing more than ones and zeroes. The meaning of each depends on context. memView will show the following:
Column 1: the hexadecimal value of the actual memory address (in other words, the start of MM_pool
Column 2: the MM_pool index (from 0 to 64K - 1 which is 65,535)
Column 3: the bit sequence for that index (8 bits for 1 byte)
Column 4: the character value of that index (using only one byte)
Column 5: the ASCII # for that character
Column 6: the unsigned short value (using 2 bytes)
Column 7: the unsigned int value (using 4 bytes)
Compilation and Sample Output
-----------------------------
Please compile your implementation of MemoryManger.cpp against the provided MemoryManager.h and main.cpp.
This is only a test file. The actual file will be more complex but will only use the following functions initializeMemoryManager() // set up the MM_pool with fresh memory
allocate() // allocate some memory, return a pointer
deallocate() // deallocate some memory
freeMemory() // return the number of bytes (chars) that is still free (un-used)
usedMemory() // return the number of bytes (chars) that used to be InUse, but have since been deallocated
inUseMemory() // return the number of bytes (chars) that are currently inUse
size() // return the size of the variable at this memory location. Similar to sizeof( ) in C++;
**********************************************************************************MemoryManager.h********************************************************************
#ifndef __MEM_MANAGER_H__
#define __MEM_MANAGER_H__
namespace MemoryManager {
void memView(int start, int finish); // Show the contents of memory from start to finish
void initializeMemoryManager(void); // Initialize any data needed to manage the memory pool
void deallocate(void *); // Free up a chunk previously allocated
int freeMemory(void); // Scan memory pool, return the total free space remaining
int usedMemory(void); // Scan memory pool, return the total used memory that has been deallocated
int inUseMemory(void); // Scan the memory pool and return the total memory being currently used
void onOutOfMemory(void); // Call if no space is left for the allocation request
int size(void *); // Return the size (in bytes) of the variable that lives at this address
};
#endif // __MEM_MANAGER_H__
**************************************************************************************Tester.cpp************************************************************************************
/* This is the driver for your memory manager. Do not make any changes to this file
Compile it with your MemoryManager.h and MemoryManager.cpp files.
The output that I got is at the bottom. Your ourput should be similar.
Notice that Total Memory = Free Memory + InUse Memory + Used Memory
Include this file when you submit your program
*/
#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS // allow Microsoft Visual C++ to user strcpy insted of strcpy_s
#endif
#include
#include
#include
#include
#include
#include "MemoryManager.h"
using namespace std;
using namespace MemoryManager; // allow functions in Memory Manager to be used without prefix
void memStats();
int main(void)
{
initializeMemoryManager();
int alloc = 0;
int dealloc = 0;
int start = freeMemory();
cout << "Program Starting...";
memStats();
cout << " Allocate some memory";
short* shortPtr = (short*)allocate(sizeof(short));
alloc++;
char* charPtr = (char*)allocate(sizeof(char));
alloc++;
int* intPtr = (int*)allocate(sizeof(int));
alloc++;
char* name = (char*)allocate(11);
alloc++;
memStats();
*charPtr = 'A';
*shortPtr = 30000;
*intPtr = 2000000000;
strcpy(name, "Steve Gold");
cout << endl;
cout << "charPtr Value :" << setw(11) << *charPtr << " address:" << (long*)charPtr << endl;
cout << "shortPtr Value:" << setw(11) << *shortPtr << " address:" << (long*)shortPtr << endl;
cout << "intPtr Value :" << setw(11) << *intPtr << " address:" << (long*)intPtr << endl;
cout << "name Value :" << setw(11) << name << " address:" << (long*)name << endl;
cout << " Deallocating intPtr:";
deallocate(intPtr);
dealloc++;
memStats();
cout << " Deallocating charPtr:";
deallocate(charPtr);
dealloc++;
memStats();
// std::cout << "Deallacting shortPtr:";
// deallocate(shortPtr);
std::cout << " Deallocating name:";
deallocate(name);
dealloc++;
memStats();
cout << " Free memory at start = " << start << endl;
cout << " Free memory now = " << freeMemory() << endl;
cout << " Total Memory used = " << start - freeMemory() << endl;
memStats();
cout << endl;
// this next section will print a '+' when it allocates and '-' when it deallocates
// It will test how much memory s left and stop before it runs out.
int i = 0;
while (freeMemory() > 100) // this loop has a very bad memory leak...
{
int *ptr = (int*)allocate(4);
alloc++;
cout << '+';
i++;
if (!(i % 75)) cout << endl;
if ((i % 37) == 0) // randomly deallocate
{
deallocate(ptr);
dealloc++;
cout << '-';
i++;
if (!(i % 75)) cout << endl;
}
}
cout << " Free memory at start = " << start << endl;
cout << " Free memory now = " << freeMemory() << endl;
cout << " Total Allocations made: " << alloc;
cout << " Total deallocations made: " << dealloc << endl;
memStats();
}
void memStats()
{
int total = freeMemory() + inUseMemory() + usedMemory();
cout << " #######################################";
cout << " #Total:" << total << " Free:" << freeMemory() << " InUse:" << inUseMemory() << " Used:" << usedMemory();
cout << " #Press any key to continue...";
cout << " #######################################";
cin.get();
}
// output from this program:
/*
Program Starting...
#######################################
#Total:65530 Free:65530 InUse:0 Used:0
#Press any key to continue...
#######################################
Allocate some memory
#######################################
#Total:65530 Free:65488 InUse:42 Used:0
#Press any key to continue...
#######################################
charPtr Value : A address:00EC5444
shortPtr Value: 30000 address:00EC543C
intPtr Value : 2000000000 address:00EC544B
name Value : Steve Gold address:00EC5455
Deallocating intPtr:
#######################################
#Total:65530 Free:65488 InUse:32 Used:10
#Press any key to continue...
#######################################
Deallocating charPtr:
#######################################
#Total:65530 Free:65488 InUse:25 Used:17
#Press any key to continue...
#######################################
Deallocating name:
#######################################
#Total:65530 Free:65488 InUse:8 Used:34
#Press any key to continue...
#######################################
Free memory at start = 65530
Free memory now = 65488
Total Memory used = 42
#######################################
#Total:65530 Free:65488 InUse:8 Used:34
#Press any key to continue...
#######################################
+++++++++++++++++++++++++++++++++++++-++++++++++++++++++++++++++++++++++++-
++++++++++++++++++++++++++++++++++++-++++++++++++++++++++++++++++++++++++-+
+++++++++++++++++++++++++++++++++++-++++++++++++++++++++++++++++++++++++-++
++++++++++++++++++++++++++++++++++-++++++++++++++++++++++++++++++++++++-+++
+++++++++++++++++++++++++++++++++-++++++++++++++++++++++++++++++++++++-++++
++++++++++++++++++++++++++++++++-++++++++++++++++++++++++++++++++++++-+++++
+++++++++++++++++++++++++++++++-++++++++++++++++++++++++++++++++++++-++++++
++++++++++++++++++++++++++++++-++++++++++++++++++++++++++++++++++++-+++++++
+++++++++++++++++++++++++++++-++++++++++++++++++++++++++++++++++++-++++++++
++++++++++++++++++++++++++++-++++++++++++++++++++++++++++++++++++-+++++++++
+++++++++++++++++++++++++++-++++++++++++++++++++++++++++++++++++-++++++++++
++++++++++++++++++++++++++-++++++++++++++++++++++++++++++++++++-+++++++++++
+++++++++++++++++++++++++-++++++++++++++++++++++++++++++++++++-++++++++++++
++++++++++++++++++++++++-++++++++++++++++++++++++++++++++++++-+++++++++++++
+++++++++++++++++++++++-++++++++++++++++++++++++++++++++++++-++++++++++++++
++++++++++++++++++++++-++++++++++++++++++++++++++++++++++++-+++++++++++++++
+++++++++++++++++++++-++++++++++++++++++++++++++++++++++++-++++++++++++++++
++++++++++++++++++++-++++++++++++++++++++++++++++++++++++-+++++++++++++++++
+++++++++++++++++++-++++++++++++++++++++++++++++++++++++-++++++++++++++++++
++++++++++++++++++-++++++++++++++++++++++++++++++++++++-+++++++++++++++++++
+++++++++++++++++-++++++++++++++++++++++++++++++++++++-++++++++++++++++++++
++++++++++++++++-++++++++++++++++++++++++++++++++++++-+++++++++++++++++++++
+++++++++++++++-++++++++++++++++++++++++++++++++++++-++++++++++++++++++++++
++++++++++++++-++++++++++++++++++++++++++++++++++++-+++++++++++++++++++++++
+++++++++++++-++++++++++++++++++++++++++++++++++++-++++++++++++++++++++++++
++++++++++++-++++++++++++++++++++++++++++++++++++-+++++++++++++++++++++++++
+++++++++++-++++++++++++++++++++++++++++++++++++-++++++++++++++++++++++++++
++++++++++-++++++++++++++++++++++++++++++++++++-+++++++++++++++++++++++++++
+++++++++-++++++++++++++++++++++++++++++++++++-++++++++++++++++++++++++++++
++++++++-++++++++++++++++++++++++++++++++++++-+++++++++++++++++++++++++++++
+++++++-++++++++++++++++++++++++++++++++++++-++++++++++++++++++++++++++++++
++++++-++++++++++++++++++++++++++++++++++++-+++++++++++++++++++++++++++++++
+++++-++++++++++++++++++++++++++++++++++++-++++++++++++++++++++++++++++++++
++++-++++++++++++++++++++++++++++++++++++-+++++++++++++++++++++++++++++++++
+++-++++++++++++++++++++++++++++++++++++-++++++++++++++++++++++++++++++++++
++-++++++++++++++++++++++++++++++++++++-+++++++++++++++++++++++++++++++++++
+-++++++++++++++++++++++++++++++++++++-++++++++++++++++++++++++++++++++++++
-++++++++++++++++++++++++++++++++++++-++++++++++++++++++++++++++++++++++++-
++++++++++++++++++++++++++++++++++++-++++++++++++++++++++++++++++++++++++-+
+++++++++++++++++++++++++++++++++++-++++++++++++++++++++++++++++++++++++-++
++++++++++++++++++++++++++++++++++-++++++++++++++++++++++++++++++++++++-+++
+++++++++++++++++++++++++++++++++-++++++++++++++++++++++++++++++++++++-++++
++++++++++++++++++++++++++++++++-++++++++++++++++++++++++++++++++++++-+++++
+++++++++++++++++++++++++++++++-++++++++++++++++++++++++++++++++++++-++++++
++++++++++++++++++++++++++++++-++++++++++++++++++++++++++++++++++++-+++++++
+++++++++++++++++++++++++++++-++++++++++++++++++++++++++++++++++++-++++++++
++++++++++++++++++++++++++++-++++++++++++++++++++++++++++++++++++-+++++++++
+++++++++++++++++++++++++++-++++++++++++++++++++++++++++++++++++-++++++++++
++++++++++++++++++++++++++-++++++++++++++++++++++++++++++++++++-+++++++++++
+++++++++++++++++++++++++-++++++++++++++++++++++++++++++++++++-++++++++++++
++++++++++++++++++++++++-++++++++++++++++++++++++++++++++++++-+++++++++++++
+++++++++++++++++++++++-++++++++++++++++++++++++++++++++++++-++++++++++++++
++++++++++++++++++++++-++++++++++++++++++++++++++++++++++++-+++++++++++++++
+++++++++++++++++++++-++++++++++++++++++++++++++++++++++++-++++++++++++++++
++++++++++++++++++++-++++++++++++++++++++++++++++++++++++-+++++++++++++++++
+++++++++++++++++++-++++++++++++++++++++++++++++++++++++-++++++++++++++++++
++++++++++++++++++-++++++++++++++++++++++++++++++++++++-+++++++++++++++++++
+++++++++++++++++-++++++++++++++++++++++++++++++++++++-++++++++++++++++++++
++++++++++++++++-++++++++++++++++++++++++++++++++++++-+++++++++++++++++++++
+++++++++++++++-++++++++++++++++++++++++++++++++++++-++++++++++++++++++++++
++++++++++++++-++++++++++++++++++++++++++++++++++++-+++++++++++++++++++++++
+++++++++++++-++++++++++++++++++++++++++++++++++++-++++++++++++++++++++++++
++++++++++++-++++++++++++++++++++++++++++++++++++-+++++++++++++++++++++++++
+++++++++++-++++++++++++++++++++++++++++++++++++-++++++++++++++++++++++++++
++++++++++-++++++++++++++++++++++++++++++++++++-+++++++++++++++++++++++++++
+++++++++-++++++++++++++++++++++++++++++++++++-++++++++++++++++++++++++++++
++++++++-++++++++++++++++++++++++++++++++++++-+++++++++++++++++++++++++++++
+++++++-++++++++++++++++++++++++++++++++++++-++++++++++++++++++++++++++++++
++++++-++++++++++++++++++++++++++++++++++++-+++++++++++++++++++++++++++++++
+++++-++++++++++++++++++++++++++++++++++++-++++++++++++++++++++++++++++++++
++++-++++++++++++++++++++++++++++++++++++-+++++++++++++++++++++++++++++++++
+++-++++++++++++++++++++++++++++++++++++-++++++++++++++++++++++++++++++++++
++-++++++++++++++++++++++++++++++++++++-+++++++++++++++++++++++++++++++++++
+-++++++++++++++++++++++++++++++++++++-++++++++++++++++++++++++++++++++++++
-++++++++++++++++++++++++++++++++++++-++++++++++++++++++++++++++++++++++++-
++++++++++++++++++++++++++++++++++++-++++++++++++++++++++++++++++++++++++-+
+++++++++++++++++++++++++++++++++++-++++++++++++++++++++++++++++++++++++-++
++++++++++++++++++++++++++++++++++-++++++++++++++++++++++++++++++++++++-+++
+++++++++++++++++++++++++++++++++-++++++++++++++++++++++++++++++++++++-++++
++++++++++++++++++++++++++++++++-++++++++++++++++++++++++++++++++++++-+++++
+++++++++++++++++++++++++++++++-++++++++++++++++++++++++++++++++++++-++++++
++++++++++++++++++++++++++++++-++++++++++++++++++++++++++++++++++++-+++++++
+++++++++++++++++++++++++++++-++++++++++++++++++++++++++++++++++++-++++++++
++++++++++++++++++++++++++++-++++++++++++++++++++++++++++++++++++-+++++++++
+++++++++++++++++++++++++++-++++++++++++++++++++++++++++++++++++-++++++++++
++++++++++++++++++++++++++-++++++++++++++++++++++++++++++++++++-+++++++++++
+++++++++++++++++++++++++-++++++++++++++++++++++++++++++++++++-++++++++++++
++++++++++++++++++++++++-++++++++++++++++++++++++++++++++++++-+++++++++++++
+++++++++++++++++++++++-++++++++++++++++++++++++++++++++++++-++++++++++++++
++++++++++++++++++++++-++++++++++++++++++++++
Free memory at start = 65530
Free memory now = 98
Total Allocations made: 6543
Total deallocations made: 184
#######################################
#Total:65530 Free:98 InUse:63588 Used:1844
#Press any key to continue...
#######################################
*/
***************************************************************************************MemoryManager.cpp*******************************************************************************************
#include "MemoryManager.h"
#include
#include
using namespace std;
namespace MemoryManager {
// IMPORTANT
//
// This is the only static memory that you may use, no other global variables
// may be created, if you need to save data make it fit in MM_pool
const int MM_POOL_SIZE = 65536;
char MM_pool[MM_POOL_SIZE];
// I have provided this tool for your use
void memView(int start, int end) {
const unsigned int SHIFT = 8;
const unsigned int MASK = 1 << SHIFT - 1;
unsigned int value; // used to facilitate bit shifting and masking
cout << " Pool Unsigned Unsigned " << endl;
cout << "Mem Add indx bits chr ASCII# short int " << endl;
cout << "-------- ---- -------- --- ------ -------- ------------" << endl;
for (int i = start; i <= end; i++) {
cout << (long*)(MM_pool + i) << ':'; // the actual address in hexadecimal
cout << '[' << setw(2) << i << ']'; // the index int MM_pool
value = MM_pool[i];
cout << " ";
for (int j = 1; j <= SHIFT; j++) { // the bit sequence for this byte (8 bits)
cout << ((value & MASK) ? '1' : '0');
value <<= 1;
}
cout << " ";
cout << "|" << *(char*)(MM_pool + i) << "| ("; // the ASCII character of the 8 bits (1 byte
cout << setw(4) << ((int)(*((unsigned char*)(MM_pool + i)))) << ")"; // the ASCII number of the character
cout << " (" << setw(5) << (*(unsigned short*)(MM_pool + i)) << ")"; // the unsigned short value of 16 bits (2 bytes)
cout << " (" << setw(10) << (*(unsigned int*)(MM_pool + i)) << ")"; // the unsinged int value of 32 bits (4 bytes)
cout << endl;
}
}
// Initialize set up any data needed to manage the memory pool
void initializeMemoryManager(void) {
// your solution goes here
}
// Return a pointer inside the memory pool
// If no chunk can accommodate aSize call onOutOfMemory()
void* allocate(int aSize) {
// your solution goes here
}
// Free up a chunk previously allocated memory
void deallocate(void* aPointer) {
// your solution goes here
}
// Scan the memory pool and return the total free space remaining in bytes
int freeMemory(void) {
// your solution goes here
}
// Scan the memory pool and return the total deallocated memory in bytes
int usedMemory(void) {
// your solution goes here
}
// Scan the memory pool and return the total in use memory
int inUseMemory(void) {
// your solution goes here
}
// Return the size (in bytes) associated with this memory address
int size(void *aPointer) {
// your solution goes here
}
// This is called when no more memory is available to allocate
void onOutOfMemory(void) {
std::cerr << "/nMemory pool out of memory" << std::endl;
cin.get();
exit(1);
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
