OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright 2009 The Native Client Authors. All rights reserved. | 2 * Copyright 2009 The Native Client Authors. All rights reserved. |
3 * Use of this source code is governed by a BSD-style license that can | 3 * Use of this source code is governed by a BSD-style license that can |
4 * be found in the LICENSE file. | 4 * be found in the LICENSE file. |
5 * Copyright 2009, Google Inc. | 5 * Copyright 2009, Google Inc. |
6 */ | 6 */ |
7 | 7 |
8 #include "native_client/src/trusted/validator_arm/address_set.h" | 8 #include "native_client/src/trusted/validator_arm/address_set.h" |
9 #include <stdio.h> | 9 #include <stdio.h> |
10 #include <string.h> | 10 #include <string.h> |
11 | 11 |
12 #define ADDRFACTOR sizeof(uint16_t) | |
13 #define BITSINADDRF (ADDRFACTOR * 8) | |
14 #define RUPADDR (ADDRFACTOR - 1) | |
15 | |
12 namespace nacl_arm_val { | 16 namespace nacl_arm_val { |
13 | 17 |
14 AddressSet::AddressSet(uint32_t base, uint32_t size) | 18 AddressSet::AddressSet(uint32_t base, uint32_t size) |
15 : base_(base), size_(size), bits_(new uint32_t[(size + 3) / 4]) { | 19 : base_(base), size_(size), |
16 memset(bits_, 0, sizeof(uint32_t) * ((size + 3) / 4)); | 20 bits_(new uint32_t[(size + RUPADDR) / ADDRFACTOR]) { |
bsy
2011/09/28 00:31:13
as discussed, this is a bit vector the max size of
| |
21 memset(bits_, 0, sizeof(uint32_t) * ((size + RUPADDR) / ADDRFACTOR)); | |
17 } | 22 } |
18 | 23 |
19 AddressSet::~AddressSet() { | 24 AddressSet::~AddressSet() { |
20 delete[] bits_; | 25 delete[] bits_; |
21 } | 26 } |
22 | 27 |
23 void AddressSet::add(uint32_t address) { | 28 void AddressSet::add(uint32_t address) { |
24 if ((address - base_) < size_) { | 29 if ((address - base_) < size_) { |
25 uint32_t word_address = (address - base_) / sizeof(uint32_t); | 30 uint32_t word_address = (address - base_) / ADDRFACTOR; |
26 | 31 |
27 bits_[word_address / 32] |= 1 << (word_address % 32); | 32 bits_[word_address / 32] |= 1 << (word_address % 32); |
bsy
2011/09/27 23:27:28
this should be BITSINADDRF instead of 32 in both o
bsy
2011/09/28 00:31:13
this code was correct. my apologies.
it would be
| |
28 } | 33 } |
29 } | 34 } |
30 | 35 |
31 bool AddressSet::contains(uint32_t address) const { | 36 bool AddressSet::contains(uint32_t address) const { |
32 if ((address - base_) < size_) { | 37 if ((address - base_) < size_) { |
33 uint32_t word_address = (address - base_) / sizeof(uint32_t); | 38 uint32_t word_address = (address - base_) / ADDRFACTOR; |
34 | 39 |
35 return bits_[word_address / 32] & (1 << (word_address % 32)); | 40 return bits_[word_address / 32] & (1 << (word_address % 32)); |
bsy
2011/09/27 23:27:28
ditto
bsy
2011/09/28 00:31:13
ditto
| |
36 } else { | 41 } else { |
37 return false; | 42 return false; |
38 } | 43 } |
39 } | 44 } |
40 | 45 |
41 AddressSet::Iterator AddressSet::begin() const { | 46 AddressSet::Iterator AddressSet::begin() const { |
42 return Iterator(*this, 0, 0); | 47 return Iterator(*this, 0, 0); |
43 } | 48 } |
44 | 49 |
45 AddressSet::Iterator AddressSet::end() const { | 50 AddressSet::Iterator AddressSet::end() const { |
46 return Iterator(*this, (size_ + 3) / 4, 0); | 51 return Iterator(*this, (size_ + RUPADDR) / ADDRFACTOR, 0); |
47 } | 52 } |
48 | 53 |
49 AddressSet::Iterator::Iterator(const AddressSet &parent, | 54 AddressSet::Iterator::Iterator(const AddressSet &parent, |
50 uint32_t index, | 55 uint32_t index, |
51 uint32_t shift) | 56 uint32_t shift) |
52 : parent_(parent), index_(index), shift_(shift) { | 57 : parent_(parent), index_(index), shift_(shift) { |
53 advance(); | 58 advance(); |
54 } | 59 } |
55 | 60 |
56 AddressSet::Iterator &AddressSet::Iterator::operator++() { | 61 AddressSet::Iterator &AddressSet::Iterator::operator++() { |
57 shift_++; // Skip the current bit, if any, and | 62 shift_++; // Skip the current bit, if any, and |
58 advance(); // seek to the next 1 bit. | 63 advance(); // seek to the next 1 bit. |
59 return *this; | 64 return *this; |
60 } | 65 } |
61 | 66 |
62 bool AddressSet::Iterator::operator!=(const AddressSet::Iterator &other) const { | 67 bool AddressSet::Iterator::operator!=(const AddressSet::Iterator &other) const { |
63 return index_ != other.index_ || shift_ != other.shift_; | 68 return index_ != other.index_ || shift_ != other.shift_; |
64 } | 69 } |
65 | 70 |
66 uint32_t AddressSet::Iterator::operator*() const { | 71 uint32_t AddressSet::Iterator::operator*() const { |
67 return parent_.base_ + 4 * ((index_ * 32) + shift_); | 72 return parent_.base_ + ADDRFACTOR * ((index_ * 32) + shift_); |
bsy
2011/09/27 23:27:28
should this be 32?
| |
68 } | 73 } |
69 | 74 |
70 void AddressSet::Iterator::advance() { | 75 void AddressSet::Iterator::advance() { |
71 uint32_t max_index = (parent_.size_ + 3) / 4; | 76 uint32_t max_index = (parent_.size_ + RUPADDR) / ADDRFACTOR; |
72 | 77 |
73 for (; index_ < max_index; index_++) { | 78 for (; index_ < max_index; index_++) { |
74 uint32_t word = (shift_ > 31)? 0 : parent_.bits_[index_] >> shift_; | 79 uint32_t word = (shift_ > 31)? 0 : parent_.bits_[index_] >> shift_; |
75 while (word) { | 80 while (word) { |
76 if (word & 1) return; | 81 if (word & 1) return; |
77 | 82 |
78 // A meager optimization for sparse words | 83 // A meager optimization for sparse words |
79 if (!(word & 0xFFFF)) { | 84 if (!(word & 0xFFFF)) { |
80 word >>= 16; | 85 word >>= 16; |
81 shift_ += 16; | 86 shift_ += 16; |
82 } else if (!(word & 0xFF)) { | 87 } else if (!(word & 0xFF)) { |
83 word >>= 8; | 88 word >>= 8; |
84 shift_ += 8; | 89 shift_ += 8; |
85 } else { | 90 } else { |
86 word >>= 1; | 91 word >>= 1; |
87 shift_++; | 92 shift_++; |
88 } | 93 } |
89 } | 94 } |
90 shift_ = 0; | 95 shift_ = 0; |
91 } | 96 } |
92 } | 97 } |
93 | 98 |
94 } // namespace | 99 } // namespace |
OLD | NEW |