2016-05-12 04:02:06 +03:00
|
|
|
/* -*- 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_BufferList_h
|
|
|
|
#define mozilla_BufferList_h
|
|
|
|
|
|
|
|
#include <algorithm>
|
2020-11-23 19:12:02 +03:00
|
|
|
#include <cstdint>
|
|
|
|
#include <cstring>
|
2021-06-22 21:17:23 +03:00
|
|
|
#include <numeric>
|
Bug 1609996 - Reorder some includes affected by the previous patches. r=froydnj
This was done by:
This was done by applying:
```
diff --git a/python/mozbuild/mozbuild/code-analysis/mach_commands.py b/python/mozbuild/mozbuild/code-analysis/mach_commands.py
index 789affde7bbf..fe33c4c7d4d1 100644
--- a/python/mozbuild/mozbuild/code-analysis/mach_commands.py
+++ b/python/mozbuild/mozbuild/code-analysis/mach_commands.py
@@ -2007,7 +2007,7 @@ class StaticAnalysis(MachCommandBase):
from subprocess import Popen, PIPE, check_output, CalledProcessError
diff_process = Popen(self._get_clang_format_diff_command(commit), stdout=PIPE)
- args = [sys.executable, clang_format_diff, "-p1", "-binary=%s" % clang_format]
+ args = [sys.executable, clang_format_diff, "-p1", "-binary=%s" % clang_format, '-sort-includes']
if not output_file:
args.append("-i")
```
Then running `./mach clang-format -c <commit-hash>`
Then undoing that patch.
Then running check_spidermonkey_style.py --fixup
Then running `./mach clang-format`
I had to fix four things:
* I needed to move <utility> back down in GuardObjects.h because I was hitting
obscure problems with our system include wrappers like this:
0:03.94 /usr/include/stdlib.h:550:14: error: exception specification in declaration does not match previous declaration
0:03.94 extern void *realloc (void *__ptr, size_t __size)
0:03.94 ^
0:03.94 /home/emilio/src/moz/gecko-2/obj-debug/dist/include/malloc_decls.h:53:1: note: previous declaration is here
0:03.94 MALLOC_DECL(realloc, void*, void*, size_t)
0:03.94 ^
0:03.94 /home/emilio/src/moz/gecko-2/obj-debug/dist/include/mozilla/mozalloc.h:22:32: note: expanded from macro 'MALLOC_DECL'
0:03.94 MOZ_MEMORY_API return_type name##_impl(__VA_ARGS__);
0:03.94 ^
0:03.94 <scratch space>:178:1: note: expanded from here
0:03.94 realloc_impl
0:03.94 ^
0:03.94 /home/emilio/src/moz/gecko-2/obj-debug/dist/include/mozmemory_wrap.h:142:41: note: expanded from macro 'realloc_impl'
0:03.94 #define realloc_impl mozmem_malloc_impl(realloc)
Which I really didn't feel like digging into.
* I had to restore the order of TrustOverrideUtils.h and related files in nss
because the .inc files depend on TrustOverrideUtils.h being included earlier.
* I had to add a missing include to RollingNumber.h
* Also had to partially restore include order in JsepSessionImpl.cpp to avoid
some -WError issues due to some static inline functions being defined in a
header but not used in the rest of the compilation unit.
Differential Revision: https://phabricator.services.mozilla.com/D60327
--HG--
extra : moz-landing-system : lando
2020-01-20 19:19:48 +03:00
|
|
|
|
2020-11-23 19:12:02 +03:00
|
|
|
#include "mozilla/Assertions.h"
|
|
|
|
#include "mozilla/Attributes.h"
|
2018-05-19 01:59:00 +03:00
|
|
|
#include "mozilla/Maybe.h"
|
2017-07-26 00:53:41 +03:00
|
|
|
#include "mozilla/MemoryReporting.h"
|
2016-05-12 04:02:06 +03:00
|
|
|
#include "mozilla/Vector.h"
|
|
|
|
|
|
|
|
// BufferList represents a sequence of buffers of data. A BufferList can choose
|
|
|
|
// to own its buffers or not. The class handles writing to the buffers,
|
|
|
|
// iterating over them, and reading data out. Unlike SegmentedVector, the
|
|
|
|
// buffers may be of unequal size. Like SegmentedVector, BufferList is a nice
|
|
|
|
// way to avoid large contiguous allocations (which can trigger OOMs).
|
|
|
|
|
2017-06-20 22:06:13 +03:00
|
|
|
class InfallibleAllocPolicy;
|
|
|
|
|
2016-05-12 04:02:06 +03:00
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
template <typename AllocPolicy>
|
|
|
|
class BufferList : private AllocPolicy {
|
|
|
|
// Each buffer in a BufferList has a size and a capacity. The first mSize
|
|
|
|
// bytes are initialized and the remaining |mCapacity - mSize| bytes are free.
|
|
|
|
struct Segment {
|
|
|
|
char* mData;
|
|
|
|
size_t mSize;
|
|
|
|
size_t mCapacity;
|
|
|
|
|
|
|
|
Segment(char* aData, size_t aSize, size_t aCapacity)
|
|
|
|
: mData(aData), mSize(aSize), mCapacity(aCapacity) {}
|
|
|
|
|
|
|
|
Segment(const Segment&) = delete;
|
|
|
|
Segment& operator=(const Segment&) = delete;
|
|
|
|
|
|
|
|
Segment(Segment&&) = default;
|
|
|
|
Segment& operator=(Segment&&) = default;
|
|
|
|
|
|
|
|
char* Start() const { return mData; }
|
|
|
|
char* End() const { return mData + mSize; }
|
|
|
|
};
|
|
|
|
|
2016-07-14 11:27:29 +03:00
|
|
|
template <typename OtherAllocPolicy>
|
|
|
|
friend class BufferList;
|
|
|
|
|
2016-05-12 04:02:06 +03:00
|
|
|
public:
|
|
|
|
// For the convenience of callers, all segments are required to be a multiple
|
|
|
|
// of 8 bytes in capacity. Also, every buffer except the last one is required
|
|
|
|
// to be full (i.e., size == capacity). Therefore, a byte at offset N within
|
|
|
|
// the BufferList and stored in memory at an address A will satisfy
|
|
|
|
// (N % Align == A % Align) if Align == 2, 4, or 8.
|
|
|
|
static const size_t kSegmentAlignment = 8;
|
|
|
|
|
|
|
|
// Allocate a BufferList. The BufferList will free all its buffers when it is
|
2017-06-20 22:06:13 +03:00
|
|
|
// destroyed. If an infallible allocator is used, an initial buffer of size
|
|
|
|
// aInitialSize and capacity aInitialCapacity is allocated automatically. This
|
|
|
|
// data will be contiguous and can be accessed via |Start()|. If a fallible
|
|
|
|
// alloc policy is used, aInitialSize must be 0, and the fallible |Init()|
|
|
|
|
// method may be called instead. Subsequent buffers will be allocated with
|
2016-05-12 04:02:06 +03:00
|
|
|
// capacity aStandardCapacity.
|
|
|
|
BufferList(size_t aInitialSize, size_t aInitialCapacity,
|
|
|
|
size_t aStandardCapacity, AllocPolicy aAP = AllocPolicy())
|
|
|
|
: AllocPolicy(aAP),
|
|
|
|
mOwning(true),
|
2016-04-22 13:04:20 +03:00
|
|
|
mSegments(aAP),
|
2016-05-12 04:02:06 +03:00
|
|
|
mSize(0),
|
|
|
|
mStandardCapacity(aStandardCapacity) {
|
|
|
|
MOZ_ASSERT(aInitialCapacity % kSegmentAlignment == 0);
|
|
|
|
MOZ_ASSERT(aStandardCapacity % kSegmentAlignment == 0);
|
|
|
|
|
|
|
|
if (aInitialCapacity) {
|
2017-06-20 22:06:13 +03:00
|
|
|
MOZ_ASSERT((aInitialSize == 0 ||
|
2020-03-28 16:57:20 +03:00
|
|
|
std::is_same_v<AllocPolicy, InfallibleAllocPolicy>),
|
2017-06-20 22:06:13 +03:00
|
|
|
"BufferList may only be constructed with an initial size when "
|
|
|
|
"using an infallible alloc policy");
|
|
|
|
|
2016-05-12 04:02:06 +03:00
|
|
|
AllocateSegment(aInitialSize, aInitialCapacity);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
BufferList(const BufferList& aOther) = delete;
|
|
|
|
|
|
|
|
BufferList(BufferList&& aOther)
|
|
|
|
: mOwning(aOther.mOwning),
|
2018-05-30 22:15:35 +03:00
|
|
|
mSegments(std::move(aOther.mSegments)),
|
2016-05-12 04:02:06 +03:00
|
|
|
mSize(aOther.mSize),
|
|
|
|
mStandardCapacity(aOther.mStandardCapacity) {
|
|
|
|
aOther.mSegments.clear();
|
|
|
|
aOther.mSize = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
BufferList& operator=(const BufferList& aOther) = delete;
|
|
|
|
|
|
|
|
BufferList& operator=(BufferList&& aOther) {
|
|
|
|
Clear();
|
|
|
|
|
|
|
|
mOwning = aOther.mOwning;
|
2018-05-30 22:15:35 +03:00
|
|
|
mSegments = std::move(aOther.mSegments);
|
2016-05-12 04:02:06 +03:00
|
|
|
mSize = aOther.mSize;
|
|
|
|
aOther.mSegments.clear();
|
|
|
|
aOther.mSize = 0;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
~BufferList() { Clear(); }
|
|
|
|
|
2017-06-20 22:06:13 +03:00
|
|
|
// Initializes the BufferList with a segment of the given size and capacity.
|
|
|
|
// May only be called once, before any segments have been allocated.
|
|
|
|
bool Init(size_t aInitialSize, size_t aInitialCapacity) {
|
|
|
|
MOZ_ASSERT(mSegments.empty());
|
|
|
|
MOZ_ASSERT(aInitialCapacity != 0);
|
|
|
|
MOZ_ASSERT(aInitialCapacity % kSegmentAlignment == 0);
|
|
|
|
|
|
|
|
return AllocateSegment(aInitialSize, aInitialCapacity);
|
|
|
|
}
|
|
|
|
|
2018-07-22 14:53:23 +03:00
|
|
|
bool CopyFrom(const BufferList& aOther) {
|
|
|
|
MOZ_ASSERT(mOwning);
|
|
|
|
|
|
|
|
Clear();
|
|
|
|
|
|
|
|
// We don't make an exact copy of aOther. Instead, create a single segment
|
|
|
|
// with enough space to hold all data in aOther.
|
|
|
|
if (!Init(aOther.mSize, (aOther.mSize + kSegmentAlignment - 1) &
|
|
|
|
~(kSegmentAlignment - 1))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t offset = 0;
|
|
|
|
for (const Segment& segment : aOther.mSegments) {
|
|
|
|
memcpy(Start() + offset, segment.mData, segment.mSize);
|
|
|
|
offset += segment.mSize;
|
|
|
|
}
|
|
|
|
MOZ_ASSERT(offset == mSize);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-05-12 04:02:06 +03:00
|
|
|
// Returns the sum of the sizes of all the buffers.
|
|
|
|
size_t Size() const { return mSize; }
|
|
|
|
|
2017-07-26 00:53:41 +03:00
|
|
|
size_t SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) {
|
|
|
|
size_t size = mSegments.sizeOfExcludingThis(aMallocSizeOf);
|
|
|
|
for (Segment& segment : mSegments) {
|
|
|
|
size += aMallocSizeOf(segment.Start());
|
|
|
|
}
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2016-05-12 04:02:06 +03:00
|
|
|
void Clear() {
|
|
|
|
if (mOwning) {
|
|
|
|
for (Segment& segment : mSegments) {
|
2018-07-21 02:58:34 +03:00
|
|
|
this->free_(segment.mData, segment.mCapacity);
|
2016-05-12 04:02:06 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
mSegments.clear();
|
|
|
|
|
|
|
|
mSize = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Iterates over bytes in the segments. You can advance it by as many bytes as
|
|
|
|
// you choose.
|
|
|
|
class IterImpl {
|
|
|
|
// Invariants:
|
2017-06-20 22:06:13 +03:00
|
|
|
// (0) mSegment <= bufferList.mSegments.length()
|
2016-05-12 04:02:06 +03:00
|
|
|
// (1) mData <= mDataEnd
|
|
|
|
// (2) If mSegment is not the last segment, mData < mDataEnd
|
2021-06-22 21:17:23 +03:00
|
|
|
uintptr_t mSegment{0};
|
|
|
|
char* mData{nullptr};
|
|
|
|
char* mDataEnd{nullptr};
|
|
|
|
size_t mAbsoluteOffset{0};
|
2016-05-12 04:02:06 +03:00
|
|
|
|
|
|
|
friend class BufferList;
|
|
|
|
|
|
|
|
public:
|
2021-06-22 21:17:23 +03:00
|
|
|
explicit IterImpl(const BufferList& aBuffers) {
|
2016-05-12 04:02:06 +03:00
|
|
|
if (!aBuffers.mSegments.empty()) {
|
|
|
|
mData = aBuffers.mSegments[0].Start();
|
|
|
|
mDataEnd = aBuffers.mSegments[0].End();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns a pointer to the raw data. It is valid to access up to
|
|
|
|
// RemainingInSegment bytes of this buffer.
|
|
|
|
char* Data() const {
|
|
|
|
MOZ_RELEASE_ASSERT(!Done());
|
|
|
|
return mData;
|
|
|
|
}
|
|
|
|
|
2022-04-22 06:08:47 +03:00
|
|
|
bool operator==(const IterImpl& other) const {
|
|
|
|
return mAbsoluteOffset == other.mAbsoluteOffset;
|
|
|
|
}
|
|
|
|
bool operator!=(const IterImpl& other) const { return !(*this == other); }
|
|
|
|
|
2016-05-12 04:02:06 +03:00
|
|
|
// Returns true if the memory in the range [Data(), Data() + aBytes) is all
|
|
|
|
// part of one contiguous buffer.
|
|
|
|
bool HasRoomFor(size_t aBytes) const {
|
2021-06-22 21:17:23 +03:00
|
|
|
return RemainingInSegment() >= aBytes;
|
2016-05-12 04:02:06 +03:00
|
|
|
}
|
|
|
|
|
2021-06-22 21:17:23 +03:00
|
|
|
// Returns the largest value aBytes for which HasRoomFor(aBytes) will be
|
2016-05-12 04:02:06 +03:00
|
|
|
// true.
|
|
|
|
size_t RemainingInSegment() const {
|
|
|
|
MOZ_RELEASE_ASSERT(mData <= mDataEnd);
|
|
|
|
return mDataEnd - mData;
|
|
|
|
}
|
|
|
|
|
2021-06-22 21:17:23 +03:00
|
|
|
// Returns true if there are at least aBytes entries remaining in the
|
|
|
|
// BufferList after this iterator.
|
|
|
|
bool HasBytesAvailable(const BufferList& aBuffers, size_t aBytes) const {
|
|
|
|
return TotalBytesAvailable(aBuffers) >= aBytes;
|
|
|
|
}
|
2018-04-19 23:53:29 +03:00
|
|
|
|
2021-06-22 21:17:23 +03:00
|
|
|
// Returns the largest value `aBytes` for which HasBytesAvailable(aBytes)
|
|
|
|
// will be true.
|
|
|
|
size_t TotalBytesAvailable(const BufferList& aBuffers) const {
|
|
|
|
return aBuffers.mSize - mAbsoluteOffset;
|
2018-04-19 23:53:29 +03:00
|
|
|
}
|
|
|
|
|
2016-05-12 04:02:06 +03:00
|
|
|
// Advances the iterator by aBytes bytes. aBytes must be less than
|
|
|
|
// RemainingInSegment(). If advancing by aBytes takes the iterator to the
|
|
|
|
// end of a buffer, it will be moved to the beginning of the next buffer
|
|
|
|
// unless it is the last buffer.
|
|
|
|
void Advance(const BufferList& aBuffers, size_t aBytes) {
|
|
|
|
const Segment& segment = aBuffers.mSegments[mSegment];
|
|
|
|
MOZ_RELEASE_ASSERT(segment.Start() <= mData);
|
|
|
|
MOZ_RELEASE_ASSERT(mData <= mDataEnd);
|
|
|
|
MOZ_RELEASE_ASSERT(mDataEnd == segment.End());
|
|
|
|
|
|
|
|
MOZ_RELEASE_ASSERT(HasRoomFor(aBytes));
|
|
|
|
mData += aBytes;
|
2021-06-22 21:17:23 +03:00
|
|
|
mAbsoluteOffset += aBytes;
|
2016-05-12 04:02:06 +03:00
|
|
|
|
|
|
|
if (mData == mDataEnd && mSegment + 1 < aBuffers.mSegments.length()) {
|
|
|
|
mSegment++;
|
|
|
|
const Segment& nextSegment = aBuffers.mSegments[mSegment];
|
|
|
|
mData = nextSegment.Start();
|
|
|
|
mDataEnd = nextSegment.End();
|
|
|
|
MOZ_RELEASE_ASSERT(mData < mDataEnd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Advance the iterator by aBytes, possibly crossing segments. This function
|
|
|
|
// returns false if it runs out of buffers to advance through. Otherwise it
|
|
|
|
// returns true.
|
|
|
|
bool AdvanceAcrossSegments(const BufferList& aBuffers, size_t aBytes) {
|
2021-06-22 21:17:23 +03:00
|
|
|
// If we don't need to cross segments, we can directly use `Advance` to
|
|
|
|
// get to our destination.
|
|
|
|
if (MOZ_LIKELY(aBytes <= RemainingInSegment())) {
|
|
|
|
Advance(aBuffers, aBytes);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if we have enough bytes to scan this far forward.
|
|
|
|
if (!HasBytesAvailable(aBuffers, aBytes)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compare the distance to our target offset from the end of the
|
|
|
|
// BufferList to the distance from the start of our next segment.
|
|
|
|
// Depending on which is closer, we'll advance either forwards or
|
|
|
|
// backwards.
|
|
|
|
size_t targetOffset = mAbsoluteOffset + aBytes;
|
|
|
|
size_t fromEnd = aBuffers.mSize - targetOffset;
|
|
|
|
if (aBytes - RemainingInSegment() < fromEnd) {
|
|
|
|
// Advance through the buffer list until we reach the desired absolute
|
|
|
|
// offset.
|
|
|
|
while (mAbsoluteOffset < targetOffset) {
|
|
|
|
Advance(aBuffers, std::min(targetOffset - mAbsoluteOffset,
|
|
|
|
RemainingInSegment()));
|
2016-05-12 04:02:06 +03:00
|
|
|
}
|
2021-06-22 21:17:23 +03:00
|
|
|
MOZ_ASSERT(mAbsoluteOffset == targetOffset);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Scanning starting from the end of the BufferList. We advance
|
|
|
|
// backwards from the final segment until we find the segment to end in.
|
|
|
|
//
|
|
|
|
// If we end on a segment boundary, make sure to place the cursor at the
|
|
|
|
// beginning of the next segment.
|
|
|
|
mSegment = aBuffers.mSegments.length() - 1;
|
|
|
|
while (fromEnd > aBuffers.mSegments[mSegment].mSize) {
|
|
|
|
fromEnd -= aBuffers.mSegments[mSegment].mSize;
|
|
|
|
mSegment--;
|
2021-06-22 00:53:12 +03:00
|
|
|
}
|
2021-06-22 21:17:23 +03:00
|
|
|
mDataEnd = aBuffers.mSegments[mSegment].End();
|
|
|
|
mData = mDataEnd - fromEnd;
|
|
|
|
mAbsoluteOffset = targetOffset;
|
|
|
|
MOZ_ASSERT_IF(Done(), mSegment == aBuffers.mSegments.length() - 1);
|
|
|
|
MOZ_ASSERT_IF(Done(), mAbsoluteOffset == aBuffers.mSize);
|
2016-05-12 04:02:06 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns true when the iterator reaches the end of the BufferList.
|
|
|
|
bool Done() const { return mData == mDataEnd; }
|
2017-01-20 01:02:40 +03:00
|
|
|
|
2021-06-22 21:17:23 +03:00
|
|
|
// The absolute offset of this iterator within the BufferList.
|
|
|
|
size_t AbsoluteOffset() const { return mAbsoluteOffset; }
|
2021-06-22 04:03:56 +03:00
|
|
|
|
2021-06-22 21:17:23 +03:00
|
|
|
private:
|
2017-01-20 01:02:40 +03:00
|
|
|
bool IsIn(const BufferList& aBuffers) const {
|
|
|
|
return mSegment < aBuffers.mSegments.length() &&
|
|
|
|
mData >= aBuffers.mSegments[mSegment].mData &&
|
|
|
|
mData < aBuffers.mSegments[mSegment].End();
|
|
|
|
}
|
2016-05-12 04:02:06 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
// Special convenience method that returns Iter().Data().
|
2017-06-20 22:06:13 +03:00
|
|
|
char* Start() {
|
|
|
|
MOZ_RELEASE_ASSERT(!mSegments.empty());
|
|
|
|
return mSegments[0].mData;
|
|
|
|
}
|
2016-08-30 13:26:59 +03:00
|
|
|
const char* Start() const { return mSegments[0].mData; }
|
2016-05-12 04:02:06 +03:00
|
|
|
|
|
|
|
IterImpl Iter() const { return IterImpl(*this); }
|
|
|
|
|
|
|
|
// Copies aSize bytes from aData into the BufferList. The storage for these
|
|
|
|
// bytes may be split across multiple buffers. Size() is increased by aSize.
|
2021-03-17 06:01:21 +03:00
|
|
|
[[nodiscard]] inline bool WriteBytes(const char* aData, size_t aSize);
|
2016-05-12 04:02:06 +03:00
|
|
|
|
2017-06-20 02:51:34 +03:00
|
|
|
// Allocates a buffer of at most |aMaxBytes| bytes and, if successful, returns
|
|
|
|
// that buffer, and places its size in |aSize|. If unsuccessful, returns null
|
|
|
|
// and leaves |aSize| undefined.
|
|
|
|
inline char* AllocateBytes(size_t aMaxSize, size_t* aSize);
|
|
|
|
|
2016-05-12 04:02:06 +03:00
|
|
|
// Copies possibly non-contiguous byte range starting at aIter into
|
|
|
|
// aData. aIter is advanced by aSize bytes. Returns false if it runs out of
|
|
|
|
// data before aSize.
|
|
|
|
inline bool ReadBytes(IterImpl& aIter, char* aData, size_t aSize) const;
|
|
|
|
|
|
|
|
// Return a new BufferList that shares storage with this BufferList. The new
|
|
|
|
// BufferList is read-only. It allows iteration over aSize bytes starting at
|
|
|
|
// aIter. Borrow can fail, in which case *aSuccess will be false upon
|
|
|
|
// return. The borrowed BufferList can use a different AllocPolicy than the
|
|
|
|
// original one. However, it is not responsible for freeing buffers, so the
|
|
|
|
// AllocPolicy is only used for the buffer vector.
|
|
|
|
template <typename BorrowingAllocPolicy>
|
|
|
|
BufferList<BorrowingAllocPolicy> Borrow(
|
|
|
|
IterImpl& aIter, size_t aSize, bool* aSuccess,
|
2016-04-22 13:04:20 +03:00
|
|
|
BorrowingAllocPolicy aAP = BorrowingAllocPolicy()) const;
|
2016-05-12 04:02:06 +03:00
|
|
|
|
2016-07-14 11:27:29 +03:00
|
|
|
// Return a new BufferList and move storage from this BufferList to it. The
|
|
|
|
// new BufferList owns the buffers. Move can fail, in which case *aSuccess
|
|
|
|
// will be false upon return. The new BufferList can use a different
|
|
|
|
// AllocPolicy than the original one. The new OtherAllocPolicy is responsible
|
|
|
|
// for freeing buffers, so the OtherAllocPolicy must use freeing method
|
|
|
|
// compatible to the original one.
|
|
|
|
template <typename OtherAllocPolicy>
|
|
|
|
BufferList<OtherAllocPolicy> MoveFallible(
|
|
|
|
bool* aSuccess, OtherAllocPolicy aAP = OtherAllocPolicy());
|
|
|
|
|
2017-01-20 01:02:40 +03:00
|
|
|
// Return the number of bytes from 'start' to 'end', two iterators within
|
|
|
|
// this BufferList.
|
|
|
|
size_t RangeLength(const IterImpl& start, const IterImpl& end) const {
|
|
|
|
MOZ_ASSERT(start.IsIn(*this) && end.IsIn(*this));
|
2021-06-22 21:17:23 +03:00
|
|
|
return end.mAbsoluteOffset - start.mAbsoluteOffset;
|
2017-01-20 01:02:40 +03:00
|
|
|
}
|
|
|
|
|
2017-11-29 18:11:13 +03:00
|
|
|
// This takes ownership of the data
|
2023-11-02 21:05:31 +03:00
|
|
|
[[nodiscard]] bool WriteBytesZeroCopy(char* aData, size_t aSize,
|
|
|
|
size_t aCapacity) {
|
2017-11-29 18:11:13 +03:00
|
|
|
MOZ_ASSERT(mOwning);
|
2023-11-02 21:05:31 +03:00
|
|
|
MOZ_ASSERT(aSize <= aCapacity);
|
|
|
|
|
|
|
|
// Don't create zero-length segments; that can cause problems for
|
|
|
|
// consumers of the data (bug 1595453).
|
|
|
|
if (aSize == 0) {
|
|
|
|
this->free_(aData, aCapacity);
|
|
|
|
return true;
|
|
|
|
}
|
2017-11-29 18:11:13 +03:00
|
|
|
|
|
|
|
if (!mSegments.append(Segment(aData, aSize, aCapacity))) {
|
2018-07-21 02:58:34 +03:00
|
|
|
this->free_(aData, aCapacity);
|
2023-11-02 21:05:31 +03:00
|
|
|
return false;
|
2017-11-29 18:11:13 +03:00
|
|
|
}
|
|
|
|
mSize += aSize;
|
2023-11-02 21:05:31 +03:00
|
|
|
return true;
|
2017-11-29 18:11:13 +03:00
|
|
|
}
|
|
|
|
|
2021-06-22 21:17:23 +03:00
|
|
|
// Truncate this BufferList at the given iterator location, discarding all
|
|
|
|
// data after this point. After this call, all other iterators will be
|
|
|
|
// invalidated, and the passed-in iterator will be "Done".
|
|
|
|
//
|
|
|
|
// Returns the number of bytes discarded by this truncation.
|
|
|
|
size_t Truncate(IterImpl& aIter);
|
|
|
|
|
2016-05-12 04:02:06 +03:00
|
|
|
private:
|
|
|
|
explicit BufferList(AllocPolicy aAP)
|
|
|
|
: AllocPolicy(aAP), mOwning(false), mSize(0), mStandardCapacity(0) {}
|
|
|
|
|
2017-06-20 02:51:34 +03:00
|
|
|
char* AllocateSegment(size_t aSize, size_t aCapacity) {
|
2016-05-12 04:02:06 +03:00
|
|
|
MOZ_RELEASE_ASSERT(mOwning);
|
2017-06-20 22:06:13 +03:00
|
|
|
MOZ_ASSERT(aCapacity != 0);
|
2017-03-10 04:35:00 +03:00
|
|
|
MOZ_ASSERT(aSize <= aCapacity);
|
2016-05-12 04:02:06 +03:00
|
|
|
|
|
|
|
char* data = this->template pod_malloc<char>(aCapacity);
|
|
|
|
if (!data) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
if (!mSegments.append(Segment(data, aSize, aCapacity))) {
|
2018-07-21 02:58:34 +03:00
|
|
|
this->free_(data, aCapacity);
|
2016-05-12 04:02:06 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
mSize += aSize;
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2021-06-22 21:17:23 +03:00
|
|
|
void AssertConsistentSize() const {
|
|
|
|
#ifdef DEBUG
|
|
|
|
size_t realSize = 0;
|
|
|
|
for (const auto& segment : mSegments) {
|
|
|
|
realSize += segment.mSize;
|
|
|
|
}
|
|
|
|
MOZ_ASSERT(realSize == mSize, "cached size value is inconsistent!");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2016-05-12 04:02:06 +03:00
|
|
|
bool mOwning;
|
|
|
|
Vector<Segment, 1, AllocPolicy> mSegments;
|
|
|
|
size_t mSize;
|
|
|
|
size_t mStandardCapacity;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename AllocPolicy>
|
2021-03-17 06:01:21 +03:00
|
|
|
[[nodiscard]] bool BufferList<AllocPolicy>::WriteBytes(const char* aData,
|
|
|
|
size_t aSize) {
|
2016-05-12 04:02:06 +03:00
|
|
|
MOZ_RELEASE_ASSERT(mOwning);
|
|
|
|
MOZ_RELEASE_ASSERT(mStandardCapacity);
|
|
|
|
|
|
|
|
size_t copied = 0;
|
2017-06-20 02:51:34 +03:00
|
|
|
while (copied < aSize) {
|
|
|
|
size_t toCopy;
|
|
|
|
char* data = AllocateBytes(aSize - copied, &toCopy);
|
|
|
|
if (!data) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
memcpy(data, aData + copied, toCopy);
|
|
|
|
copied += toCopy;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename AllocPolicy>
|
|
|
|
char* BufferList<AllocPolicy>::AllocateBytes(size_t aMaxSize, size_t* aSize) {
|
|
|
|
MOZ_RELEASE_ASSERT(mOwning);
|
|
|
|
MOZ_RELEASE_ASSERT(mStandardCapacity);
|
2016-05-12 04:02:06 +03:00
|
|
|
|
|
|
|
if (!mSegments.empty()) {
|
|
|
|
Segment& lastSegment = mSegments.back();
|
|
|
|
|
2017-06-20 02:51:34 +03:00
|
|
|
size_t capacity = lastSegment.mCapacity - lastSegment.mSize;
|
|
|
|
if (capacity) {
|
|
|
|
size_t size = std::min(aMaxSize, capacity);
|
|
|
|
char* data = lastSegment.mData + lastSegment.mSize;
|
2016-05-12 04:02:06 +03:00
|
|
|
|
2017-06-20 02:51:34 +03:00
|
|
|
lastSegment.mSize += size;
|
|
|
|
mSize += size;
|
2016-05-12 04:02:06 +03:00
|
|
|
|
2017-06-20 02:51:34 +03:00
|
|
|
*aSize = size;
|
|
|
|
return data;
|
2016-05-12 04:02:06 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-20 02:51:34 +03:00
|
|
|
size_t size = std::min(aMaxSize, mStandardCapacity);
|
|
|
|
char* data = AllocateSegment(size, mStandardCapacity);
|
|
|
|
if (data) {
|
|
|
|
*aSize = size;
|
|
|
|
}
|
|
|
|
return data;
|
2016-05-12 04:02:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename AllocPolicy>
|
|
|
|
bool BufferList<AllocPolicy>::ReadBytes(IterImpl& aIter, char* aData,
|
|
|
|
size_t aSize) const {
|
|
|
|
size_t copied = 0;
|
|
|
|
size_t remaining = aSize;
|
|
|
|
while (remaining) {
|
|
|
|
size_t toCopy = std::min(aIter.RemainingInSegment(), remaining);
|
|
|
|
if (!toCopy) {
|
|
|
|
// We've run out of data in the last segment.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
memcpy(aData + copied, aIter.Data(), toCopy);
|
|
|
|
copied += toCopy;
|
|
|
|
remaining -= toCopy;
|
|
|
|
|
|
|
|
aIter.Advance(*this, toCopy);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename AllocPolicy>
|
|
|
|
template <typename BorrowingAllocPolicy>
|
|
|
|
BufferList<BorrowingAllocPolicy> BufferList<AllocPolicy>::Borrow(
|
|
|
|
IterImpl& aIter, size_t aSize, bool* aSuccess,
|
2016-04-22 13:04:20 +03:00
|
|
|
BorrowingAllocPolicy aAP) const {
|
2016-05-12 04:02:06 +03:00
|
|
|
BufferList<BorrowingAllocPolicy> result(aAP);
|
|
|
|
|
|
|
|
size_t size = aSize;
|
|
|
|
while (size) {
|
|
|
|
size_t toAdvance = std::min(size, aIter.RemainingInSegment());
|
|
|
|
|
2016-07-14 11:27:29 +03:00
|
|
|
if (!toAdvance || !result.mSegments.append(
|
|
|
|
typename BufferList<BorrowingAllocPolicy>::Segment(
|
|
|
|
aIter.mData, toAdvance, toAdvance))) {
|
2016-05-12 04:02:06 +03:00
|
|
|
*aSuccess = false;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
aIter.Advance(*this, toAdvance);
|
|
|
|
size -= toAdvance;
|
|
|
|
}
|
|
|
|
|
|
|
|
result.mSize = aSize;
|
|
|
|
*aSuccess = true;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-07-14 11:27:29 +03:00
|
|
|
template <typename AllocPolicy>
|
|
|
|
template <typename OtherAllocPolicy>
|
|
|
|
BufferList<OtherAllocPolicy> BufferList<AllocPolicy>::MoveFallible(
|
|
|
|
bool* aSuccess, OtherAllocPolicy aAP) {
|
|
|
|
BufferList<OtherAllocPolicy> result(0, 0, mStandardCapacity, aAP);
|
|
|
|
|
|
|
|
IterImpl iter = Iter();
|
|
|
|
while (!iter.Done()) {
|
|
|
|
size_t toAdvance = iter.RemainingInSegment();
|
|
|
|
|
|
|
|
if (!toAdvance ||
|
|
|
|
!result.mSegments.append(typename BufferList<OtherAllocPolicy>::Segment(
|
|
|
|
iter.mData, toAdvance, toAdvance))) {
|
|
|
|
*aSuccess = false;
|
2016-09-30 12:37:44 +03:00
|
|
|
result.mSegments.clear();
|
2016-07-14 11:27:29 +03:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
iter.Advance(*this, toAdvance);
|
|
|
|
}
|
|
|
|
|
|
|
|
result.mSize = mSize;
|
|
|
|
mSegments.clear();
|
|
|
|
mSize = 0;
|
|
|
|
*aSuccess = true;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-06-22 21:17:23 +03:00
|
|
|
template <typename AllocPolicy>
|
|
|
|
size_t BufferList<AllocPolicy>::Truncate(IterImpl& aIter) {
|
|
|
|
MOZ_ASSERT(aIter.IsIn(*this) || aIter.Done());
|
|
|
|
if (aIter.Done()) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t prevSize = mSize;
|
|
|
|
|
|
|
|
// Remove any segments after the iterator's current segment.
|
|
|
|
while (mSegments.length() > aIter.mSegment + 1) {
|
|
|
|
Segment& toFree = mSegments.back();
|
|
|
|
mSize -= toFree.mSize;
|
|
|
|
if (mOwning) {
|
|
|
|
this->free_(toFree.mData, toFree.mCapacity);
|
|
|
|
}
|
|
|
|
mSegments.popBack();
|
|
|
|
}
|
|
|
|
|
|
|
|
// The last segment is now aIter's current segment. Truncate or remove it.
|
|
|
|
Segment& seg = mSegments.back();
|
|
|
|
MOZ_ASSERT(aIter.mDataEnd == seg.End());
|
|
|
|
mSize -= aIter.RemainingInSegment();
|
|
|
|
seg.mSize -= aIter.RemainingInSegment();
|
|
|
|
if (!seg.mSize) {
|
|
|
|
if (mOwning) {
|
|
|
|
this->free_(seg.mData, seg.mCapacity);
|
|
|
|
}
|
|
|
|
mSegments.popBack();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Correct `aIter` to point to the new end of the BufferList.
|
|
|
|
if (mSegments.empty()) {
|
|
|
|
MOZ_ASSERT(mSize == 0);
|
|
|
|
aIter.mSegment = 0;
|
|
|
|
aIter.mData = aIter.mDataEnd = nullptr;
|
|
|
|
} else {
|
|
|
|
aIter.mSegment = mSegments.length() - 1;
|
|
|
|
aIter.mData = aIter.mDataEnd = mSegments.back().End();
|
|
|
|
}
|
|
|
|
MOZ_ASSERT(aIter.Done());
|
|
|
|
|
|
|
|
AssertConsistentSize();
|
|
|
|
return prevSize - mSize;
|
|
|
|
}
|
|
|
|
|
2016-05-12 04:02:06 +03:00
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif /* mozilla_BufferList_h */
|