Stylistic changes for RangeMap (#24). r=bryner

http://groups.google.com/group/airbag-dev/browse_thread/thread/97c378bd175ab7c0


git-svn-id: http://google-breakpad.googlecode.com/svn/trunk@25 4c0a9323-5329-0410-9bdc-e9ce6186880e
This commit is contained in:
mmentovai 2006-09-14 19:19:24 +00:00
parent 683c86e6c5
commit d9fb68c3e0
2 changed files with 41 additions and 47 deletions

View File

@ -32,9 +32,6 @@
namespace google_airbag { namespace google_airbag {
using std::map;
template<typename AddressType, typename EntryType> template<typename AddressType, typename EntryType>
class RangeMap { class RangeMap {
public: public:
@ -43,13 +40,13 @@ class RangeMap {
// Inserts a range into the map. Returns false for a parameter error, // Inserts a range into the map. Returns false for a parameter error,
// or if the location of the range would conflict with a range already // or if the location of the range would conflict with a range already
// stored in the map. // stored in the map.
bool StoreRange(const AddressType& base, bool StoreRange(const AddressType &base,
const AddressType& size, const AddressType &size,
const EntryType& entry); const EntryType &entry);
// Locates the range encompassing the supplied address. If there is // Locates the range encompassing the supplied address. If there is
// no such range, or if there is a parameter error, returns false. // no such range, or if there is a parameter error, returns false.
bool RetrieveRange(const AddressType& address, EntryType* entry) const; bool RetrieveRange(const AddressType &address, EntryType *entry) const;
// Empties the range map, restoring it to the state it was when it was // Empties the range map, restoring it to the state it was when it was
// initially created. // initially created.
@ -58,7 +55,7 @@ class RangeMap {
private: private:
class Range { class Range {
public: public:
Range(const AddressType& base, const EntryType& entry) Range(const AddressType &base, const EntryType &entry)
: base_(base), entry_(entry) {} : base_(base), entry_(entry) {}
AddressType base() const { return base_; } AddressType base() const { return base_; }
@ -69,13 +66,12 @@ class RangeMap {
// be stored, because RangeMap uses it as the key to the map. // be stored, because RangeMap uses it as the key to the map.
const AddressType base_; const AddressType base_;
// The entry, owned by the Range object. // The entry corresponding to a range.
const EntryType entry_; const EntryType entry_;
}; };
typedef map<AddressType, Range> AddressToRangeMap; // Convenience types.
typedef std::map<AddressType, Range> AddressToRangeMap;
// Can't depend on implicit typenames in a template
typedef typename AddressToRangeMap::const_iterator const_iterator; typedef typename AddressToRangeMap::const_iterator const_iterator;
typedef typename AddressToRangeMap::value_type value_type; typedef typename AddressToRangeMap::value_type value_type;
@ -85,9 +81,9 @@ class RangeMap {
template<typename AddressType, typename EntryType> template<typename AddressType, typename EntryType>
bool RangeMap<AddressType, EntryType>::StoreRange(const AddressType& base, bool RangeMap<AddressType, EntryType>::StoreRange(const AddressType &base,
const AddressType& size, const AddressType &size,
const EntryType& entry) { const EntryType &entry) {
AddressType high = base + size - 1; AddressType high = base + size - 1;
// Check for undersize or overflow. // Check for undersize or overflow.
@ -124,8 +120,7 @@ bool RangeMap<AddressType, EntryType>::StoreRange(const AddressType& base,
template<typename AddressType, typename EntryType> template<typename AddressType, typename EntryType>
bool RangeMap<AddressType, EntryType>::RetrieveRange( bool RangeMap<AddressType, EntryType>::RetrieveRange(
const AddressType& address, const AddressType &address, EntryType *entry) const {
EntryType* entry) const {
if (!entry) if (!entry)
return false; return false;
@ -152,7 +147,7 @@ void RangeMap<AddressType, EntryType>::Clear() {
} }
} // namespace google_airbag } // namespace google_airbag
#endif // PROCESSOR_RANGE_MAP_H__ #endif // PROCESSOR_RANGE_MAP_H__

View File

@ -17,9 +17,8 @@
// Author: Mark Mentovai // Author: Mark Mentovai
#include <stdio.h>
#include <climits> #include <climits>
#include <cstdio>
#include <memory> #include <memory>
#include "processor/range_map.h" #include "processor/range_map.h"
@ -33,8 +32,8 @@ using google_airbag::RangeMap;
// allocated CountedObjects is maintained to help test memory management. // allocated CountedObjects is maintained to help test memory management.
class CountedObject { class CountedObject {
public: public:
CountedObject(int id) : id_(id) { ++count_; } explicit CountedObject(int id) : id_(id) { ++count_; }
CountedObject(const CountedObject& that) : id_(that.id_) { ++count_; } CountedObject(const CountedObject &that) : id_(that.id_) { ++count_; }
~CountedObject() { --count_; } ~CountedObject() { --count_; }
static int count() { return count_; } static int count() { return count_; }
@ -42,7 +41,7 @@ class CountedObject {
private: private:
static int count_; static int count_;
int id_; int id_;
}; };
int CountedObject::count_; int CountedObject::count_;
@ -62,10 +61,10 @@ struct RangeTest {
AddressType size; AddressType size;
// Unique ID of range - unstorable ranges must have unique IDs too // Unique ID of range - unstorable ranges must have unique IDs too
int id; int id;
// Whether this range is expected to be stored successfully or not // Whether this range is expected to be stored successfully or not
bool expect_storable; bool expect_storable;
}; };
@ -73,17 +72,17 @@ struct RangeTest {
// sequence on the same RangeMap. // sequence on the same RangeMap.
struct RangeTestSet { struct RangeTestSet {
// An array of RangeTests // An array of RangeTests
const RangeTest* range_tests; const RangeTest *range_tests;
// The number of tests in the set // The number of tests in the set
unsigned int range_test_count; unsigned int range_test_count;
}; };
// StoreTest uses the data in a RangeTest and calls StoreRange on the // StoreTest uses the data in a RangeTest and calls StoreRange on the
// test RangeMap. It returns true if the expected result occurred, and // test RangeMap. It returns true if the expected result occurred, and
// false if something else happened. // false if something else happened.
bool StoreTest(TestMap* range_map, const RangeTest* range_test) { bool StoreTest(TestMap *range_map, const RangeTest *range_test) {
CountedObject object(range_test->id); CountedObject object(range_test->id);
bool stored = range_map->StoreRange(range_test->address, bool stored = range_map->StoreRange(range_test->address,
range_test->size, range_test->size,
@ -107,7 +106,7 @@ bool StoreTest(TestMap* range_map, const RangeTest* range_test) {
// map entry at the specified range,) it returns true, otherwise, it returns // map entry at the specified range,) it returns true, otherwise, it returns
// false. RetrieveTest will check the values around the base address and // false. RetrieveTest will check the values around the base address and
// the high address of a range to guard against off-by-one errors. // the high address of a range to guard against off-by-one errors.
bool RetrieveTest(TestMap* range_map, const RangeTest* range_test) { bool RetrieveTest(TestMap *range_map, const RangeTest *range_test) {
for (unsigned int side = 0; side <= 1; ++side) { for (unsigned int side = 0; side <= 1; ++side) {
// When side == 0, check the low side (base address) of each range. // When side == 0, check the low side (base address) of each range.
// When side == 1, check the high side (base + size) of each range. // When side == 1, check the high side (base + size) of each range.
@ -122,10 +121,10 @@ bool RetrieveTest(TestMap* range_map, const RangeTest* range_test) {
AddressType low_offset = -1; AddressType low_offset = -1;
AddressType high_offset = 1; AddressType high_offset = 1;
if (range_test->size == 1) { if (range_test->size == 1) {
if (!side) // when checking the low side, if (!side) // When checking the low side,
high_offset = 0; // don't check one over the target high_offset = 0; // don't check one over the target.
else // when checking the high side, else // When checking the high side,
low_offset = 0; // don't check one under the target low_offset = 0; // don't check one under the target.
} }
for (AddressType offset = low_offset; offset <= high_offset; ++offset) { for (AddressType offset = low_offset; offset <= high_offset; ++offset) {
@ -134,14 +133,14 @@ bool RetrieveTest(TestMap* range_map, const RangeTest* range_test) {
(!side ? range_test->address : (!side ? range_test->address :
range_test->address + range_test->size - 1); range_test->address + range_test->size - 1);
bool expected_result = false; // correct for tests not stored bool expected_result = false; // This is correct for tests not stored.
if (range_test->expect_storable) { if (range_test->expect_storable) {
if (offset == 0) // when checking target, if (offset == 0) // When checking the target address,
expected_result = true; // should always succeed expected_result = true; // test should always succeed.
else if (offset == -1) // when checking one below target, else if (offset == -1) // When checking one below the target,
expected_result = side; // should fail low and succeed high expected_result = side; // should fail low and succeed high.
else // when checking one above target, else // When checking one above the target,
expected_result = !side; // should succeed low and fail high expected_result = !side; // should succeed low and fail high.
} }
CountedObject object(-1); CountedObject object(-1);
@ -149,7 +148,7 @@ bool RetrieveTest(TestMap* range_map, const RangeTest* range_test) {
bool observed_result = retrieved && object.id() == range_test->id; bool observed_result = retrieved && object.id() == range_test->id;
if (observed_result != expected_result) { if (observed_result != expected_result) {
fprintf(stderr, "FAILED: " fprintf(stderr, "FAILED: "
"RetrieveRange id %d, side %d, offset %d, " "RetrieveRange id %d, side %d, offset %d, "
"expected %s, observed %s\n", "expected %s, observed %s\n",
@ -267,7 +266,7 @@ bool RunTests() {
for (unsigned int range_test_set_index = 0; for (unsigned int range_test_set_index = 0;
range_test_set_index < range_test_set_count; range_test_set_index < range_test_set_count;
++range_test_set_index) { ++range_test_set_index) {
const RangeTest* range_tests = const RangeTest *range_tests =
range_test_sets[range_test_set_index].range_tests; range_test_sets[range_test_set_index].range_tests;
unsigned int range_test_count = unsigned int range_test_count =
range_test_sets[range_test_set_index].range_test_count; range_test_sets[range_test_set_index].range_test_count;
@ -278,7 +277,7 @@ bool RunTests() {
for (unsigned int range_test_index = 0; for (unsigned int range_test_index = 0;
range_test_index < range_test_count; range_test_index < range_test_count;
++range_test_index) { ++range_test_index) {
const RangeTest* range_test = &range_tests[range_test_index]; const RangeTest *range_test = &range_tests[range_test_index];
if (!StoreTest(range_map.get(), range_test)) if (!StoreTest(range_map.get(), range_test))
return false; return false;
@ -301,7 +300,7 @@ bool RunTests() {
for (unsigned int range_test_index = 0; for (unsigned int range_test_index = 0;
range_test_index < range_test_count; range_test_index < range_test_count;
++range_test_index) { ++range_test_index) {
const RangeTest* range_test = &range_tests[range_test_index]; const RangeTest *range_test = &range_tests[range_test_index];
if (!RetrieveTest(range_map.get(), range_test)) if (!RetrieveTest(range_map.get(), range_test))
return false; return false;
} }
@ -329,6 +328,6 @@ bool RunTests() {
return true; return true;
} }
int main(int argc, char** argv) { int main(int argc, char **argv) {
return RunTests() ? 0 : 1; return RunTests() ? 0 : 1;
} }