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 be
4 * found in the LICENSE file.
5 * Copyright 2009, Google Inc.
8 #include "native_client/src/trusted/validator_arm/address_set.h"
12 namespace nacl_arm_val {
14 AddressSet::AddressSet(uint32_t base, uint32_t size)
15 : base_(base), size_(size), bits_(new uint32_t[(size + 3) / 4]) {
16 memset(bits_, 0, sizeof(uint32_t) * ((size + 3) / 4));
19 AddressSet::~AddressSet() {
23 void AddressSet::add(uint32_t address) {
24 if ((address - base_) < size_) {
25 uint32_t word_address = (address - base_) / sizeof(uint32_t);
27 bits_[word_address / 32] |= 1 << (word_address % 32);
31 bool AddressSet::contains(uint32_t address) const {
32 if ((address - base_) < size_) {
33 uint32_t word_address = (address - base_) / sizeof(uint32_t);
35 return bits_[word_address / 32] & (1 << (word_address % 32));
41 AddressSet::Iterator AddressSet::begin() const {
42 return Iterator(*this, 0, 0);
45 AddressSet::Iterator AddressSet::end() const {
46 return Iterator(*this, (size_ + 3) / 4, 0);
49 AddressSet::Iterator::Iterator(const AddressSet &parent,
52 : parent_(parent), index_(index), shift_(shift) {
56 AddressSet::Iterator &AddressSet::Iterator::operator++() {
57 shift_++; // Skip the current bit, if any, and
58 advance(); // seek to the next 1 bit.
62 bool AddressSet::Iterator::operator!=(const AddressSet::Iterator &other) const {
63 return index_ != other.index_ || shift_ != other.shift_;
66 uint32_t AddressSet::Iterator::operator*() const {
67 return parent_.base_ + 4 * ((index_ * 32) + shift_);
70 void AddressSet::Iterator::advance() {
71 uint32_t max_index = (parent_.size_ + 3) / 4;
73 for (; index_ < max_index; index_++) {
74 uint32_t word = (shift_ > 31)? 0 : parent_.bits_[index_] >> shift_;
78 // A meager optimization for sparse words
79 if (!(word & 0xFFFF)) {
82 } else if (!(word & 0xFF)) {