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

Your assignment is to create a Memory Manager which will allow the user to alloc

ID: 3765526 • Letter: Y

Question

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 compile and be capable of managing a variable number of allocations and deallocations, with no requirements for deallocations to be in the same order as the allocations. However, a pointer will never be deallocated more than once.

You should provide implementations of the five 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.

Please do not include any other header files. If you need to do this for development, please remove it before submission. For example if you include for printf, please remove the printf (or comment them out) as well as the #include.

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

Common Cases

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

On average while your system is running, there will be allocations that range in size from 2 bytes to 16k. You may expect 10% of the allocations will be 8 bytes or smaller. Note that despite the average case you may be asked to satisfy any sized allocation

//header file

#pragma once

#ifndef __MEMORY_MANAGER_H__

#define __MEMORY_MANAGER_H__

// DO NOT CHANGE THIS HEADER FILE

namespace MemoryManager

{

//--- CORE Functions, these will need to be completed by the applicant

// Initialize any data needed to manage the memory pool

void initializeMemoryManager(void);

// return a pointer inside the memory pool

// If no chunk can accommodate aSize call OnAllocFail()

void* allocate(int aSize);

// Free up a chunk previously allocated

void deallocate(void* aPointer);

//--- support routines

// Will scan the memory pool and return the total free space remaining

int freeRemaining(void);

// Will scan the memory pool and return the largest free space remaining

int largestFree(void);

// will scan the memory pool and return the smallest free space remaining

int smallestFree(void);
//--- error conditions. None of these functions will return
//--- These routines do not need to be implemented by the candidate
// Call if no space is left for the allocation request

void onOutOfMemory(void);

// Call if a pointer over run condition is detected

void onOverrunDetected(void);

// If the caller makes any illegal request your code should call this

// provided failure function (which will not return):

void onIllegalOperation(const char* fmt,...);

// eg:

// int errorCode;

// onIllegalOperation("Error in createQueue: %d", errorCode);

};

#endif // __MEMORY_MANAGER_H

//cpp file

#include "MemoryManager.h"

//MemoryManager.cpp

namespace MemoryManager

{
// 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

// IMPORTANT IMPORTANT IMPORTANT IMPORTANT IMPORTANT IMPORTANT IMPORTANT

const int MM_POOL_SIZE = 65536;

char MM_pool[MM_POOL_SIZE];

// Initialize set up any data needed to manage the memory pool

void initializeMemoryManager(void)

{

// TODO : IMPLEMENT ME

}

// return a pointer inside the memory pool

// If no chunk can accommodate aSize call onOutOfMemory()

void* allocate(int aSize)

{

// TODO: IMPLEMENT ME

return ((void*) 0);

}

// Free up a chunk previously allocated

void deallocate(void* aPointer)

{

// TODO: IMPLEMENT ME

}

//---

//--- support routines

//---

// Will scan the memory pool and return the total free space remaining

int freeRemaining(void)

{

// TODO: IMPLEMENT ME

return 0;

}

// Will scan the memory pool and return the largest free space remaining

int largestFree(void)

{

// TODO: IMPLEMENT ME

return 0;

}

// will scan the memory pool and return the smallest free space remaining

int smallestFree(void)

{

// TODO: IMPLEMENT ME

return 0;

}
}

// main

#include "MemoryManager.h"
#include <iostream>
using namespace std;

int main(void)

{

using namespace MemoryManager;

initializeMemoryManager();


long* int_pointer;

char* string_pointer;

std::cout << "Free memory = " << freeRemaining() << std::endl;

int_pointer = (long *) allocate(sizeof(long));

std::cout<<"int_pointer Mem Address:"<<(long )int_pointer<

std::cout << "Free memory = " << freeRemaining() << std::endl;

std::cout << "Largest Free Memory Block = " << largestFree() << std::endl;

string_pointer = (char*) allocate(255);

std::cout<<"string_pointer Mem Address:"<<(long )string_pointer<

//std::cout<<"checkpoint 1 ";

//std::cin>>a;

*int_pointer = 0xDEADBEEF;

*string_pointer = 'X';

*(string_pointer+1) = 'Y';

*(string_pointer+2) = 'Z';

*(string_pointer+3) = '';

std::cout<<< std::endl;

std::cout << "Free memory = " << freeRemaining() << std::endl;

std::cout << "Largest Free Memory Block = " << largestFree() << std::endl;

//std::cout<<"checkpoint 2 ";

//std::cin>>a;

strcpy(string_pointer,"It was the best of times, it was the worst of times");

std::cout<<

//std::cout<<"checkpoint 3 ";

std::cout << "Free memory = " << freeRemaining() << std::endl;

std::cout << "Largest Free Memory Block = " << largestFree() << std::endl;

deallocate(int_pointer);

deallocate(string_pointer);


int a;

std::cin>>a;

}

namespace MemoryManager

