Commit 901d58a2 authored by David 'Digit' Turner's avatar David 'Digit' Turner Committed by Commit Bot

android: crazy_linker: Minor cleanups.

Minor cleanups to the code base. Preparing for future changes.

BUG=843987,802068
R=agrieve@chromium.org, pasko@chromium.org, rmcilroy@chromium.org

Change-Id: I65598259521b3e21b540cedadba99cfa58f13051
Reviewed-on: https://chromium-review.googlesource.com/1169480Reviewed-by: default avataragrieve <agrieve@chromium.org>
Reviewed-by: default avatarRoss McIlroy <rmcilroy@chromium.org>
Reviewed-by: default avatarEgor Pasko <pasko@chromium.org>
Commit-Queue: David Turner <digit@chromium.org>
Cr-Commit-Position: refs/heads/master@{#582088}
parent ac76759c
......@@ -116,7 +116,7 @@ if (is_android) {
"src/src/crazy_linker_system_mock.h",
]
defines = [
"UNIT_TESTS",
"UNIT_TEST",
"CRAZY_DEBUG=1",
]
} else {
......
......@@ -6,7 +6,8 @@
namespace crazy {
static SearchResult BinarySearch(const Vector<void*>& items, void* key) {
static SearchResult BinarySearch(const Vector<const void*>& items,
const void* key) {
auto key_val = reinterpret_cast<uintptr_t>(key);
size_t min = 0, max = items.GetCount();
while (min < max) {
......@@ -23,7 +24,7 @@ static SearchResult BinarySearch(const Vector<void*>& items, void* key) {
return {false, min};
}
bool PointerSet::Add(void* item) {
bool PointerSet::Add(const void* item) {
SearchResult ret = BinarySearch(items_, item);
if (ret.found)
return true;
......@@ -32,7 +33,7 @@ bool PointerSet::Add(void* item) {
return false;
}
bool PointerSet::Remove(void* item) {
bool PointerSet::Remove(const void* item) {
SearchResult ret = BinarySearch(items_, item);
if (!ret.found)
return false;
......@@ -41,7 +42,7 @@ bool PointerSet::Remove(void* item) {
return true;
}
bool PointerSet::Has(void* item) const {
bool PointerSet::Has(const void* item) const {
SearchResult ret = BinarySearch(items_, item);
return ret.found;
}
......
......@@ -15,23 +15,23 @@ class PointerSet {
PointerSet() = default;
// Add a new value to the set.
bool Add(void* item);
bool Add(const void* item);
// Remove value |item| from the set, if needed. Returns true if the value
// was previously in the set, false otherwise.
bool Remove(void* item);
bool Remove(const void* item);
// Returns true iff the set contains |item|, false otherwise.
bool Has(void* item) const;
bool Has(const void* item) const;
// Return a reference to the values in the set, only for testing.
const Vector<void*>& GetValuesForTesting() const { return items_; }
const Vector<const void*>& GetValuesForTesting() const { return items_; }
private:
// TECHNICAL NOTE: The current implementation uses a simple sorted array,
// and thus should perform well for sets of a few hundred items, when
// insertions and removals are pretty rare, but lookups need to be fast.
Vector<void*> items_;
Vector<const void*> items_;
};
} // namespace crazy
......
......@@ -12,7 +12,7 @@ TEST(PointerSet, DefaultIsEmpty) {
PointerSet set;
EXPECT_FALSE(set.Has(nullptr));
const Vector<void*>& values = set.GetValuesForTesting();
const Vector<const void*>& values = set.GetValuesForTesting();
EXPECT_TRUE(values.IsEmpty());
}
......@@ -24,7 +24,7 @@ TEST(PointerSet, Has) {
EXPECT_FALSE(set.Add(nullptr));
EXPECT_TRUE(set.Has(nullptr));
const Vector<void*>& values = set.GetValuesForTesting();
const Vector<const void*>& values = set.GetValuesForTesting();
EXPECT_FALSE(values.IsEmpty());
EXPECT_EQ(1U, values.GetCount());
EXPECT_EQ(nullptr, values[0]);
......@@ -45,7 +45,7 @@ TEST(PointerSet, Add) {
EXPECT_FALSE(set.Has(kTwo));
EXPECT_TRUE(set.Has(kTen));
const Vector<void*>& values = set.GetValuesForTesting();
const Vector<const void*>& values = set.GetValuesForTesting();
EXPECT_FALSE(values.IsEmpty());
EXPECT_EQ(2U, values.GetCount());
EXPECT_EQ(kOne, values[0]);
......@@ -67,7 +67,7 @@ TEST(PointerSet, Remove) {
EXPECT_TRUE(set.Has(kTwo));
EXPECT_TRUE(set.Has(kTen));
const Vector<void*>& values = set.GetValuesForTesting();
const Vector<const void*>& values = set.GetValuesForTesting();
EXPECT_FALSE(values.IsEmpty());
EXPECT_EQ(3U, values.GetCount());
......
......@@ -53,7 +53,7 @@ String MakeAbsolutePathFrom(const char* path, size_t path_len) {
} // namespace crazy
#ifndef UNIT_TESTS
#ifndef UNIT_TEST
namespace crazy {
......@@ -219,4 +219,4 @@ void operator delete[](void* ptr) {
#endif // !CRAZY_LINKER_ENABLE_FUZZING
#endif // !UNIT_TESTS
#endif // !UNIT_TEST
......@@ -341,7 +341,7 @@ MockSystem s_mock_fs;
namespace crazy {
#ifdef UNIT_TESTS
#ifdef UNIT_TEST
bool PathExists(const char* path) {
s_mock_fs.Check();
......@@ -464,6 +464,6 @@ void SystemMock::SetCurrentDir(const char* path) {
s_mock_fs.SetCurrentDir(path);
}
#endif // UNIT_TESTS
#endif // UNIT_TEST
} // namespace crazy
......@@ -115,6 +115,14 @@ void String::InitFrom(const char* str, size_t len) {
}
}
bool String::operator==(const String& other) const {
return size_ == other.size_ && !::memcmp(ptr_, other.ptr_, size_);
}
bool String::operator==(const char* str) const {
return !::strcmp(ptr_, str);
}
VectorBase::~VectorBase() {
::free(data_);
}
......
......@@ -145,6 +145,16 @@ class String {
void Append(const char* str) { Append(str, strlen(str)); }
// Comparison operators.
bool operator==(const String& other) const;
bool operator==(const char* str) const;
inline bool operator!=(const String& other) const {
return !(*this == other);
}
inline bool operator!=(const char* str) const { return !(*this == str); }
private:
inline void Init() {
ptr_ = const_cast<char*>(kEmpty);
......
......@@ -101,6 +101,30 @@ TEST(String, ArrayAccess) {
EXPECT_EQ('\0', s[6]);
}
TEST(String, EqualityOperators) {
String a("Foo");
String b("Bar");
EXPECT_TRUE(a == String("Foo"));
EXPECT_TRUE(a == "Foo");
EXPECT_TRUE(b != String("Foo"));
EXPECT_TRUE(b != "Foo");
EXPECT_TRUE(a != b);
EXPECT_TRUE(b == String("Bar"));
EXPECT_TRUE(b == "Bar");
EXPECT_FALSE(a == "Foo ");
EXPECT_FALSE(b == " Bar");
EXPECT_FALSE(a == "foo");
EXPECT_TRUE(a != "Foo ");
EXPECT_TRUE(b != " Bar");
EXPECT_TRUE(a != "foo");
}
TEST(String, Resize) {
String s("A very long string to have fun");
s.Resize(10);
......
......@@ -38,7 +38,7 @@ namespace crazy {
namespace {
#ifndef UNIT_TESTS
#ifndef UNIT_TEST
// LLVM's demangler is large, and we have no need of it. Overriding it with
// our own stub version here stops a lot of code being pulled in from libc++.
// This reduces the on-disk footprint of the crazy linker library by more than
......@@ -56,7 +56,7 @@ extern "C" char* __cxa_demangle(const char* mangled_name,
*status = kMemoryAllocFailure;
return NULL;
}
#endif // UNIT_TESTS
#endif // UNIT_TEST
#ifdef __arm__
extern "C" int __cxa_atexit(void (*)(void*), void*, void*);
......@@ -281,16 +281,16 @@ void* GetDlCloseWrapperAddressForTesting() {
// the address of a heap-allocated array of pointers, which must be explicitly
// free()-ed by the caller. This returns nullptr is the array is empty.
// On exit, sets |*p_count| to the number of items in the array.
void** GetValidDlopenHandlesForTesting(size_t* p_count) {
const void** GetValidDlopenHandlesForTesting(size_t* p_count) {
ScopedLockedGlobals globals;
const Vector<void*>& handles =
const Vector<const void*>& handles =
globals->valid_handles()->GetValuesForTesting();
*p_count = handles.GetCount();
if (handles.IsEmpty())
return nullptr;
auto* ptr =
reinterpret_cast<void**>(malloc(handles.GetCount() * sizeof(void*)));
auto* ptr = reinterpret_cast<const void**>(
malloc(handles.GetCount() * sizeof(void*)));
for (size_t n = 0; n < handles.GetCount(); ++n) {
ptr[n] = handles[n];
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment