// Exercise concurrent serialization and deserialization. TEST_F(PushDeserializerTest, SerializationDeserialization) { auto const trajectory = BuildTrajectory(); int const byte_size = trajectory->ByteSize(); for (int i = 0; i < kRunsPerTest; ++i) { auto read_trajectory = make_not_null_unique<Trajectory>(); auto written_trajectory = BuildTrajectory(); auto storage = std::make_unique<std::uint8_t[]>(byte_size); std::uint8_t* data = &storage[0]; pull_serializer_ = std::make_unique<PullSerializer>(kSerializerChunkSize, kNumberOfChunks); push_deserializer_ = std::make_unique<PushDeserializer>( kDeserializerChunkSize, kNumberOfChunks); pull_serializer_->Start(std::move(written_trajectory)); push_deserializer_->Start( std::move(read_trajectory), PushDeserializerTest::CheckSerialization); for (;;) { Bytes const bytes = pull_serializer_->Pull(); std::memcpy(data, bytes.data, static_cast<size_t>(bytes.size)); push_deserializer_->Push(Bytes(data, bytes.size), std::bind(&PushDeserializerTest::Stomp, Bytes(data, bytes.size))); data = &data[bytes.size]; if (bytes.size == 0) { break; } } // Destroying the deserializer waits until deserialization is done. It is // important that this happens before |storage| is destroyed. pull_serializer_.reset(); push_deserializer_.reset(); } }
void Disassembler::disassemble(const image::ByteSource *source, ByteAddr begin, ByteAddr end, InstructionCallback callback, const CancellationToken &canceled) { assert(source != NULL); assert(begin <= end); const SmallByteSize maxInstructionSize = architecture_->maxInstructionSize(); const ByteSize bufferSize = std::min(ByteSize(std::max(65536, maxInstructionSize)), end - begin); const std::unique_ptr<char[]> buffer(new char[bufferSize]); auto bufferBegin = begin; auto bufferEnd = begin; for (ByteAddr pc = begin; pc < end; canceled.poll()) { if (pc + maxInstructionSize > bufferEnd && bufferEnd < end) { bufferBegin = pc; bufferEnd = bufferBegin + source->readBytes(pc, buffer.get(), std::min(bufferSize, end - pc)); } auto instruction = disassembleSingleInstruction(pc, buffer.get() + (pc - bufferBegin), bufferEnd - pc); if (instruction) { assert(instruction->size() > 0); pc += instruction->size(); callback(std::move(instruction)); } else { ++pc; } } }
bool IntType::canCastTo (Type* symDestType) const { DEBUG_REQUIRE (symDestType != NULL); if (st::isa<st::IntType>(symDestType)) { return symDestType->hasByteSize() && ByteSize() <= symDestType->ByteSize(); } return false; }
void CVirtualDrawDevice::ConstructL(TDisplayMode aDisplayMode) { // Attempt to create a screen device CFbsDrawDevice* drawDevice = NULL; TRAPD(ret, drawDevice = CFbsDrawDevice::NewScreenDeviceL(KDefaultScreenNo, aDisplayMode)); if (ret != KErrNone) { // Screen device cannot be created so create an off screen bitmap draw device iSize = GetDisplaySizeInPixels(); iDrawDevice = CFbsDrawDevice::NewBitmapDeviceL(iSize, aDisplayMode, ByteSize(iSize, aDisplayMode) / iSize.iHeight); iDeviceMemory = new (ELeave) TUint8[ByteSize(iSize, aDisplayMode)]; iDrawDevice->SetBits(iDeviceMemory); iIsDrawDevice = EFalse; } else { iDrawDevice = drawDevice; iSize = iDrawDevice->SizeInPixels(); iDrawDevice->SetAutoUpdate(ETrue); iIsDrawDevice = ETrue; } }
TEST_F(PullSerializerTest, SerializationThreading) { Trajectory read_trajectory; auto const trajectory = BuildTrajectory(); int const byte_size = trajectory->ByteSize(); auto expected_serialized_trajectory = std::make_unique<std::uint8_t[]>(byte_size); trajectory->SerializePartialToArray(&expected_serialized_trajectory[0], byte_size); // Run this test repeatedly to detect threading issues (it will flake in case // of problems). for (int i = 0; i < kRunsPerTest; ++i) { auto trajectory = BuildTrajectory(); auto actual_serialized_trajectory = std::make_unique<std::uint8_t[]>(byte_size); std::uint8_t* data = &actual_serialized_trajectory[0]; // The serialization happens concurrently with the test. pull_serializer_ = std::make_unique<PullSerializer>(kChunkSize, kNumberOfChunks); pull_serializer_->Start(std::move(trajectory)); for (;;) { Bytes const bytes = pull_serializer_->Pull(); std::memcpy(data, bytes.data, static_cast<size_t>(bytes.size)); data = &data[bytes.size]; if (bytes.size == 0) { break; } } pull_serializer_.reset(); // Check if the serialized version can be parsed and if not print the first // difference. if (!read_trajectory.ParseFromArray(&actual_serialized_trajectory[0], byte_size)) { for (int i = 0; i < byte_size; ++i) { if (expected_serialized_trajectory[i] != actual_serialized_trajectory[i]) { LOG(FATAL) << "position=" << i << ", expected=" << static_cast<int>(expected_serialized_trajectory[i]) << ", actual=" << static_cast<int>(actual_serialized_trajectory[i]); } } } } }
void List<T>::Reserve( int capacity ) { assert( capacity > 0 ); if( capacity <= maxSize ) return; auto byteLen = (int)Round2n( capacity * sizeof( T ) ); maxSize = byteLen / sizeof( T ); auto newBuf = ( T* )new char[ byteLen ]; if( std::is_pod<T>::value || std::is_base_of<Memmoveable, T>::value ) memcpy( newBuf, buf, ByteSize() ); else for( int i = 0; i < size; ++i ) new ( newBuf + i ) T( std::move( buf[ i ] ) ); delete[]( char* )buf; buf = newBuf; }
status_t DwarfType::ResolveObjectDataLocation(const ValueLocation& objectLocation, ValueLocation*& _location) { // TODO: In some source languages the object address might be a pointer // to a descriptor, not the actual object data. // If the given location looks good already, just clone it. int32 count = objectLocation.CountPieces(); if (count == 0) return B_BAD_VALUE; ValuePieceLocation piece = objectLocation.PieceAt(0); if (count > 1 || piece.type != VALUE_PIECE_LOCATION_MEMORY || piece.size != 0 || piece.bitSize != 0) { ValueLocation* location = new(std::nothrow) ValueLocation(objectLocation); if (location == NULL || location->CountPieces() != count) { delete location; return B_NO_MEMORY; } _location = location; return B_OK; } // The location contains just a single address piece with a zero size // -- set the type's size. piece.SetSize(ByteSize()); // TODO: Use bit size and bit offset, if specified! ValueLocation* location = new(std::nothrow) ValueLocation( objectLocation.IsBigEndian()); if (location == NULL || !location->AddPiece(piece)) { delete location; return B_NO_MEMORY; } _location = location; return B_OK; }
bool Hprose_Message::Serialize(void * buf, std::size_t len) { if (len < ByteSize()) return false; memcpy(buf, body_.data(), len); return true; }
} // Destroying the deserializer waits until deserialization is done. It is // important that this happens before |storage| is destroyed. pull_serializer_.reset(); push_deserializer_.reset(); } } // Check that deserialization fails if we stomp on one extra bytes. TEST_F(PushDeserializerDeathTest, Stomp) { EXPECT_DEATH({ const int kStompChunk = 77; auto read_trajectory = make_not_null_unique<Trajectory>(); auto const trajectory = BuildTrajectory(); int const byte_size = trajectory->ByteSize(); auto serialized_trajectory = std::make_unique<std::uint8_t[]>(byte_size); trajectory->SerializePartialToArray(&serialized_trajectory[0], byte_size); push_deserializer_->Start( std::move(read_trajectory), &PushDeserializerTest::CheckSerialization); int left = byte_size; for (int i = 0; i < byte_size; i += kStompChunk) { Bytes bytes(&serialized_trajectory[i], std::min(left, kStompChunk)); push_deserializer_->Push(bytes, std::bind(&PushDeserializerTest::Stomp, Bytes(bytes.data, bytes.size + 1))); left -= kStompChunk; } push_deserializer_->Push(Bytes(), nullptr); push_deserializer_.reset();
status_t DwarfCompoundType::ResolveDataMemberLocation(DataMember* _member, const ValueLocation& parentLocation, ValueLocation*& _location) { DwarfDataMember* member = dynamic_cast<DwarfDataMember*>(_member); if (member == NULL) return B_BAD_VALUE; DwarfTypeContext* typeContext = TypeContext(); bool isBitField = true; DIEMember* memberEntry = member->Entry(); // TODO: handle DW_AT_data_bit_offset if (!memberEntry->ByteSize()->IsValid() && !memberEntry->BitOffset()->IsValid() && !memberEntry->BitSize()->IsValid()) { isBitField = false; } ValueLocation* location; status_t error = _ResolveDataMemberLocation(member->GetDwarfType(), member->Entry()->Location(), parentLocation, isBitField, location); if (error != B_OK) return error; // If the member isn't a bit field, we're done. if (!isBitField) { _location = location; return B_OK; } BReference<ValueLocation> locationReference(location); // get the byte size target_addr_t byteSize; if (memberEntry->ByteSize()->IsValid()) { BVariant value; error = typeContext->File()->EvaluateDynamicValue( typeContext->GetCompilationUnit(), typeContext->AddressSize(), typeContext->SubprogramEntry(), memberEntry->ByteSize(), typeContext->TargetInterface(), typeContext->InstructionPointer(), typeContext->FramePointer(), value); if (error != B_OK) return error; byteSize = value.ToUInt64(); } else byteSize = ByteSize(); // get the bit offset uint64 bitOffset = 0; if (memberEntry->BitOffset()->IsValid()) { BVariant value; error = typeContext->File()->EvaluateDynamicValue( typeContext->GetCompilationUnit(), typeContext->AddressSize(), typeContext->SubprogramEntry(), memberEntry->BitOffset(), typeContext->TargetInterface(), typeContext->InstructionPointer(), typeContext->FramePointer(), value); if (error != B_OK) return error; bitOffset = value.ToUInt64(); } // get the bit size uint64 bitSize = byteSize * 8; if (memberEntry->BitSize()->IsValid()) { BVariant value; error = typeContext->File()->EvaluateDynamicValue( typeContext->GetCompilationUnit(), typeContext->AddressSize(), typeContext->SubprogramEntry(), memberEntry->BitSize(), typeContext->TargetInterface(), typeContext->InstructionPointer(), typeContext->FramePointer(), value); if (error != B_OK) return error; bitSize = value.ToUInt64(); } TRACE_LOCALS("bit field: byte size: %" B_PRIu64 ", bit offset/size: %" B_PRIu64 "/%" B_PRIu64 "\n", byteSize, bitOffset, bitSize); if (bitOffset + bitSize > byteSize * 8) return B_BAD_VALUE; // create the bit field value location ValueLocation* bitFieldLocation = new(std::nothrow) ValueLocation; if (bitFieldLocation == NULL) return B_NO_MEMORY; BReference<ValueLocation> bitFieldLocationReference(bitFieldLocation, true); if (!bitFieldLocation->SetTo(*location, bitOffset, bitSize)) return B_NO_MEMORY; _location = bitFieldLocationReference.Detach(); return B_OK; }
status_t DwarfType::ResolveLocation(DwarfTypeContext* typeContext, const LocationDescription* description, target_addr_t objectAddress, bool hasObjectAddress, ValueLocation& _location) { status_t error = typeContext->File()->ResolveLocation( typeContext->GetCompilationUnit(), typeContext->AddressSize(), typeContext->SubprogramEntry(), description, typeContext->TargetInterface(), typeContext->InstructionPointer(), objectAddress, hasObjectAddress, typeContext->FramePointer(), typeContext->RelocationDelta(), _location); if (error != B_OK) return error; // translate the DWARF register indices and the bit offset/size semantics const Register* registers = typeContext->GetArchitecture()->Registers(); bool bigEndian = typeContext->GetArchitecture()->IsBigEndian(); int32 count = _location.CountPieces(); for (int32 i = 0; i < count; i++) { ValuePieceLocation piece = _location.PieceAt(i); if (piece.type == VALUE_PIECE_LOCATION_REGISTER) { int32 reg = typeContext->FromDwarfRegisterMap()->MapRegisterIndex( piece.reg); if (reg >= 0) { piece.reg = reg; // The bit offset for registers is to the least // significant bit, while we want the offset to the most // significant bit. if (registers[reg].BitSize() > piece.bitSize) { piece.bitOffset = registers[reg].BitSize() - piece.bitSize - piece.bitOffset; } } else piece.SetToUnknown(); } else if (piece.type == VALUE_PIECE_LOCATION_MEMORY) { // Whether the bit offset is to the least or most significant bit // is target architecture and source language specific. // TODO: Check whether this is correct! // TODO: Source language! if (!bigEndian && piece.size * 8 > piece.bitSize) { piece.bitOffset = piece.size * 8 - piece.bitSize - piece.bitOffset; } } piece.Normalize(bigEndian); _location.SetPieceAt(i, piece); } // If we only have one piece and that doesn't have a size, try to retrieve // the size of the type. if (count == 1) { ValuePieceLocation piece = _location.PieceAt(0); if (piece.IsValid() && piece.size == 0 && piece.bitSize == 0) { piece.SetSize(ByteSize()); // TODO: Use bit size and bit offset, if specified! _location.SetPieceAt(0, piece); TRACE_LOCALS(" set single piece size to %" B_PRIu64 "\n", ByteSize()); } } return B_OK; }
ByteSize ByteSize::operator+(const ByteSize & other) { return ByteSize(bytes() + other.bytes()); }