OLD | NEW |
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/bitmap.h" | 5 #include "vm/bitmap.h" |
6 | 6 |
7 #include "platform/assert.h" | 7 #include "platform/assert.h" |
8 #include "vm/object.h" | 8 #include "vm/object.h" |
9 | 9 |
10 namespace dart { | 10 namespace dart { |
(...skipping 12 matching lines...) Expand all Loading... |
23 // Clear the rest. | 23 // Clear the rest. |
24 ++byte_offset; | 24 ++byte_offset; |
25 if (byte_offset < data_size_in_bytes_) { | 25 if (byte_offset < data_size_in_bytes_) { |
26 memset(&data_[byte_offset], 0, data_size_in_bytes_ - byte_offset); | 26 memset(&data_[byte_offset], 0, data_size_in_bytes_ - byte_offset); |
27 } | 27 } |
28 } | 28 } |
29 } | 29 } |
30 length_ = new_length; | 30 length_ = new_length; |
31 } | 31 } |
32 | 32 |
33 | |
34 bool BitmapBuilder::Get(intptr_t bit_offset) const { | 33 bool BitmapBuilder::Get(intptr_t bit_offset) const { |
35 if (!InRange(bit_offset)) { | 34 if (!InRange(bit_offset)) { |
36 return false; | 35 return false; |
37 } | 36 } |
38 intptr_t byte_offset = bit_offset >> kBitsPerByteLog2; | 37 intptr_t byte_offset = bit_offset >> kBitsPerByteLog2; |
39 // Bits not covered by the backing store are implicitly false. | 38 // Bits not covered by the backing store are implicitly false. |
40 return (byte_offset < data_size_in_bytes_) && GetBit(bit_offset); | 39 return (byte_offset < data_size_in_bytes_) && GetBit(bit_offset); |
41 } | 40 } |
42 | 41 |
43 | |
44 void BitmapBuilder::Set(intptr_t bit_offset, bool value) { | 42 void BitmapBuilder::Set(intptr_t bit_offset, bool value) { |
45 if (!InRange(bit_offset)) { | 43 if (!InRange(bit_offset)) { |
46 length_ = bit_offset + 1; | 44 length_ = bit_offset + 1; |
47 // Bits not covered by the backing store are implicitly false. | 45 // Bits not covered by the backing store are implicitly false. |
48 if (!value) return; | 46 if (!value) return; |
49 // Grow the backing store if necessary. | 47 // Grow the backing store if necessary. |
50 intptr_t byte_offset = bit_offset >> kBitsPerByteLog2; | 48 intptr_t byte_offset = bit_offset >> kBitsPerByteLog2; |
51 if (byte_offset >= data_size_in_bytes_) { | 49 if (byte_offset >= data_size_in_bytes_) { |
52 uint8_t* old_data = data_; | 50 uint8_t* old_data = data_; |
53 intptr_t old_size = data_size_in_bytes_; | 51 intptr_t old_size = data_size_in_bytes_; |
54 data_size_in_bytes_ = | 52 data_size_in_bytes_ = |
55 Utils::RoundUp(byte_offset + 1, kIncrementSizeInBytes); | 53 Utils::RoundUp(byte_offset + 1, kIncrementSizeInBytes); |
56 ASSERT(data_size_in_bytes_ > 0); | 54 ASSERT(data_size_in_bytes_ > 0); |
57 data_ = Thread::Current()->zone()->Alloc<uint8_t>(data_size_in_bytes_); | 55 data_ = Thread::Current()->zone()->Alloc<uint8_t>(data_size_in_bytes_); |
58 ASSERT(data_ != NULL); | 56 ASSERT(data_ != NULL); |
59 memmove(data_, old_data, old_size); | 57 memmove(data_, old_data, old_size); |
60 memset(&data_[old_size], 0, (data_size_in_bytes_ - old_size)); | 58 memset(&data_[old_size], 0, (data_size_in_bytes_ - old_size)); |
61 } | 59 } |
62 } | 60 } |
63 SetBit(bit_offset, value); | 61 SetBit(bit_offset, value); |
64 } | 62 } |
65 | 63 |
66 | |
67 void BitmapBuilder::SetRange(intptr_t min, intptr_t max, bool value) { | 64 void BitmapBuilder::SetRange(intptr_t min, intptr_t max, bool value) { |
68 for (intptr_t i = min; i <= max; i++) { | 65 for (intptr_t i = min; i <= max; i++) { |
69 Set(i, value); | 66 Set(i, value); |
70 } | 67 } |
71 } | 68 } |
72 | 69 |
73 | |
74 void BitmapBuilder::Print() const { | 70 void BitmapBuilder::Print() const { |
75 for (intptr_t i = 0; i < Length(); i++) { | 71 for (intptr_t i = 0; i < Length(); i++) { |
76 if (Get(i)) { | 72 if (Get(i)) { |
77 OS::Print("1"); | 73 OS::Print("1"); |
78 } else { | 74 } else { |
79 OS::Print("0"); | 75 OS::Print("0"); |
80 } | 76 } |
81 } | 77 } |
82 } | 78 } |
83 | 79 |
84 | |
85 bool BitmapBuilder::GetBit(intptr_t bit_offset) const { | 80 bool BitmapBuilder::GetBit(intptr_t bit_offset) const { |
86 if (!InRange(bit_offset)) { | 81 if (!InRange(bit_offset)) { |
87 return false; | 82 return false; |
88 } | 83 } |
89 intptr_t byte_offset = bit_offset >> kBitsPerByteLog2; | 84 intptr_t byte_offset = bit_offset >> kBitsPerByteLog2; |
90 ASSERT(byte_offset < data_size_in_bytes_); | 85 ASSERT(byte_offset < data_size_in_bytes_); |
91 intptr_t bit_remainder = bit_offset & (kBitsPerByte - 1); | 86 intptr_t bit_remainder = bit_offset & (kBitsPerByte - 1); |
92 uint8_t mask = 1U << bit_remainder; | 87 uint8_t mask = 1U << bit_remainder; |
93 ASSERT(data_ != NULL); | 88 ASSERT(data_ != NULL); |
94 return ((data_[byte_offset] & mask) != 0); | 89 return ((data_[byte_offset] & mask) != 0); |
95 } | 90 } |
96 | 91 |
97 | |
98 void BitmapBuilder::SetBit(intptr_t bit_offset, bool value) { | 92 void BitmapBuilder::SetBit(intptr_t bit_offset, bool value) { |
99 if (!InRange(bit_offset)) { | 93 if (!InRange(bit_offset)) { |
100 FATAL1( | 94 FATAL1( |
101 "Fatal error in BitmapBuilder::SetBit :" | 95 "Fatal error in BitmapBuilder::SetBit :" |
102 " invalid bit_offset, %" Pd "\n", | 96 " invalid bit_offset, %" Pd "\n", |
103 bit_offset); | 97 bit_offset); |
104 } | 98 } |
105 intptr_t byte_offset = bit_offset >> kBitsPerByteLog2; | 99 intptr_t byte_offset = bit_offset >> kBitsPerByteLog2; |
106 ASSERT(byte_offset < data_size_in_bytes_); | 100 ASSERT(byte_offset < data_size_in_bytes_); |
107 intptr_t bit_remainder = bit_offset & (kBitsPerByte - 1); | 101 intptr_t bit_remainder = bit_offset & (kBitsPerByte - 1); |
108 uint8_t mask = 1U << bit_remainder; | 102 uint8_t mask = 1U << bit_remainder; |
109 ASSERT(data_ != NULL); | 103 ASSERT(data_ != NULL); |
110 if (value) { | 104 if (value) { |
111 data_[byte_offset] |= mask; | 105 data_[byte_offset] |= mask; |
112 } else { | 106 } else { |
113 data_[byte_offset] &= ~mask; | 107 data_[byte_offset] &= ~mask; |
114 } | 108 } |
115 } | 109 } |
116 | 110 |
117 } // namespace dart | 111 } // namespace dart |
OLD | NEW |