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

📄 form1.h

📁 一个内存管理的PC端的模拟器。是初学者对内存管理有个感性的认识
💻 H
📖 第 1 页 / 共 3 页
字号:
			 //_myGraphics->DrawString("0x123456789", _BusyFont, _BusyBrush, _BusyItem[1].X, _BusyItem[1].Y + _BusyItem[1].Height);
		 }


		 ///<summary>
		 ///随机分配内存测试按钮
		 ///</summary>
private: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e) 
		 {
			 Random^	randObj = gcnew Random();

			 int	i = randObj->Next(32, (G_TestMemLen / 10));
			
			 //i = 1024;
			 do
			 {
				 if((Test_sp[Test_sp_num] = (char *)Fun_Malloc(i)) != NULL)
				 {
					Test_Space[Test_sp_num] = i;
					Test_sp_num += 1;
					Test_Alloc_Mem += i; 
					break;
				 }
				 else
				 {
					 if((G_TestMemLen - Test_Alloc_Mem - Test_sp_num * 32 - 16) <= 32)
					 {
						 return;
					 }
					 i = randObj->Next(32, (G_TestMemLen - Test_Alloc_Mem - Test_sp_num * 32 - 16));
					 continue;
				 }
			 }
			 while(1);
			 this->textBox1->Text = Test_sp_num.ToString();

		 }


		 ///<summary>
		 ///绘制忙链表Checked
		 ///</summary>
private: System::Void checkBox1_CheckedChanged(System::Object^  sender, System::EventArgs^  e) 
		 {
			 if(this->checkBox1->Checked == false)
			 {
				 _myGraphics->Clear(this->pictureBox1->BackColor);
				 return;
			 }

			 int	i;						  			  

			 !_BusyItem;  //释放
			 !_BusyItemPoint;
			 !_BusyItemInfo;

			 i = Fun_RetBusyList();
			 _BusyItemSum = i;
			 if(i == 0)
			 {
				 return;
			 }
			
			 _BusyItem = gcnew array<Rectangle>(i);
			 _BusyItemPoint = gcnew array<Point,2>(i, 4);
			 _BusyItemInfo = gcnew array<String^, 2>(i, 8);

			 Sub_ComputerRectAndPoint(_BusyItem, _BusyItemPoint, Org_X, Org_Y, i);			 			 			  
			
			 int Next, Prev, Addr, SpaceSize, Mem_LB, Mem_RB, SelfAddr, BusyFlag;
			 int j, ret;

			 for(j = 0; j < i; j++)
			 {
				 ret = Fun_RetBusyListItemInfo(j + 1, &Next, &Prev, &Addr, &SpaceSize, &Mem_LB, &Mem_RB, &SelfAddr, &BusyFlag);
				 if(ret == 1)
				 {
					 _BusyItemInfo[j, 0] = "Next:" + Next.ToString("X");
					 _BusyItemInfo[j, 1] = "Prev:" + Prev.ToString("X");
					 _BusyItemInfo[j, 2] = "Addr:" + Addr.ToString("X");
					 _BusyItemInfo[j, 3] = "Spac:" + SpaceSize.ToString();
					 _BusyItemInfo[j, 4] = "MeLb:" + Mem_LB.ToString("X");
					 _BusyItemInfo[j, 5] = "MeRb:" + Mem_RB.ToString("X");
					 _BusyItemInfo[j, 6] = "Self:" + SelfAddr.ToString("X");
					 _BusyItemInfo[j, 7] = "Busy:" + BusyFlag.ToString("X");

				 }
			 }

			 Sub_DrawList(_BusyItem, _BusyItemPoint, _BusyPen, _BusyBrush, _BusyItemInfo, _BusyFont, i);
			 
		 }


		 ///<summary>
		 ///分析繁忙链表按钮
		 ///</summary>
