实现了两种内存分配方法,和内存回收机制
First fit 分配法
# include <iostream>
# include <algorithm>
# define M 20
# define N 20
using namespace std;
struct my_empty
{
int address_of_start;
int size_of_empty;
int yesorno;
} Empty[ M] ;
struct not_empty
{
int process_id;
int address_of_start;
int size_of_notempty;
int yesorno;
} Not_Empty[ N] ;
int CountOfEmpty = 1 , CountOfNotEmpty = 0 ;
int Max_Size = 256 ;
int Num_Of_Row = 20 ;
int Num_of_Holes = 0 ;
int Time[ N] ;
int num = 0 ;
bool cmp ( const my_empty& a, const my_empty& b) {
return a. address_of_start < b. address_of_start;
}
bool cmp2 ( const my_empty& a, const my_empty& b) {
return a. address_of_start > b. address_of_start;
}
bool cmp_NotEmpty ( const not_empty& a, const not_empty& b) {
return a. address_of_start < b. address_of_start;
}
bool cmp_NotEmpty2 ( const not_empty& a, const not_empty& b) {
return a. address_of_start > b. address_of_start;
}
int enough ( int Size_Of_NotEmpty)
{
int i;
for ( i = 0 ; i < CountOfEmpty; i++ )
{
if ( Empty[ i] . size_of_empty >= Size_Of_NotEmpty) {
Time[ num] += 1 ;
num++ ;
return i;
}
else {
Time[ num] += 1 ;
}
}
return - 1 ;
}
int fenpei ( )
{
int temp, i;
CountOfNotEmpty += 1 ;
Not_Empty[ CountOfNotEmpty] . yesorno = 0 ;
for ( i = 0 ; i < CountOfNotEmpty; i++ ) {
if ( Not_Empty[ i] . yesorno == 0 ) {
cout << "输入进程ID (0 ~ 255),占用空间大小(0 ~ " << 10 << ")\n" ;
cin >> Not_Empty[ i] . process_id >> Not_Empty[ i] . size_of_notempty;
if ( Not_Empty[ i] . process_id < 0 || Not_Empty[ i] . process_id > 255 )
{
CountOfNotEmpty -= 1 ;
cout << "进程号 0 ~ 255\n" ;
return - 1 ;
}
for ( int ii = 0 ; ii < CountOfNotEmpty; ii++ )
{
if ( Not_Empty[ i] . process_id == Not_Empty[ ii] . process_id && i != ii)
{
CountOfNotEmpty -= 1 ;
cout << "进程号必须唯一\n" ;
return - 1 ;
}
}
temp = enough ( Not_Empty[ i] . size_of_notempty) ;
if ( temp >= 0 )
{
Not_Empty[ i] . address_of_start = Empty[ temp] . address_of_start;
Not_Empty[ i] . yesorno = 1 ;
Empty[ temp] . address_of_start += Not_Empty[ i] . size_of_notempty;
Empty[ temp] . size_of_empty -= Not_Empty[ i] . size_of_notempty;
if ( Empty[ temp] . size_of_empty == 0 ) {
Empty[ temp] . yesorno = 0 ;
}
if ( Empty[ temp] . size_of_empty <= 2 && Empty[ temp] . size_of_empty >= 1 )
Num_of_Holes++ ;
return 0 ;
}
else
{
CountOfNotEmpty -= 1 ;
cout << "未找到足够大的空闲分区\n" ;
return - 1 ;
}
}
}
}
int huishou ( ) {
int id, flag = 0 ;
cout << "输入进程ID (0 ~ 255):\n" ;
cin >> id;
if ( id > 255 || id < 0 ) {
cout << "进程号 0 ~ 255\n" ;
return - 1 ;
}
for ( int i = 0 ; i < CountOfNotEmpty; i++ ) {
if ( Not_Empty[ i] . process_id == id) {
int tmp_start = Not_Empty[ i] . address_of_start;
int tmp_size = Not_Empty[ i] . size_of_notempty;
int tmp_end = tmp_start + tmp_size;
Not_Empty[ i] . process_id = 0 ;
Not_Empty[ i] . address_of_start = 0 ;
Not_Empty[ i] . size_of_notempty = 0 ;
Not_Empty[ i] . yesorno = 0 ;
for ( int j = 0 ; j < CountOfEmpty; j++ ) {
if ( Empty[ j] . yesorno == 1 ) {
if ( tmp_end == Empty[ j] . address_of_start) {
Empty[ j] . address_of_start -= tmp_size;
Empty[ j] . size_of_empty += tmp_size;
flag = 1 ;
break ;
}
else if ( Empty[ j] . address_of_start + Empty[ j] . size_of_empty == tmp_start) {
Empty[ j] . size_of_empty += tmp_size;
if ( j + 1 < CountOfEmpty && Empty[ j + 1 ] . address_of_start == tmp_end) {
Empty[ j] . size_of_empty += Empty[ j + 1 ] . size_of_empty;
Empty[ j + 1 ] . address_of_start = 0 ;
Empty[ j + 1 ] . size_of_empty = 0 ;
Empty[ j + 1 ] . yesorno = 0 ;
sort ( Empty, Empty + CountOfEmpty, cmp2) ;
CountOfEmpty-- ;
sort ( Empty, Empty + CountOfEmpty, cmp) ;
flag = 1 ;
break ;
}
}
}
}
if ( flag == 0 ) {
Empty[ CountOfEmpty] . address_of_start = tmp_start;
Empty[ CountOfEmpty] . size_of_empty = tmp_size;
Empty[ CountOfEmpty] . yesorno = 1 ;
CountOfEmpty += 1 ;
sort ( Empty, Empty + CountOfEmpty, cmp) ;
flag = 1 ;
break ;
}
}
}
if ( flag == 0 ) {
cout << "进程不存在" << endl;
}
return 1 ;
}
int fragment_count ( ) {
return Num_of_Holes;
}
double NumofReaserch ( ) {
int sum = 0 ;
for ( int i = 0 ; i < num; i++ ) {
sum += Time[ i] ;
}
return sum * 1.0 / num;
}
void show_empty ( )
{
int i;
cout << "\n空闲分区列表\n" ;
for ( i = 0 ; i < CountOfEmpty; i++ )
{
if ( Empty[ i] . yesorno == 1 )
{
cout << Empty[ i] . address_of_start << ' ' << Empty[ i] . size_of_empty << endl;
}
}
cout << "\n碎片空闲分区数: " << fragment_count ( ) << endl;
}
void show ( ) {
int k = 0 ;
sort ( Not_Empty, Not_Empty + CountOfNotEmpty, cmp_NotEmpty) ;
for ( int i = 0 ; i < Max_Size; i++ ) {
if ( i % Num_Of_Row == 0 ) {
cout << endl;
}
while ( Not_Empty[ k] . yesorno == 0 ) { k++ ; }
if ( Not_Empty[ k] . address_of_start == i) {
for ( int j = 0 ; j < Not_Empty[ k] . size_of_notempty; j++ ) {
if ( j == 0 ) {
cout << Not_Empty[ k] . process_id;
}
else {
cout << "+" ;
if ( ( i+ j+ 1 ) % Num_Of_Row == 0 && j != Not_Empty[ k] . size_of_notempty- 1 ) {
cout << endl;
}
}
}
i += Not_Empty[ k] . size_of_notempty - 1 ;
k++ ;
}
else {
cout << "-" ;
}
}
}
int main ( )
{
Empty[ 0 ] . address_of_start = 0 ;
Empty[ 0 ] . size_of_empty = Max_Size;
Empty[ 0 ] . yesorno = 1 ;
int i;
cout << "******************Memory Size: " << Max_Size << "KB*******************\n" << endl;
cout << "1 分配; 2 回收;\n3 内存占用情况图; 4 退出\n" ;
cout << "\n请输入你的选择(1~4):\n" ;
while ( cin >> i)
{
switch ( i)
{
case 1 :
if ( fenpei ( ) == 0 )
show ( ) ;
show_empty ( ) ;
cout<< "\n平均搜索空闲区次数: " << NumofReaserch ( ) << endl;
break ;
case 2 : huishou ( ) ;
show ( ) ;
show_empty ( ) ;
break ;
case 3 :
show ( ) ;
break ;
case 4 : return 0 ;
default : break ;
}
cout << "******************Memory Size: " << Max_Size << "KB*******************\n" << endl;
cout << "1 分配; 2 回收;\n3 内存占用情况图; 4 退出\n" ;
cout << "\n请输入你的选择(1~4):\n" ;
}
return 0 ;
}
Next Fit分配法
# include <iostream>
# include <algorithm>
# define M 20
# define N 20
using namespace std;
struct my_empty
{
int address_of_start;
int size_of_empty;
int yesorno;
} Empty[ M] ;
struct not_empty
{
int process_id;
int address_of_start;
int size_of_notempty;
int yesorno;
} Not_Empty[ N] ;
int CountOfEmpty = 1 , CountOfNotEmpty = 0 ;
int Max_Size = 256 ;
int Num_Of_Row = 20 ;
int Num_of_Holes = 0 ;
int Time[ N] ;
int num = 0 ;
int Next_fit_count = 0 ;
bool cmp ( const my_empty& a, const my_empty& b) {
return a. address_of_start < b. address_of_start;
}
bool cmp2 ( const my_empty& a, const my_empty& b) {
return a. address_of_start > b. address_of_start;
}
bool cmp_NotEmpty ( const not_empty& a, const not_empty& b) {
return a. address_of_start < b. address_of_start;
}
bool cmp_NotEmpty2 ( const not_empty& a, const not_empty& b) {
return a. address_of_start > b. address_of_start;
}
int enough ( int Size_Of_NotEmpty)
{
for ( int i = Next_fit_count; i < CountOfEmpty; i++ )
{
if ( Empty[ i] . size_of_empty >= Size_Of_NotEmpty) {
Time[ num] += 1 ;
num++ ;
Next_fit_count = i;
return i;
}
else {
Time[ num] += 1 ;
}
}
for ( int i = 0 ; i < Next_fit_count; i++ )
{
if ( Empty[ i] . size_of_empty >= Size_Of_NotEmpty) {
Time[ num] += 1 ;
num++ ;
return i;
}
else {
Time[ num] += 1 ;
}
}
return - 1 ;
}
int fenpei ( )
{
int temp, i;
CountOfNotEmpty += 1 ;
Not_Empty[ CountOfNotEmpty] . yesorno = 0 ;
for ( i = 0 ; i < CountOfNotEmpty; i++ ) {
if ( Not_Empty[ i] . yesorno == 0 ) {
cout << "输入进程ID (0 ~ 255),占用空间大小(0 ~ " << 10 << ")\n" ;
cin >> Not_Empty[ i] . process_id >> Not_Empty[ i] . size_of_notempty;
if ( Not_Empty[ i] . process_id < 0 || Not_Empty[ i] . process_id > 255 )
{
CountOfNotEmpty -= 1 ;
cout << "进程号 0 ~ 255\n" ;
return - 1 ;
}
for ( int ii = 0 ; ii < CountOfNotEmpty; ii++ )
{
if ( Not_Empty[ i] . process_id == Not_Empty[ ii] . process_id && i != ii)
{
CountOfNotEmpty -= 1 ;
cout << "进程号必须唯一\n" ;
return - 1 ;
}
}
temp = enough ( Not_Empty[ i] . size_of_notempty) ;
if ( temp >= 0 )
{
Not_Empty[ i] . address_of_start = Empty[ temp] . address_of_start;
Not_Empty[ i] . yesorno = 1 ;
Empty[ temp] . address_of_start += Not_Empty[ i] . size_of_notempty;
Empty[ temp] . size_of_empty -= Not_Empty[ i] . size_of_notempty;
if ( Empty[ temp] . size_of_empty == 0 ) {
Empty[ temp] . yesorno = 0 ;
}
if ( Empty[ temp] . size_of_empty <= 2 && Empty[ temp] . size_of_empty >= 1 )
Num_of_Holes++ ;
return 0 ;
}
else
{
CountOfNotEmpty -= 1 ;
cout << "未找到足够大的空闲分区\n" ;
return - 1 ;
}
}
}
}
int huishou ( ) {
int id, flag = 0 ;
cout << "输入进程ID (0 ~ 255):\n" ;
cin >> id;
if ( id > 255 || id < 0 ) {
cout << "进程号 0 ~ 255\n" ;
return - 1 ;
}
for ( int i = 0 ; i < CountOfNotEmpty; i++ ) {
if ( Not_Empty[ i] . process_id == id) {
int tmp_start = Not_Empty[ i] . address_of_start;
int tmp_size = Not_Empty[ i] . size_of_notempty;
int tmp_end = tmp_start + tmp_size;
Not_Empty[ i] . process_id = 0 ;
Not_Empty[ i] . address_of_start = 0 ;
Not_Empty[ i] . size_of_notempty = 0 ;
Not_Empty[ i] . yesorno = 0 ;
for ( int j = 0 ; j < CountOfEmpty; j++ ) {
if ( Empty[ j] . yesorno == 1 ) {
if ( tmp_end == Empty[ j] . address_of_start) {
Empty[ j] . address_of_start -= tmp_size;
Empty[ j] . size_of_empty += tmp_size;
flag = 1 ;
break ;
}
else if ( Empty[ j] . address_of_start + Empty[ j] . size_of_empty == tmp_start) {
Empty[ j] . size_of_empty += tmp_size;
if ( j + 1 < CountOfEmpty && Empty[ j + 1 ] . address_of_start == tmp_end) {
Empty[ j] . size_of_empty += Empty[ j + 1 ] . size_of_empty;
Empty[ j + 1 ] . address_of_start = 0 ;
Empty[ j + 1 ] . size_of_empty = 0 ;
Empty[ j + 1 ] . yesorno = 0 ;
sort ( Empty, Empty + CountOfEmpty, cmp2) ;
CountOfEmpty-- ;
sort ( Empty, Empty + CountOfEmpty, cmp) ;
flag = 1 ;
break ;
}
}
}
}
if ( flag == 0 ) {
Empty[ CountOfEmpty] . address_of_start = tmp_start;
Empty[ CountOfEmpty] . size_of_empty = tmp_size;
Empty[ CountOfEmpty] . yesorno = 1 ;
CountOfEmpty += 1 ;
sort ( Empty, Empty + CountOfEmpty, cmp) ;
flag = 1 ;
break ;
}
}
}
if ( flag == 0 ) {
cout << "进程不存在" << endl;
}
return 1 ;
}
int fragment_count ( ) {
return Num_of_Holes;
}
double NumofReaserch ( ) {
int sum = 0 ;
for ( int i = 0 ; i < num; i++ ) {
sum += Time[ i] ;
}
return sum * 1.0 / num;
}
void show_empty ( )
{
int i;
cout << "\n空闲分区列表\n" ;
for ( i = 0 ; i < CountOfEmpty; i++ )
{
if ( Empty[ i] . yesorno == 1 )
{
cout << Empty[ i] . address_of_start << ' ' << Empty[ i] . size_of_empty << endl;
}
}
cout << "\n碎片空闲分区数: " << fragment_count ( ) << endl;
}
void show ( ) {
int k = 0 ;
sort ( Not_Empty, Not_Empty + CountOfNotEmpty, cmp_NotEmpty) ;
for ( int i = 0 ; i < Max_Size; i++ ) {
if ( i % Num_Of_Row == 0 ) {
cout << endl;
}
while ( Not_Empty[ k] . yesorno == 0 ) { k++ ; }
if ( Not_Empty[ k] . address_of_start == i) {
for ( int j = 0 ; j < Not_Empty[ k] . size_of_notempty; j++ ) {
if ( j == 0 ) {
cout << Not_Empty[ k] . process_id;
}
else {
cout << "+" ;
if ( ( i+ j+ 1 ) % Num_Of_Row == 0 && j != Not_Empty[ k] . size_of_notempty- 1 ) {
cout << endl;
}
}
}
i += Not_Empty[ k] . size_of_notempty - 1 ;
k++ ;
}
else {
cout << "-" ;
}
}
}
int main ( )
{
Empty[ 0 ] . address_of_start = 0 ;
Empty[ 0 ] . size_of_empty = Max_Size;
Empty[ 0 ] . yesorno = 1 ;
int i;
cout << "******************Memory Size: " << Max_Size << "KB*******************\n" << endl;
cout << "1 分配; 2 回收;\n3 内存占用情况图; 4 退出\n" ;
cout << "\n请输入你的选择(1~4):\n" ;
while ( cin >> i)
{
switch ( i)
{
case 1 :
if ( fenpei ( ) == 0 )
show ( ) ;
show_empty ( ) ;
cout<< "\n平均搜索空闲区次数: " << NumofReaserch ( ) << endl;
break ;
case 2 : huishou ( ) ;
show ( ) ;
show_empty ( ) ;
break ;
case 3 :
show ( ) ;
break ;
case 4 : return 0 ;
default : break ;
}
cout << "******************Memory Size: " << Max_Size << "KB*******************\n" << endl;
cout << "1 分配; 2 回收;\n3 内存占用情况图; 4 退出\n" ;
cout << "\n请输入你的选择(1~4):\n" ;
}
return 0 ;
}