import java.util.*; import java.io.*; public class AllocatedMemory { public int [ ] memory; // the memory pool private int poolSize; // size of the memory pool private int firstFreeBlock; // index of header info for first free block private boolean DEBUGGING = true; private int HEADERSIZE = 3; private int TRAILERSIZE = 1; public static void main (String [ ] args) { BufferedReader keyboard = new BufferedReader (new InputStreamReader (System.in)); AllocatedMemory storage = new AllocatedMemory (20); while (true) { // Read commands of the form "a #" (in order to allocate // the given number of units), or "f ptr" (in order to free // the space pointed to by the given "pointer". // A "pointer" here is either 0 (representing null) // or a pool array index. System.out.println ( ); System.out.print ("Command? "); String line; try { line = keyboard.readLine ( ); } catch (Exception e) { e.printStackTrace ( ); break; } StringTokenizer tokens = new StringTokenizer (line); if (tokens.countTokens ( ) == 0) { System.out.println (line + " is empty."); System.out.println ("Commands are either 'q', 'a #', or 'f ptr'."); continue; } String cmd = tokens.nextToken ( ); if (cmd.equals ("q")) { return; } if (!tokens.hasMoreElements ( )) { System.out.println (line + " has no argument."); System.out.println ("Commands are either 'q', 'a #', or 'f ptr'."); continue; } int arg; try { arg = Integer.parseInt (tokens.nextToken ( )); } catch (Exception e) { System.out.println (line + " has an illegal command."); System.out.println ("Commands are either 'q', 'a #', or 'f ptr'."); continue; } if (cmd.equals ("a")) { int ptr = storage.allocate (arg); if (ptr == 0) { System.out.println ("CanŐt allocate!"); } else { // Write a bunch of identifying stuff to the memory just acquired. for (int k=ptr; kpoolSize) { System.out.println ("*** Invalid prev!"); } if (next(b)<0 || next(b)>poolSize) { System.out.println ("*** Invalid next!"); } } } // Return the absolute value of the argument. private int abs (int k) { return k > 0? k: -k; } }