13:64
  !   ˆÉ�þ\t’¬Ù$‘¸Ü-à?Î„�ºßÍÌËÊ ?÷     /* -*- Mode: C++; tab-width: 2; 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 ModuloBuffer_h
#define ModuloBuffer_h

#include "mozilla/leb128iterator.h"
#include "mozilla/Maybe.h"
#include "mozilla/MemoryReporting.h"
#include "mozilla/NotNull.h"
#include "mozilla/PowerOfTwo.h"
#include "mozilla/ProfileBufferEntrySerialization.h"
#include "mozilla/UniquePtr.h"

#include <functional>
#include <iterator>
#include <limits>
#include <type_traits>

namespace mozilla {

// The ModuloBuffer class is a circular buffer that holds raw byte values, with
// data-read/write helpers.
//
// OffsetT: Type of the internal offset into the buffer of bytes, it should be
// large enough to access all bytes of the buffer. It will also be used as
// Length (in bytes) of the buffer and of any subset. Default uint32_t
// IndexT: Type of the external index, it should be large enough that overflows
// should not happen during the lifetime of the ModuloBuffer.
//
// The basic usage is to create an iterator-like object with `ReaderAt(Index)`
// or `WriterAt(Index)`, and use it to read/write data blobs. Iterators
// automatically manage the wrap-around (through "Modulo", which is effectively
// an AND-masking with the PowerOfTwo buffer size.)
//
// There is zero safety: No thread safety, no checks that iterators may be
// overwriting data that's still to be read, etc. It's up to the caller to add
// adequate checks.
// The intended use is as an underlying buffer for a safer container.
template <typename OffsetT = uint32_t, typename IndexT = uint64_t>
class ModuloBuffer {
 public:
  using Byte = uint8_t;
  static_assert(sizeof(Byte) == 1, "ModuloBuffer::Byte must be 1 byte");
  using Offset = OffsetT;
  static_assert(!std::numeric_limits<Offset>::is_signed,
                "ModuloBuffer::Offset must be an unsigned integral type");
  using Length = Offset;
  using Index = IndexT;
  static_assert(!std::numeric_limits<Index>::is_signed,
                "ModuloBuffer::Index must be an unsigned integral type");
  static_assert(sizeof(Index) >= sizeof(Offset),
                "ModuloBuffer::Index size must >= Offset");

  // Create a buffer of the given length.
  explicit ModuloBuffer(PowerOfTwo<Length> aLength)
      : mMask(aLength.Mask()),
        mBuffer(WrapNotNull(new Byte[aLength.Value()])),
        mBufferDeleter([](Byte* aBuffer) { delete[] aBuffer; }) {}

  // Take ownership of an existing buffer. Existing contents is ignored.
  // Done by extracting the raw pointer from UniquePtr<Byte[]>, and adding
  // an equivalent `delete[]` in `mBufferDeleter`.
  ModuloBuffer(UniquePtr<Byte[]> aExistingBuffer, PowerOfTwo<Length> aLength)
      : mMask(aLength.Mask()),
        mBuffer(WrapNotNull(aExistingBuffer.release())),
        mBufferDeleter([](Byte* aBuffer) { delete[] aBuffer; }) {}

  // Use an externally-owned buffer. Existing contents is ignored.
  ModuloBuffer(Byte* aExternalBuffer, PowerOfTwo<Length> aLength)
      : mMask(aLength.Mask()), mBuffer(WrapNotNull(aExternalBuffer)) {}

  // Disallow copying, as we may uniquely own the resource.
  ModuloBuffer(const ModuloBuffer& aOther) = delete;
  ModuloBuffer& operator=(const ModuloBuffer& aOther) = delete;

  // Allow move-construction. Stealing ownership if the original had it.
  // This effectively prevents copy construction, and all assignments; needed so
  // that a ModuloBuffer may be initialized from a separate construction.
  // The moved-from ModuloBuffer still points at the resource but doesn't own
  // it, so it won't try to free it; but accesses are not guaranteed, so it
  // should not be used anymore.
  ModuloBuffer(ModuloBuffer&& aOther)
      : mMask(std::move(aOther.mMask)),
        mBuffer(std::move(aOther.mBuffer)),
        mBufferDeleter(std::move(aOther.mBufferDeleter)) {