private long reserveBlockContents(int freeListIx, int headerToFreeIx,
        int[] victimHeaders, int victimsOffset, boolean isDiscard) {
      // Try opportunistically for the common case - the same-sized, allocated buddy.
      if (enableDefragShortcut) {
        LlapAllocatorBuffer buffer = buffers[headerToFreeIx];
        byte header = headers[headerToFreeIx];
        if (buffer != null && freeListFromHeader(header) == freeListIx) {
          // Double-check the header under lock.
          FreeList freeList = freeLists[freeListIx];
          freeList.lock.lock();
          try {
            // Noone can take this buffer out and thus change the level after we lock, and if
            // they take it out before we lock, then we will fail to lock (same as
            // prepareOneHeaderForMove).
            if (headers[headerToFreeIx] == header
                && buffer.startMoveOrDiscard(arenaIx, headerToFreeIx, isDiscard)) {
              if (assertsEnabled) {
                assertBufferLooksValid(freeListIx, buffer, arenaIx, headerToFreeIx);
                CasLog.logMove(arenaIx, headerToFreeIx, System.identityHashCode(buffer));
              }
              victimHeaders[victimsOffset] = headerToFreeIx;
              return makeIntPair(1, buffer.allocSize);
            }
          } finally {
            freeList.lock.unlock();
          }
          // We don't bail on failure - try in detail below.
        }
      }
      // Examine the buddy block and its sub-blocks in detail.
      long[] stack = new long[freeListIx + 1]; // Can never have more than this in elements.
      int stackSize = 1;
      // Seed with the buddy of this block (so the first iteration would target this block).
      stack[0] = makeIntPair(freeListIx, getBuddyHeaderIx(freeListIx, headerToFreeIx));
      int victimCount = 0;
      int totalMoveSize = 0;
      // We traverse the leaf nodes of the tree. The stack entries indicate the existing leaf
      // nodes that we need to see siblings for, and sibling levels.
      while (stackSize > 0) {
        --stackSize;
        long next = stack[stackSize];
        int listLevel = getFirstInt(next); // This is not an actual list; see intermList.
        int sourceHeaderIx = getSecondInt(next);
        // Find the buddy of the header at list level. We don't know what list it is actually in.
        int levelBuddyHeaderIx = getBuddyHeaderIx(listLevel, sourceHeaderIx);
        // First, handle the actual thing we found.
        long result = prepareOneHeaderForMove(levelBuddyHeaderIx, isDiscard, freeListIx);
        if (result == -1) {
          // We have failed to reserve a single header. Do not undo the previous ones here,
          // the caller has to handle this to avoid races.
          return makeIntPair(victimCount, FAILED_TO_RESERVE);
        }
        int allocSize = getFirstInt(result);
        totalMoveSize += allocSize;
        victimHeaders[victimsOffset + victimCount] = levelBuddyHeaderIx;
        ++victimCount;
        // Explaining this would really require a picture. Basically if the level is lower than
        // our level, that means (imagine a tree) we are on the leftmost leaf node of the sub-tree
        // under our sibling in the tree. So we'd need to look at the buddies of that leftmost leaf
        // block on all the intermediate levels (aka all intermediate levels of the tree between
        // this guy and our sibling). Including its own buddy on its own level. And so on for every
        // sub-tree where our buddy is not on the same level as us (i.e. does not cover the entire
        // sub-tree).
        int actualBuddyListIx = getSecondInt(result);
        for (int intermListIx = listLevel - 1; intermListIx >= actualBuddyListIx; --intermListIx) {
          stack[stackSize++] = makeIntPair(intermListIx, levelBuddyHeaderIx);
        }
      }
      return makeIntPair(victimCount, totalMoveSize);
    }
Reserve all the contents of a particular block to merge them together.
@param freeListIx The list to which the hypothetical block belongs.
@param freeHeaderIx The header of the base block.
llap-server/src/java/org/apache/hadoop/hive/llap/cache/BuddyAllocator.java