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     */
013    package org.jikesrvm.compilers.opt.instrsched;
014    
015    /**
016     * Describes a reservation on a particular resource.
017     * A reservation is for a continuous period of time.
018     * Used in OperatorClass.
019     *
020     * @see OperatorClass
021     */
022    final class ResourceReservation {
023      /**
024       * Resource Class.
025       */
026      private final int rclass;
027      /**
028       * Start Time.
029       */
030      final int start;
031      /**
032       * Duration of Use.
033       */
034      final int duration;
035    
036      /**
037       * Creates a new reservation for specified resource class
038       * starting at 0 with given duration.
039       *
040       * @param rclass resource class
041       * @param duration duration
042       * @see #ResourceReservation(int,int,int)
043       */
044      public ResourceReservation(int rclass, int duration) {
045        this(rclass, 0, duration);
046      }
047    
048      /**
049       * Creates a new reservation for specified resource class
050       * starting at specified time with given duration.
051       *
052       * @param rclass resource class
053       * @param start start time
054       * @param duration duration
055       */
056      public ResourceReservation(int rclass, int start, int duration) {
057        this.rclass = rclass;
058        this.start = start;
059        this.duration = duration;
060      }
061    
062      /**
063       * The resource class of this reservation.
064       *
065       * @return resource class of this reservation
066       */
067      public int rclass() {
068        return (rclass & 0x7FFFFFFF);
069      }
070    
071      /**
072       * Checks whether this reservation is for all available units of the class.
073       *
074       * @return true if it's a global reservation, false otherwise
075       */
076      public boolean isGlobal() {
077        return (rclass & 0x80000000) != 0;
078      }
079    
080      /**
081       * Compares this reservation with another reservation.
082       * @param r the other reservation
083       * @return a negative int if this is less than the other reservation,
084       *  a positive int if this is greater than the other and zero if
085       *  they are equal
086       */
087      private int compareTo(ResourceReservation r) {
088        if (rclass() != r.rclass()) {
089          return rclass() - r.rclass();
090        }
091        if (rclass != r.rclass) {
092          // if either of them is global then global goes first
093          return r.rclass - rclass;
094        }
095        if (start != r.start) {
096          return start - r.start;
097        }
098        return duration - r.duration;
099      }
100    
101      /**
102       * Sorts an array of reservations in accordance with internal ordering.
103       *
104       * @param usage array of reservations
105       */
106      public static void sort(ResourceReservation[] usage) {
107        // bubble sort
108        for (int i = 0; i < usage.length; i++) {
109          for (int j = i; j > 0 && usage[j - 1].compareTo(usage[j]) > 0; j--) {
110            ResourceReservation t = usage[j];
111            usage[j] = usage[j - 1];
112            usage[j - 1] = t;
113          }
114        }
115      }
116    
117      /**
118       * Compares this object against the specified object.
119       *
120       * @param o   The object to compare with
121       * @return <code>true</code> if the objects are the same; <code>false</code> otherwise.
122       */
123      @Override
124      public boolean equals(Object o) {
125        if (!(o instanceof ResourceReservation)) {
126          return false;
127        }
128        return compareTo((ResourceReservation) o) == 0;
129      }
130    
131      /**
132       * Checks whether this reservation conflicts with specified reservation.
133       *
134       * @param rsrv the reservation to check
135       * @return {@code true} if the reservations conflict; {@code false} otherwise.
136       */
137      public boolean conflicts(ResourceReservation rsrv) {
138        return (rclass() == rsrv.rclass() && start < rsrv.start + rsrv.duration && start + duration > rsrv.start);
139      }
140    }
141    
142    
143