⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mbtlist.cpp

📁 内存管理
💻 CPP
字号:
#include "stdafx.h"
#include "MBTList.h"
#include <iostream.h>
MBTList::MBTList()
{
	headMBT = 0;
	tailMBT = 0;
	len = 0;
	count = 0;
}
MBTList::~MBTList()
{
	if(IsEmpty())
	{
		return;
	}
	MBT *tempMBT = tailMBT;
	while(tempMBT != 0 && tailMBT->preMBT != 0)
	{
		tempMBT = tailMBT->preMBT;
		delete tailMBT;
		tailMBT = tempMBT;
	}
	if(tailMBT->preMBT == 0)
		delete tailMBT;
}
bool MBTList::InsertHeadMBT(MBT *mbt)//插入第一个存储块
{
	bool result = false;
	if(IsEmpty() == true)
	{
		result = true;
		headMBT = mbt;
		tailMBT = mbt;
		headMBT->nextMBT = 0;
		tailMBT->preMBT = 0;
	}
	return result;
}
bool MBTList::InsertAfterHead(MBT *mbt)//在第一个存储块的后面添加一个存储块
{
	bool result = false;
	if(IsOnlyOne() == true)
	{
		result = true;
		headMBT->preMBT = 0;
		headMBT->nextMBT = mbt;
		mbt->nextMBT = 0;
		mbt->preMBT = headMBT;
		tailMBT = mbt;
	}
	else if(IsEmpty() == false)
	{
		result = true;
		headMBT->nextMBT->preMBT = mbt;
		mbt->preMBT = headMBT;
		mbt->nextMBT = headMBT->nextMBT;
		headMBT->nextMBT = mbt;
	}
	return result;
}
bool MBTList::InsertAfterTail(MBT *mbt)//在最后一个存储块的后面添加一个存储块
{
	bool result = false;
	if(IsEmpty() == false)
	{
		result = true;
		tailMBT->nextMBT = mbt;
		mbt->preMBT = tailMBT;
		tailMBT = mbt;
		tailMBT->nextMBT = 0;
		headMBT->preMBT = 0;
	}
	return result;
}
bool MBTList::InsertBeforeHead(MBT *mbt)//在第一个存储块前插入一个存储块
{
	bool result = false;
	if(IsEmpty() == false)
	{
		result = true;
		headMBT->preMBT = mbt;
		mbt->nextMBT = headMBT;
		headMBT = mbt;
		headMBT->preMBT = 0;
		tailMBT->nextMBT = 0;
	}
	return result;
}
bool MBTList::InsertBeforeTail(MBT *mbt)//在最后一个存储块的前面插入一个存储块
{
	bool result = false;
	if(IsOnlyOne() == true)
	{
		result = true;
		tailMBT->preMBT = mbt;
		tailMBT->nextMBT = 0;
		mbt->nextMBT = tailMBT;
		mbt->preMBT = 0;
		headMBT = mbt;
	}
	else if(IsEmpty() == false)
	{
		result = true;
		tailMBT->preMBT->nextMBT = mbt;
		mbt->nextMBT = tailMBT;
		mbt->preMBT = tailMBT->preMBT;
		tailMBT->preMBT = mbt;
	}
	return result;
}
bool MBTList::IsEmpty()//判断是否为空
{
	bool result = false;
	if(count == 0)
		result = true;
	return result;
}
bool MBTList::IsOnlyOne()//判断是否只有一个存储块
{
	bool result = false;
	if(count == 1)
		result = true;
	return result;
}


bool MBTList::InsertMBT(MBT *mbt, int sort)
{
	mbt->nextMBT = 0;
	mbt->preMBT = 0;
	bool result = InsertHeadMBT(mbt);
	if(result == false)
	{
		MBT *tempMBT = headMBT;
		while(tempMBT != 0 && result == false)
		{
			int cur = 0, temp = 0;
			switch(sort)
			{
				case 1://按照地址从低到高的顺序
					cur = mbt->startAdd;
					temp = tempMBT->startAdd;
					break;
				case 2://按照存储块长度由小到大
					cur = mbt->len;
					temp = tempMBT->len;
					break;
				case 3://按照存储块长度由大到小
					cur = tempMBT->len;
					temp = mbt->len;
					break;
				default:
					cout << "数据有误!" << endl;
					return false;

			}
			if(cur < temp)
			{
				if(tempMBT == headMBT)
				{
					result = InsertBeforeHead(mbt);
				}
				else if(tempMBT == tailMBT)
				{
					result = InsertBeforeTail(mbt);
				}
				else
				{
					result = true;
					tempMBT->preMBT->nextMBT = mbt;
					mbt->preMBT = tempMBT->preMBT;
					tempMBT->preMBT = mbt;
					mbt->nextMBT = tempMBT;
				}
			}
			else if(cur == temp)
			{
				result = InsertMBT(tempMBT, mbt);
			}
			tempMBT = tempMBT->nextMBT;
		}
	}
	if(result == false)
	{
		result = InsertAfterTail(mbt);
	}
	count++;
	len = len + mbt->len;
	return result;
}
	

