001    /*
002     *  This file is part of the Jikes RVM project (http://jikesrvm.org).
003     *
004     *  This file is licensed to You under the Eclipse Public License (EPL);
005     *  You may not use this file except in compliance with the License. You
006     *  may obtain a copy of the License at
007     *
008     *      http://www.opensource.org/licenses/eclipse-1.0.php
009     *
010     *  See the COPYRIGHT.txt file distributed with this work for information
011     *  regarding copyright ownership.
012     */
013    package org.mmtk.utility.deque;
014    
015    import org.mmtk.utility.Constants;
016    
017    import org.mmtk.vm.VM;
018    
019    import org.vmmagic.unboxed.*;
020    import org.vmmagic.pragma.*;
021    
022    /**
023     * This supports <i>unsynchronized</i> pushing and popping of addresses.
024     * In addition, this can sort the entries currently on the shared stack.
025     */
026    @Uninterruptible public class SortTODAddressStack extends LocalDeque
027      implements Constants {
028    
029      /****************************************************************************
030       *
031       * Public instance methods
032       */
033    
034      /**
035       * Constructor
036       *
037       * @param queue The shared stack to which this stack will append
038       * its buffers (when full or flushed) and from which it will acquire new
039       * buffers when it has exhausted its own.
040       */
041      public SortTODAddressStack(SortTODSharedDeque queue) {
042        super(queue);
043      }
044    
045      /**
046       * Sort the address on the shared stack.
047       */
048      public final void sort() {
049        flushLocal();
050        ((SortTODSharedDeque) queue).sort();
051      }
052    
053      /**
054       * Push an address onto the address stack.
055       *
056       * @param addr the address to be pushed onto the address queue
057       */
058      @Inline
059      public final void push(Address addr) {
060        if (VM.VERIFY_ASSERTIONS) VM.assertions._assert(!addr.isZero());
061        checkHeadInsert(1);
062        uncheckedHeadInsert(addr);
063      }
064    
065      /**
066       * Pop an address from the address stack, return zero if the stack
067       * is empty.
068       *
069       * @return The next address in the address stack, or zero if the
070       * stack is empty
071       */
072      @Inline
073      public final Address pop() {
074        if (checkDequeue(1)) {
075          return uncheckedDequeue();
076        } else {
077          return Address.zero();
078        }
079      }
080    
081      /**
082       * Check if the (local and shared) stacks are empty.
083       *
084       * @return True if there are no more entries on the local & shared stack,
085       *         false otherwise.
086       */
087      @Inline
088      public final boolean isEmpty() {
089        return !checkDequeue(1);
090      }
091    }