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

📄 os1dlg.cpp

📁 主存空间的分配和回收 可变分区管理方式下采用首次适应算法实现主存分配和回收
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// OS1Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "OS1.h"
#include "OS1Dlg.h"
#include "OS1NEWDIAG.h"
#include "MyFreeView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// COS1Dlg dialog

COS1Dlg::COS1Dlg(CWnd* pParent /*=NULL*/)
	: CDialog(COS1Dlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(COS1Dlg)
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDI_ICON1);
}

void COS1Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(COS1Dlg)
	DDX_Control(pDX, IDC_GIF, m_gif);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(COS1Dlg, CDialog)
	//{{AFX_MSG_MAP(COS1Dlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_SETCURSOR()
	ON_COMMAND(IDM_QUIT, OnQuit)
	ON_COMMAND(IDM_HELP, OnHelp)
	ON_BN_CLICKED(IDC_BALLOC, OnBalloc)
	ON_BN_CLICKED(IDC_BUNALLOC, OnBunalloc)
	ON_COMMAND(IDM_VIEW, OnView)
	ON_COMMAND(IDM_BEGIN, OnBegin)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// COS1Dlg message handlers

BOOL COS1Dlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	if (m_gif.Load(MAKEINTRESOURCE(IDR_GIF1),_T("gif")))
	{
		m_gif.Draw();
	}
	//设置窗体背景图片
	m_DlgBackground.LoadBitmap(IDB_BITMAP1);
	//初始化空闲块链表
	pHead=NULL;
	pHead1=NULL;
	CFREEMEM *pnew;
	pnew = (CFREEMEM *)(new CFREEMEM);
	pnew->BaseAddress=0;
	pnew->FreeLength=260000;
	pnew->pNext=pHead;
	pHead=pnew;
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void COS1Dlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void COS1Dlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		//CDialog::OnPaint();
		CPaintDC dc(this);				//对话框的dc
		CDC dcMem; 
		dcMem.CreateCompatibleDC(&dc);   //创建与对话框dc兼容的内存dc

		CRect rect;
		GetClientRect(&rect);

		BITMAP bitMap;
		m_DlgBackground.GetBitmap(&bitMap);

		CBitmap *pbmpOld=dcMem.SelectObject(&m_DlgBackground);		//将背景位图选入内存dc中
		dc.StretchBlt(0,0,rect.Width(),rect.Height(),&dcMem,0,0,bitMap.bmWidth,bitMap.bmHeight,SRCCOPY);   //将内存dc中的位图拉伸显示在对话框的dc中
		//dc.BitBlt(0,0,rect.Width(),rect.Height(),&dcMem,0,0,SRCCOPY);
	}
	CWnd *pWnd = GetDlgItem(IDC_ALLOC);
	pWnd->UpdateWindow();
	pWnd = GetDlgItem(IDC_UNALLOC);
	pWnd->UpdateWindow();
	pWnd = GetDlgItem(IDC_BK);
	pWnd->UpdateWindow();
	OnInitDraw();
	pCurrent1=pHead1;
	if(pCurrent1!=NULL)
	{
	    while(pCurrent1!=NULL)
		{
		 OnAllocDraw(pCurrent1->proBaseAddress,pCurrent1->proLength);
		 pCurrent1=pCurrent1->pNext1;
		}
	}
	pCurrent=pHead;
	if(pCurrent!=NULL)
	{
	    while(pCurrent!=NULL)
		{
		 OnUnAllocDraw(pCurrent->BaseAddress,pCurrent->FreeLength);
		 pCurrent=pCurrent->pNext;
		}
	}
	OnView();
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR COS1Dlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}
BOOL COS1Dlg::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message) 
{
//	if(pWnd->GetDlgCtrlID()==IDC_HOMEBTN || pWnd->GetDlgCtrlID()==IDC_CLOSEBTN)
//	{
	//	SetCursor(AfxGetApp()->LoadCursor(IDC_HAND));
	//	return true;
	//}

	SetCursor(AfxGetApp()->LoadCursor(IDC_CURSOR1));
	return true;
	
	//return CDialog::OnSetCursor(pWnd, nHitTest, message);
}

