From 93205e395f777c1dd81d3f164cf9a4aec4bde45f Mon Sep 17 00:00:00 2001 From: Vladimir Marko Date: Wed, 13 Apr 2016 11:59:46 +0100 Subject: Move Assemblers to the Arena. And clean up some APIs to return std::unique_ptr<> instead of raw pointers that don't communicate ownership. Change-Id: I3017302307a0253d661240750298802fb0d9585e --- compiler/utils/assembler.cc | 55 +++++++++++++++++++++------------------------ 1 file changed, 25 insertions(+), 30 deletions(-) (limited to 'compiler/utils/assembler.cc') diff --git a/compiler/utils/assembler.cc b/compiler/utils/assembler.cc index f784d2c3f8..c2aa574f76 100644 --- a/compiler/utils/assembler.cc +++ b/compiler/utils/assembler.cc @@ -44,14 +44,10 @@ namespace art { -static uint8_t* NewContents(size_t capacity) { - return new uint8_t[capacity]; -} - - -AssemblerBuffer::AssemblerBuffer() { +AssemblerBuffer::AssemblerBuffer(ArenaAllocator* arena) + : arena_(arena) { static const size_t kInitialBufferCapacity = 4 * KB; - contents_ = NewContents(kInitialBufferCapacity); + contents_ = arena_->AllocArray(kInitialBufferCapacity); cursor_ = contents_; limit_ = ComputeLimit(contents_, kInitialBufferCapacity); fixup_ = nullptr; @@ -68,7 +64,9 @@ AssemblerBuffer::AssemblerBuffer() { AssemblerBuffer::~AssemblerBuffer() { - delete[] contents_; + if (arena_->IsRunningOnMemoryTool()) { + arena_->MakeInaccessible(contents_, Capacity()); + } } @@ -100,19 +98,12 @@ void AssemblerBuffer::ExtendCapacity(size_t min_capacity) { new_capacity = std::max(new_capacity, min_capacity); // Allocate the new data area and copy contents of the old one to it. - uint8_t* new_contents = NewContents(new_capacity); - memmove(reinterpret_cast(new_contents), - reinterpret_cast(contents_), - old_size); - - // Compute the relocation delta and switch to the new contents area. - ptrdiff_t delta = new_contents - contents_; - delete[] contents_; - contents_ = new_contents; + contents_ = reinterpret_cast( + arena_->Realloc(contents_, old_capacity, new_capacity, kArenaAllocAssembler)); // Update the cursor and recompute the limit. - cursor_ += delta; - limit_ = ComputeLimit(new_contents, new_capacity); + cursor_ = contents_ + old_size; + limit_ = ComputeLimit(contents_, new_capacity); // Verify internal state. CHECK_EQ(Capacity(), new_capacity); @@ -129,36 +120,40 @@ void DebugFrameOpCodeWriterForAssembler::ImplicitlyAdvancePC() { } } -Assembler* Assembler::Create(InstructionSet instruction_set, - const InstructionSetFeatures* instruction_set_features) { +std::unique_ptr Assembler::Create( + ArenaAllocator* arena, + InstructionSet instruction_set, + const InstructionSetFeatures* instruction_set_features) { switch (instruction_set) { #ifdef ART_ENABLE_CODEGEN_arm case kArm: - return new arm::Arm32Assembler(); + return std::unique_ptr(new (arena) arm::Arm32Assembler(arena)); case kThumb2: - return new arm::Thumb2Assembler(); + return std::unique_ptr(new (arena) arm::Thumb2Assembler(arena)); #endif #ifdef ART_ENABLE_CODEGEN_arm64 case kArm64: - return new arm64::Arm64Assembler(); + return std::unique_ptr(new (arena) arm64::Arm64Assembler(arena)); #endif #ifdef ART_ENABLE_CODEGEN_mips case kMips: - return new mips::MipsAssembler(instruction_set_features != nullptr - ? instruction_set_features->AsMipsInstructionSetFeatures() - : nullptr); + return std::unique_ptr(new (arena) mips::MipsAssembler( + arena, + instruction_set_features != nullptr + ? instruction_set_features->AsMipsInstructionSetFeatures() + : nullptr)); #endif #ifdef ART_ENABLE_CODEGEN_mips64 case kMips64: - return new mips64::Mips64Assembler(); + return std::unique_ptr(new (arena) mips64::Mips64Assembler(arena)); #endif #ifdef ART_ENABLE_CODEGEN_x86 case kX86: - return new x86::X86Assembler(); + return std::unique_ptr(new (arena) x86::X86Assembler(arena)); #endif #ifdef ART_ENABLE_CODEGEN_x86_64 case kX86_64: - return new x86_64::X86_64Assembler(); + return std::unique_ptr(new (arena) x86_64::X86_64Assembler(arena)); #endif default: LOG(FATAL) << "Unknown InstructionSet: " << instruction_set; -- cgit v1.2.3-59-g8ed1b