diff options
author | Lei Zhang <thestig@chromium.org> | 2018-03-24 10:38:40 +0900 |
---|---|---|
committer | Qijiang Fan <fqj@google.com> | 2020-06-05 08:52:10 +0900 |
commit | 7d8c9b7952f774b8e60d23e3808b8f7521f23b53 (patch) | |
tree | d34137a018081a31b853a27932bdb2016351f13f /mojo | |
parent | c2c9cfb27461019611de40444ca579ce41257b3e (diff) | |
download | libchrome-7d8c9b7952f774b8e60d23e3808b8f7521f23b53.tar.gz |
Add a data length method to mojo::ScopedSharedBufferHandle.
BUG=788243
Change-Id: Icd243edd24295125599f09adbebcadec62487e89
Reviewed-on: https://chromium-review.googlesource.com/976689
Commit-Queue: Lei Zhang <thestig@chromium.org>
Reviewed-by: Ken Rockot <rockot@chromium.org>
Cr-Commit-Position: refs/heads/master@{#545656}
CrOS-Libchrome-Original-Commit: 7b818885624401be6df25eff4cb6f3e16e43296d
Diffstat (limited to 'mojo')
-rw-r--r-- | mojo/edk/embedder/entrypoints.cc | 11 | ||||
-rw-r--r-- | mojo/edk/system/core.cc | 15 | ||||
-rw-r--r-- | mojo/edk/system/core.h | 3 | ||||
-rw-r--r-- | mojo/edk/system/dispatcher.cc | 4 | ||||
-rw-r--r-- | mojo/edk/system/dispatcher.h | 2 | ||||
-rw-r--r-- | mojo/edk/system/multiprocess_message_pipe_unittest.cc | 9 | ||||
-rw-r--r-- | mojo/edk/system/shared_buffer_dispatcher.cc | 9 | ||||
-rw-r--r-- | mojo/edk/system/shared_buffer_dispatcher.h | 1 | ||||
-rw-r--r-- | mojo/public/c/system/buffer.h | 44 | ||||
-rw-r--r-- | mojo/public/c/system/tests/core_api_unittest.cc | 21 | ||||
-rw-r--r-- | mojo/public/c/system/thunks.cc | 7 | ||||
-rw-r--r-- | mojo/public/c/system/thunks.h | 3 | ||||
-rw-r--r-- | mojo/public/cpp/system/buffer.cc | 8 | ||||
-rw-r--r-- | mojo/public/cpp/system/buffer.h | 3 | ||||
-rw-r--r-- | mojo/public/cpp/system/tests/core_unittest.cc | 1 |
15 files changed, 134 insertions, 7 deletions
diff --git a/mojo/edk/embedder/entrypoints.cc b/mojo/edk/embedder/entrypoints.cc index ca8675c602..2a96ef64ab 100644 --- a/mojo/edk/embedder/entrypoints.cc +++ b/mojo/edk/embedder/entrypoints.cc @@ -193,7 +193,7 @@ MojoResult MojoEndReadDataImpl(MojoHandle data_pipe_consumer_handle, } MojoResult MojoCreateSharedBufferImpl( - const struct MojoCreateSharedBufferOptions* options, + const MojoCreateSharedBufferOptions* options, uint64_t num_bytes, MojoHandle* shared_buffer_handle) { return g_core->CreateSharedBuffer(options, num_bytes, shared_buffer_handle); @@ -201,7 +201,7 @@ MojoResult MojoCreateSharedBufferImpl( MojoResult MojoDuplicateBufferHandleImpl( MojoHandle buffer_handle, - const struct MojoDuplicateBufferHandleOptions* options, + const MojoDuplicateBufferHandleOptions* options, MojoHandle* new_buffer_handle) { return g_core->DuplicateBufferHandle(buffer_handle, options, new_buffer_handle); @@ -219,6 +219,12 @@ MojoResult MojoUnmapBufferImpl(void* buffer) { return g_core->UnmapBuffer(buffer); } +MojoResult MojoGetBufferInfoImpl(MojoHandle buffer_handle, + const MojoSharedBufferOptions* options, + MojoSharedBufferInfo* info) { + return g_core->GetBufferInfo(buffer_handle, options, info); +} + MojoResult MojoWrapPlatformHandleImpl(const MojoPlatformHandle* platform_handle, MojoHandle* mojo_handle) { return g_core->WrapPlatformHandle(platform_handle, mojo_handle); @@ -279,6 +285,7 @@ MojoSystemThunks g_thunks = {sizeof(MojoSystemThunks), MojoDuplicateBufferHandleImpl, MojoMapBufferImpl, MojoUnmapBufferImpl, + MojoGetBufferInfoImpl, MojoCreateTrapImpl, MojoAddTriggerImpl, MojoRemoveTriggerImpl, diff --git a/mojo/edk/system/core.cc b/mojo/edk/system/core.cc index d6477ad3dc..bf2f95c06b 100644 --- a/mojo/edk/system/core.cc +++ b/mojo/edk/system/core.cc @@ -1008,6 +1008,21 @@ MojoResult Core::UnmapBuffer(void* buffer) { return result; } +MojoResult Core::GetBufferInfo(MojoHandle buffer_handle, + const MojoSharedBufferOptions* options, + MojoSharedBufferInfo* info) { + if (options && options->struct_size != sizeof(MojoSharedBufferOptions)) + return MOJO_RESULT_INVALID_ARGUMENT; + if (!info || info->struct_size != sizeof(MojoSharedBufferInfo)) + return MOJO_RESULT_INVALID_ARGUMENT; + + scoped_refptr<Dispatcher> dispatcher(GetDispatcher(buffer_handle)); + if (!dispatcher) + return MOJO_RESULT_INVALID_ARGUMENT; + + return dispatcher->GetBufferInfo(info); +} + MojoResult Core::WrapPlatformHandle(const MojoPlatformHandle* platform_handle, MojoHandle* mojo_handle) { ScopedPlatformHandle handle; diff --git a/mojo/edk/system/core.h b/mojo/edk/system/core.h index 542a448e65..478971e2dc 100644 --- a/mojo/edk/system/core.h +++ b/mojo/edk/system/core.h @@ -282,6 +282,9 @@ class MOJO_SYSTEM_IMPL_EXPORT Core { void** buffer, MojoMapBufferFlags flags); MojoResult UnmapBuffer(void* buffer); + MojoResult GetBufferInfo(MojoHandle buffer_handle, + const MojoSharedBufferOptions* options, + MojoSharedBufferInfo* info); // These methods correspond to the API functions defined in // "mojo/public/c/system/platform_handle.h". diff --git a/mojo/edk/system/dispatcher.cc b/mojo/edk/system/dispatcher.cc index 43959288a8..e736276dfd 100644 --- a/mojo/edk/system/dispatcher.cc +++ b/mojo/edk/system/dispatcher.cc @@ -66,6 +66,10 @@ MojoResult Dispatcher::MapBuffer( return MOJO_RESULT_INVALID_ARGUMENT; } +MojoResult Dispatcher::GetBufferInfo(MojoSharedBufferInfo* info) { + return MOJO_RESULT_INVALID_ARGUMENT; +} + MojoResult Dispatcher::ReadData(void* elements, uint32_t* num_bytes, MojoReadDataFlags flags) { diff --git a/mojo/edk/system/dispatcher.h b/mojo/edk/system/dispatcher.h index ec3d0193e0..3e13c1bd14 100644 --- a/mojo/edk/system/dispatcher.h +++ b/mojo/edk/system/dispatcher.h @@ -106,6 +106,8 @@ class MOJO_SYSTEM_IMPL_EXPORT Dispatcher MojoMapBufferFlags flags, std::unique_ptr<PlatformSharedBufferMapping>* mapping); + virtual MojoResult GetBufferInfo(MojoSharedBufferInfo* info); + ///////////// Data pipe consumer API ///////////// virtual MojoResult ReadData(void* elements, diff --git a/mojo/edk/system/multiprocess_message_pipe_unittest.cc b/mojo/edk/system/multiprocess_message_pipe_unittest.cc index 8a6a3c3f74..c9a00de91d 100644 --- a/mojo/edk/system/multiprocess_message_pipe_unittest.cc +++ b/mojo/edk/system/multiprocess_message_pipe_unittest.cc @@ -338,6 +338,11 @@ TEST_F(MultiprocessMessagePipeTest, SharedBufferPassing) { MojoHandle shared_buffer; ASSERT_EQ(MOJO_RESULT_OK, MojoCreateSharedBuffer(&options, 100, &shared_buffer)); + MojoSharedBufferInfo buffer_info; + buffer_info.struct_size = sizeof(buffer_info); + ASSERT_EQ(MOJO_RESULT_OK, + MojoGetBufferInfo(shared_buffer, nullptr, &buffer_info)); + EXPECT_EQ(100U, buffer_info.size); // Send the shared buffer. const std::string go1("go 1"); @@ -346,6 +351,10 @@ TEST_F(MultiprocessMessagePipeTest, SharedBufferPassing) { ASSERT_EQ(MOJO_RESULT_OK, MojoDuplicateBufferHandle(shared_buffer, nullptr, &duplicated_shared_buffer)); + buffer_info.size = 0; + ASSERT_EQ(MOJO_RESULT_OK, + MojoGetBufferInfo(shared_buffer, nullptr, &buffer_info)); + EXPECT_EQ(100U, buffer_info.size); MojoHandle handles[1]; handles[0] = duplicated_shared_buffer; ASSERT_EQ(MOJO_RESULT_OK, diff --git a/mojo/edk/system/shared_buffer_dispatcher.cc b/mojo/edk/system/shared_buffer_dispatcher.cc index a72ebecfd1..ca9a1a8435 100644 --- a/mojo/edk/system/shared_buffer_dispatcher.cc +++ b/mojo/edk/system/shared_buffer_dispatcher.cc @@ -239,6 +239,15 @@ MojoResult SharedBufferDispatcher::MapBuffer( return MOJO_RESULT_OK; } +MojoResult SharedBufferDispatcher::GetBufferInfo(MojoSharedBufferInfo* info) { + if (!info) + return MOJO_RESULT_INVALID_ARGUMENT; + + base::AutoLock lock(lock_); + info->size = shared_buffer_->GetNumBytes(); + return MOJO_RESULT_OK; +} + void SharedBufferDispatcher::StartSerialize(uint32_t* num_bytes, uint32_t* num_ports, uint32_t* num_platform_handles) { diff --git a/mojo/edk/system/shared_buffer_dispatcher.h b/mojo/edk/system/shared_buffer_dispatcher.h index 351a7d19d3..fcaf5c1913 100644 --- a/mojo/edk/system/shared_buffer_dispatcher.h +++ b/mojo/edk/system/shared_buffer_dispatcher.h @@ -76,6 +76,7 @@ class MOJO_SYSTEM_IMPL_EXPORT SharedBufferDispatcher final : public Dispatcher { uint64_t num_bytes, MojoMapBufferFlags flags, std::unique_ptr<PlatformSharedBufferMapping>* mapping) override; + MojoResult GetBufferInfo(MojoSharedBufferInfo* info) override; void StartSerialize(uint32_t* num_bytes, uint32_t* num_ports, uint32_t* num_platform_handles) override; diff --git a/mojo/public/c/system/buffer.h b/mojo/public/c/system/buffer.h index 09f6d285f3..0e086fc3a2 100644 --- a/mojo/public/c/system/buffer.h +++ b/mojo/public/c/system/buffer.h @@ -43,6 +43,35 @@ struct MOJO_ALIGNAS(8) MojoCreateSharedBufferOptions { MOJO_STATIC_ASSERT(sizeof(MojoCreateSharedBufferOptions) == 8, "MojoCreateSharedBufferOptions has wrong size"); +// Flags passed to |MojoGetBufferInfo()| via |MojoSharedBufferOptions|. +typedef uint32_t MojoSharedBufferOptionsFlags; + +#ifdef __cplusplus +const MojoSharedBufferOptionsFlags MOJO_SHARED_BUFFER_OPTIONS_FLAG_NONE = 0; +#else +#define MOJO_SHARED_BUFFER_OPTIONS_FLAG_NONE ((MojoSharedBufferOptionsFlags)0) +#endif + +struct MOJO_ALIGNAS(8) MojoSharedBufferOptions { + // The size of this structure, used for versioning. + uint32_t struct_size; + + // See |MojoSharedBufferOptionsFlags|. + MojoSharedBufferOptionsFlags flags; +}; +MOJO_STATIC_ASSERT(sizeof(MojoSharedBufferOptions) == 8, + "MojoSharedBufferOptions has wrong size"); + +struct MOJO_ALIGNAS(8) MojoSharedBufferInfo { + // The size of this structure, used for versioning. + uint32_t struct_size; + + // The size of the shared buffer. + uint64_t size; +}; +MOJO_STATIC_ASSERT(sizeof(MojoSharedBufferInfo) == 16, + "MojoSharedBufferInfo has wrong size"); + // |MojoDuplicateBufferHandleOptions|: Used to specify parameters in duplicating // access to a shared buffer to |MojoDuplicateBufferHandle()|. // @@ -181,6 +210,21 @@ MOJO_SYSTEM_EXPORT MojoResult MojoMapBuffer(MojoHandle buffer_handle, // result of |MojoMapBuffer()| or has already been unmapped). MOJO_SYSTEM_EXPORT MojoResult MojoUnmapBuffer(void* buffer); // In. +// Retrieve information about |buffer_handle| into |info|. |options| is optional +// and reserved for future use. +// +// Returns: +// |MOJO_RESULT_OK| on success. +// |MOJO_RESULT_INVALID_ARGUMENT| if |buffer_handle| is invalid or if |info| +// is NULL. +// +// On success, |info->size| will be set to the size of the buffer. On failure it +// is not modified. +MOJO_SYSTEM_EXPORT MojoResult +MojoGetBufferInfo(MojoHandle buffer_handle, + const struct MojoSharedBufferOptions* options, // Optional. + struct MojoSharedBufferInfo* info); // Out. + #ifdef __cplusplus } // extern "C" #endif diff --git a/mojo/public/c/system/tests/core_api_unittest.cc b/mojo/public/c/system/tests/core_api_unittest.cc index ff24b5d7c0..10c049d443 100644 --- a/mojo/public/c/system/tests/core_api_unittest.cc +++ b/mojo/public/c/system/tests/core_api_unittest.cc @@ -264,23 +264,30 @@ TEST(CoreAPITest, BasicDataPipe) { } TEST(CoreAPITest, BasicSharedBuffer) { - MojoHandle h0, h1; - void* pointer; + MojoSharedBufferInfo buffer_info; + buffer_info.struct_size = sizeof(buffer_info); + MojoHandle h0 = MOJO_HANDLE_INVALID; + EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, + MojoGetBufferInfo(h0, nullptr, &buffer_info)); // Create a shared buffer (|h0|). - h0 = MOJO_HANDLE_INVALID; EXPECT_EQ(MOJO_RESULT_OK, MojoCreateSharedBuffer(nullptr, 100, &h0)); EXPECT_NE(h0, MOJO_HANDLE_INVALID); + EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, + MojoGetBufferInfo(h0, nullptr, nullptr)); + ASSERT_EQ(MOJO_RESULT_OK, MojoGetBufferInfo(h0, nullptr, &buffer_info)); + EXPECT_EQ(100U, buffer_info.size); + // Map everything. - pointer = nullptr; + void* pointer = nullptr; EXPECT_EQ(MOJO_RESULT_OK, MojoMapBuffer(h0, 0, 100, &pointer, MOJO_MAP_BUFFER_FLAG_NONE)); ASSERT_TRUE(pointer); static_cast<char*>(pointer)[50] = 'x'; // Duplicate |h0| to |h1|. - h1 = MOJO_HANDLE_INVALID; + MojoHandle h1 = MOJO_HANDLE_INVALID; EXPECT_EQ(MOJO_RESULT_OK, MojoDuplicateBufferHandle(h0, nullptr, &h1)); EXPECT_NE(h1, MOJO_HANDLE_INVALID); @@ -306,6 +313,10 @@ TEST(CoreAPITest, BasicSharedBuffer) { // Unmap it. EXPECT_EQ(MOJO_RESULT_OK, MojoUnmapBuffer(pointer)); + buffer_info.size = 0; + ASSERT_EQ(MOJO_RESULT_OK, MojoGetBufferInfo(h1, nullptr, &buffer_info)); + EXPECT_EQ(100U, buffer_info.size); + EXPECT_EQ(MOJO_RESULT_OK, MojoClose(h1)); } diff --git a/mojo/public/c/system/thunks.cc b/mojo/public/c/system/thunks.cc index b3a0564463..cbd9aed62c 100644 --- a/mojo/public/c/system/thunks.cc +++ b/mojo/public/c/system/thunks.cc @@ -138,6 +138,13 @@ MojoResult MojoUnmapBuffer(void* buffer) { return g_thunks.UnmapBuffer(buffer); } +MojoResult MojoGetBufferInfo(MojoHandle buffer_handle, + const struct MojoSharedBufferOptions* options, + struct MojoSharedBufferInfo* info) { + assert(g_thunks.GetBufferInfo); + return g_thunks.GetBufferInfo(buffer_handle, options, info); +} + MojoResult MojoCreateTrap(MojoTrapEventHandler handler, const MojoCreateTrapOptions* options, MojoHandle* trap_handle) { diff --git a/mojo/public/c/system/thunks.h b/mojo/public/c/system/thunks.h index 0564ea6025..b68e71758b 100644 --- a/mojo/public/c/system/thunks.h +++ b/mojo/public/c/system/thunks.h @@ -72,6 +72,9 @@ struct MojoSystemThunks { void** buffer, MojoMapBufferFlags flags); MojoResult (*UnmapBuffer)(void* buffer); + MojoResult (*GetBufferInfo)(MojoHandle buffer_handle, + const struct MojoSharedBufferOptions* options, + struct MojoSharedBufferInfo* info); MojoResult (*CreateTrap)(MojoTrapEventHandler handler, const struct MojoCreateTrapOptions* options, MojoHandle* trap_handle); diff --git a/mojo/public/cpp/system/buffer.cc b/mojo/public/cpp/system/buffer.cc index 49f45d8498..13f2026316 100644 --- a/mojo/public/cpp/system/buffer.cc +++ b/mojo/public/cpp/system/buffer.cc @@ -43,4 +43,12 @@ ScopedSharedBufferMapping SharedBufferHandle::MapAtOffset( return ScopedSharedBufferMapping(buffer); } +uint64_t SharedBufferHandle::GetSize() const { + MojoSharedBufferInfo buffer_info; + buffer_info.struct_size = sizeof(buffer_info); + return MojoGetBufferInfo(value(), nullptr, &buffer_info) == MOJO_RESULT_OK + ? buffer_info.size + : 0; +} + } // namespace mojo diff --git a/mojo/public/cpp/system/buffer.h b/mojo/public/cpp/system/buffer.h index 660b7503a1..d72d197650 100644 --- a/mojo/public/cpp/system/buffer.h +++ b/mojo/public/cpp/system/buffer.h @@ -69,6 +69,9 @@ class MOJO_CPP_SYSTEM_EXPORT SharedBufferHandle : public Handle { // Maps |size| bytes of this shared buffer, starting |offset| bytes into the // buffer. On failure, this will return a null mapping. ScopedSharedBufferMapping MapAtOffset(uint64_t size, uint64_t offset) const; + + // Get the size of this shared buffer. + uint64_t GetSize() const; }; static_assert(sizeof(SharedBufferHandle) == sizeof(Handle), diff --git a/mojo/public/cpp/system/tests/core_unittest.cc b/mojo/public/cpp/system/tests/core_unittest.cc index b134e9bf18..d727431fb0 100644 --- a/mojo/public/cpp/system/tests/core_unittest.cc +++ b/mojo/public/cpp/system/tests/core_unittest.cc @@ -386,6 +386,7 @@ TEST(CoreCppTest, ScopedHandleMoveCtor) { TEST(CoreCppTest, BasicSharedBuffer) { ScopedSharedBufferHandle h0 = SharedBufferHandle::Create(100); ASSERT_TRUE(h0.is_valid()); + EXPECT_EQ(100U, h0->GetSize()); // Map everything. ScopedSharedBufferMapping mapping = h0->Map(100); |