You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BlockRevQueue.java 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.revwalk;
  44. import java.io.IOException;
  45. import org.eclipse.jgit.errors.IncorrectObjectTypeException;
  46. import org.eclipse.jgit.errors.MissingObjectException;
  47. abstract class BlockRevQueue extends AbstractRevQueue {
  48. protected BlockFreeList free;
  49. /** Create an empty revision queue. */
  50. protected BlockRevQueue() {
  51. free = new BlockFreeList();
  52. }
  53. BlockRevQueue(final Generator s) throws MissingObjectException,
  54. IncorrectObjectTypeException, IOException {
  55. free = new BlockFreeList();
  56. outputType = s.outputType();
  57. s.shareFreeList(this);
  58. for (;;) {
  59. final RevCommit c = s.next();
  60. if (c == null)
  61. break;
  62. add(c);
  63. }
  64. }
  65. /**
  66. * Reconfigure this queue to share the same free list as another.
  67. * <p>
  68. * Multiple revision queues can be connected to the same free list, making
  69. * it less expensive for applications to shuttle commits between them. This
  70. * method arranges for the receiver to take from / return to the same free
  71. * list as the supplied queue.
  72. * <p>
  73. * Free lists are not thread-safe. Applications must ensure that all queues
  74. * sharing the same free list are doing so from only a single thread.
  75. *
  76. * @param q
  77. * the other queue we will steal entries from.
  78. */
  79. public void shareFreeList(final BlockRevQueue q) {
  80. free = q.free;
  81. }
  82. static final class BlockFreeList {
  83. private Block next;
  84. Block newBlock() {
  85. Block b = next;
  86. if (b == null)
  87. return new Block();
  88. next = b.next;
  89. b.clear();
  90. return b;
  91. }
  92. void freeBlock(final Block b) {
  93. b.next = next;
  94. next = b;
  95. }
  96. void clear() {
  97. next = null;
  98. }
  99. }
  100. static final class Block {
  101. static final int BLOCK_SIZE = 256;
  102. /** Next block in our chain of blocks; null if we are the last. */
  103. Block next;
  104. /** Our table of queued commits. */
  105. final RevCommit[] commits = new RevCommit[BLOCK_SIZE];
  106. /** Next valid entry in {@link #commits}. */
  107. int headIndex;
  108. /** Next free entry in {@link #commits} for addition at. */
  109. int tailIndex;
  110. boolean isFull() {
  111. return tailIndex == BLOCK_SIZE;
  112. }
  113. boolean isEmpty() {
  114. return headIndex == tailIndex;
  115. }
  116. boolean canUnpop() {
  117. return headIndex > 0;
  118. }
  119. void add(final RevCommit c) {
  120. commits[tailIndex++] = c;
  121. }
  122. void unpop(final RevCommit c) {
  123. commits[--headIndex] = c;
  124. }
  125. RevCommit pop() {
  126. return commits[headIndex++];
  127. }
  128. RevCommit peek() {
  129. return commits[headIndex];
  130. }
  131. void clear() {
  132. next = null;
  133. headIndex = 0;
  134. tailIndex = 0;
  135. }
  136. void resetToMiddle() {
  137. headIndex = tailIndex = BLOCK_SIZE / 2;
  138. }
  139. void resetToEnd() {
  140. headIndex = tailIndex = BLOCK_SIZE;
  141. }
  142. }
  143. }