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

Side by Side Diff: vm/class_table.cc

Issue 10311006: - Grow the class table as needed instead of preallocating. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/runtime/
Patch Set: Created 8 years, 7 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
« no previous file with comments | « vm/class_table.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 #include "vm/class_table.h" 5 #include "vm/class_table.h"
6 #include "vm/flags.h" 6 #include "vm/flags.h"
7 #include "vm/object.h" 7 #include "vm/object.h"
8 #include "vm/raw_object.h" 8 #include "vm/raw_object.h"
9 #include "vm/visitor.h" 9 #include "vm/visitor.h"
10 10
11 namespace dart { 11 namespace dart {
12 12
13 DEFINE_FLAG(bool, print_class_table, false, "Print initial class table."); 13 DEFINE_FLAG(bool, print_class_table, false, "Print initial class table.");
14 14
15 ClassTable::ClassTable() 15 ClassTable::ClassTable()
16 : top_(kNumPredefinedKinds), capacity_(initial_capacity_), table_(NULL) { 16 : top_(kNumPredefinedKinds), capacity_(0), table_(NULL) {
17 table_ = reinterpret_cast<RawClass**>(calloc(capacity_, 17 if (Dart::vm_isolate() == NULL) {
18 sizeof(RawClass*))); // NOLINT 18 capacity_ = initial_capacity_;
19 // Duplicate the class table from the VM isolate. 19 table_ = reinterpret_cast<RawClass**>(
20 if (Dart::vm_isolate() != NULL) { 20 calloc(capacity_, sizeof(RawClass*))); // NOLINT
21 } else {
22 // Duplicate the class table from the VM isolate.
21 ClassTable* vm_class_table = Dart::vm_isolate()->class_table(); 23 ClassTable* vm_class_table = Dart::vm_isolate()->class_table();
22 for (int i = kObject; i < kInstance; i++) { 24 capacity_ = vm_class_table->capacity_;
25 table_ = reinterpret_cast<RawClass**>(
26 calloc(capacity_, sizeof(RawClass*))); // NOLINT
27 for (intptr_t i = kObject; i < kInstance; i++) {
23 table_[i] = vm_class_table->At(i); 28 table_[i] = vm_class_table->At(i);
24 } 29 }
25 table_[kNullClassIndex] = vm_class_table->At(kNullClassIndex); 30 table_[kNullClassIndex] = vm_class_table->At(kNullClassIndex);
26 table_[kDynamicClassIndex] = vm_class_table->At(kDynamicClassIndex); 31 table_[kDynamicClassIndex] = vm_class_table->At(kDynamicClassIndex);
27 table_[kVoidClassIndex] = vm_class_table->At(kVoidClassIndex); 32 table_[kVoidClassIndex] = vm_class_table->At(kVoidClassIndex);
28 } 33 }
29 } 34 }
30 35
31 36
32 ClassTable::~ClassTable() { 37 ClassTable::~ClassTable() {
33 free(table_); 38 free(table_);
34 } 39 }
35 40
36 41
37 void ClassTable::Register(const Class& cls) { 42 void ClassTable::Register(const Class& cls) {
38 intptr_t index = cls.index(); 43 intptr_t index = cls.index();
39 if (index != kIllegalObjectKind) { 44 if (index != kIllegalObjectKind) {
40 ASSERT(index > 0); 45 ASSERT(index > 0);
41 ASSERT(index < kNumPredefinedKinds); 46 ASSERT(index < kNumPredefinedKinds);
42 ASSERT(table_[index] == 0); 47 ASSERT(table_[index] == 0);
48 ASSERT(index < capacity_);
43 table_[index] = cls.raw(); 49 table_[index] = cls.raw();
44 } else { 50 } else {
51 if (top_ == capacity_) {
52 // Grow the capacity of the class table.
53 intptr_t new_capacity = capacity_ + capacity_increment_;
54 RawClass** new_table = reinterpret_cast<RawClass**>(
55 realloc(table_, new_capacity * sizeof(RawClass*))); // NOLINT
56 for (intptr_t i = capacity_; i < new_capacity; i++) {
57 new_table[i] = NULL;
58 }
59 capacity_ = new_capacity;
60 table_ = new_table;
61 }
62 ASSERT(top_ < capacity_);
45 cls.set_index(top_); 63 cls.set_index(top_);
46 table_[top_] = cls.raw(); 64 table_[top_] = cls.raw();
47 top_++; // Increment next index. 65 top_++; // Increment next index.
48 ASSERT(top_ < capacity_);
49 } 66 }
50 } 67 }
51 68
52 69
53 void ClassTable::VisitObjectPointers(ObjectPointerVisitor* visitor) { 70 void ClassTable::VisitObjectPointers(ObjectPointerVisitor* visitor) {
54 ASSERT(visitor != NULL); 71 ASSERT(visitor != NULL);
55 visitor->VisitPointers(reinterpret_cast<RawObject**>(&table_[0]), top_); 72 visitor->VisitPointers(reinterpret_cast<RawObject**>(&table_[0]), top_);
56 } 73 }
57 74
58 75
59 void ClassTable::Print() { 76 void ClassTable::Print() {
60 Class& cls = Class::Handle(); 77 Class& cls = Class::Handle();
61 String& name = String::Handle(); 78 String& name = String::Handle();
62 79
63 for (intptr_t i = 1; i < top_; i++) { 80 for (intptr_t i = 1; i < top_; i++) {
64 cls = At(i); 81 cls = At(i);
65 if (cls.raw() != reinterpret_cast<RawClass*>(0)) { 82 if (cls.raw() != reinterpret_cast<RawClass*>(0)) {
66 name = cls.Name(); 83 name = cls.Name();
67 OS::Print("%d: %s\n", i, name.ToCString()); 84 OS::Print("%d: %s\n", i, name.ToCString());
68 } 85 }
69 } 86 }
70 } 87 }
71 88
72 } // namespace dart 89 } // namespace dart
OLDNEW
« no previous file with comments | « vm/class_table.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698