private: System::Void button4_Click(System::Object^  sender, System::EventArgs^  e) 
		 {
			 int	i;
			 
			 !_BusyItemInfo;

			 i = Fun_RetBusyList();
			 _BusyItemSum = i;
			 if(i != Test_sp_num)
			 {

			 }
			 else
			 {
				 int	j, ret;
				 int	Next, Prev, Addr, SpaceSize, Mem_LB, Mem_RB, SelfAddr, Busy_Flag;
				 int	All_Space;

				 All_Space = 0;
				 _BusyItemInfo = gcnew array<String^, 2>(i, 8);

				 for(j = 0; j < i; j++)
				 {
					 ret = Fun_RetBusyListItemInfo(j + 1, &Next, &Prev, &Addr, &SpaceSize, &Mem_LB, &Mem_RB, &SelfAddr, &Busy_Flag);
					 if(ret == 1)
					 {
						 All_Space += SpaceSize;
						 _BusyItemInfo[j, 0] = "Next:" + Next.ToString("X");
						 _BusyItemInfo[j, 1] = "Prev:" + Prev.ToString("X");
						 _BusyItemInfo[j, 2] = "Addr:" + Addr.ToString("X");
						 _BusyItemInfo[j, 3] = "Spac:" + SpaceSize.ToString();
						 _BusyItemInfo[j, 4] = "MeLb:" + Mem_LB.ToString("X");
						 _BusyItemInfo[j, 5] = "MeRb:" + Mem_RB.ToString("X");
						 _BusyItemInfo[j, 6] = "Self:" + SelfAddr.ToString("X");
						 _BusyItemInfo[j, 7] = "Busy:" + Busy_Flag.ToString("X");
					 }
				 }
				 All_Space -= i * 32;

				 if(All_Space != Test_Alloc_Mem)
				 {
					 this->textBox2->Text = "分配出大小与实际大小不同";
					 return;
				 }
				 
				 int	count;
				 count = Fun_AnalyseList(_BusyItemInfo, i, _ErrItem, _ErrItemInfo, _ErrItemPoint); 			  
				 this->textBox2->Text = "发现错误" + count.ToString() + "个";	
				 if(count != 0)
				 {
					 this->checkBox2->Enabled = true;
				 }
				 else
				 {
					 this->checkBox2->Enabled = false;
				 }
				 _ErrCount = count;
			 }
		 }


		 ///<summary>
		 ///绘制出错链表Checked
		 ///</summary>
private: System::Void checkBox2_CheckedChanged(System::Object^  sender, System::EventArgs^  e) 
		 {
			 if(this->checkBox2->Checked == true)
			 {
				 _ErrPen = gcnew System::Drawing::Pen(Color::Black);		//链表出错表项使用Pen

				 _ErrPen->EndCap = System::Drawing::Drawing2D::LineCap::ArrowAnchor;
				 _ErrPen->DashStyle = System::Drawing::Drawing2D::DashStyle::DashDotDot;

				 _ErrBrush = gcnew SolidBrush(Color::Black);		//链表出错表项使用Bursh

				 Sub_DrawList(_ErrItem, _ErrItemPoint, _ErrPen, _ErrBrush, _ErrItemInfo, _BusyFont, _ErrCount);
			 }
		 }


		///<summary>
		///绘制空链表Checked
		///</summary>
private: System::Void checkBox3_CheckedChanged(System::Object^  sender, System::EventArgs^  e) 
		 {
			 if(this->checkBox3->Checked == false)
			 {
				 _myGraphics->Clear(this->pictureBox1->BackColor);
				 return;
			 }

			 int	i;						  			  

			 !_IdleItem;  //释放
			 !_IdleItemPoint;
			 !_IdleItemInfo;

			 i = Fun_RetIdleList();
			 _IdleItemSum = i;
			 _IdleItem = gcnew array<Rectangle>(i);
			 _IdleItemPoint = gcnew array<Point,2>(i, 4);
			 _IdleItemInfo = gcnew array<String^, 2>(i, 8);

			 Sub_ComputerRectAndPoint(_IdleItem, _IdleItemPoint, Org_X, Org_Y, i);			 			 			  
			
			 int Next, Prev, Addr, SpaceSize, Mem_LB, Mem_RB, SelfAddr, BusyFlag;
			 int j, ret;

			 for(j = 0; j < i; j++)
			 {
				 ret = Fun_RetIDLEListItemInfo(j + 1, &Next, &Prev, &Addr, &SpaceSize, &Mem_LB, &Mem_RB, &SelfAddr, &BusyFlag);
				 if(ret == 1)
				 {
					 _IdleItemInfo[j, 0] = "Next:" + Next.ToString("X");
					 _IdleItemInfo[j, 1] = "Prev:" + Prev.ToString("X");
					 _IdleItemInfo[j, 2] = "Addr:" + Addr.ToString("X");
					 _IdleItemInfo[j, 3] = "Spac:" + SpaceSize.ToString();
					 _IdleItemInfo[j, 4] = "MeLb:" + Mem_LB.ToString("X");
					 _IdleItemInfo[j, 5] = "MeRb:" + Mem_RB.ToString("X");
					 _IdleItemInfo[j, 6] = "Self:" + SelfAddr.ToString("X");
					 _IdleItemInfo[j, 7] = "Busy:" + BusyFlag.ToString("X");
				 }
			 }

			 if(i == 1)
			 {
				 _myGraphics->FillRectangle(_IdleBrush, _IdleItem[0]);
			 }
			 else
			 {
				 Sub_DrawList(_IdleItem, _IdleItemPoint, _IdlePen, _IdleBrush, _IdleItemInfo, _BusyFont, i);
			 }
		 }


		///<summary>
		///随机释放内存按钮
		///</summary>
