DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Mercurial (95ab8f81aa22)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
 * vim: set ts=8 sts=2 et sw=2 tw=80:
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef mozilla_recordreplay_SplayTree_h
#define mozilla_recordreplay_SplayTree_h

#include "mozilla/Types.h"
#include "ProcessRecordReplay.h"

//#define ENABLE_COHERENCY_CHECKS

namespace mozilla {
namespace recordreplay {

/*
 * Class which represents a splay tree with nodes allocated from an alloc
 * policy.
 *
 * Splay trees are balanced binary search trees for which search, insert and
 * remove are all amortized O(log n).
 *
 * T indicates the type of tree elements, L has a Lookup type and a static
 * 'ssize_t compare(const L::Lookup&, const T&)' method ordering the elements.
 */
template <class T, class L, class AllocPolicy, size_t ChunkPages>
class SplayTree {
  struct Node {
    T mItem;
    Node* mLeft;
    Node* mRight;
    Node* mParent;

    explicit Node(const T& aItem)
        : mItem(aItem), mLeft(nullptr), mRight(nullptr), mParent(nullptr) {}
  };

  AllocPolicy mAlloc;
  Node* mRoot;
  Node* mFreeList;

  SplayTree(const SplayTree&) = delete;
  SplayTree& operator=(const SplayTree&) = delete;

 public:
  explicit SplayTree(const AllocPolicy& aAlloc = AllocPolicy())
      : mAlloc(aAlloc), mRoot(nullptr), mFreeList(nullptr) {}

  bool empty() const { return !mRoot; }

  void clear() {
    while (mRoot) {
      remove(mRoot);
    }
  }

  Maybe<T> maybeLookup(const typename L::Lookup& aLookup,
                       bool aRemove = false) {
    if (!mRoot) {
      return Nothing();
    }
    Node* last = lookup(aLookup);
    splay(last);
    checkCoherency(mRoot, nullptr);
    Maybe<T> res;
    if (L::compare(aLookup, last->mItem) == 0) {
      res = Some(last->mItem);
      if (aRemove) {
        remove(last);
      }
    }
    return res;
  }

  // Lookup an item which matches aLookup, or the closest item less than it.
  Maybe<T> lookupClosestLessOrEqual(const typename L::Lookup& aLookup,
                                    bool aRemove = false) {
    if (!mRoot) {
      return Nothing();
    }
    Node* last = lookup(aLookup);
    Node* search = last;
    while (search && L::compare(aLookup, search->mItem) < 0) {
      search = search->mParent;
    }
    Maybe<T> res = search ? Some(search->mItem) : Nothing();
    if (aRemove && search) {
      remove(search);
    } else {
      splay(last);
    }
    checkCoherency(mRoot, nullptr);
    return res;
  }

  void insert(const typename L::Lookup& aLookup, const T& aValue) {
    MOZ_RELEASE_ASSERT(L::compare(aLookup, aValue) == 0);
    Node* element = allocateNode(aValue);
    if (!mRoot) {
      mRoot = element;
      return;
    }
    Node* last = lookup(aLookup);
    ssize_t cmp = L::compare(aLookup, last->mItem);

    Node** parentPointer;
    if (cmp < 0) {
      parentPointer = &last->mLeft;
    } else if (cmp > 0) {
      parentPointer = &last->mRight;
    } else {
      // The lookup matches an existing entry in the tree. Place it to the left
      // of the element just looked up.
      if (!last->mLeft) {
        parentPointer = &last->mLeft;
      } else {
        last = last->mLeft;
        while (last->mRight) {
          last = last->mRight;
        }
        parentPointer = &last->mRight;
      }
    }
    MOZ_RELEASE_ASSERT(!*parentPointer);
    *parentPointer = element;
    element->mParent = last;

    splay(element);
    checkCoherency(mRoot, nullptr);
  }

  class Iter {
    friend class SplayTree;

    SplayTree* mTree;
    Node* mNode;
    bool mRemoved;

    Iter(SplayTree* aTree, Node* aNode)
        : mTree(aTree), mNode(aNode), mRemoved(false) {}

   public:
    const T& ref() { return mNode->mItem; }

    bool done() { return !mNode; }

    Iter& operator++() {
      MOZ_RELEASE_ASSERT(!mRemoved);
      if (mNode->mRight) {
        mNode = mNode->mRight;
        while (mNode->mLeft) {
          mNode = mNode->mLeft;
        }
      } else {
        while (true) {
          Node* cur = mNode;
          mNode = mNode->mParent;
          if (!mNode || mNode->mLeft == cur) {
            break;
          }
        }
      }
      return *this;
    }

    void removeEntry() {
      mTree->remove(mNode);
      mRemoved = true;
    }
  };

  Iter begin() {
    Node* node = mRoot;
    while (node && node->mLeft) {
      node = node->mLeft;
    }
    return Iter(this, node);
  }

 private:
  // Lookup an item matching aLookup, or the closest node to it.
  Node* lookup(const typename L::Lookup& aLookup) const {
    MOZ_RELEASE_ASSERT(mRoot);
    Node* node = mRoot;
    Node* parent;
    do {
      parent = node;
      ssize_t c = L::compare(aLookup, node->mItem);
      if (c == 0) {
        return node;
      }
      node = (c < 0) ? node->mLeft : node->mRight;
    } while (node);
    return parent;
  }

  void remove(Node* aNode) {
    splay(aNode);
    MOZ_RELEASE_ASSERT(aNode && aNode == mRoot);

    // Find another node which can be swapped in for the root: either the
    // rightmost child of the root's left, or the leftmost child of the
    // root's right.
    Node* swap;
    Node* swapChild;
    if (mRoot->mLeft) {
      swap = mRoot->mLeft;
      while (swap->mRight) {
        swap = swap->mRight;
      }
      swapChild = swap->mLeft;
    } else if (mRoot->mRight) {
      swap = mRoot->mRight;
      while (swap->mLeft) {
        swap = swap->mLeft;
      }
      swapChild = swap->mRight;
    } else {
      freeNode(mRoot);
      mRoot = nullptr;
      return;
    }

    // The selected node has at most one child, in swapChild. Detach it
    // from the subtree by replacing it with that child.
    if (swap == swap->mParent->mLeft) {
      swap->mParent->mLeft = swapChild;
    } else {
      swap->mParent->mRight = swapChild;
    }
    if (swapChild) {
      swapChild->mParent = swap->mParent;
    }

    mRoot->mItem = swap->mItem;
    freeNode(swap);

    checkCoherency(mRoot, nullptr);
  }

  size_t NodesPerChunk() const { return ChunkPages * PageSize / sizeof(Node); }

  Node* allocateNode(const T& aValue) {
    if (!mFreeList) {
      Node* nodeArray = mAlloc.template pod_malloc<Node>(NodesPerChunk());
      for (size_t i = 0; i < NodesPerChunk() - 1; i++) {
        nodeArray[i].mLeft = &nodeArray[i + 1];
      }
      mFreeList = nodeArray;
    }
    Node* node = mFreeList;
    mFreeList = node->mLeft;
    new (node) Node(aValue);
    return node;
  }

  void freeNode(Node* aNode) {
    aNode->mLeft = mFreeList;
    mFreeList = aNode;
  }

  void splay(Node* aNode) {
    // Rotate the element until it is at the root of the tree. Performing
    // the rotations in this fashion preserves the amortized balancing of
    // the tree.
    MOZ_RELEASE_ASSERT(aNode);
    while (aNode != mRoot) {
      Node* parent = aNode->mParent;
      if (parent == mRoot) {
        // Zig rotation.
        rotate(aNode);
        MOZ_RELEASE_ASSERT(aNode == mRoot);
        return;
      }
      Node* grandparent = parent->mParent;
      if ((parent->mLeft == aNode) == (grandparent->mLeft == parent)) {
        // Zig-zig rotation.
        rotate(parent);
        rotate(aNode);
      } else {
        // Zig-zag rotation.
        rotate(aNode);
        rotate(aNode);
      }
    }
  }

  void rotate(Node* aNode) {
    // Rearrange nodes so that node becomes the parent of its current
    // parent, while preserving the sortedness of the tree.
    Node* parent = aNode->mParent;
    if (parent->mLeft == aNode) {
      //     x          y
      //   y  c  ==>  a  x
      //  a b           b c
      parent->mLeft = aNode->mRight;
      if (aNode->mRight) {
        aNode->mRight->mParent = parent;
      }
      aNode->mRight = parent;
    } else {
      MOZ_RELEASE_ASSERT(parent->mRight == aNode);
      //   x             y
      //  a  y   ==>   x  c
      //    b c       a b
      parent->mRight = aNode->mLeft;
      if (aNode->mLeft) {
        aNode->mLeft->mParent = parent;
      }
      aNode->mLeft = parent;
    }
    aNode->mParent = parent->mParent;
    parent->mParent = aNode;
    if (Node* grandparent = aNode->mParent) {
      if (grandparent->mLeft == parent) {
        grandparent->mLeft = aNode;
      } else {
        grandparent->mRight = aNode;
      }
    } else {
      mRoot = aNode;
    }
  }

#ifdef ENABLE_COHERENCY_CHECKS
  Node* checkCoherency(Node* aNode, Node* aMinimum) {
    if (!aNode) {
      MOZ_RELEASE_ASSERT(!mRoot);
      return nullptr;
    }
    MOZ_RELEASE_ASSERT(aNode->mParent || aNode == mRoot);
    MOZ_RELEASE_ASSERT(!aMinimum || L::compare(L::getLookup(aMinimum->mItem),
                                               aNode->mItem) <= 0);
    if (aNode->mLeft) {
      MOZ_RELEASE_ASSERT(aNode->mLeft->mParent == aNode);
      Node* leftMaximum = checkCoherency(aNode->mLeft, aMinimum);
      MOZ_RELEASE_ASSERT(
          L::compare(L::getLookup(leftMaximum->mItem), aNode->mItem) <= 0);
    }
    if (aNode->mRight) {
      MOZ_RELEASE_ASSERT(aNode->mRight->mParent == aNode);
      return checkCoherency(aNode->mRight, aNode);
    }
    return aNode;
  }
#else
  inline void checkCoherency(Node* aNode, Node* aMinimum) {}
#endif
};

}  // namespace recordreplay
}  // namespace mozilla

#endif  // mozilla_recordreplay_SplayTree_h