Форум программистов, компьютерный форум CyberForum.ru

Стратегия управления оперативной памятью - C++

Восстановить пароль Регистрация
 
Мasy
0 / 0 / 0
Регистрация: 07.10.2013
Сообщений: 58
08.12.2015, 13:37     Стратегия управления оперативной памятью #1
Помогите редактировать код, чтобы сортировка данных йшла по убыванию
Стратегия наименее пригоден.
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <new>

using namespace std;

typedef unsigned int ui;

class MemoryManager {
protected:
struct MMI {
ui address = 0;
ui size = 0;
bool free = true;
MMI * previous = nullptr;
MMI * next = nullptr;
} *oHEAD = nullptr, *oTAIL = nullptr, *fHEAD = nullptr, *fTAIL = nullptr;

ui start_address = 0;
ui number_of_free_blocks = 0;
ui number_of_occupied_blocks = 0;
ui size_of_allowed_memory = 0;

void firstAllocateMemory(ui size) {
this->fHEAD = new MMI;
this->fHEAD->address = this->start_address;
this->fHEAD->size = size;
this->fHEAD->free = true;
this->fTAIL = this->fHEAD;
}

void unite() {
MMI * tmpo = this->oHEAD;

if (tmpo) {
tmpo->address = this->start_address;
tmpo = tmpo->next;
while (tmpo) {
tmpo->address = tmpo->previous->address + tmpo->previous->size;
tmpo = tmpo->next;
}
}

MMI * tmpf = this->fHEAD;
MMI * new_free_node = new MMI;
MMI * old_tmp;

if (tmpf) {
new_free_node->size += tmpf->size;
old_tmp = tmpf;
tmpf = tmpf->next;
delete old_tmp;
while (tmpf) {
new_free_node->size += tmpf->size;
old_tmp = tmpf;
tmpf = tmpf->next;
delete old_tmp;
}

if (this->oTAIL) new_free_node->address = this->oTAIL->address + this->oTAIL->size;
else new_free_node->address = start_address;

this->fHEAD = new_free_node;
this->fTAIL = new_free_node;
this->number_of_free_blocks = 1;
}

return;
}



//Разбивает блок на два блока
MMI * partitionBlock(MMI *& block, ui realSize) {
MMI * tmp = new MMI;

tmp->address = block->address;
tmp->size = realSize;
block->address += tmp->size;
block->size -= realSize;
this->number_of_free_blocks++;

return tmp;
}

MMI * searchBlock(ui size) {
MMI * f = this->fHEAD;
ui index = 0, indexSize = 0;
bool find = false;

//Нахождение самого неподходящего блока
for (ui i = 0; f; ++i) {
if (f->size >= size) {
if (f->size > indexSize) {
indexSize = f->size;
index = i;
find = true;
}
}
f = f->next;
}

//Прогон списка к самому подходящему блоку
f = this->fHEAD;
for (ui i = 0; f && i < index; ++i) f = f->next;

//Если блок не равен запрашиваемому размеру, разделить блок
if (find)
{
if (f->size != size) {
//f = this->partitionBlock(f, size);
return this->partitionBlock(f, size);
}
if (f->previous) f->previous->next = f->next;
else this->fHEAD = f->next;
if (f->next) f->next->previous = f->previous;
else this->fTAIL = f->previous;
f->previous = nullptr;
f->next = nullptr;
}

return find ? f : nullptr;
}

bool allocate(ui size) {
MMI * otmp = this->searchBlock(size);
if (!otmp) {

this->unite();
otmp = this->searchBlock(size);
if (!otmp) return false;
}
if (!oHEAD) {
this->oHEAD = otmp;
this->oHEAD->free = false;
this->oHEAD->next = nullptr;
this->oHEAD->previous = nullptr;
this->oTAIL = this->oHEAD;

this->number_of_occupied_blocks++;
this->number_of_free_blocks--;
return true;
}
this->number_of_free_blocks--;
this->number_of_occupied_blocks++;

otmp->free = false;
this->insertOccupiedBlock(otmp);
/*otmp->previous = this->oTAIL;
this->oTAIL->next = otmp;
this->oTAIL = otmp;*/
return true;
}

void insertOccupiedBlock(MMI * occupied_block) {
MMI * tmp = this->oHEAD;
bool find = false;

ui index = 0, address = 0;

for (ui i = 0; tmp; tmp = tmp->next, i++) {
if (tmp->address < occupied_block->address) {
if (tmp->address >= address) {
address = tmp->address;
index = i;
find = true;
}
}
}

tmp = this->oHEAD;
for (ui i = 0; i < index && tmp; ++i, tmp = tmp->next);

if (find) {
occupied_block->next = tmp->next;
occupied_block->previous = tmp;
tmp->next = occupied_block;
if (!occupied_block->next) this->oTAIL = occupied_block;
}
else {
occupied_block->next = tmp;
occupied_block->previous = nullptr;
tmp->previous = occupied_block;
this->oHEAD = occupied_block;
}

occupied_block->free = true;
}

MMI * searchBlockFromIndex(ui index) {
MMI * occupied_node = this->oHEAD;

for (ui i = 0; i < index && occupied_node; ++i, occupied_node = occupied_node->next);

return occupied_node;
}

void insertFreeBlock(MMI * free_block) {
MMI * tmp = this->fHEAD;
bool find = false;

ui index = 0, address = 0;

for (ui i = 0; tmp; tmp = tmp->next, i++) {
if (tmp->address < free_block->address) {
if (tmp->address >= address) {
address = tmp->address;
index = i;
find = true;
}
}
}

tmp = this->fHEAD;
for (ui i = 0; i < index && tmp; ++i, tmp = tmp->next);

if (find) {
free_block->next = tmp->next;
free_block->previous = tmp;
tmp->next = free_block;
if (!free_block->next) this->fTAIL = free_block;
}
else {
free_block->next = tmp;
free_block->previous = nullptr;
tmp->previous = free_block;
this->fHEAD = free_block;
}

free_block->free = true;
}


public:
MemoryManager(){};
MemoryManager(ui size) {
this->size_of_allowed_memory = size;
this->firstAllocateMemory(size);
}

~MemoryManager() {
MMI * tmp = this->fHEAD;

while (this->fHEAD) {
tmp = this->fHEAD;
this->fHEAD = this->fHEAD->next;
cout << "delete free" << endl;
delete tmp;
}

tmp = this->oHEAD;
while (this->oHEAD) {
tmp = this->oHEAD;
this->oHEAD = this->oHEAD->next;
cout << "delete occupied" << endl;
delete tmp;
}
}

bool setAllowedMemory(ui size) {
if (this->size_of_allowed_memory != 0) return false;
this->size_of_allowed_memory = size;
this->number_of_free_blocks++;
this->firstAllocateMemory(size);
return true;
}

bool allocateMemory(ui size) {
if (size > this->size_of_allowed_memory || size == 0) return false;
else return this->allocate(size);
}

bool freeMemory(ui index){
if (index > this->number_of_occupied_blocks) return false;

MMI * tmp = this->searchBlockFromIndex(index);

if (!tmp) return false;

if (tmp->previous) tmp->previous->next = tmp->next;
else this->oHEAD = tmp->next;
if (tmp->next) tmp->next->previous = tmp->previous;
else this->oTAIL = tmp->previous;

this->insertFreeBlock(tmp);

this->number_of_free_blocks++;
this->number_of_occupied_blocks--;

return true;
}

void show() {
cout << "--------------------------------" << endl;
cout << "Size: " << this->size_of_allowed_memory << endl;
cout << "Occupied blocks: " << this->number_of_occupied_blocks << endl;
cout << "Free blocks: " << this->number_of_free_blocks << endl;

MMI * tmp = this->oHEAD;
this->oTAIL;

cout << std::boolalpha;
cout << "\nOccupied blocks:" << endl;
cout << "Address:\tFree:\tSize:" << endl;
while (tmp) {
cout << tmp->address << "\t\t" << tmp->free << "\t" << tmp->size << endl;
tmp = tmp->next;
}

tmp = this->fHEAD;
this->fTAIL;

cout << "\nFree blocks:" << endl;
cout << "Address:\tFree:\tSize:" << endl;
while (tmp) {
cout << tmp->address << "\t\t" << tmp->free << "\t" << tmp->size << endl;
tmp = tmp->next;
}
cout << "--------------------------------" << endl;
}
};

