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