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 */
013package org.mmtk.utility.deque;
014
015import org.mmtk.vm.VM;
016
017import org.vmmagic.unboxed.*;
018import org.vmmagic.pragma.*;
019
020/**
021 * This supports <i>unsynchronized</i> pushing and popping of addresses.
022 * In addition, this can sort the entries currently on the shared stack.
023 */
024@Uninterruptible public class SortTODAddressStack extends LocalDeque {
025
026  /****************************************************************************
027   *
028   * Public instance methods
029   */
030
031  /**
032   * Constructor
033   *
034   * @param queue The shared stack to which this stack will append
035   * its buffers (when full or flushed) and from which it will acquire new
036   * buffers when it has exhausted its own.
037   */
038  public SortTODAddressStack(SortTODSharedDeque queue) {
039    super(queue);
040  }
041
042  /**
043   * Sort the address on the shared stack.
044   */
045  public final void sort() {
046    flushLocal();
047    ((SortTODSharedDeque) queue).sort();
048  }
049
050  /**
051   * Push an address onto the address stack.
052   *
053   * @param addr the address to be pushed onto the address queue
054   */
055  @Inline
056  public final void push(Address addr) {
057    if (VM.VERIFY_ASSERTIONS) VM.assertions._assert(!addr.isZero());
058    checkHeadInsert(1);
059    uncheckedHeadInsert(addr);
060  }
061
062  /**
063   * Pop an address from the address stack, return zero if the stack
064   * is empty.
065   *
066   * @return The next address in the address stack, or zero if the
067   * stack is empty
068   */
069  @Inline
070  public final Address pop() {
071    if (checkDequeue(1)) {
072      return uncheckedDequeue();
073    } else {
074      return Address.zero();
075    }
076  }
077
078  /**
079   * Check if the (local and shared) stacks are empty.
080   *
081   * @return True if there are no more entries on the local &amp; shared stack,
082   *         false otherwise.
083   */
084  @Inline
085  public final boolean isEmpty() {
086    return !checkDequeue(1);
087  }
088}