void COS1Dlg::OnQuit() 
{
	// TODO: Add your command handler code here
	DestroyWindow();
}

void COS1Dlg::OnHelp() 
{
	// TODO: Add your command handler code here
	CAboutDlg pAbout;
	pAbout.DoModal();
}

void COS1Dlg::OnOK() 
{
 //  注意不加处理
	CWnd* pWndNext = GetNextDlgTabItem(GetFocus());
	if (pWndNext)
	{
		pWndNext->SetFocus();
	}
}

//void COS1Dlg::OnCancel()
//{
//注意不加处理
//}



void COS1Dlg::OnInitDraw()
{
//填充“已分配”和“未分配”区域	
	CWnd *pWnd = GetDlgItem(IDC_ALLOC);
	CDC *pDC =pWnd->GetDC();
	CBrush drawBrush;
	CPen newPen,*oldPen;
	drawBrush.CreateSolidBrush(RGB(255,0,0));
	CBrush *oldBrush = pDC->SelectObject(&drawBrush);
	CRect rcClient;
	pWnd->GetClientRect(rcClient);
	pDC->Rectangle(rcClient);
	pDC->SelectObject(oldBrush);
	pWnd = GetDlgItem(IDC_UNALLOC);
	pDC =pWnd->GetDC();
	drawBrush.Detach();
	drawBrush.CreateSolidBrush(RGB(152,101,101));
	oldBrush = pDC->SelectObject(&drawBrush);
	pWnd->GetClientRect(rcClient);
	pDC->Rectangle(rcClient);
	pDC->SelectObject(oldBrush);
	pWnd = GetDlgItem(IDC_BK);
	pDC =pWnd->GetDC();
	drawBrush.Detach();
	drawBrush.CreateSolidBrush(RGB(202,243,227));
	newPen.CreatePen(PS_SOLID,1,RGB(0,0,0));
	oldBrush = pDC->SelectObject(&drawBrush);
	oldPen =pDC->SelectObject(&newPen);
	pWnd->GetClientRect(rcClient);
	pDC->Rectangle(rcClient);
	pDC->SelectObject(oldBrush);
	pDC->SelectObject(oldPen);
	newPen.Detach();
	newPen.CreatePen(PS_SOLID,2,RGB(152,101,101));
	oldPen =pDC->SelectObject(&newPen);
	for(int i=0;i<128;i++)
	{
	  pDC->MoveTo(rcClient.left+24+3*i,rcClient.top+18);
	  pDC->LineTo(rcClient.left+24+3*i,rcClient.top+rcClient.Height()/2-1);
	  pDC->MoveTo(rcClient.left+24+3*i,rcClient.top+rcClient.Height()/2+1);
	  pDC->LineTo(rcClient.left+24+3*i,rcClient.top+rcClient.Height()-19);
	}
	pDC->SelectObject(oldPen);
	newPen.Detach();
	LOGFONT LocalLogFont;
	memset(&LocalLogFont,0,sizeof(LOGFONT));
	LocalLogFont.lfCharSet=GB2312_CHARSET;
    strcpy(LocalLogFont.lfFaceName, "黑体");
    LocalLogFont.lfEscapement = 2700;
    LocalLogFont.lfOrientation = 2700;
	pDC->SetBkColor(RGB(202,243,227));
	pDC->SetTextColor(RGB(255,0,0));
	CFont MyFont,*cMyOldFont;
    MyFont.CreateFontIndirect(&LocalLogFont);
    cMyOldFont = pDC->SelectObject(&MyFont);
    pDC->TextOut(rcClient.left+10,rcClient.top+24,"单位");	
	pDC->SelectObject(cMyOldFont);
	MyFont.DeleteObject();
	LocalLogFont.lfWeight = 700;
	MyFont.CreateFontIndirect(&LocalLogFont);
	cMyOldFont = pDC->SelectObject(&MyFont);
	pDC->TextOut(rcClient.left+10,rcClient.top+55,":KB");
	pDC->SelectObject(cMyOldFont);
	MyFont.DeleteObject();
    LocalLogFont.lfEscapement = 0;
    LocalLogFont.lfOrientation = 0;
	MyFont.CreateFontIndirect(&LocalLogFont);
	cMyOldFont = pDC->SelectObject(&MyFont);
	pDC->TextOut(rcClient.left+24,rcClient.top+118,"0 KB");
	pDC->TextOut(rcClient.left+90,rcClient.top+118,"已分配:");
	pDC->TextOut(rcClient.left+345,rcClient.top+118,"260000 KB");
	pDC->SelectObject(cMyOldFont);
	MyFont.DeleteObject();
	pDC->SetTextColor(RGB(7,180,286));
	MyFont.CreateFontIndirect(&LocalLogFont);
	cMyOldFont = pDC->SelectObject(&MyFont);
	pDC->TextOut(rcClient.left+145,rcClient.top+118,"0%( 剩余260000KB)");
	pDC->SelectObject(cMyOldFont);
	MyFont.DeleteObject();
	
}

