/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ /* vim: set ts=8 sts=2 et sw=2 tw=80: */ // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file.
// We want to copy data to our payload as efficiently as possible. // memcpy fits the bill for copying, but not all compilers or // architectures support inlining memcpy from void*, which has unknown // static alignment. However, we know that all the members of our // payload will be aligned on memberAlignmentType boundaries. We // therefore use that knowledge to construct a copier that will copy // efficiently (via standard C++ assignment mechanisms) if the datatype // needs that alignment or less, and memcpy otherwise. (The compiler // may still inline memcpy, of course.)
template <typename T> void PickleIterator::CopyInto(T* dest) {
static_assert(std::is_trivially_copyable<T>::value, "Copied type must be a POD type");
Copier<T, sizeof(T), (alignof(T) <= sizeof(Pickle::memberAlignmentType))>::
Copy(dest, iter_.Data());
} templatevoid PickleIterator::CopyInto<char>(char*);
bool Pickle::IteratorHasRoomFor(const PickleIterator& iter,
uint32_t len) const { // Make sure we don't get into trouble where AlignInt(len) == 0.
MOZ_RELEASE_ASSERT(len < 64);
void Pickle::UpdateIter(PickleIterator* iter, uint32_t bytes) const { // Make sure we don't get into trouble where AlignInt(bytes) == 0.
MOZ_RELEASE_ASSERT(bytes < 64);
iter->iter_.Advance(buffers_, AlignInt(bytes));
}
// Payload is sizeof(Pickle::memberAlignmentType) aligned.
// Always written as a 64-bit value since the size for this type can // differ between architectures. bool Pickle::ReadLong(PickleIterator* iter, long* result) const {
int64_t big_result; if (!ReadScalar(iter, &big_result)) returnfalse;
// Always written as a 64-bit value since the size for this type can // differ between architectures. bool Pickle::ReadULong(PickleIterator* iter, unsignedlong* result) const {
uint64_t big_result; if (!ReadScalar(iter, &big_result)) returnfalse;
DCHECK(big_result <= ULONG_MAX);
*result = static_cast<unsignedlong>(big_result);
// Always written as a 64-bit value since the size for this type can // differ between architectures. bool Pickle::ReadIntPtr(PickleIterator* iter, intptr_t* result) const {
DCHECK(iter);
int64_t big_result; if (!ReadScalar(iter, &big_result)) returnfalse;
int len; if (!ReadLength(iter, &len)) returnfalse; // Avoid integer multiplication overflow. if (len > INT_MAX / static_cast<int>(sizeof(wchar_t))) returnfalse;
auto chars = mozilla::MakeUnique<wchar_t[]>(len); if (!ReadBytesInto(iter, chars.get(), len * sizeof(wchar_t))) { returnfalse;
}
result->assign(chars.get(), len);
bool Pickle::WriteLong(long value) { // Always written as a 64-bit value since the size for this type can // differ between architectures. return WriteInt64(int64_t(value));
}
bool Pickle::WriteULong(unsignedlong value) { // Always written as a 64-bit value since the size for this type can // differ between architectures. return WriteUInt64(uint64_t(value));
}
bool Pickle::WriteIntPtr(intptr_t value) { // Always written as a 64-bit value since the size for this type can // differ between architectures. return WriteInt64(int64_t(value));
}
uint32_t new_capacity = AlignInt(capacity); #ifndef MOZ_MEMORY if (new_capacity > capacity) { // If the buffer we were given is not large enough to contain padding // after the data, reallocate it to make it so. When using jemalloc, // we're guaranteed the buffer size is going to be at least 4-bytes // aligned, so we skip realloc altogether. Even with other allocators, // the realloc is likely not necessary, but we don't take chances. // At least with ASan, it does matter to realloc to inform ASan we're // going to use more data from the buffer (and let it actually realloc // if it needs to).
data = realloc(data, new_capacity);
} #endif
// Shouldn't fail, because we're using InfallibleAllocPolicy.
MOZ_ALWAYS_TRUE(buffers_.WriteBytesZeroCopy(reinterpret_cast<char*>(data),
data_len, new_capacity));
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.