Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(334)

Side by Side Diff: courgette/assembly_program.cc

Issue 6677141: Switch out use of std::string and std::vector for large allocations for a buffer class that doesn... (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 9 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "courgette/assembly_program.h" 5 #include "courgette/assembly_program.h"
6 6
7 #include <memory.h> 7 #include <memory.h>
8 #include <algorithm> 8 #include <algorithm>
9 #include <map> 9 #include <map>
10 #include <set> 10 #include <set>
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
103 } 103 }
104 if (byte_instruction_cache_) { 104 if (byte_instruction_cache_) {
105 for (size_t i = 0; i < 256; ++i) 105 for (size_t i = 0; i < 256; ++i)
106 delete byte_instruction_cache_[i]; 106 delete byte_instruction_cache_[i];
107 delete[] byte_instruction_cache_; 107 delete[] byte_instruction_cache_;
108 } 108 }
109 DeleteContainedLabels(rel32_labels_); 109 DeleteContainedLabels(rel32_labels_);
110 DeleteContainedLabels(abs32_labels_); 110 DeleteContainedLabels(abs32_labels_);
111 } 111 }
112 112
113 void AssemblyProgram::EmitMakeRelocsInstruction() { 113 CheckBool AssemblyProgram::EmitMakeRelocsInstruction() {
114 Emit(new MakeRelocsInstruction()); 114 return Emit(new(std::nothrow) MakeRelocsInstruction());
115 } 115 }
116 116
117 void AssemblyProgram::EmitOriginInstruction(RVA rva) { 117 CheckBool AssemblyProgram::EmitOriginInstruction(RVA rva) {
118 Emit(new OriginInstruction(rva)); 118 return Emit(new(std::nothrow) OriginInstruction(rva));
119 } 119 }
120 120
121 void AssemblyProgram::EmitByteInstruction(uint8 byte) { 121 CheckBool AssemblyProgram::EmitByteInstruction(uint8 byte) {
122 Emit(GetByteInstruction(byte)); 122 return Emit(GetByteInstruction(byte));
123 } 123 }
124 124
125 void AssemblyProgram::EmitRel32(Label* label) { 125 CheckBool AssemblyProgram::EmitRel32(Label* label) {
126 Emit(new InstructionWithLabel(REL32, label)); 126 return Emit(new(std::nothrow) InstructionWithLabel(REL32, label));
127 } 127 }
128 128
129 void AssemblyProgram::EmitAbs32(Label* label) { 129 CheckBool AssemblyProgram::EmitAbs32(Label* label) {
130 Emit(new InstructionWithLabel(ABS32, label)); 130 return Emit(new(std::nothrow) InstructionWithLabel(ABS32, label));
131 } 131 }
132 132
133 Label* AssemblyProgram::FindOrMakeAbs32Label(RVA rva) { 133 Label* AssemblyProgram::FindOrMakeAbs32Label(RVA rva) {
134 return FindLabel(rva, &abs32_labels_); 134 return FindLabel(rva, &abs32_labels_);
135 } 135 }
136 136
137 Label* AssemblyProgram::FindOrMakeRel32Label(RVA rva) { 137 Label* AssemblyProgram::FindOrMakeRel32Label(RVA rva) {
138 return FindLabel(rva, &rel32_labels_); 138 return FindLabel(rva, &rel32_labels_);
139 } 139 }
140 140
(...skipping 21 matching lines...) Expand all
162 162
163 Label* AssemblyProgram::InstructionRel32Label( 163 Label* AssemblyProgram::InstructionRel32Label(
164 const Instruction* instruction) const { 164 const Instruction* instruction) const {
165 if (instruction->op() == REL32) 165 if (instruction->op() == REL32)
166 return static_cast<const InstructionWithLabel*>(instruction)->label(); 166 return static_cast<const InstructionWithLabel*>(instruction)->label();
167 return NULL; 167 return NULL;
168 } 168 }
169 169
170 Label* AssemblyProgram::FindLabel(RVA rva, RVAToLabel* labels) { 170 Label* AssemblyProgram::FindLabel(RVA rva, RVAToLabel* labels) {
171 Label*& slot = (*labels)[rva]; 171 Label*& slot = (*labels)[rva];
172 if (slot == 0) { 172 if (slot == 0) {
grt (UTC plus 2) 2011/04/05 17:10:08 use NULL rather than 0
tommi (sloooow) - chröme 2011/04/05 19:06:53 Done.
173 slot = new Label(rva); 173 slot = new(std::nothrow) Label(rva);
174 } 174 }
175 return slot; 175 return slot;
176 } 176 }
177 177
178 void AssemblyProgram::UnassignIndexes(RVAToLabel* labels) { 178 void AssemblyProgram::UnassignIndexes(RVAToLabel* labels) {
179 for (RVAToLabel::iterator p = labels->begin(); p != labels->end(); ++p) { 179 for (RVAToLabel::iterator p = labels->begin(); p != labels->end(); ++p) {
180 Label* current = p->second; 180 Label* current = p->second;
181 current->index_ = Label::kNoIndex; 181 current->index_ = Label::kNoIndex;
182 } 182 }
183 } 183 }
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after
300 for (RVAToLabel::const_iterator p = labels.begin(); 300 for (RVAToLabel::const_iterator p = labels.begin();
301 ok && p != labels.end(); 301 ok && p != labels.end();
302 ++p) { 302 ++p) {
303 Label* label = p->second; 303 Label* label = p->second;
304 ok = (encoded_format->*define_label)(label->index_, label->rva_); 304 ok = (encoded_format->*define_label)(label->index_, label->rva_);
305 } 305 }
306 return ok; 306 return ok;
307 } 307 }
308 308
309 EncodedProgram* AssemblyProgram::Encode() const { 309 EncodedProgram* AssemblyProgram::Encode() const {
310 scoped_ptr<EncodedProgram> encoded(new EncodedProgram()); 310 scoped_ptr<EncodedProgram> encoded(new(std::nothrow) EncodedProgram());
311 if (!encoded.get())
312 return NULL;
313
311 encoded->set_image_base(image_base_); 314 encoded->set_image_base(image_base_);
312 315
313 if (!DefineLabels(abs32_labels_, encoded.get(), 316 if (!DefineLabels(abs32_labels_, encoded.get(),
314 &EncodedProgram::DefineAbs32Label) || 317 &EncodedProgram::DefineAbs32Label) ||
315 !DefineLabels(rel32_labels_, encoded.get(), 318 !DefineLabels(rel32_labels_, encoded.get(),
316 &EncodedProgram::DefineRel32Label)) { 319 &EncodedProgram::DefineRel32Label)) {
317 return NULL; 320 return NULL;
318 } 321 }
319 322
320 encoded->EndLabels(); 323 encoded->EndLabels();
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
356 NOTREACHED() << "Unknown Insn OP kind"; 359 NOTREACHED() << "Unknown Insn OP kind";
357 } 360 }
358 } 361 }
359 } 362 }
360 363
361 return encoded.release(); 364 return encoded.release();
362 } 365 }
363 366
364 Instruction* AssemblyProgram::GetByteInstruction(uint8 byte) { 367 Instruction* AssemblyProgram::GetByteInstruction(uint8 byte) {
365 if (!byte_instruction_cache_) { 368 if (!byte_instruction_cache_) {
366 byte_instruction_cache_ = new Instruction*[256]; 369 byte_instruction_cache_ = new(std::nothrow) Instruction*[256];
370 if (!byte_instruction_cache_)
371 return NULL;
372
367 for (int i = 0; i < 256; ++i) { 373 for (int i = 0; i < 256; ++i) {
368 byte_instruction_cache_[i] = new ByteInstruction(static_cast<uint8>(i)); 374 byte_instruction_cache_[i] =
375 new(std::nothrow) ByteInstruction(static_cast<uint8>(i));
376 if (!byte_instruction_cache_[i]) {
377 for (int j = 0; j < i; ++j)
378 delete byte_instruction_cache_[j];
379 delete [] byte_instruction_cache_;
380 return NULL;
381 }
369 } 382 }
370 } 383 }
371 384
372 return byte_instruction_cache_[byte]; 385 return byte_instruction_cache_[byte];
373 } 386 }
374 387
375 //////////////////////////////////////////////////////////////////////////////// 388 ////////////////////////////////////////////////////////////////////////////////
376 389
377 Status Encode(AssemblyProgram* program, EncodedProgram** output) { 390 Status Encode(AssemblyProgram* program, EncodedProgram** output) {
378 *output = NULL; 391 *output = NULL;
379 EncodedProgram *encoded = program->Encode(); 392 EncodedProgram *encoded = program->Encode();
380 if (encoded) { 393 if (encoded) {
381 *output = encoded; 394 *output = encoded;
382 return C_OK; 395 return C_OK;
383 } else { 396 } else {
384 return C_GENERAL_ERROR; 397 return C_GENERAL_ERROR;
385 } 398 }
386 } 399 }
387 400
388 } // namespace courgette 401 } // namespace courgette
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698