private: System::Void button5_Click(System::Object^  sender, System::EventArgs^  e) 
		 {
			 Random^	randObj = gcnew Random();

			 int	i;
			 i = randObj->Next(0, Test_sp_num - 1);
			 Fun_Free((void *)Test_sp[i]);
			 Test_Alloc_Mem -= Test_Space[i];
			 Test_sp_num -= 1;
			 this->textBox1->Text = Test_sp_num.ToString();

			 int	j;
			 for(j = i; j < Test_sp_num; j++)
			 {
				Test_sp[j] = Test_sp[j + 1];
				Test_Space[j] = Test_Space[j + 1];
			 }
		 }


		///<summary>
		///绘制物理内存块Checked
		///</summary>
private: System::Void checkBox4_CheckedChanged(System::Object^  sender, System::EventArgs^  e) 
		 {
			 if(this->checkBox4->Checked == false)
			 {
				 _myGraphics->Clear(this->pictureBox1->BackColor);
				 return;
			 }

			 _PhyCount = Fun_RetPhyList();

			 _PhyItem = gcnew array<Rectangle, 1>(_PhyCount);		//物理链表中的表项数组
			 _PhyItemPoint = gcnew array<Point, 2>(_PhyCount, 4);	//物理链表中的表项的4个关键点next,prev, next, prev
			 _PhyItemInfo = gcnew array<String^, 2>(_PhyCount, 8);	//物理链表中的表项的关键信息

			 Sub_ComputerRectAndPoint(_PhyItem, _PhyItemPoint, Org_X, Org_Y, _PhyCount);	

			 int Next, Prev, Addr, SpaceSize, Mem_LB, Mem_RB, SelfAddr, Busy_Flag;
			 int j, ret;
			 
			 for(j = 0; j < _PhyCount; j++)
			 {
				 ret = Fun_RetPhyItemInfo(j + 1, &Next, &Prev, &Addr, &SpaceSize, &Mem_LB, &Mem_RB, &SelfAddr, &Busy_Flag);				 
				 _PhyItemInfo[j, 0] = "Next:" + Next.ToString("X");
				 _PhyItemInfo[j, 1] = "Prev:" + Prev.ToString("X");
				 _PhyItemInfo[j, 2] = "Addr:" + Addr.ToString("X");
				 _PhyItemInfo[j, 3] = "Spac:" + SpaceSize.ToString();
				 _PhyItemInfo[j, 4] = "MeLb:" + Mem_LB.ToString("X");
				 _PhyItemInfo[j, 5] = "MeRb:" + Mem_RB.ToString("X");
				 _PhyItemInfo[j, 6] = "Self:" + SelfAddr.ToString("X");
				 _PhyItemInfo[j, 7] = "Busy:" + Busy_Flag.ToString("X");
			 }

			 Sub_DrawPhyListItem(_PhyItem, _PhyItemPoint, _BusyPen, _BusyBrush, _IdlePen, _IdleBrush, _PhyItemInfo, _BusyFont, _PhyCount);
		 }


		 ///<summary>
		 ///分析空闲链表 
		 ///</summary>