bool MBTList::InsertMBT(MBT *tempMBT, MBT *mbt)
{
	
	bool result = false;
	MBT *curMBT = tempMBT;
	while(tempMBT != 0 && tempMBT->len == mbt->len)
	{
		if(tempMBT->startAdd > mbt->startAdd)
		{
			if(tempMBT == headMBT)
			{
				result = InsertBeforeHead(mbt);
			}
			else if(tempMBT == tailMBT)
			{
				result = InsertBeforeTail(mbt);
			}
			else
			{
				result = true;
				tempMBT->preMBT->nextMBT = mbt;
				mbt->preMBT = tempMBT->preMBT;
				mbt->nextMBT = tempMBT;
				tempMBT->preMBT = mbt;
			}
			break;
		}
		curMBT = tempMBT;
		tempMBT = tempMBT->nextMBT;
	}
	if(result == false && curMBT != 0)
	{
		if(curMBT == tailMBT)
		{
			result = InsertAfterTail(mbt);
		}
		else
		{
			result = true;
			curMBT->nextMBT->preMBT = mbt;
			mbt->nextMBT = curMBT->nextMBT;
			mbt->preMBT = curMBT;
			curMBT->nextMBT = mbt;
		}
	}
	return result;
}
void MBTList::Print()
{
	cout << "起始地址" << "   " << "长度" << "  " << "进程名称" << endl;
	MBT *tempMBT = headMBT;
	while(tempMBT != 0)
	{
		cout << tempMBT->startAdd << "   " << tempMBT->len << "  ";
		if(tempMBT->curPCB != 0)
			cout << tempMBT->curPCB->name;
		cout << endl;
		tempMBT = tempMBT->nextMBT;
	}
}

MBT* MBTList::Destribute(PCB *pcb)
{
	MBT *get = 0;
	MBT *tempMBT = headMBT;
	while(tempMBT != 0)
	{
		if(tempMBT->len >= pcb->len)
		{
			int pL = tempMBT->len - pcb->len;
			tempMBT->len = pcb->len;
			int stAdd = tempMBT->startAdd + tempMBT->len;
			get = new MBT(1, pcb);
			get->startAdd = tempMBT->startAdd;
			get->len = pcb->len;
			tempMBT->startAdd = stAdd;
			tempMBT->len = pL;
			break;
		}
		tempMBT = tempMBT->nextMBT;
	}
	return get;
}
void MBTList::Delete(MBT *mbt)
{
	MBT *tempMBT = headMBT;
	while(tempMBT != 0)
	{
		if(tempMBT == mbt)
		{
			tempMBT->preMBT->nextMBT = tempMBT->nextMBT;
			tempMBT->nextMBT->preMBT = tempMBT->preMBT;
			break;
		}
		tempMBT = tempMBT->nextMBT;
	}
}
MBT* MBTList::Recyle(char *n)
{
	MBT *get = 0;
	MBT *tempMBT = headMBT;
	while(tempMBT != 0)
	{
		if(tempMBT->curPCB->name == n)
		{
			get = tempMBT;
			Delete(get);
			break;
		}
		tempMBT = tempMBT->nextMBT;
	}
	return get;
}

void MBTList::UnionMBT(MBT *mbt, int sort)
{
	InsertMBT(mbt, sort);
	MBT *tempMBT = headMBT;
	bool result = false;
	while(tempMBT != 0)
	{
		if(tempMBT->nextMBT != 0)
		{
			if(tempMBT->startAdd + tempMBT->len == tempMBT->nextMBT->startAdd)
			{
				result = true;
				MBT *delMBT = 0;
				tempMBT->flag = 0;
				tempMBT->curPCB = 0;
				tempMBT->len = tempMBT->len + tempMBT->nextMBT->len;
				if(tempMBT->nextMBT->nextMBT != 0)
				{
					tempMBT->nextMBT->nextMBT->preMBT = tempMBT;
					delMBT = tempMBT->nextMBT;
					tempMBT->nextMBT = tempMBT->nextMBT->nextMBT;
				}
				delete delMBT;
			}
			else
			{
				if(result == true)return;
				tempMBT = tempMBT->nextMBT;
			}
		}
		else
		{
			tempMBT = tempMBT->nextMBT;
		}
	}
}




















⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -