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.vmutil.options;
014
015import org.vmmagic.pragma.Uninterruptible;
016import org.vmmagic.unboxed.Extent;
017
018/**
019 * A memory option that stores values as a whole number of pages.
020 */
021public class PagesOption extends Option {
022  // values
023  protected int defaultValue;
024  protected int value;
025
026  /**
027   * Create a new pages option.
028   *
029   * @param set The option set this option belongs to.
030   * @param name The space separated name for the option.
031   * @param desc The purpose of the option
032   * @param defaultPages The default value of the option.
033   */
034  protected PagesOption(OptionSet set, String name, String desc, int defaultPages) {
035    super(set, PAGES_OPTION, name, desc);
036    this.value = this.defaultValue = defaultPages;
037  }
038
039  /**
040   * Read the current value of the option in pages.
041   *
042   * @return The option value.
043   */
044  @Uninterruptible
045  public int getPages() {
046    return this.value;
047  }
048
049  /**
050   * Read the current value of the option in bytes.
051   *
052   * @return The option value.
053   */
054  @Uninterruptible
055  public Extent getBytes() {
056    return set.pagesToBytes(this.value);
057  }
058
059  /**
060   * Read the default value of the option in bytes.
061   *
062   * @return The default value.
063   */
064  @Uninterruptible
065  public Extent getDefaultBytes() {
066    return set.pagesToBytes(this.defaultValue);
067  }
068
069  /**
070   * Read the default value of the option in pages.
071   *
072   * @return The default value.
073   */
074  @Uninterruptible
075  public int getDefaultPages() {
076    return this.defaultValue;
077  }
078
079  /**
080   * Update the value of the option, echoing the change if logChanges is set.
081   * A warning is raised if the value is not a whole multiple of pages, and
082   * then the validate method is called to allow subclasses to perform any
083   * additional validation.
084   *
085   * @param value The new value for the option.
086   */
087  public void setBytes(Extent value) {
088    int pages = set.bytesToPages(value);
089    warnIf(value.NE(set.pagesToBytes(pages)), "Value rounded up to a whole number of pages");
090    setPages(pages);
091  }
092
093  /**
094   * Update the value of the option, echoing the change if logChanges is set.
095   * The validate method is called to allow subclasses to perform any additional
096   * validation.
097   *
098   * @param pages The new value for the option.
099   */
100  public void setPages(int pages) {
101    this.value = pages;
102    validate();
103    set.logChange(this);
104  }
105
106  /**
107   * Modify the default value of the option.
108   *
109   * @param value The new default value for the option.
110   */
111  public void setDefaultPages(int value) {
112    this.value = this.defaultValue = value;
113  }
114}