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.jikesrvm.classloader;
014
015import org.jikesrvm.mm.mminterface.MemoryManager;
016
017import org.jikesrvm.VM;
018
019/**
020 * The manager of specialized methods.
021 */
022public final class SpecializedMethodManager {
023  /** The number of specialized methods. Currently the MM is the only consumer. */
024  private static final int numSpecializedMethods = MemoryManager.numSpecializedMethods();
025
026  /** All the specialized methods */
027  private static final SpecializedMethod[] methods = new SpecializedMethod[numSpecializedMethods];
028
029  /** @return the number of specialized methods */
030  public static int numSpecializedMethods() {
031    return numSpecializedMethods;
032  }
033
034  /**
035   * Sets up the specialized methods for the given type.
036   * @param type the type that was instantiated
037   */
038  public static void notifyTypeInstantiated(RVMType type) {
039    for (int i = 0; i < numSpecializedMethods; i++) {
040      if (methods[i] == null) {
041        initializeSpecializedMethod(i);
042      }
043      type.setSpecializedMethod(i, methods[i].specializeMethod(type));
044    }
045  }
046
047  /**
048   * Refreshes the specialized methods for the given type.
049   * @param type the type whose methods need to be refreshed
050   */
051  public static void refreshSpecializedMethods(RVMType type) {
052    for (int i = 0; i < numSpecializedMethods; i++) {
053      if (VM.VerifyAssertions) VM._assert(methods[i] != null, "Specialized method missing!");
054      type.setSpecializedMethod(i, methods[i].specializeMethod(type));
055    }
056  }
057
058  /**
059   * Initializes a specialized method with a given id. No specialized
060   * method with this id may exist at this point.
061   * @param id id of the specialized
062   */
063  private static void initializeSpecializedMethod(int id) {
064    if (VM.VerifyAssertions) VM._assert(id >= 0);
065    if (VM.VerifyAssertions) VM._assert(id < numSpecializedMethods);
066    if (VM.VerifyAssertions) VM._assert(methods[id] == null);
067    methods[id] = MemoryManager.createSpecializedMethod(id);
068  }
069
070  /** Can not create an instance of the manager */
071  private SpecializedMethodManager() {}
072}