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.heap;
014
015import static org.mmtk.utility.Constants.*;
016
017import org.vmmagic.unboxed.*;
018
019/**
020 * This class manages the encoding and decoding of virtual memory requests.<p>
021 *
022 * By encapsulating this aspect of the construction of a space, we greatly
023 * reduce the number of constructors required.
024 */
025public final class VMRequest {
026
027  public static final int REQUEST_DISCONTIGUOUS = 0;
028  public static final int REQUEST_FIXED = 1;
029  public static final int REQUEST_EXTENT = 3;
030  public static final int REQUEST_FRACTION = 4;
031
032  public final int type;
033  public final Address start;
034  public final Extent extent;
035  public final float frac;
036  public final boolean top;
037
038  private VMRequest(int type, Address start, Extent bytes, float frac, boolean top) {
039    this.type = type;
040    this.start = start;
041    this.extent = bytes;
042    this.frac = frac;
043    this.top = top;
044  }
045
046  /**
047   * Is this a discontiguous space request?
048   * @return true if this is a discontiguous space request, false otherwise
049   */
050  public boolean isDiscontiguous() {
051    return type == REQUEST_DISCONTIGUOUS;
052  }
053
054  /**
055   * A request for a discontiguous region of memory
056   *
057   * @return The request object
058   */
059  public static VMRequest discontiguous() {
060    return new VMRequest(REQUEST_DISCONTIGUOUS, Address.zero(), Extent.zero(), 0f, false);
061  }
062
063  /**
064   * A request for an explicit region of memory
065   *
066   * @param start The start of the region
067   * @param extent The size of the region
068   * @return The request object
069   */
070  public static VMRequest fixed(Address start, Extent extent) {
071    return new VMRequest(REQUEST_FIXED, start, extent, 0f, false);
072  }
073
074  /**
075   * A request for a number of megabytes of memory
076   *
077   * @param mb The number of megabytes
078   * @return The request object
079   */
080  public static VMRequest fixedSize(int mb) {
081    return new VMRequest(REQUEST_EXTENT, Address.zero(), Word.fromIntSignExtend(mb).lsh(LOG_BYTES_IN_MBYTE).toExtent(), 0f, false);
082  }
083
084  /**
085   * A request for a fraction of available memory
086   *
087   * @param frac The fraction
088   * @return The request object
089   */
090  public static VMRequest fraction(float frac) {
091    return new VMRequest(REQUEST_FRACTION, Address.zero(), Extent.zero(), frac, false);
092  }
093
094  /**
095   * A request for a number of megabytes of memory at the highest available addresses
096   *
097   * @param mb The number of megabytes
098   * @return The request object
099   */
100  public static VMRequest highFixedSize(int mb) {
101    return new VMRequest(REQUEST_EXTENT, Address.zero(), Word.fromIntSignExtend(mb).lsh(LOG_BYTES_IN_MBYTE).toExtent(), 0f, true);
102  }
103
104  /**
105   * A request for a fraction of available memory, optionally requesting the highest available.
106   *
107   * @param frac The fraction
108   * @return The request object
109   */
110  public static VMRequest highFraction(float frac) {
111    return new VMRequest(REQUEST_FRACTION, Address.zero(), Extent.zero(), frac, true);
112  }
113
114  /**
115   * A request for a number of bytes of memory, optionally requesting the highest available.
116   *
117   * @param extent The number of bytes
118   * @param top True to request high memory
119   * @return The request object
120   */
121  public static VMRequest fixedExtent(Extent extent, boolean top) {
122    return new VMRequest(REQUEST_EXTENT, Address.zero(), extent, 0f, top);
123  }
124}