Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- const int MAXSIZE = 1048576, SIZE = 10;
- class MemoryCell {
- public:
- MemoryCell() = default;
- MemoryCell(int size, int start, int end) {
- if (size > MAXSIZE) {
- barbieSize = MAXSIZE;
- } else {
- barbieSize = size;
- }
- Full = true;
- cell = new char[barbieSize];
- startBlock = start, endBlock = end;
- }
- char *cell{};
- int startBlock{}, endBlock{}, barbieSize{};
- bool Full = false;
- };
- class MemoryManager {
- public:
- MemoryCell *array;
- int StackTop;
- MemoryManager(int size = SIZE) {
- array = new MemoryCell[size]();
- StackTop = 0;
- std::cout << "STACK CREATED" << std::endl;
- }
- ~MemoryManager() = default;
- void Push(int size) {
- UpdateTop();
- int CellQuantity;
- if (size % MAXSIZE == 0) {
- CellQuantity = size / MAXSIZE;
- } else {
- CellQuantity = size / MAXSIZE + 1;
- }
- if (StackTop + CellQuantity > SIZE) {
- std::cout
- << "SORRY, STACK IS FULL. CLEAR OR DEFRAG IT TO PUSH SOMETHING"
- << std::endl;
- return;
- }
- int startBlock = StackTop, endBlock = StackTop + CellQuantity - 1;
- for (int i = startBlock; i <= endBlock; i = i + 1) {
- array[i] = MemoryCell(size, startBlock, endBlock);
- size = size - MAXSIZE;
- }
- StackTop = endBlock + 1;
- std::cout << "INFORMATION ADDED" << std::endl << std::endl;
- }
- void Print() {
- for (int i = 0; i < StackTop; i = i + 1) {
- std::cout << "Cell Number '" << i << "' Memory Volume '"
- << array[i].barbieSize << "' Begin Block '"
- << array[i].startBlock << "' End Block '"
- << array[i].endBlock << "' Fullness '" << array[i].Full
- << "'" << std::endl;
- }
- std::cout << std::endl;
- }
- void UpdateTop () {
- for (int i = StackTop-1; i >= 0; i = i - 1){
- if (array[i].Full == 1) {
- StackTop = i+1;
- break;
- }
- }
- }
- void DeleteMemory(int num) {
- for (int i = 0; i <= StackTop; i = i + 1) {
- if (array[i].startBlock <= num && array[i].endBlock >= num) {
- array[i].Full = false;
- array[i].barbieSize = 0;
- delete array[i].cell;
- }
- }
- }
- void Defragmentation() {
- for (int i = 0; i < StackTop; i = i + 1) {
- if (!array[i].Full) {
- for (int j = i; j <= StackTop; j = j + 1) {
- array[j] = array[j + 1];
- array[j].startBlock = array[j].startBlock - 1;
- array[j].endBlock = array[j].endBlock - 1;
- }
- StackTop = StackTop - 1;
- i = i - 1;
- }
- }
- }
- };
- int main() {
- auto *Memorymanager = new MemoryManager();
- std::cout << "Hello, weclome to the best memory manager in the world))"
- << std::endl;
- while (true) {
- std::cout << "Please, choose any option" << std::endl;
- std::cout << "1)Add Information" << std::endl;
- std::cout << "2)Print Information" << std::endl;
- std::cout << "3)Delete Information" << std::endl;
- std::cout << "4)Defragmentation" << std::endl;
- std::cout << "Enter any other number to exit" << std::endl;
- std::cout << " Number of your option: ";
- int n;
- std::cin >> n;
- std::cout << std::endl;
- switch (n) {
- case 1:
- int mem;
- std::cout << "Choose how many bites you want to apply: ";
- std::cin >> mem;
- Memorymanager->Push(mem);
- break;
- case 2:
- std::cout << "CURRENT STACK" << std::endl;
- Memorymanager->Print();
- std::cout << std::endl;
- break;
- case 3:
- int cell;
- std::cout << "Choose cell to delete: ";
- std::cin >> cell;
- Memorymanager->DeleteMemory(cell);
- std::cout << "INFORMATION DELETED" << std::endl << std::endl;
- break;
- case 4:
- Memorymanager->Defragmentation();
- std::cout << "DEFRAGMENTATION ENDED" << std::endl << std::endl;
- break;
- default:
- delete Memorymanager;
- std::cout << "Memory is clear. See you later!" << std::endl;
- return 0;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement