7:64
  7   ˆl–äY>” ”¾R( 	ÅÍÆŸ¸”Å£‘äÇò 8àFá]•ÆämŸçÂƒh¹ÁÀ¿      7:3
  @   ˆl–Ðz¾”Ên-jeAà_¸'ÔÅ£�þE!YÀ67d óÃ„@Û¿Â_ˆI|¥�+y,ÂÁ +1    /* -*- 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/. */

// A simple segmented vector class.
//
// This class should be used in preference to mozilla::Vector or nsTArray when
// you are simply gathering items in order to later iterate over them.
//
// - In the case where you don't know the final size in advance, using
//   SegmentedVector avoids the need to repeatedly allocate increasingly large
//   buffers and copy the data into them.
//
// - In the case where you know the final size in advance and so can set the
//   capacity appropriately, using SegmentedVector still avoids the need for
//   large allocations (which can trigger OOMs).

#ifndef mozilla_SegmentedVector_h
#define mozilla_SegmentedVector_h

#include <new>  // for placement new
#include <utility>

#include "mozilla/AllocPolicy.h"
#include "mozilla/Array.h"
#include "mozilla/Attributes.h"
#include "mozilla/LinkedList.h"
#include "mozilla/MemoryReporting.h"
#include "mozilla/OperatorNewExtensions.h"

#ifdef IMPL_LIBXUL
#  include "mozilla/Likely.h"
#  include "mozilla/mozalloc_oom.h"
#endif  // IMPL_LIBXUL

namespace mozilla {

// |IdealSegmentSize| specifies how big each segment will be in bytes (or as
// close as is possible). Use the following guidelines to choose a size.
//
// - It should be a power-of-two, to avoid slop.
//
// - It should not be too small, so that segment allocations are infrequent,
//   and so that per-segment bookkeeping overhead is low. Typically each
//   segment should be able to hold hundreds of elements, at least.
//
// - It should not be too large, so that OOMs are unlikely when allocating
//   segments, and so that not too much space is wasted when the final segment
//   is not full.
//
// The ideal size depends on how the SegmentedVector is used and the size of
// |T|, but reasonable sizes include 1024, 4096 (the default), 8192, and 16384.
//
template <typename T, size_t IdealSegmentSize = 4096,
          typename AllocPolicy = MallocAllocPolicy>
class SegmentedVector : private AllocPolicy {
  template <size_t SegmentCapacity>
  struct SegmentImpl
      : public mozilla::LinkedListElement<SegmentImpl<SegmentCapacity>> {
   private:
    uint32_t mLength;
    alignas(T) MOZ_INIT_OUTSIDE_CTOR
        unsigned char mData[sizeof(T) * SegmentCapacity];

    // Some versions of GCC treat it as a -Wstrict-aliasing violation (ergo a
    // -Werror compile error) to reinterpret_cast<> |mData| to |T*|, even
    // through |void*|.  Placing the latter cast in these separate functions
    // breaks the chain such that affected GCC versions no longer warn/error.
    void* RawData() { return mData; }

   public:
    SegmentImpl() : mLength(0) {}

    ~SegmentImpl() {
      for (uint32_t i = 0; i < mLength; i++) {
        (*this)[i].~T();
      }
    }

    uint32_t Length() const { return mLength; }

    T* Elems() { return reinterpret_cast<T*>(RawData()); }

    T& operator[](size_t aIndex) {
      MOZ_ASSERT(aIndex < mLength);
      return Elems()[aIndex];
    }

    const T& operator[](size_t aIndex) const {
      MOZ_ASSERT(aIndex < mLength);
      return Elems()[aIndex];
    }

    template <typename U>
    void Append(U&& aU) {
      MOZ_ASSERT(mLength < SegmentCapacity);
      // Pre-increment mLength so that the bounds-check in operator[] passes.
      mLength++;
      T* elem = &(*this)[mLength - 1];
      new (KnownNotNull, elem) T(std::forward<U>(aU));
    }

    void PopLast() {
      MOZ_ASSERT(mLength > 0);
      (*this)[mLength - 1].~T();
      mLength--;
    }
  };

  // See how many we elements we can fit in a segment of IdealSegmentSize. If
  // IdealSegmentSize is too small, it'll be just one. Th