|
| 1 | +// The contents of this file are subject to the Mozilla Public License |
| 2 | +// Version 1.1 (the "License"); you may not use this file except in |
| 3 | +// compliance with the License. You may obtain a copy of the License |
| 4 | +// at http://www.mozilla.org/MPL/ |
| 5 | +// |
| 6 | +// Software distributed under the License is distributed on an "AS IS" |
| 7 | +// basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See |
| 8 | +// the License for the specific language governing rights and |
| 9 | +// limitations under the License. |
| 10 | +// |
| 11 | +// The Original Code is RabbitMQ. |
| 12 | +// |
| 13 | +// The Initial Developer of the Original Code is VMware, Inc. |
| 14 | +// Copyright (c) 2007-2011 VMware, Inc. All rights reserved. |
| 15 | +// |
| 16 | + |
| 17 | +package com.rabbitmq.utility; |
| 18 | + |
| 19 | +import java.util.BitSet; |
| 20 | + |
| 21 | +/** |
| 22 | + * A class for allocating integers from a given range that uses a |
| 23 | + * {@link BitSet} representation of the free integers. |
| 24 | + * |
| 25 | + * <p/><strong>Concurrent Semantics:</strong><br /> |
| 26 | + * This class is <b><i>not</i></b> thread safe. |
| 27 | + * |
| 28 | + * <p/><b>Implementation notes:</b> |
| 29 | + * <br/>This was originally an ordered chain of non-overlapping Intervals, |
| 30 | + * together with a fixed size array cache for freed integers. |
| 31 | + * <br/>{@link #reserve()} was expensive in this scheme, whereas in the |
| 32 | + * present implementation it is O(1), as is {@link #free()}. |
| 33 | + * <br/>Although {@link #allocate()} is slightly slower than O(1) and in the |
| 34 | + * worst case could be O(N), the use of the {@link #lastIndex} field |
| 35 | + * for starting the next scan for free integers means this is negligible. |
| 36 | + * <br/>The data representation overhead is O(N) where N is the size of the |
| 37 | + * allocation range. One <code>long</code> is used for every 64 integers in the |
| 38 | + * range. |
| 39 | + * <br/>Very little Object creation and destruction occurs in use. |
| 40 | + */ |
| 41 | +public class IntBitSetAllocator { |
| 42 | + |
| 43 | + private final int loRange; // the integer bit 0 represents |
| 44 | + private final int hiRange; // the integer(+1) the highest bit represents |
| 45 | + private final int numberOfBits; // relevant in freeSet |
| 46 | + private int lastIndex = 0; // for searching for FREE integers |
| 47 | + /** A bit is SET in freeSet if the corresponding integer is FREE |
| 48 | + * <br/>A bit is UNSET in freeSet if the corresponding integer is ALLOCATED |
| 49 | + */ |
| 50 | + private final BitSet freeSet; |
| 51 | + |
| 52 | + /** |
| 53 | + * Creates an IntBitSetAllocator allocating integer IDs within the |
| 54 | + * inclusive range [<code>bottom</code>, <code>top</code>]. |
| 55 | + * @param bottom lower end of range |
| 56 | + * @param top upper end of range (inclusive) |
| 57 | + */ |
| 58 | + public IntBitSetAllocator(int bottom, int top) { |
| 59 | + this.loRange = bottom; |
| 60 | + this.hiRange = top + 1; |
| 61 | + this.numberOfBits = hiRange - loRange; |
| 62 | + this.freeSet = new BitSet(this.numberOfBits); |
| 63 | + this.freeSet.set(0, this.numberOfBits); // All integers FREE initially |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Allocate an unallocated integer from the range, or return -1 if no |
| 68 | + * more integers are available. |
| 69 | + * @return the allocated integer, or -1 |
| 70 | + */ |
| 71 | + public int allocate() { |
| 72 | + int setIndex = this.freeSet.nextSetBit(this.lastIndex); |
| 73 | + if (setIndex<0) { // means none found in trailing part |
| 74 | + setIndex = this.freeSet.nextSetBit(0); |
| 75 | + } |
| 76 | + if (setIndex<0) return -1; |
| 77 | + this.lastIndex = setIndex; |
| 78 | + this.freeSet.clear(setIndex); |
| 79 | + return setIndex + this.loRange; |
| 80 | + } |
| 81 | + /** |
| 82 | + * Make the provided integer available for allocation again. This operation |
| 83 | + * runs in O(1) time. |
| 84 | + * <br/>No error checking is performed, so if you double free or free an |
| 85 | + * integer that was not originally allocated the results are undefined. |
| 86 | + * @param reservation the previously allocated integer to free |
| 87 | + */ |
| 88 | + public void free(int reservation) { |
| 89 | + this.freeSet.set(reservation - this.loRange); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Attempt to reserve the provided ID as if it had been allocated. Returns |
| 94 | + * true if it is available, false otherwise. |
| 95 | + * <br/> |
| 96 | + * This operation runs in O(1) time. |
| 97 | + * @param reservation the integer to be allocated, if possible |
| 98 | + * @return <code><b>true</b></code> if allocated, <code><b>false</b></code> |
| 99 | + * if already allocated |
| 100 | + */ |
| 101 | + public boolean reserve(int reservation) { |
| 102 | + int index = reservation - this.loRange; |
| 103 | + if (this.freeSet.get(index)) { // FREE |
| 104 | + this.freeSet.clear(index); |
| 105 | + return true; |
| 106 | + } else { |
| 107 | + return false; |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + @Override |
| 112 | + public String toString() { |
| 113 | + StringBuilder sb |
| 114 | + = new StringBuilder("IntBitSetAllocator{allocated = ["); |
| 115 | + |
| 116 | + int firstClearBit = this.freeSet.nextClearBit(0); |
| 117 | + if (firstClearBit < this.numberOfBits) { |
| 118 | + int firstSetAfterThat = this.freeSet.nextSetBit(firstClearBit+1); |
| 119 | + if (firstSetAfterThat < 0) |
| 120 | + firstSetAfterThat = this.numberOfBits; |
| 121 | + |
| 122 | + stringInterval(sb, firstClearBit, firstSetAfterThat); |
| 123 | + for (int i = this.freeSet.nextClearBit(firstSetAfterThat+1); |
| 124 | + i < this.numberOfBits; |
| 125 | + i = this.freeSet.nextClearBit(i+1)) { |
| 126 | + int nextSet = this.freeSet.nextSetBit(i); |
| 127 | + if (nextSet<0) nextSet = this.numberOfBits; |
| 128 | + stringInterval(sb.append(", "), i, nextSet); |
| 129 | + i = nextSet; |
| 130 | + } |
| 131 | + } |
| 132 | + sb.append("]}"); |
| 133 | + return sb.toString(); |
| 134 | + } |
| 135 | + private void stringInterval(StringBuilder sb, int i1, int i2) { |
| 136 | + sb.append(i1 + this.loRange); |
| 137 | + if (i1+1 != i2) { |
| 138 | + sb.append("..").append(i2-1 + this.loRange); |
| 139 | + } |
| 140 | + } |
| 141 | +} |
0 commit comments