View Javadoc

1   package com.tapina.robe.swi.clib.stdlib;
2   
3   import com.tapina.robe.runtime.Environment;
4   import com.tapina.robe.runtime.MemoryMap;
5   import com.tapina.robe.runtime.RawDataBlock;
6   import com.tapina.robe.runtime.DataBlock;
7   import com.tapina.robe.swi.clib.Stub;
8   
9   /***
10   * This function changes the size of a space initially allocated with calloc , malloc , or realloc. It takes a
11   * pointer to the space in a1, and a new size in a2. The contents of the space are unaltered (although they
12   * may be chopped off the new size is smaller). Any new bytes allocated have undefined values. The space may be
13   * moved rather than just extended and so this function returns a new pointer.
14   * If a1 is NULL then this function behaves as malloc , else if a2 is zero then this function behaves as free.
15   * If the space cannot be reallocated then the contents are unchanged. However, the function will return NULL so
16   * you will not be able to access the memory using the original pointer.
17   * Returns: Pointer to space, or NULL if failed.
18   */
19  public class ReAlloc extends Stub {
20      public void executeStub(Environment environment) {
21          final int[] R = environment.getCpu().R;
22          final MemoryMap memoryMap = environment.getMemoryMap();
23          final DataBlock oldBlock = R[0] == 0? null : memoryMap.getDataBlock(R[0]);
24          if (oldBlock != null) {
25              if (R[1] != 0) {
26                  memoryMap.resizeSystemDataBlock(oldBlock, R[1]);
27                  R[0] = oldBlock.getAddress();
28              } else {
29                  memoryMap.removeSystemDataBlock(oldBlock);
30                  R[0] = 0;
31              }
32          } else {
33              R[0] = memoryMap.createSystemDataBlock(R[1]).getAddress();
34          }
35      }
36  }