Question: Write the memory system with Cpp: 1 . Write all programs in cross - platform C + + 2 . No Containers, NO STL allowed

Write the memory system with Cpp:
1. Write all programs in cross-platform C++
2. No Containers, NO STL allowed {Vector, Lists, Sets, etc...}
3. Simple Old-fashion C++. No modern C++
4. No Adding files
Create a memory system within a heap
Take the given memory system framework for the heap layout:
o Add the allocators
o Add the de-allocators
//1. Mem.h
#include "Heap.h"
class Mem
{
public:
static const uint32_t TotalSize =(50*1024);
static const uint32_t HeapAlign =16;
static const uint32_t HeapAlignMask = HeapAlign -1;
static const uint32_t HeaderGuards =128;
enum class Guard {Type_A, Type_5, Type_None};
public:
Mem( Guard type );
Mem()= default;
Mem(const Mem &)= delete;
Mem & operator =(const Mem &)= delete;
~Mem();
void free( void * const data );
void *malloc( const uint32_t size );
void initialize();
Heap *GetHeap();
void Print(int count);
private:
// Useful in malloc and free
Heap *poHeap;
//!!!!Below: Not used in malloc, free, or initialize
Guard type;
void *poRawMem;
};
#endif
//--- End of File ---
//2. Mem.cpp
#include "Mem.h"
#include "Heap.h"
#include "Type.h"
// To help with coalescing... not required
struct SecretPtr
{
Free *pFree;
};
//----------------------------------------------------
// Initialized the Memory block:
// Update the Heap
// Create a free block
// Heap structure links to free hdr
//-----------------------------------------------------
void Mem::initialize()
{
// TODO
}
//----------------------------------------------------
// Find a free block that fits
// Change it to used (may require subdivision)
// Correct the heap Links (used,free) headers
// Update stats
// Return pointer to block
//-----------------------------------------------------
void *Mem::malloc( const uint32_t _size )
{
// TODO
}
//----------------------------------------------------
// Return the free block to the system
// May require Coalescing
// Correct the heap Links (used,free) headers
// Update stats
//-----------------------------------------------------
void Mem::free( void * const data )
{
// TODO
}
Mem::~Mem()
{
//--skip-- do not modify
}
Heap *Mem::GetHeap()
{
return this->poHeap;
}
Mem::Mem(Guard _type)// do not modify
{
this->poHeap = nullptr;
this->poRawMem = nullptr;
this->type =_type;
// Do a land grab --- get the space for the entire heap
// Since OS have Different Alignments... I forced it to 16 byte aligned
poRawMem =_aligned_malloc(Mem::TotalSize + Mem::HeaderGuards, Mem::HeapAlign);
assert(poRawMem != nullptr);
if (this->type == Guard::Type_A)
{
HEAP_SET_BOTTOM_A_GUARDS
}
else
{
HEAP_SET_BOTTOM_5_GUARDS
}
assert(((uint32_t)poRawMem & Mem::HeapAlignMask)==0x0);
Heap *p = new(poRawMem) Heap();
assert(p);
this->poHeap = p;
}
void Mem::Print(int count)
{
//--skip-- do not modify
}
//--- End of File ---
//3. Heap.h
#include "Used.h"
#include "Free.h"
class Heap
{
public:
Heap();
Heap(const Heap&)= delete;
Heap& operator =(const Heap&)= delete;
~Heap()= default;
public:
Used *pUsedHead;
Free *pFreeHead;
Free *pNextFit;
uint32_t currNumUsedBlocks;
uint32_t currUsedMem;
uint32_t currNumFreeBlocks;
uint32_t currFreeMem;
uint32_t pad;
};
#endif
//4. Heap.cpp
#include "Heap.h"
#include "Mem.h"
Heap::Heap()
: pUsedHead(nullptr),
pFreeHead(nullptr),
pNextFit(nullptr),
currNumUsedBlocks(0),
currUsedMem(0),
currNumFreeBlocks(0),
currFreeMem(0),
pad(0)
{
}
// Free.h
#include "Type.h"
class Used;
class Free
{
public:
// Do not reorder, change or add data fields
// You can add methods if you wish
Free *pNext;
Free *pPrev;
uint32_t mData;
Free()= default;
};
#endif
//6.Free.cpp
#include "Used.h"
#include "Free.h"
#include "Type.h"
// TODO
// Used.h
#ifndef USED_H
#define USED_H
#include "Type.h"
class Free;
class Used
{
public:
// Do not reorder, change or add data fields
// You can add methods if you wish
Used *pNext;
Used *pPrev;
uint32_t mData;
Used()= default;
};
#endif
// Used.cpp
#include "Free.h"
#include "Used.h"
// TODO

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!