bool ArgumentDecoder::alignBufferPosition(unsigned alignment, size_t size) { uint8_t* alignedPosition = roundUpToAlignment(m_bufferPos, alignment); if (!alignedBufferIsLargeEnoughToContain(alignedPosition, m_bufferEnd, size)) { // We've walked off the end of this buffer. markInvalid(); return false; } m_bufferPos = alignedPosition; return true; }
void ArgumentDecoder::initialize(const uint8_t* buffer, size_t bufferSize) { // This is the largest primitive type we expect to unpack from the message. const size_t expectedAlignment = sizeof(uint64_t); m_allocatedBase = static_cast<uint8_t*>(malloc(bufferSize + expectedAlignment)); m_buffer = roundUpToAlignment(m_allocatedBase, expectedAlignment); ASSERT(!(reinterpret_cast<uintptr_t>(m_buffer) % expectedAlignment)); m_bufferPos = m_buffer; m_bufferEnd = m_buffer + bufferSize; memcpy(m_buffer, buffer, bufferSize); }
bool ArgumentDecoder::alignBufferPosition(unsigned alignment, size_t size) { uint8_t* buffer = roundUpToAlignment(m_bufferPos, alignment); if (static_cast<size_t>(m_bufferEnd - buffer) < size) { // We've walked off the end of this buffer. markInvalid(); return false; } m_bufferPos = buffer; return true; }
uint8_t* ArgumentEncoder::grow(unsigned alignment, size_t size) { size_t alignedSize = roundUpToAlignment(m_bufferSize, alignment); if (alignedSize + size > m_bufferCapacity) { size_t newCapacity = std::max(alignedSize + size, std::max(static_cast<size_t>(32), m_bufferCapacity + m_bufferCapacity / 4 + 1)); if (!m_buffer) m_buffer = static_cast<uint8_t*>(fastMalloc(newCapacity)); else m_buffer = static_cast<uint8_t*>(fastRealloc(m_buffer, newCapacity)); // FIXME: What should we do if allocating memory fails? m_bufferCapacity = newCapacity; } m_bufferSize = alignedSize + size; m_bufferPointer = m_buffer + alignedSize + size; return m_buffer + alignedSize; }
bool ArgumentDecoder::bufferIsLargeEnoughToContain(unsigned alignment, size_t size) const { return alignedBufferIsLargeEnoughToContain(roundUpToAlignment(m_bufferPos, alignment), m_bufferEnd, size); }
bool ArgumentDecoder::bufferIsLargeEnoughToContain(unsigned alignment, size_t size) const { return static_cast<size_t>(m_bufferEnd - roundUpToAlignment(m_bufferPos, alignment)) >= size; }