{

void onOutOfMemory(void)

{

std::cerr << "Memory pool out of memory" << std::endl;

exit( 1 );

}

// Call if a pointer over run condition is detected

void onOverrunDetected(void)

{

std::cerr << "Pointer overrun detected" << std::endl;

exit( 1 );

}

// call for any other error condition, providing meaningful error messages are appreciated

void onIllegalOperation(const char* fmt,...)

{

if ( fmt == NULL )

{

std::cerr << "Unknown illegal operation" << std::endl;

exit( 1 );

}

else

{

char   buf[8192];

va_list argptr;

va_start (argptr,fmt);

vsprintf (buf,fmt,argptr);

va_end (argptr);

std::cerr << "Illegal operation: "" << buf << """ << std::endl;

exit( 1 );

}

}

}

Output should be:

Free memory = 65532

int_pointer Mem Address: 9192500

Free memory = 65522

Largest Free Memory Block = 65522

string_pointer Mem Address:9192510

XYZ

Free memory = 65261

It was the best of times, it was the worst of time

Free memory = 65261

Largest Free Memory Block = 65261

Only modify MemoryManager.cpp, tks!!!

Explanation / Answer

namespace MemoryManager
{
   const int MM_POOL_SIZE = 65536;
   const int MM_POOL_NO = 10;
   char MM_pool[MM_POOL_NO][MM_POOL_SIZE];

   struct info
   {
       bool freememory;
       unsigned int chunkofSize;
       info(size_t size):
       freememory(true),chunkofSize(size) {}
   };
  
   void initializeMemoryManager(void)
   {
       for( int i(0); i < MM_POOL_NO; ++i )
       {
           memset(&MM_pool[i][0],0,MM_POOL_SIZE);
           info first(MM_POOL_SIZE-sizeof(info));
           memcpy(&MM_pool[i][0],&first,sizeof(info));
       }

   }

   void* allocate(unsigned int anSize, bool AllowUnfragmentation )
   {
      
       for( int j(0); j < MM_POOL_NO; ++j )
       {
           for(int i=0;i<MM_POOL_SIZE;
           {  
               info* iter = (info*)&MM_pool[j][i];
               if(iter->freememory&&iter->chunkofSize>=anSize)
               {
                   iter->freememory=false;
                   if(iter->chunkofSize>anSize+sizeof(info))
                   {
                      
                       info nextHead(iter->chunkofSize-anSize-sizeof(info));
                       memcpy(&MM_pool[j][i+anSize+sizeof(info)],&nextHead,sizeof(info));
                       iter->chunkofSize=anSize;
                   }
                  
                   return &MM_pool[j][i+sizeof(info)];
               }
               else
               {
                  
                   i+=iter->chunkofSize+sizeof(info);
               }
           }
       }
       if( AllowUnfragmentation )
       {
           unfrag();
           return allocate( anSize, false );
       }
      
       onOutOfMemory( anSize );
   }

   void deallocate(void* aPointer)
   {
       info* start = (info*)((char*)aPointer-sizeof(info));
       start->freememory=true;
       info* next = (info*)((char*)aPointer+sizeof(info)+start->chunkofSize);
   }

  

   void unfrag()
   {
       for( int j(0); j < MM_POOL_NO; ++j )
       {
           int i=0;
           while(i<MM_POOL_SIZE)
           {  
               info* iter = (info*)&MM_pool[j][i];
               info* next = (info*)&MM_pool[j][i+iter->chunkofSize+sizeof(info)];
               if(iter->freememory&&next->free)
               {
                   iter->chunkofSize+=next->chunkofSize+sizeof(info);
                   memset(next,0,sizeof(info));
               }
               i+=iter->chunkofSize+sizeof(info);
           }
       }

   }

  
   int freeRemaining(void)
   {
       unfrag();
       int tot=0;
       for( int j(0); j < MM_POOL_NO; ++j )
       {
           for(int i=0;i<MM_POOL_SIZE;)
           {
               info* iter = (info*)&MM_pool[j][i];
               if(iter->freememory)
               {
                   tot+=iter->chunkofSize;
               }
               i+=iter->chunkofSize+sizeof(info);
           }
       }


       return tot;
   }

  
   int largestFree(void)
   {
       unfrag();
       int large=0;
       for( int j(0); j < MM_POOL_NO; ++j )
       {
           for(int i=0;i<MM_POOL_SIZE;)
           {
               info* iter = (info*)&MM_pool[j][i];
               if(iter->free&&iter->chunkofSize>large)
               {
                   large = iter->chunkofSize;
               }
               i+=iter->chunkofSize+sizeof(info);
           }
       }


       return large;
   }

  
   int smallestFree(void)
   {
       unfrag();
       int small = MM_POOL_SIZE;
       for( int j(0); j < MM_POOL_NO; ++j )
       {
           for(int i=0;i<MM_POOL_SIZE;)
           {
               info* iter = (info*)&MM_pool[j][i];
               if(iter->free&&iter->chunkofSize<small)
               {
                   small = iter->chunkofSize;
               }
               i+=iter->chunkofSize+sizeof(info);
           }
       }

       return small;
   }
              void onOutOfMemory( unsigned int Size )
   {
       std::cout<< "The Allocation:" << Size << " The Largest:"<< MemoryManager::largestFree() << " The Smallest:" << MemoryManager::smallestFree() << "Remaining: " << MemoryManager::freeRemaining() << std::endl;
       std::cerr << "The Memory pool out of the memory" << std::endl;
       system("PAUSE");
       exit( 1 );
   }
}

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