View Javadoc

1   package com.tapina.robe.module;
2   
3   import com.tapina.robe.runtime.DataBlock;
4   import com.tapina.robe.runtime.RawDataBlock;
5   import com.tapina.robe.swi.SWIError;
6   
7   import java.util.TreeMap;
8   import java.util.SortedMap;
9   import java.util.Iterator;
10  
11  /***
12   * This class represents the relocatable module area, an area of memory which is shared by all processes and can
13   * have Modules loaded into it, as well as other memory claimed.
14   */
15  public class RelocatableModuleArea {
16      public static final int RMA_BASE = 0x2800000;
17      public static final int RMA_TOP = 0x37fffff;
18  
19      private final SortedMap dataMap = new TreeMap();
20  
21      public final Iterator iterator() {
22          return dataMap.values().iterator();
23      }
24  
25      public static boolean isRmaAddress(int address) {
26          return address >= RelocatableModuleArea.RMA_BASE && address <= RMA_TOP;
27      }
28  
29      public DataBlock claimBlock(int length) {
30          final int address;
31          if (!dataMap.containsKey(new Integer(RMA_BASE))) {
32              address = RMA_BASE;
33          } else {
34              final DataBlock lastBlock = (DataBlock) dataMap.get(dataMap.lastKey());
35              address = (lastBlock.getAddress() + lastBlock.getSize() + 3) & ~3;
36          }
37          final RawDataBlock dataBlock = new RawDataBlock(address, length);
38          dataMap.put(new Integer(address), dataBlock);
39          return dataBlock;
40      }
41  
42      public void freeBlock(int address) {
43          final Integer key = new Integer(address);
44          if (!dataMap.containsKey(key)) {
45              throw new SWIError(0, "RMA block not found");
46          }
47          dataMap.remove(key);
48      }
49  }