Skip to content

Commit

Permalink
Align base::hash_map with C++11, part 1.
Browse files Browse the repository at this point in the history
C++11's std::hash provides a default hash for T* that hashes by pointer value.
So does MSVC's stdext::hash_value, but not GCC's __gnu_cxx::hash (and STLPort's
std::hash). To align non-MSVC with both MSVC and the standard, provide a
default hash for pointers. This removes a lot of GCC-specific hash definitions.

Confusingly, all three provide a default hash for const char * that hashes as C
string, while not changing their equality. This CL does not change this on any
platform.

BUG=420242

Review URL: https://codereview.chromium.org/630503002

Cr-Commit-Position: refs/heads/master@{#299217}
  • Loading branch information
davidben authored and Commit bot committed Oct 11, 2014
1 parent 45097b2 commit 254bd40
Show file tree
Hide file tree
Showing 16 changed files with 11 additions and 196 deletions.
11 changes: 11 additions & 0 deletions base/containers/hash_tables.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,17 @@ DEFINE_TRIVIAL_HASH(unsigned long long);
#undef DEFINE_TRIVIAL_HASH
#endif // !defined(OS_ANDROID)

// To align with C++11's std::hash and MSVC's pre-standard stdext::hash_value,
// provide a default hash function for raw pointers. Note: const char * is still
// specialized to hash as a C string. This is consistent with the currently used
// stdext::hash_value, but not C++11.
template<typename T>
struct hash<T*> {
std::size_t operator()(T* value) const {
return hash<uintptr_t>()(reinterpret_cast<uintptr_t>(value));
}
};

// Implement string hash functions so that strings of various flavors can
// be used as keys in STL maps and sets. The hash algorithm comes from the
// GNU C++ library, in <tr1/functional>. It is duplicated here because GCC
Expand Down
11 changes: 0 additions & 11 deletions base/debug/trace_event_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,6 @@
template <typename Type>
struct DefaultSingletonTraits;

#if defined(COMPILER_GCC)
namespace BASE_HASH_NAMESPACE {
template <>
struct hash<base::MessageLoop*> {
std::size_t operator()(base::MessageLoop* value) const {
return reinterpret_cast<std::size_t>(value);
}
};
} // BASE_HASH_NAMESPACE
#endif

namespace base {

class WaitableEvent;
Expand Down
12 changes: 0 additions & 12 deletions base/memory/discardable_memory_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,6 @@ class DiscardableMemoryManagerAllocation {
} // namespace internal
} // namespace base

#if defined(COMPILER_GCC)
namespace BASE_HASH_NAMESPACE {
template <>
struct hash<base::internal::DiscardableMemoryManagerAllocation*> {
size_t operator()(
base::internal::DiscardableMemoryManagerAllocation* ptr) const {
return hash<size_t>()(reinterpret_cast<size_t>(ptr));
}
};
} // namespace BASE_HASH_NAMESPACE
#endif // COMPILER

namespace base {
namespace internal {

Expand Down
10 changes: 0 additions & 10 deletions cc/resources/prioritized_resource_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,6 @@
#include "cc/trees/proxy.h"
#include "ui/gfx/size.h"

#if defined(COMPILER_GCC)
namespace BASE_HASH_NAMESPACE {
template <> struct hash<cc::PrioritizedResource*> {
size_t operator()(cc::PrioritizedResource* ptr) const {
return hash<size_t>()(reinterpret_cast<size_t>(ptr));
}
};
} // namespace BASE_HASH_NAMESPACE
#endif // COMPILER

namespace cc {

class PriorityCalculator;
Expand Down
13 changes: 0 additions & 13 deletions cc/trees/layer_sorter.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,6 @@
#include "ui/gfx/rect_f.h"
#include "ui/gfx/vector3d_f.h"

#if defined(COMPILER_GCC)
namespace cc { struct GraphEdge; }

namespace BASE_HASH_NAMESPACE {
template <>
struct hash<cc::GraphEdge*> {
size_t operator()(cc::GraphEdge* ptr) const {
return hash<size_t>()(reinterpret_cast<size_t>(ptr));
}
};
} // namespace BASE_HASH_NAMESPACE
#endif // COMPILER

namespace gfx {
class Transform;
}
Expand Down
11 changes: 0 additions & 11 deletions cc/trees/layer_tree_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,6 @@
#include "cc/output/renderer.h"
#include "cc/resources/ui_resource_client.h"

#if defined(COMPILER_GCC)
namespace BASE_HASH_NAMESPACE {
template<>
struct hash<cc::LayerImpl*> {
size_t operator()(cc::LayerImpl* ptr) const {
return hash<size_t>()(reinterpret_cast<size_t>(ptr));
}
};
} // namespace BASE_HASH_NAMESPACE
#endif // COMPILER

namespace base {
namespace debug {
class TracedValue;
Expand Down
11 changes: 0 additions & 11 deletions chrome/browser/download/chrome_download_manager_delegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,6 @@ namespace user_prefs {
class PrefRegistrySyncable;
}

#if defined(COMPILER_GCC) && defined(ENABLE_EXTENSIONS)
namespace BASE_HASH_NAMESPACE {
template<>
struct hash<extensions::CrxInstaller*> {
std::size_t operator()(extensions::CrxInstaller* const& p) const {
return reinterpret_cast<std::size_t>(p);
}
};
} // namespace BASE_HASH_NAMESPACE
#endif

// This is the Chrome side helper for the download system.
class ChromeDownloadManagerDelegate
: public content::DownloadManagerDelegate,
Expand Down
13 changes: 0 additions & 13 deletions chrome/browser/profiles/profile.h
Original file line number Diff line number Diff line change
Expand Up @@ -407,17 +407,4 @@ struct ProfileCompare {
bool operator()(Profile* a, Profile* b) const;
};

#if defined(COMPILER_GCC)
namespace BASE_HASH_NAMESPACE {

template<>
struct hash<Profile*> {
std::size_t operator()(Profile* const& p) const {
return reinterpret_cast<std::size_t>(p);
}
};

} // namespace BASE_HASH_NAMESPACE
#endif

#endif // CHROME_BROWSER_PROFILES_PROFILE_H_
13 changes: 0 additions & 13 deletions chrome/browser/safe_browsing/protocol_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,6 @@ class URLFetcher;
class URLRequestContextGetter;
} // namespace net

#if defined(COMPILER_GCC)
// Allows us to use URLFetchers in a hash_map with gcc (MSVC is okay without
// specifying this).
namespace BASE_HASH_NAMESPACE {
template<>
struct hash<const net::URLFetcher*> {
size_t operator()(const net::URLFetcher* fetcher) const {
return reinterpret_cast<size_t>(fetcher);
}
};
}
#endif

class SBProtocolManagerFactory;
class SafeBrowsingProtocolManagerDelegate;

Expand Down
11 changes: 0 additions & 11 deletions content/browser/manifest/manifest_manager_host.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,6 @@
#include "base/id_map.h"
#include "content/public/browser/web_contents_observer.h"

#if defined(COMPILER_GCC)
namespace BASE_HASH_NAMESPACE {
template<>
struct hash<content::RenderFrameHost*> {
uint64 operator()(content::RenderFrameHost* ptr) const {
return hash<uint64>()(reinterpret_cast<uint64>(ptr));
}
};
}
#endif

namespace content {

class RenderFrameHost;
Expand Down
29 changes: 0 additions & 29 deletions content/child/npapi/np_channel_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,35 +21,6 @@ namespace base {
class MessageLoopProxy;
}

#if defined(COMPILER_GCC)
namespace BASE_HASH_NAMESPACE {

template<>
struct hash<NPObject*> {
std::size_t operator()(NPObject* const& ptr) const {
return hash<size_t>()(reinterpret_cast<size_t>(ptr));
}
};

template<>
struct hash<struct _NPP*> {
std::size_t operator()(struct _NPP* const& ptr) const {
return hash<size_t>()(reinterpret_cast<size_t>(ptr));
}
};

} // namespace __gnu_cxx
#elif defined(COMPILER_MSVC)
namespace stdext {

template<>
inline size_t hash_value(NPObject* const& ptr) {
return hash_value(reinterpret_cast<size_t>(ptr));
}

} // namespace stdext
#endif // COMPILER

namespace content {

// Encapsulates an IPC channel between a renderer and another process. Used to
Expand Down
11 changes: 0 additions & 11 deletions content/common/gpu/gpu_memory_manager_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,6 @@

using gpu::MemoryAllocation;

#if defined(COMPILER_GCC)
namespace BASE_HASH_NAMESPACE {
template<>
struct hash<content::GpuMemoryManagerClient*> {
uint64 operator()(content::GpuMemoryManagerClient* ptr) const {
return hash<uint64>()(reinterpret_cast<uint64>(ptr));
}
};
} // namespace BASE_HASH_NAMESPACE
#endif // COMPILER

class FakeMemoryTracker : public gpu::gles2::MemoryTracker {
public:
virtual void TrackMemoryAllocatedChange(
Expand Down
13 changes: 0 additions & 13 deletions content/public/browser/browser_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -168,17 +168,4 @@ class CONTENT_EXPORT BrowserContext : public base::SupportsUserData {

} // namespace content

#if defined(COMPILER_GCC)
namespace BASE_HASH_NAMESPACE {

template<>
struct hash<content::BrowserContext*> {
std::size_t operator()(content::BrowserContext* const& p) const {
return reinterpret_cast<std::size_t>(p);
}
};

} // namespace BASE_HASH_NAMESPACE
#endif

#endif // CONTENT_PUBLIC_BROWSER_BROWSER_CONTEXT_H_
11 changes: 0 additions & 11 deletions gpu/command_buffer/service/async_pixel_transfer_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,6 @@
#include "gpu/command_buffer/service/texture_manager.h"
#include "gpu/gpu_export.h"

#if defined(COMPILER_GCC)
namespace BASE_HASH_NAMESPACE {
template <>
struct hash<gpu::gles2::TextureRef*> {
size_t operator()(gpu::gles2::TextureRef* ptr) const {
return hash<size_t>()(reinterpret_cast<size_t>(ptr));
}
};
} // namespace BASE_HASH_NAMESPACE
#endif // COMPILER

namespace gfx {
class GLContext;
}
Expand Down
11 changes: 0 additions & 11 deletions net/quic/quic_ack_notifier_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,6 @@
#include "base/containers/hash_tables.h"
#include "net/quic/quic_protocol.h"

#if defined(COMPILER_GCC)
namespace BASE_HASH_NAMESPACE {
template<>
struct hash<net::QuicAckNotifier*> {
std::size_t operator()(const net::QuicAckNotifier* ptr) const {
return hash<size_t>()(reinterpret_cast<size_t>(ptr));
}
};
}
#endif

namespace net {

class QuicAckNotifier;
Expand Down
16 changes: 0 additions & 16 deletions tools/gn/target.h
Original file line number Diff line number Diff line change
Expand Up @@ -315,20 +315,4 @@ class Target : public Item {
DISALLOW_COPY_AND_ASSIGN(Target);
};

namespace BASE_HASH_NAMESPACE {

#if defined(COMPILER_GCC)
template<> struct hash<const Target*> {
std::size_t operator()(const Target* t) const {
return reinterpret_cast<std::size_t>(t);
}
};
#elif defined(COMPILER_MSVC)
inline size_t hash_value(const Target* t) {
return reinterpret_cast<size_t>(t);
}
#endif // COMPILER...

} // namespace BASE_HASH_NAMESPACE

#endif // TOOLS_GN_TARGET_H_

0 comments on commit 254bd40

Please sign in to comment.