/*
 * (C) Copyright Department of Computer Science,
 *     Australian National University. 2002
 */
package s3.core.services.memory.JMTk;

import s3.core.services.memory.vmInterface.Constants;
import ovm.core.services.memory.VM_Address;

/**
 * This supports <i>unsynchronized</i> enqueuing and dequeuing of addresses
 *
 * @author <a href="http://cs.anu.edu.au/~Steve.Blackburn">Steve Blackburn</a>
 * @version $Revision: 1.2 $
 * @date $Date: 2003/12/04 16:44:05 $
 */ 

import s3.core.services.memory.vmInterface.VM_Interface;

public class AddressDeque extends LocalDeque implements Constants, VM_Uninterruptible {
   public final static String Id = "$Id: AddressDeque.java,v 1.2 2003/12/04 16:44:05 flack Exp $"; 
 
  /****************************************************************************
   *
   * Public instance methods
   */
  public final String name;

  /**
   * Constructor
   *
   * @param queue The shared queue to which this queue will append
   * its buffers (when full or flushed) and from which it will aquire new
   * buffers when it has exhausted its own.
   */
  AddressDeque(String n, SharedDeque queue) {
    super(queue);
    name = n;
  }

  /**
   * Insert an address into the address queue.
   *
   * @param addr the address to be inserted into the address queue*
   */
  public final void insert(VM_Address addr) throws VM_PragmaInline {
    if (VM_Interface.VerifyAssertions) VM_Interface._assert(!addr.isNull());
    checkInsert(1);
    uncheckedInsert(addr);
  }

  /**
   * Push an address onto the address queue.
   *
   * @param addr the address to be pushed onto the address queue
   */
  public final void push(VM_Address addr) throws VM_PragmaInline {
    if (VM_Interface.VerifyAssertions) VM_Interface._assert(!addr.isNull());
    checkPush(1);
    uncheckedPush(addr);
  }

  /**
   * Push an address onto the address queue, force this out of line
   * ("OOL"), in some circumstnaces it is too expensive to have the
   * push inlined, so this call is made.
   *
   * @param addr the address to be pushed onto the address queue
   */
  public final void pushOOL(VM_Address addr) throws VM_PragmaNoInline {
    push(addr);
  }

  /**
   * Pop an address from the address queue, return zero if the queue
   * is empty.
   *
   * @return The next address in the address queue, or zero if the
   * queue is empty
   */
  public final VM_Address pop() throws VM_PragmaInline {
    if (checkPop(1)) {
      return uncheckedPop();
    }
    else {
      return VM_Address.fromInt(0);
    }
  }

  public final boolean isEmpty() throws VM_PragmaInline {
    return !checkPop(1);
  }

  public final boolean isNonEmpty() throws VM_PragmaInline {
    return checkPop(1);
  }

}