void menu();

int main() {



menu();

return 0;
}

void menu() {
MemoryManager mmi;

mmi.setAllowedMemory(700);

char mode;
ui tmp = 0;

do {
cout << "A - allocate memory" << endl;
cout << "F - free memory" << endl;
cout << "M - map of memory" << endl;
cout << "E - exit" << endl;
cin >> mode;

switch (mode) {
case 'A':
case 'a':
cout << "Size of allowed memory: ";
cin >> tmp;
mmi.allocateMemory(tmp);
mmi.show();
break;
case 'F':
case 'f':
cout << "Set index for free memory: ";
cin >> tmp;
mmi.freeMemory(tmp);
mmi.show();
break;
case 'M':
case 'm':
mmi.show();
break;
case 'E':
case 'e':
default: break;
}

} while (mode != 'E' && mode != 'e');

}
Similar
Эксперт
41792 / 34177 / 6122
Регистрация: 12.04.2006
Сообщений: 57,940
08.12.2015, 13:37     Стратегия управления оперативной памятью
Посмотрите здесь:

C++ Стратегия выборки
Стратегия "получение ресурса есть инициализация" C++
разработка подсистемы управления памятью C++
Паттерн стратегия и ошибки при компиляции C++
Пошаговая стратегия(начало) C++
Паттерн стратегия C++
Стратегия выделения/освобождения памяти C++
C++ Паттерн Стратегия на примере сортировки

Искать еще темы с ответами

Или воспользуйтесь поиском по форуму:
После регистрации реклама в сообщениях будет скрыта и будут доступны все возможности форума.
Ответ Создать тему
Опции темы

Текущее время: 10:04. Часовой пояс GMT +3.
КиберФорум - форум программистов, компьютерный форум, программирование
Powered by vBulletin® Version 3.8.9
Copyright ©2000 - 2016, vBulletin Solutions, Inc.
Рейтинг@Mail.ru