操作系统 动态分区分配 Java 实现
1. 分配算法原理
操作系统 动态分区分配
2. 代码实现
package dynamicMemoAlloc ;
class Block {
private int id;
private int begin;
private int end;
private int size;
private Block next;
public Block ( int id, int begin, int size) {
this . id = id;
this . begin = begin;
this . end = begin + size - 1 ;
this . size = size;
this . next = null ;
}
public int getId ( ) {
return id;
}
public int getBegin ( ) {
return begin;
}
public int getEnd ( ) {
return end;
}
public int getSize ( ) {
return size;
}
public Block getNext ( ) {
return next;
}
public void setId ( int id) {
this . id = id;
}
public void setBegin ( int begin) {
this . begin = begin;
this . size = this . end - begin + 1 ;
}
public void setEnd ( int end) {
this . end = end;
this . size = end - this . begin + 1 ;
}
public void setSize ( int size) {
this . size = size;
}
public void setNext ( Block next) {
this . next = next;
}
@Override
public String toString ( ) {
return "Block{" +
"id=" + ( id+ 1 ) +
", begin=" + begin +
", end=" + end +
", size=" + size +
'}' ;
}
}
public class Memory {
private int memoSize;
private Block head;
private int idCounter;
public Memory ( int memoSize) {
this . memoSize = memoSize;
this . head = new Block ( - 2 , 0 , 0 ) ;
Block b = new Block ( - 1 , 0 , memoSize) ;
this . head. setNext ( b) ;
this . idCounter = - 1 ;
}
public void allocByFF ( int size) {
Block b = head. getNext ( ) ;
Block last = head;
while ( b != null ) {
if ( b. getId ( ) == - 1 && b. getSize ( ) >= size) {
Block all = new Block ( ++ idCounter, b. getBegin ( ) , size) ;
b. setBegin ( all. getEnd ( ) + 1 ) ;
last. setNext ( all) ;
all. setNext ( b) ;
break ;
}
last = b;
b = b. getNext ( ) ;
}
}
public void allocByBF ( int size) {
int min = memoSize;
int counter = 0 ;
Block tarBefore = null ;
Block b = head. getNext ( ) ;
Block last = head;
while ( b != null ) {
if ( b. getId ( ) == - 1 && b. getSize ( ) >= size ) {
if ( min > size)
tarBefore = last;
}
last = b;
b = b. getNext ( ) ;
}
Block target = tarBefore. getNext ( ) ;
Block all = new Block ( ++ idCounter, target. getBegin ( ) , size) ;
target. setBegin ( all. getEnd ( ) + 1 ) ;
tarBefore. setNext ( all) ;
all. setNext ( target) ;
}
public void free ( int size) {
Block b = head. getNext ( ) ;
Block last = head;
while ( b != null ) {
if ( b. getSize ( ) == size) {
b. setId ( - 1 ) ;
Block next = b. getNext ( ) ;
if ( last. getId ( ) == - 1 && next. getId ( ) == - 1 ) {
last. setEnd ( next. getEnd ( ) ) ;
last. setNext ( next. getNext ( ) ) ;
next. setNext ( null ) ;
} else {
if ( last. getId ( ) == - 1 ) {
last. setEnd ( b. getEnd ( ) ) ;
last. setNext ( b. getNext ( ) ) ;
}
if ( next. getId ( ) == - 1 ) {
next. setBegin ( b. getBegin ( ) ) ;
last. setNext ( next) ;
b. setNext ( null ) ;
}
}
break ;
}
last = b;
b = b. getNext ( ) ;
}
}
public void showMemoInfo ( String info) {
System . out. println ( "-----------------------------------------" ) ;
System . out. println ( info + ": " ) ;
Block b = head. getNext ( ) ;
while ( b != null ) {
System . out. println ( b) ;
b = b. getNext ( ) ;
}
}
public static void main ( String [ ] args) {
Memory memo = new Memory ( 640 ) ;
memo. allocByFF ( 130 ) ;
memo. allocByFF ( 60 ) ;
memo. allocByFF ( 100 ) ;
memo. free ( 60 ) ;
memo. allocByFF ( 200 ) ;
memo. free ( 100 ) ;
memo. free ( 130 ) ;
memo. allocByFF ( 140 ) ;
memo. allocByFF ( 60 ) ;
memo. allocByFF ( 50 ) ;
memo. free ( 60 ) ;
memo. showMemoInfo ( "FF" ) ;
Memory memo1 = new Memory ( 640 ) ;
memo1. allocByBF ( 130 ) ;
memo1. allocByBF ( 60 ) ;
memo1. allocByBF ( 100 ) ;
memo1. free ( 60 ) ;
memo1. allocByBF ( 200 ) ;
memo1. free ( 100 ) ;
memo1. free ( 130 ) ;
memo1. allocByBF ( 140 ) ;
memo1. allocByBF ( 60 ) ;
memo1. allocByBF ( 50 ) ;
memo1. free ( 60 ) ;
memo1. showMemoInfo ( "BF" ) ;
}
}