From 982359bf96e9384f54d68048a327e8398f67daf3 Mon Sep 17 00:00:00 2001 From: Esun Kim <veblush@google.com> Date: Thu, 3 Oct 2019 10:29:39 -0700 Subject: [PATCH] Add grpc_core::Map with Allocator --- .../ext/filters/client_channel/backend_metric.cc | 4 ++-- .../ext/filters/client_channel/client_channel.cc | 10 +++++----- src/core/ext/filters/client_channel/lb_policy.h | 4 ++-- .../filters/client_channel/lb_policy/xds/xds.cc | 4 ++-- .../lb_policy/xds/xds_client_stats.h | 14 +++++++------- .../lb_policy/xds/xds_load_balancer_api.h | 2 +- src/core/ext/filters/client_channel/subchannel.h | 6 +++--- src/core/lib/channel/channelz.h | 8 ++++---- src/core/lib/channel/channelz_registry.h | 2 +- src/core/lib/gprpp/map.h | 3 +++ src/core/lib/gprpp/memory.h | 10 ++++++++++ src/core/lib/security/credentials/credentials.cc | 12 ++++++------ src/core/lib/security/credentials/credentials.h | 6 +++--- src/core/lib/transport/connectivity_state.h | 4 ++-- 14 files changed, 51 insertions(+), 38 deletions(-) diff --git a/src/core/ext/filters/client_channel/backend_metric.cc b/src/core/ext/filters/client_channel/backend_metric.cc index b36614f5b8..0d6aa2f6e2 100644 --- a/src/core/ext/filters/client_channel/backend_metric.cc +++ b/src/core/ext/filters/client_channel/backend_metric.cc @@ -26,12 +26,12 @@ namespace grpc_core { namespace { template <typename EntryType> -std::map<StringView, double, StringLess> ParseMap( +Map<StringView, double, StringLess> ParseMap( udpa_data_orca_v1_OrcaLoadReport* msg, EntryType** (*entry_func)(udpa_data_orca_v1_OrcaLoadReport*, size_t*), upb_strview (*key_func)(const EntryType*), double (*value_func)(const EntryType*), Arena* arena) { - std::map<StringView, double, StringLess> result; + Map<StringView, double, StringLess> result; size_t size; const auto* const* entries = entry_func(msg, &size); for (size_t i = 0; i < size; ++i) { diff --git a/src/core/ext/filters/client_channel/client_channel.cc b/src/core/ext/filters/client_channel/client_channel.cc index 12ef32ea94..6168000c6c 100644 --- a/src/core/ext/filters/client_channel/client_channel.cc +++ b/src/core/ext/filters/client_channel/client_channel.cc @@ -291,7 +291,7 @@ class ChannelData { RefCountedPtr<ServiceConfig> saved_service_config_; bool received_first_resolver_result_ = false; // The number of SubchannelWrapper instances referencing a given Subchannel. - std::map<Subchannel*, int> subchannel_refcount_map_; + Map<Subchannel*, int> subchannel_refcount_map_; // The set of SubchannelWrappers that currently exist. // No need to hold a ref, since the map is updated in the control-plane // combiner when the SubchannelWrappers are created and destroyed. @@ -299,8 +299,8 @@ class ChannelData { // Pending ConnectedSubchannel updates for each SubchannelWrapper. // Updates are queued here in the control plane combiner and then applied // in the data plane mutex when the picker is updated. - std::map<RefCountedPtr<SubchannelWrapper>, RefCountedPtr<ConnectedSubchannel>, - RefCountedPtrLess<SubchannelWrapper>> + Map<RefCountedPtr<SubchannelWrapper>, RefCountedPtr<ConnectedSubchannel>, + RefCountedPtrLess<SubchannelWrapper>> pending_subchannel_updates_; // @@ -321,7 +321,7 @@ class ChannelData { // synchronously via grpc_channel_num_external_connectivity_watchers(). // mutable Mutex external_watchers_mu_; - std::map<grpc_closure*, ExternalConnectivityWatcher*> external_watchers_; + Map<grpc_closure*, ExternalConnectivityWatcher*> external_watchers_; }; // @@ -1116,7 +1116,7 @@ class ChannelData::SubchannelWrapper : public SubchannelInterface { // subchannel. This is needed so that when the LB policy calls // CancelConnectivityStateWatch() with its watcher, we know the // corresponding WrapperWatcher to cancel on the underlying subchannel. - std::map<ConnectivityStateWatcherInterface*, WatcherWrapper*> watcher_map_; + Map<ConnectivityStateWatcherInterface*, WatcherWrapper*> watcher_map_; // To be accessed only in the control plane combiner. RefCountedPtr<ConnectedSubchannel> connected_subchannel_; // To be accessed only in the data plane mutex. diff --git a/src/core/ext/filters/client_channel/lb_policy.h b/src/core/ext/filters/client_channel/lb_policy.h index bf6efb00fc..3b7c604d4f 100644 --- a/src/core/ext/filters/client_channel/lb_policy.h +++ b/src/core/ext/filters/client_channel/lb_policy.h @@ -92,11 +92,11 @@ class LoadBalancingPolicy : public InternallyRefCounted<LoadBalancingPolicy> { /// Application-specific requests cost metrics. Metric names are /// determined by the application. Each value is an absolute cost /// (e.g. 3487 bytes of storage) associated with the request. - std::map<StringView, double, StringLess> request_cost; + Map<StringView, double, StringLess> request_cost; /// Application-specific resource utilization metrics. Metric names /// are determined by the application. Each value is expressed as a /// fraction of total resources available. - std::map<StringView, double, StringLess> utilization; + Map<StringView, double, StringLess> utilization; }; /// Interface for accessing per-call state. diff --git a/src/core/ext/filters/client_channel/lb_policy/xds/xds.cc b/src/core/ext/filters/client_channel/lb_policy/xds/xds.cc index 368caacc67..468c8c759f 100644 --- a/src/core/ext/filters/client_channel/lb_policy/xds/xds.cc +++ b/src/core/ext/filters/client_channel/lb_policy/xds/xds.cc @@ -571,8 +571,8 @@ class XdsLb : public LoadBalancingPolicy { RefCountedPtr<XdsLb> xds_policy_; - std::map<RefCountedPtr<XdsLocalityName>, OrphanablePtr<Locality>, - XdsLocalityName::Less> + Map<RefCountedPtr<XdsLocalityName>, OrphanablePtr<Locality>, + XdsLocalityName::Less> localities_; const uint32_t priority_; grpc_connectivity_state connectivity_state_ = GRPC_CHANNEL_IDLE; diff --git a/src/core/ext/filters/client_channel/lb_policy/xds/xds_client_stats.h b/src/core/ext/filters/client_channel/lb_policy/xds/xds_client_stats.h index 8a8af4426b..6e8dd961ea 100644 --- a/src/core/ext/filters/client_channel/lb_policy/xds/xds_client_stats.h +++ b/src/core/ext/filters/client_channel/lb_policy/xds/xds_client_stats.h @@ -111,9 +111,9 @@ class XdsClientStats { double total_metric_value_{0}; }; - using LoadMetricMap = std::map<UniquePtr<char>, LoadMetric, StringLess>; + using LoadMetricMap = Map<UniquePtr<char>, LoadMetric, StringLess>; using LoadMetricSnapshotMap = - std::map<UniquePtr<char>, LoadMetric::Snapshot, StringLess>; + Map<UniquePtr<char>, LoadMetric::Snapshot, StringLess>; struct Snapshot { // TODO(juanlishen): Change this to const method when const_iterator is @@ -180,12 +180,12 @@ class XdsClientStats { // UniquePtr<>. We should remove this wrapper if the value type of Map<> // doesn't have to be movable. using LocalityStatsMap = - std::map<RefCountedPtr<XdsLocalityName>, RefCountedPtr<LocalityStats>, - XdsLocalityName::Less>; + Map<RefCountedPtr<XdsLocalityName>, RefCountedPtr<LocalityStats>, + XdsLocalityName::Less>; using LocalityStatsSnapshotMap = - std::map<RefCountedPtr<XdsLocalityName>, LocalityStats::Snapshot, - XdsLocalityName::Less>; - using DroppedRequestsMap = std::map<UniquePtr<char>, uint64_t, StringLess>; + Map<RefCountedPtr<XdsLocalityName>, LocalityStats::Snapshot, + XdsLocalityName::Less>; + using DroppedRequestsMap = Map<UniquePtr<char>, uint64_t, StringLess>; using DroppedRequestsSnapshotMap = DroppedRequestsMap; struct Snapshot { diff --git a/src/core/ext/filters/client_channel/lb_policy/xds/xds_load_balancer_api.h b/src/core/ext/filters/client_channel/lb_policy/xds/xds_load_balancer_api.h index 25396682bb..1b56bef7d8 100644 --- a/src/core/ext/filters/client_channel/lb_policy/xds/xds_load_balancer_api.h +++ b/src/core/ext/filters/client_channel/lb_policy/xds/xds_load_balancer_api.h @@ -57,7 +57,7 @@ class XdsPriorityListUpdate { size_t size() const { return localities.size(); } - std::map<RefCountedPtr<XdsLocalityName>, Locality, XdsLocalityName::Less> + Map<RefCountedPtr<XdsLocalityName>, Locality, XdsLocalityName::Less> localities; }; diff --git a/src/core/ext/filters/client_channel/subchannel.h b/src/core/ext/filters/client_channel/subchannel.h index f7387b2626..de447d3a2a 100644 --- a/src/core/ext/filters/client_channel/subchannel.h +++ b/src/core/ext/filters/client_channel/subchannel.h @@ -294,8 +294,8 @@ class Subchannel { private: // TODO(roth): Once we can use C++-14 heterogenous lookups, this can // be a set instead of a map. - std::map<ConnectivityStateWatcherInterface*, - OrphanablePtr<ConnectivityStateWatcherInterface>> + Map<ConnectivityStateWatcherInterface*, + OrphanablePtr<ConnectivityStateWatcherInterface>> watchers_; }; @@ -328,7 +328,7 @@ class Subchannel { private: class HealthWatcher; - std::map<const char*, OrphanablePtr<HealthWatcher>, StringLess> map_; + Map<const char*, OrphanablePtr<HealthWatcher>, StringLess> map_; }; class ConnectedSubchannelStateWatcher; diff --git a/src/core/lib/channel/channelz.h b/src/core/lib/channel/channelz.h index 5a063b4df9..545b9ebeb8 100644 --- a/src/core/lib/channel/channelz.h +++ b/src/core/lib/channel/channelz.h @@ -221,8 +221,8 @@ class ChannelNode : public BaseNode { // TODO(roth): We don't actually use the values here, only the keys, so // these should be sets instead of maps, but we don't currently have a set // implementation. Change this if/when we have one. - std::map<intptr_t, bool> child_channels_; - std::map<intptr_t, bool> child_subchannels_; + Map<intptr_t, bool> child_channels_; + Map<intptr_t, bool> child_subchannels_; }; // Handles channelz bookkeeping for servers @@ -262,8 +262,8 @@ class ServerNode : public BaseNode { CallCountingHelper call_counter_; ChannelTrace trace_; Mutex child_mu_; // Guards child maps below. - std::map<intptr_t, RefCountedPtr<SocketNode>> child_sockets_; - std::map<intptr_t, RefCountedPtr<ListenSocketNode>> child_listen_sockets_; + Map<intptr_t, RefCountedPtr<SocketNode>> child_sockets_; + Map<intptr_t, RefCountedPtr<ListenSocketNode>> child_listen_sockets_; }; // Handles channelz bookkeeping for sockets diff --git a/src/core/lib/channel/channelz_registry.h b/src/core/lib/channel/channelz_registry.h index c60a5554fb..e04d7c4488 100644 --- a/src/core/lib/channel/channelz_registry.h +++ b/src/core/lib/channel/channelz_registry.h @@ -87,7 +87,7 @@ class ChannelzRegistry { // protects members Mutex mu_; - std::map<intptr_t, BaseNode*> node_map_; + Map<intptr_t, BaseNode*> node_map_; intptr_t uuid_generator_ = 0; }; diff --git a/src/core/lib/gprpp/map.h b/src/core/lib/gprpp/map.h index 7e954cc2be..fc44cd2cb8 100644 --- a/src/core/lib/gprpp/map.h +++ b/src/core/lib/gprpp/map.h @@ -35,6 +35,9 @@ namespace grpc_core { +template <class Key, class T, class Compare = std::less<Key>> +using Map = std::map<Key, T, Compare, Allocator<std::pair<const Key, T>>>; + struct StringLess { bool operator()(const char* a, const char* b) const { return strcmp(a, b) < 0; diff --git a/src/core/lib/gprpp/memory.h b/src/core/lib/gprpp/memory.h index 26917ef1c8..b60fc87bfb 100644 --- a/src/core/lib/gprpp/memory.h +++ b/src/core/lib/gprpp/memory.h @@ -130,6 +130,16 @@ class Allocator { } }; +template <class T, class U> +bool operator==(Allocator<T> const&, Allocator<U> const&) noexcept { + return true; +} + +template <class T, class U> +bool operator!=(Allocator<T> const& x, Allocator<U> const& y) noexcept { + return false; +} + } // namespace grpc_core #endif /* GRPC_CORE_LIB_GPRPP_MEMORY_H */ diff --git a/src/core/lib/security/credentials/credentials.cc b/src/core/lib/security/credentials/credentials.cc index 3767871570..f2ec9b7f2f 100644 --- a/src/core/lib/security/credentials/credentials.cc +++ b/src/core/lib/security/credentials/credentials.cc @@ -45,18 +45,18 @@ void grpc_channel_credentials_release(grpc_channel_credentials* creds) { if (creds) creds->Unref(); } -static std::map<grpc_core::UniquePtr<char>, - grpc_core::RefCountedPtr<grpc_channel_credentials>, - grpc_core::StringLess>* g_grpc_control_plane_creds; +static grpc_core::Map<grpc_core::UniquePtr<char>, + grpc_core::RefCountedPtr<grpc_channel_credentials>, + grpc_core::StringLess>* g_grpc_control_plane_creds; static gpr_mu g_control_plane_creds_mu; static void do_control_plane_creds_init() { gpr_mu_init(&g_control_plane_creds_mu); GPR_ASSERT(g_grpc_control_plane_creds == nullptr); g_grpc_control_plane_creds = grpc_core::New< - std::map<grpc_core::UniquePtr<char>, - grpc_core::RefCountedPtr<grpc_channel_credentials>, - grpc_core::StringLess>>(); + grpc_core::Map<grpc_core::UniquePtr<char>, + grpc_core::RefCountedPtr<grpc_channel_credentials>, + grpc_core::StringLess>>(); } void grpc_control_plane_credentials_init() { diff --git a/src/core/lib/security/credentials/credentials.h b/src/core/lib/security/credentials/credentials.h index e0700c41a9..16f0454907 100644 --- a/src/core/lib/security/credentials/credentials.h +++ b/src/core/lib/security/credentials/credentials.h @@ -148,9 +148,9 @@ struct grpc_channel_credentials private: const char* type_; - std::map<grpc_core::UniquePtr<char>, - grpc_core::RefCountedPtr<grpc_channel_credentials>, - grpc_core::StringLess> + grpc_core::Map<grpc_core::UniquePtr<char>, + grpc_core::RefCountedPtr<grpc_channel_credentials>, + grpc_core::StringLess> local_control_plane_creds_; }; diff --git a/src/core/lib/transport/connectivity_state.h b/src/core/lib/transport/connectivity_state.h index 89828dd2fa..3895cc5c83 100644 --- a/src/core/lib/transport/connectivity_state.h +++ b/src/core/lib/transport/connectivity_state.h @@ -118,8 +118,8 @@ class ConnectivityStateTracker { Atomic<grpc_connectivity_state> state_; // TODO(roth): Once we can use C++-14 heterogenous lookups, this can // be a set instead of a map. - std::map<ConnectivityStateWatcherInterface*, - OrphanablePtr<ConnectivityStateWatcherInterface>> + Map<ConnectivityStateWatcherInterface*, + OrphanablePtr<ConnectivityStateWatcherInterface>> watchers_; }; -- GitLab