| OLD | NEW |
| (Empty) |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef MOJO_EDK_SYSTEM_CHANNEL_ENDPOINT_ID_H_ | |
| 6 #define MOJO_EDK_SYSTEM_CHANNEL_ENDPOINT_ID_H_ | |
| 7 | |
| 8 #include <stddef.h> | |
| 9 #include <stdint.h> | |
| 10 | |
| 11 #include <ostream> | |
| 12 | |
| 13 #include "base/containers/hash_tables.h" | |
| 14 #include "base/gtest_prod_util.h" | |
| 15 #include "base/macros.h" | |
| 16 #include "mojo/edk/system/system_impl_export.h" | |
| 17 | |
| 18 namespace mojo { | |
| 19 namespace system { | |
| 20 | |
| 21 // ChannelEndpointId ----------------------------------------------------------- | |
| 22 | |
| 23 class LocalChannelEndpointIdGenerator; | |
| 24 FORWARD_DECLARE_TEST(LocalChannelEndpointIdGeneratorTest, WrapAround); | |
| 25 FORWARD_DECLARE_TEST(RemoteChannelEndpointIdGeneratorTest, WrapAround); | |
| 26 | |
| 27 // Represents an ID for an endpoint (i.e., one side of a message pipe) on a | |
| 28 // |Channel|. This class must be POD. | |
| 29 // | |
| 30 // Note: The terminology "remote" for a |ChannelEndpointId| means a destination | |
| 31 // ID that was actually allocated by the sender, or similarly a source ID that | |
| 32 // was allocated by the receiver. | |
| 33 // | |
| 34 // From the standpoint of the |Channel| with such a remote ID in its endpoint | |
| 35 // table, such an ID is a "remotely-allocated local ID". From the standpoint of | |
| 36 // the |Channel| allocating such a remote ID (for its peer |Channel|), it's a | |
| 37 // "locally-allocated remote ID". | |
| 38 class MOJO_SYSTEM_IMPL_EXPORT ChannelEndpointId { | |
| 39 public: | |
| 40 ChannelEndpointId() : value_(0) {} | |
| 41 ChannelEndpointId(const ChannelEndpointId& other) : value_(other.value_) {} | |
| 42 | |
| 43 // Returns the local ID to use for the first message pipe endpoint on a | |
| 44 // channel. | |
| 45 static ChannelEndpointId GetBootstrap() { return ChannelEndpointId(1); } | |
| 46 | |
| 47 bool operator==(const ChannelEndpointId& other) const { | |
| 48 return value_ == other.value_; | |
| 49 } | |
| 50 bool operator!=(const ChannelEndpointId& other) const { | |
| 51 return !operator==(other); | |
| 52 } | |
| 53 // So that we can be used in |std::map|, etc. | |
| 54 bool operator<(const ChannelEndpointId& other) const { | |
| 55 return value_ < other.value_; | |
| 56 } | |
| 57 | |
| 58 bool is_valid() const { return !!value_; } | |
| 59 bool is_remote() const { return !!(value_ & kRemoteFlag); } | |
| 60 const uint32_t& value() const { return value_; } | |
| 61 | |
| 62 // Flag set in |value()| if this is a remote ID. | |
| 63 static const uint32_t kRemoteFlag = 0x80000000u; | |
| 64 | |
| 65 private: | |
| 66 friend class LocalChannelEndpointIdGenerator; | |
| 67 FRIEND_TEST_ALL_PREFIXES(LocalChannelEndpointIdGeneratorTest, WrapAround); | |
| 68 friend class RemoteChannelEndpointIdGenerator; | |
| 69 FRIEND_TEST_ALL_PREFIXES(RemoteChannelEndpointIdGeneratorTest, WrapAround); | |
| 70 | |
| 71 explicit ChannelEndpointId(uint32_t value) : value_(value) {} | |
| 72 | |
| 73 uint32_t value_; | |
| 74 | |
| 75 // Copying and assignment allowed. | |
| 76 }; | |
| 77 // This wrapper should add no overhead. | |
| 78 // TODO(vtl): Rewrite |sizeof(uint32_t)| as |sizeof(ChannelEndpointId::value)| | |
| 79 // once we have sufficient C++11 support. | |
| 80 static_assert(sizeof(ChannelEndpointId) == sizeof(uint32_t), | |
| 81 "ChannelEndpointId has incorrect size"); | |
| 82 | |
| 83 // So logging macros and |DCHECK_EQ()|, etc. work. | |
| 84 inline std::ostream& operator<<(std::ostream& out, | |
| 85 const ChannelEndpointId& channel_endpoint_id) { | |
| 86 return out << channel_endpoint_id.value(); | |
| 87 } | |
| 88 | |
| 89 // LocalChannelEndpointIdGenerator --------------------------------------------- | |
| 90 | |
| 91 // A generator for "new" local |ChannelEndpointId|s. It does not track | |
| 92 // used/existing IDs; that must be done separately. (This class is not | |
| 93 // thread-safe.) | |
| 94 class MOJO_SYSTEM_IMPL_EXPORT LocalChannelEndpointIdGenerator { | |
| 95 public: | |
| 96 LocalChannelEndpointIdGenerator() | |
| 97 : next_(ChannelEndpointId::GetBootstrap()) {} | |
| 98 | |
| 99 ChannelEndpointId GetNext(); | |
| 100 | |
| 101 private: | |
| 102 FRIEND_TEST_ALL_PREFIXES(LocalChannelEndpointIdGeneratorTest, WrapAround); | |
| 103 | |
| 104 ChannelEndpointId next_; | |
| 105 | |
| 106 DISALLOW_COPY_AND_ASSIGN(LocalChannelEndpointIdGenerator); | |
| 107 }; | |
| 108 | |
| 109 // RemoteChannelEndpointIdGenerator -------------------------------------------- | |
| 110 | |
| 111 // A generator for "new" remote |ChannelEndpointId|s, for |Channel|s to | |
| 112 // locally allocate remote IDs. (See the comment above |ChannelEndpointId| for | |
| 113 // an explanatory note.) It does not track used/existing IDs; that must be done | |
| 114 // separately. (This class is not thread-safe.) | |
| 115 class MOJO_SYSTEM_IMPL_EXPORT RemoteChannelEndpointIdGenerator { | |
| 116 public: | |
| 117 RemoteChannelEndpointIdGenerator() : next_(ChannelEndpointId::kRemoteFlag) {} | |
| 118 | |
| 119 ChannelEndpointId GetNext(); | |
| 120 | |
| 121 private: | |
| 122 FRIEND_TEST_ALL_PREFIXES(RemoteChannelEndpointIdGeneratorTest, WrapAround); | |
| 123 | |
| 124 ChannelEndpointId next_; | |
| 125 | |
| 126 DISALLOW_COPY_AND_ASSIGN(RemoteChannelEndpointIdGenerator); | |
| 127 }; | |
| 128 | |
| 129 } // namespace system | |
| 130 } // namespace mojo | |
| 131 | |
| 132 // Define "hash" functions for |ChannelEndpointId|s, so they can be used in hash | |
| 133 // tables. | |
| 134 // TODO(vtl): Once we can use |std::unordered_{map,set}|, update this (and | |
| 135 // remove the base/containers/hash_tables.h include). | |
| 136 namespace BASE_HASH_NAMESPACE { | |
| 137 | |
| 138 template <> | |
| 139 struct hash<mojo::system::ChannelEndpointId> { | |
| 140 size_t operator()(mojo::system::ChannelEndpointId channel_endpoint_id) const { | |
| 141 return static_cast<size_t>(channel_endpoint_id.value()); | |
| 142 } | |
| 143 }; | |
| 144 | |
| 145 } // namespace BASE_HASH_NAMESPACE | |
| 146 | |
| 147 #endif // MOJO_EDK_SYSTEM_CHANNEL_ENDPOINT_ID_H_ | |
| OLD | NEW |