Commit fe92ab75 authored by viettrungluu's avatar viettrungluu Committed by Commit bot

Mojo: Make room for remotely-allocated channel endpoint IDs.

R=brettw@chromium.org

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

Cr-Commit-Position: refs/heads/master@{#299043}
parent 54170a07
...@@ -4,12 +4,20 @@ ...@@ -4,12 +4,20 @@
#include "mojo/edk/system/channel_endpoint_id.h" #include "mojo/edk/system/channel_endpoint_id.h"
#include "base/compiler_specific.h"
namespace mojo { namespace mojo {
namespace system { namespace system {
STATIC_CONST_MEMBER_DEFINITION const uint32_t
ChannelEndpointId::kRemotelyAllocatedFlag;
STATIC_CONST_MEMBER_DEFINITION const uint32_t
ChannelEndpointId::kLocallyAllocatedMask;
ChannelEndpointId LocalChannelEndpointIdGenerator::GetNext() { ChannelEndpointId LocalChannelEndpointIdGenerator::GetNext() {
ChannelEndpointId rv = next_channel_endpoint_id_; ChannelEndpointId rv = next_channel_endpoint_id_;
next_channel_endpoint_id_.value_++; next_channel_endpoint_id_.value_ = (next_channel_endpoint_id_.value_ + 1) &
ChannelEndpointId::kLocallyAllocatedMask;
// Skip over the invalid value, in case we wrap. // Skip over the invalid value, in case we wrap.
if (!next_channel_endpoint_id_.is_valid()) if (!next_channel_endpoint_id_.is_valid())
next_channel_endpoint_id_.value_++; next_channel_endpoint_id_.value_++;
......
...@@ -26,6 +26,12 @@ FORWARD_DECLARE_TEST(LocalChannelEndpointIdGeneratorTest, WrapAround); ...@@ -26,6 +26,12 @@ FORWARD_DECLARE_TEST(LocalChannelEndpointIdGeneratorTest, WrapAround);
// Represents an ID for an endpoint (i.e., one side of a message pipe) on a // Represents an ID for an endpoint (i.e., one side of a message pipe) on a
// |Channel|. This class must be POD. // |Channel|. This class must be POD.
//
// Note: The terminology "remotely allocated ID" is for destination IDs with
// respect to the receiver. I.e., a destination ID in a message is remotely
// allocated if the ID was allocated by the sender (i.e., the remote side with
// respect to the receiver). Conversely, a source ID is remotely allocated if it
// was allocated by the receiver.
class MOJO_SYSTEM_IMPL_EXPORT ChannelEndpointId { class MOJO_SYSTEM_IMPL_EXPORT ChannelEndpointId {
public: public:
ChannelEndpointId() : value_(0) {} ChannelEndpointId() : value_(0) {}
...@@ -51,6 +57,9 @@ class MOJO_SYSTEM_IMPL_EXPORT ChannelEndpointId { ...@@ -51,6 +57,9 @@ class MOJO_SYSTEM_IMPL_EXPORT ChannelEndpointId {
} }
bool is_valid() const { return !!value_; } bool is_valid() const { return !!value_; }
bool is_remotely_allocated() const {
return !!(value_ & kRemotelyAllocatedFlag);
}
uint32_t value() const { return value_; } uint32_t value() const { return value_; }
private: private:
...@@ -59,6 +68,9 @@ class MOJO_SYSTEM_IMPL_EXPORT ChannelEndpointId { ...@@ -59,6 +68,9 @@ class MOJO_SYSTEM_IMPL_EXPORT ChannelEndpointId {
uint32_t value_; uint32_t value_;
static const uint32_t kRemotelyAllocatedFlag = 0x80000000u;
static const uint32_t kLocallyAllocatedMask = ~kRemotelyAllocatedFlag;
// Copying and assignment allowed. // Copying and assignment allowed.
}; };
// This wrapper should add no overhead. // This wrapper should add no overhead.
......
...@@ -30,6 +30,9 @@ TEST(ChannelEndpointIdTest, Basic) { ...@@ -30,6 +30,9 @@ TEST(ChannelEndpointIdTest, Basic) {
EXPECT_FALSE(invalid.is_valid()); EXPECT_FALSE(invalid.is_valid());
EXPECT_TRUE(bootstrap.is_valid()); EXPECT_TRUE(bootstrap.is_valid());
EXPECT_FALSE(invalid.is_remotely_allocated());
EXPECT_FALSE(bootstrap.is_remotely_allocated());
// Test assignment. // Test assignment.
ChannelEndpointId copy; ChannelEndpointId copy;
copy = bootstrap; copy = bootstrap;
...@@ -86,14 +89,17 @@ TEST(LocalChannelEndpointIdGeneratorTest, Basic) { ...@@ -86,14 +89,17 @@ TEST(LocalChannelEndpointIdGeneratorTest, Basic) {
// it needs to be friended. // it needs to be friended.
TEST(LocalChannelEndpointIdGeneratorTest, WrapAround) { TEST(LocalChannelEndpointIdGeneratorTest, WrapAround) {
LocalChannelEndpointIdGenerator gen; LocalChannelEndpointIdGenerator gen;
gen.next_channel_endpoint_id_.value_ = static_cast<uint32_t>(-1); gen.next_channel_endpoint_id_.value_ =
ChannelEndpointId::kRemotelyAllocatedFlag - 1;
ChannelEndpointId id = gen.GetNext(); ChannelEndpointId id = gen.GetNext();
EXPECT_TRUE(id.is_valid()); EXPECT_TRUE(id.is_valid());
EXPECT_EQ(static_cast<uint32_t>(-1), id.value()); EXPECT_FALSE(id.is_remotely_allocated());
EXPECT_EQ(ChannelEndpointId::kRemotelyAllocatedFlag - 1, id.value());
id = gen.GetNext(); id = gen.GetNext();
EXPECT_TRUE(id.is_valid()); EXPECT_TRUE(id.is_valid());
EXPECT_FALSE(id.is_remotely_allocated());
EXPECT_EQ(1u, id.value()); EXPECT_EQ(1u, id.value());
} }
......
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