void COS1Dlg::OnBalloc() 
{
	// TODO: Add your control notification handler code here
	CEdit *pEdit1,*pEdit2;
	CString proname="",allocnum="",baseaddr="";
	pEdit1= (CEdit *)GetDlgItem(IDC_PRONAME);
	pEdit2= (CEdit *)GetDlgItem(IDC_ALLOCNUM);
	pEdit1->GetWindowText(proname);
	pEdit2->GetWindowText(allocnum);
	if(proname=="")
	{
		MessageBox("进程名不能为空!");
		pEdit1->SetWindowText("");
	    pEdit1->SetFocus();
		return;
	}
	if(proname.GetLength()>8)
	{
		MessageBox("进程名太长了!");
		pEdit1->SetWindowText("");
	    pEdit1->SetFocus();
		return;
	}
	if(atoi(allocnum)<=0||atoi(allocnum)>260000)
	{
		MessageBox("内存分配大小不合法或无意义!");
	    pEdit2->SetWindowText("");
		pEdit2->SetFocus();
		return;
	}
	pCurrent1=pHead1;
	if(pCurrent1!=NULL)
	{  
		while(pCurrent1!=NULL)
		{ 
	      if(pCurrent1->proName==proname)
		  {
		    MessageBox("你要分配的进程已经存在,请重新输入!");
			pEdit1->SetFocus();
			return;
		  }
		  else
		  {
		   pCurrent1=pCurrent1->pNext1;
		  }
		}
	}
	pEdit1->SetWindowText("");
	pEdit2->SetWindowText("");
	CFREEMEM *pCur=NULL;
	pCur=pCurrent=pHead;
	if(pCur!=NULL)
	{
		pCur=pCur->pNext;
		if(pCur!=NULL&&(pCurrent->FreeLength<atoi(allocnum)))
		{
	     while(pCur!=NULL)
		 {   
    	    if(pCur->FreeLength>=atoi(allocnum))
			{
		      pCurrent1=pHead1;
			  if (pCurrent1!=NULL)
			  { 
	            while(pCurrent1->pNext1!=NULL)
		           pCurrent1=pCurrent1->pNext1;
		         COSPRO *pnew;
	             pnew=(COSPRO *)(new COSPRO);
		         pnew->proName=proname;
		         pnew->proLength=atoi(allocnum);
		         pnew->proBaseAddress=pCur->BaseAddress;
		         baseaddr.Format("%d",pCur->BaseAddress);
		         pCur->BaseAddress=pCur->BaseAddress+atoi(allocnum);
		         pCur->FreeLength=pCur->FreeLength-atoi(allocnum);
		         if (pCur->FreeLength==0)
				 {
			       pCurrent->pNext=pCur->pNext;
		           delete pCur;
				 }
		         pCurrent1->pNext1=pnew;
		         pnew->pNext1=NULL;
		         ProList(proname,baseaddr,allocnum);
		         OnAllocDraw(pnew->proBaseAddress,atoi(allocnum));
		         OnPaint();
			  }
			  else
			  { 
		        pHead1=(COSPRO *)(new COSPRO);
		        COSPRO *pnew;
	            pnew=(COSPRO *)(new COSPRO);
		        pnew->proName=proname;
	        	pnew->proLength=atoi(allocnum);
		        pnew->proBaseAddress=pCur->BaseAddress;

⌨️ 快捷键说明

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