private: System::Void button3_Click(System::Object^  sender, System::EventArgs^  e) 
		 {
			 int	i;						  			  

			 !_IdleItem;  //释放
			 !_IdleItemPoint;
			 !_IdleItemInfo;

			 i = Fun_RetIdleList();
			 _IdleItemSum = i;
			 _IdleItem = gcnew array<Rectangle>(i);
			 _IdleItemPoint = gcnew array<Point,2>(i, 4);
			 _IdleItemInfo = gcnew array<String^, 2>(i, 8);

			 Sub_ComputerRectAndPoint(_IdleItem, _IdleItemPoint, Org_X, Org_Y, i);			 			 			  
			
			 int Next, Prev, Addr, SpaceSize, Mem_LB, Mem_RB, SelfAddr, BusyFlag;
			 int j, ret;

			 for(j = 0; j < i; j++)
			 {
				 ret = Fun_RetIDLEListItemInfo(j + 1, &Next, &Prev, &Addr, &SpaceSize, &Mem_LB, &Mem_RB, &SelfAddr, &BusyFlag);
				 if(ret == 1)
				 {
					 _IdleItemInfo[j, 0] = "Next:" + Next.ToString("X");
					 _IdleItemInfo[j, 1] = "Prev:" + Prev.ToString("X");
					 _IdleItemInfo[j, 2] = "Addr:" + Addr.ToString("X");
					 _IdleItemInfo[j, 3] = "Spac:" + SpaceSize.ToString();
					 _IdleItemInfo[j, 4] = "MeLb:" + Mem_LB.ToString("X");
					 _IdleItemInfo[j, 5] = "MeRb:" + Mem_RB.ToString("X");
					 _IdleItemInfo[j, 6] = "Self:" + SelfAddr.ToString("X");
					 _IdleItemInfo[j, 7] = "Busy:" + BusyFlag.ToString("X");
				 }
			 }

			 UInt32	x1, errcount;
			 array<Char>^	xu_str = gcnew array<Char>(256);
			 String^	temp_str;

			 errcount = 0;
			 for(j = 0; j < i; j++)
			 {
				 xu_str->Clear(xu_str, 0, xu_str->GetLength(0));
				 _IdleItemInfo[j, 7]->CopyTo(5, xu_str, 0, _IdleItemInfo[j, 7]->Length - 5);
				 temp_str = gcnew String(xu_str);					  
				 x1 = System::UInt32::Parse(temp_str, System::Globalization::NumberStyles::AllowHexSpecifier);
				 if(x1 != 0)
				 {
					 errcount += 1;
				 }
			 }

			 this->textBox3->Text = "发现" + errcount.ToString() + "错误";
		 }


		 ///<summary>
		 ///Mouse在PICBOX1中按下 
		 ///</summary>
private: System::Void pictureBox1_MouseClick(System::Object^  sender, System::Windows::Forms::MouseEventArgs^  e) 
		 {
			 if(this->checkBox4->Checked == true)
			 {
				 int	i;
				 for(i = 0; i < _PhyCount; i++)
				 {
					 if((e->X >= _PhyItem[i].X) && (e->X <= (_PhyItem[i].X + _PhyItem[i].Width))) 
					 {
						 if((e->Y >= _PhyItem[i].Y) && (e->Y <= (_PhyItem[i].Y + _PhyItem[i].Height))) 
						 {
							 UInt32		xx;							 
							 array<Char>^	xu_str = gcnew array<Char>(256);
							 String^	temp_str;
							 
							 xu_str->Clear(xu_str, 0, xu_str->GetLength(0));
							 _PhyItemInfo[i, 7]->CopyTo(5, xu_str, 0, _PhyItemInfo[i, 7]->Length - 5);
							 temp_str = gcnew String(xu_str);					  
							 xx = System::UInt32::Parse(temp_str, System::Globalization::NumberStyles::AllowHexSpecifier);
							 if(xx != 0xaa)
							 {
								 return;
							 }

							 xu_str->Clear(xu_str, 0, xu_str->GetLength(0));
							 _PhyItemInfo[i, 2]->CopyTo(5, xu_str, 0, _PhyItemInfo[i, 2]->Length - 5);
							 temp_str = gcnew String(xu_str);					  
							 xx = System::UInt32::Parse(temp_str, System::Globalization::NumberStyles::AllowHexSpecifier);

							 for(i = 0; i < Test_sp_num; i++)
							 {
								 if(xx == (unsigned int)Test_sp[i])
								 {
									 break;
								 }
							 }

							 Fun_Free((void *)Test_sp[i]);
							 Test_Alloc_Mem -= Test_Space[i];
							 Test_sp_num -= 1;
							 this->textBox1->Text = Test_sp_num.ToString();

							 int	j;
							 for(j = i; j < Test_sp_num; j++)
							 {
								Test_sp[j] = Test_sp[j + 1];
								Test_Space[j] = Test_Space[j + 1];
							 }
							 
							 this->checkBox4->Checked = false;
							 this->checkBox4->Checked = true;

							 break;
						 }
					 }
				 }
			 }
		 }
};

}

⌨️ 快捷键说明

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