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

Side by Side Diff: runtime/lib/expando_patch.dart

Issue 18051007: - Revert r24564 and r24563 due to unexplained malloc corruption. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 7 years, 5 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 | « no previous file | runtime/lib/object.cc » ('j') | 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 patch class Expando<T> { 5 patch class Expando<T> {
6 /* patch */ Expando([this.name]) 6 /* patch */ Expando([this.name]) : _data = new List();
7 : _data = new List(_minSize),
8 _used = 0;
9
10 static const _minSize = 8;
11 static final _deletedEntry = new _WeakProperty(null, null);
12 7
13 /* patch */ T operator[](Object object) { 8 /* patch */ T operator[](Object object) {
14 _checkType(object); 9 _checkType(object);
15 10 var doCompact = false;
16 var mask = _size - 1; 11 var result = null;
17 var idx = object.hashCode & mask; 12 for (int i = 0; i < _data.length; ++i) {
18 var wp = _data[idx]; 13 var key = _data[i].key;
19 14 if (identical(key, object)) {
20 while (wp != null) { 15 result = _data[i].value;
21 if (identical(wp.key, object)) { 16 break;
22 return wp.value;
23 } else if (wp.key == null) {
24 // This entry has been cleared by the GC.
25 _data[idx] = _deletedEntry;
26 } 17 }
27 idx = (idx + 1) & mask; 18 if (key == null) {
28 wp = _data[idx]; 19 doCompact = true;
20 _data[i] = null;
21 }
29 } 22 }
30 23 if (doCompact) {
31 return null; 24 _data = _data.where((e) => (e != null)).toList();
25 }
26 return result;
32 } 27 }
33 28
34 /* patch */ void operator[]=(Object object, T value) { 29 /* patch */ void operator[]=(Object object, T value) {
35 _checkType(object); 30 _checkType(object);
36 31 var doCompact = false;
37 var mask = _size - 1; 32 int i = 0;
38 var idx = object.hashCode & mask; 33 for (; i < _data.length; ++i) {
39 var empty_idx = -1; 34 var key = _data[i].key;
40 var wp = _data[idx]; 35 if (identical(key, object)) {
41 36 break;
42 while (wp != null) {
43 if (identical(wp.key, object)) {
44 if (value != null) {
45 // Update the associated value.
46 wp.value = value;
47 } else {
48 // Mark the entry as deleted.
49 _data[idx] = _deletedEntry;
50 }
51 return;
52 } else if ((empty_idx < 0) && identical(wp, _deletedEntry)) {
53 empty_idx = idx; // Insert at this location if not found.
54 } else if (wp.key == null) {
55 // This entry has been cleared by the GC.
56 _data[idx] = _deletedEntry;
57 if (empty_idx < 0) {
58 empty_idx = idx; // Insert at this location if not found.
59 }
60 } 37 }
61 idx = (idx + 1) & mask; 38 if (key == null) {
62 wp = _data[idx]; 39 doCompact = true;
63 } 40 _data[i] = null;
64
65 if (value == null) {
66 // Not entering a null value. We just needed to make sure to clear an
67 // existing value if it existed.
68 return;
69 }
70
71 if (empty_idx >= 0) {
72 // We will be reusing the empty slot below.
73 _used--;
74 idx = empty_idx;
75 }
76
77 if (_used < _limit) {
78 _data[idx] = new _WeakProperty(object, value);
79 _used++;
80 return;
81 }
82
83 // Grow/reallocate if too many slots have been used.
84 _rehash();
85 this[object] = value; // Recursively add the value.
86 }
87
88 _rehash() {
89 // Determine the population count of the map to allocate an appropriately
90 // sized map below.
91 var count = 0;
92 var old_data = _data;
93 var len = old_data.length;
94 for (var i = 0; i < len; i++) {
95 var entry = old_data[i];
96 if ((entry != null) && (entry.key != null)) {
97 // Only count non-cleared entries.
98 count++;
99 } 41 }
100 } 42 }
101 43 if (i != _data.length && value == null) {
102 var new_size = _size; 44 doCompact = true;
103 if (count <= (new_size >> 2)) { 45 _data[i] = null;
104 new_size = new_size >> 1; 46 } else if (i != _data.length) {
105 } else if (count > (new_size >> 1)) { 47 _data[i].value = value;
106 new_size = new_size << 1; 48 } else {
49 _data.add(new _WeakProperty(object, value));
107 } 50 }
108 new_size = (new_size < _minSize) ? _minSize : new_size; 51 if (doCompact) {
109 52 _data = _data.where((e) => (e != null)).toList();
110 // Reset the mappings to empty so that we can just add the existing
111 // valid entries.
112 _data = new List(new_size);
113 _used = 0;
114
115 for (var i = 0; i < old_data.length; i++) {
116 var entry = old_data[i];
117 if ((entry != null) && (entry.key != null)) {
118 this[entry.key] = entry.value;
119 }
120 } 53 }
121 } 54 }
122 55
123 static _checkType(object) { 56 static _checkType(object) {
124 if ((object == null) || 57 if (object == null || object is bool || object is num || object is String) {
125 (object is bool) ||
126 (object is num) ||
127 (object is String)) {
128 throw new ArgumentError(object); 58 throw new ArgumentError(object);
129 } 59 }
130 } 60 }
131 61
132 get _size => _data.length;
133 get _limit => (3 * (_size ~/ 4));
134
135 List _data; 62 List _data;
136 int _used; // Number of used (active and deleted) slots.
137 } 63 }
OLDNEW
« no previous file with comments | « no previous file | runtime/lib/object.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698