diff options
author | darin@chromium.org <darin@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-01-28 06:03:43 +0000 |
---|---|---|
committer | darin@chromium.org <darin@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-01-28 06:03:43 +0000 |
commit | 811f5f2f1776b0f3ca85928779080e1cc3e283a7 (patch) | |
tree | 33657d55d5cf0b9cadcb35a9ae8efdc92d3e68e0 | |
parent | 9bda4b5057f853a6f9ad8ea9601e335f4f868fe4 (diff) | |
download | chromium_src-811f5f2f1776b0f3ca85928779080e1cc3e283a7.zip chromium_src-811f5f2f1776b0f3ca85928779080e1cc3e283a7.tar.gz chromium_src-811f5f2f1776b0f3ca85928779080e1cc3e283a7.tar.bz2 |
Mojo: re-organize public/bindings/ directory
The plan:
- mojo/public/{subdir} contains public headers and scripts (if any).
- mojo/public/{subdir}/lib contains private headers and .cc files (if any).
- mojo/public/{subdir}/tests contains test files.
This CL only implements the plan for the public/bindings/ directory. Other directories will follow.
In addition, bindings header files are broken up a bit. In particular, buffer.{h,cc} are carved up so that buffer.h can only contain public bits. This necessitates creation of allocation_scope.h. I extracted type_converter.h instead of letting it be buried inside array_internal.h.
This CL nukes bindings/sample/.
R=viettrungluu@chromium.org
Review URL: https://codereview.chromium.org/141703007
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@247409 0039d316-1c4b-4281-b951-d872f2087c98
50 files changed, 366 insertions, 275 deletions
diff --git a/mojo/public/bindings/sample/sample_service_unittests.js b/mojo/apps/js/bindings/sample_service_unittests.js index 9b9165d..9b9165d 100644 --- a/mojo/public/bindings/sample/sample_service_unittests.js +++ b/mojo/apps/js/bindings/sample_service_unittests.js diff --git a/mojo/apps/js/test/run_js_tests.cc b/mojo/apps/js/test/run_js_tests.cc index 653a7e7..9de6d7c 100644 --- a/mojo/apps/js/test/run_js_tests.cc +++ b/mojo/apps/js/test/run_js_tests.cc @@ -55,15 +55,7 @@ TEST(JSTest, codec) { } TEST(JSTest, sample_test) { - base::FilePath path; - PathService::Get(base::DIR_SOURCE_ROOT, &path); - path = path.AppendASCII("mojo") - .AppendASCII("public") - .AppendASCII("bindings") - .AppendASCII("sample") - .AppendASCII("sample_service_unittests.js"); - TestRunnerDelegate delegate; - gin::RunTestFromFile(path, &delegate); + RunTest("sample_service_unittests.js", true); } TEST(JSTest, connector) { diff --git a/mojo/common/common_type_converters.h b/mojo/common/common_type_converters.h index 1ff5787..1c4ca75 100644 --- a/mojo/common/common_type_converters.h +++ b/mojo/common/common_type_converters.h @@ -7,7 +7,8 @@ #include "base/strings/string_piece.h" #include "mojo/common/mojo_common_export.h" -#include "mojo/public/bindings/lib/bindings.h" +#include "mojo/public/bindings/array.h" +#include "mojo/public/bindings/type_converter.h" namespace mojo { diff --git a/mojo/common/common_type_converters_unittest.cc b/mojo/common/common_type_converters_unittest.cc index 39e770a..5c7a47b 100644 --- a/mojo/common/common_type_converters_unittest.cc +++ b/mojo/common/common_type_converters_unittest.cc @@ -3,6 +3,7 @@ // found in the LICENSE file. #include "mojo/common/common_type_converters.h" +#include "mojo/public/bindings/allocation_scope.h" #include "testing/gtest/include/gtest/gtest.h" namespace mojo { diff --git a/mojo/examples/aura_demo/aura_demo.cc b/mojo/examples/aura_demo/aura_demo.cc index 1cf8077..3d9bb16 100644 --- a/mojo/examples/aura_demo/aura_demo.cc +++ b/mojo/examples/aura_demo/aura_demo.cc @@ -12,7 +12,8 @@ #include "mojo/examples/aura_demo/root_window_host_mojo.h" #include "mojo/examples/compositor_app/compositor_host.h" #include "mojo/examples/compositor_app/gles2_client_impl.h" -#include "mojo/public/bindings/lib/remote_ptr.h" +#include "mojo/public/bindings/allocation_scope.h" +#include "mojo/public/bindings/remote_ptr.h" #include "mojo/public/gles2/gles2_cpp.h" #include "mojo/public/system/core.h" #include "mojo/public/system/macros.h" diff --git a/mojo/examples/aura_demo/root_window_host_mojo.cc b/mojo/examples/aura_demo/root_window_host_mojo.cc index e1f5cb7..b448123 100644 --- a/mojo/examples/aura_demo/root_window_host_mojo.cc +++ b/mojo/examples/aura_demo/root_window_host_mojo.cc @@ -6,6 +6,7 @@ #include "mojo/examples/aura_demo/demo_context_factory.h" #include "mojo/examples/compositor_app/gles2_client_impl.h" +#include "mojo/public/bindings/allocation_scope.h" #include "mojo/public/gles2/gles2.h" #include "mojo/services/native_viewport/geometry_conversions.h" #include "ui/aura/env.h" diff --git a/mojo/examples/aura_demo/root_window_host_mojo.h b/mojo/examples/aura_demo/root_window_host_mojo.h index 82277542..bbc8fc2 100644 --- a/mojo/examples/aura_demo/root_window_host_mojo.h +++ b/mojo/examples/aura_demo/root_window_host_mojo.h @@ -6,7 +6,7 @@ #define MOJO_EXAMPLES_AURA_DEMO_ROOT_WINDOW_HOST_MOJO_H_ #include "base/bind.h" -#include "mojo/public/bindings/lib/remote_ptr.h" +#include "mojo/public/bindings/remote_ptr.h" #include "mojom/native_viewport.h" #include "ui/aura/window_tree_host.h" #include "ui/gfx/rect.h" diff --git a/mojo/examples/compositor_app/compositor_app.cc b/mojo/examples/compositor_app/compositor_app.cc index 1e7cb66..de6db6b 100644 --- a/mojo/examples/compositor_app/compositor_app.cc +++ b/mojo/examples/compositor_app/compositor_app.cc @@ -8,7 +8,8 @@ #include "base/message_loop/message_loop.h" #include "mojo/examples/compositor_app/compositor_host.h" #include "mojo/examples/compositor_app/gles2_client_impl.h" -#include "mojo/public/bindings/lib/remote_ptr.h" +#include "mojo/public/bindings/allocation_scope.h" +#include "mojo/public/bindings/remote_ptr.h" #include "mojo/public/gles2/gles2_cpp.h" #include "mojo/public/system/core.h" #include "mojo/public/system/macros.h" diff --git a/mojo/examples/compositor_app/gles2_client_impl.h b/mojo/examples/compositor_app/gles2_client_impl.h index 3c91cef..e162b1d 100644 --- a/mojo/examples/compositor_app/gles2_client_impl.h +++ b/mojo/examples/compositor_app/gles2_client_impl.h @@ -7,7 +7,7 @@ #include "base/time/time.h" #include "base/timer/timer.h" -#include "mojo/public/bindings/lib/remote_ptr.h" +#include "mojo/public/bindings/remote_ptr.h" #include "mojo/public/gles2/gles2.h" #include "mojom/native_viewport.h" #include "ui/gfx/size.h" diff --git a/mojo/examples/launcher/launcher.cc b/mojo/examples/launcher/launcher.cc index 9b2c1fb..c5d4962 100644 --- a/mojo/examples/launcher/launcher.cc +++ b/mojo/examples/launcher/launcher.cc @@ -15,7 +15,8 @@ #include "mojo/examples/aura_demo/root_window_host_mojo.h" #include "mojo/examples/compositor_app/compositor_host.h" #include "mojo/examples/compositor_app/gles2_client_impl.h" -#include "mojo/public/bindings/lib/remote_ptr.h" +#include "mojo/public/bindings/allocation_scope.h" +#include "mojo/public/bindings/remote_ptr.h" #include "mojo/public/gles2/gles2_cpp.h" #include "mojo/public/system/core.h" #include "mojo/public/system/macros.h" diff --git a/mojo/examples/sample_app/gles2_client_impl.h b/mojo/examples/sample_app/gles2_client_impl.h index d927c24..dbb5e82 100644 --- a/mojo/examples/sample_app/gles2_client_impl.h +++ b/mojo/examples/sample_app/gles2_client_impl.h @@ -6,7 +6,7 @@ #define MOJO_EXAMPLES_SAMPLE_APP_GLES2_CLIENT_IMPL_H_ #include "mojo/examples/sample_app/spinning_cube.h" -#include "mojo/public/bindings/lib/remote_ptr.h" +#include "mojo/public/bindings/remote_ptr.h" #include "mojo/public/gles2/gles2.h" #include "mojom/native_viewport.h" #include "ui/gfx/point_f.h" diff --git a/mojo/examples/sample_app/sample_app.cc b/mojo/examples/sample_app/sample_app.cc index 5c3e878..50d3508 100644 --- a/mojo/examples/sample_app/sample_app.cc +++ b/mojo/examples/sample_app/sample_app.cc @@ -6,7 +6,8 @@ #include <string> #include "mojo/examples/sample_app/gles2_client_impl.h" -#include "mojo/public/bindings/lib/remote_ptr.h" +#include "mojo/public/bindings/allocation_scope.h" +#include "mojo/public/bindings/remote_ptr.h" #include "mojo/public/environment/environment.h" #include "mojo/public/gles2/gles2_cpp.h" #include "mojo/public/system/core.h" diff --git a/mojo/gles2/gles2_client_impl.h b/mojo/gles2/gles2_client_impl.h index 79c833c..7abd0fc 100644 --- a/mojo/gles2/gles2_client_impl.h +++ b/mojo/gles2/gles2_client_impl.h @@ -6,7 +6,7 @@ #define MOJO_GLES2_GLES2_CLIENT_IMPL_H_ #include "gpu/command_buffer/client/gles2_implementation.h" -#include "mojo/public/bindings/lib/remote_ptr.h" +#include "mojo/public/bindings/remote_ptr.h" #include "mojo/public/gles2/gles2.h" #include "mojom/gles2.h" diff --git a/mojo/mojo_public.gypi b/mojo/mojo_public.gypi index b921e18..448f1de 100644 --- a/mojo/mojo_public.gypi +++ b/mojo/mojo_public.gypi @@ -79,16 +79,16 @@ 'mojo_utility', ], 'sources': [ - 'public/tests/bindings/array_unittest.cc', - 'public/tests/bindings/connector_unittest.cc', - 'public/tests/bindings/handle_passing_unittest.cc', - 'public/tests/bindings/remote_ptr_unittest.cc', - 'public/tests/bindings/type_conversion_unittest.cc', - 'public/tests/bindings/buffer_unittest.cc', - 'public/tests/bindings/math_calculator.mojom', - 'public/tests/bindings/sample_factory.mojom', - 'public/tests/bindings/sample_service_unittest.cc', - 'public/tests/bindings/test_structs.mojom', + 'public/bindings/tests/array_unittest.cc', + 'public/bindings/tests/connector_unittest.cc', + 'public/bindings/tests/handle_passing_unittest.cc', + 'public/bindings/tests/remote_ptr_unittest.cc', + 'public/bindings/tests/type_conversion_unittest.cc', + 'public/bindings/tests/buffer_unittest.cc', + 'public/bindings/tests/math_calculator.mojom', + 'public/bindings/tests/sample_factory.mojom', + 'public/bindings/tests/sample_service_unittest.cc', + 'public/bindings/tests/test_structs.mojom', ], 'includes': [ 'public/bindings/mojom_bindings_generator.gypi' ], }, @@ -163,31 +163,38 @@ '..' ], 'sources': [ + 'public/bindings/allocation_scope.h', + 'public/bindings/array.h', + 'public/bindings/buffer.h', + 'public/bindings/passable.h', + 'public/bindings/remote_ptr.h', + 'public/bindings/type_converter.h', 'public/bindings/lib/array.cc', - 'public/bindings/lib/array.h', 'public/bindings/lib/array_internal.h', 'public/bindings/lib/array_internal.cc', - 'public/bindings/lib/bindings.h', 'public/bindings/lib/bindings_internal.h', 'public/bindings/lib/bindings_serialization.cc', 'public/bindings/lib/bindings_serialization.h', 'public/bindings/lib/buffer.cc', - 'public/bindings/lib/buffer.h', 'public/bindings/lib/connector.cc', 'public/bindings/lib/connector.h', + 'public/bindings/lib/fixed_buffer.cc', + 'public/bindings/lib/fixed_buffer.h', 'public/bindings/lib/message.cc', 'public/bindings/lib/message.h', 'public/bindings/lib/message_builder.cc', 'public/bindings/lib/message_builder.h', 'public/bindings/lib/message_queue.cc', 'public/bindings/lib/message_queue.h', + 'public/bindings/lib/scratch_buffer.cc', + 'public/bindings/lib/scratch_buffer.h', ], }, { 'target_name': 'mojo_sample_service', 'type': 'static_library', 'sources': [ - 'public/tests/bindings/sample_service.mojom', + 'public/bindings/tests/sample_service.mojom', ], 'includes': [ 'public/bindings/mojom_bindings_generator.gypi' ], 'export_dependent_settings': [ diff --git a/mojo/public/bindings/allocation_scope.h b/mojo/public/bindings/allocation_scope.h new file mode 100644 index 0000000..16dc426 --- /dev/null +++ b/mojo/public/bindings/allocation_scope.h @@ -0,0 +1,28 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef MOJO_PUBLIC_BINDINGS_ALLOCATION_SCOPE_H_ +#define MOJO_PUBLIC_BINDINGS_ALLOCATION_SCOPE_H_ + +#include "mojo/public/bindings/lib/scratch_buffer.h" +#include "mojo/public/system/macros.h" + +namespace mojo { + +class AllocationScope { + public: + AllocationScope() {} + ~AllocationScope() {} + + Buffer* buffer() { return &buffer_; } + + private: + internal::ScratchBuffer buffer_; + + MOJO_DISALLOW_COPY_AND_ASSIGN(AllocationScope); +}; + +} // namespace mojo + +#endif // MOJO_PUBLIC_BINDINGS_ALLOCATION_SCOPE_H_ diff --git a/mojo/public/bindings/lib/array.h b/mojo/public/bindings/array.h index ca236a5..2ab7c98 100644 --- a/mojo/public/bindings/lib/array.h +++ b/mojo/public/bindings/array.h @@ -2,8 +2,8 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#ifndef MOJO_PUBLIC_BINDINGS_LIB_ARRAY_H_ -#define MOJO_PUBLIC_BINDINGS_LIB_ARRAY_H_ +#ifndef MOJO_PUBLIC_BINDINGS_ARRAY_H_ +#define MOJO_PUBLIC_BINDINGS_ARRAY_H_ #include <string.h> @@ -12,6 +12,7 @@ #include <vector> #include "mojo/public/bindings/lib/array_internal.h" +#include "mojo/public/bindings/type_converter.h" namespace mojo { @@ -153,4 +154,4 @@ class TypeConverter<Array<T>, std::vector<E> > { } // namespace mojo -#endif // MOJO_PUBLIC_BINDINGS_LIB_ARRAY_H_ +#endif // MOJO_PUBLIC_BINDINGS_ARRAY_H_ diff --git a/mojo/public/bindings/buffer.h b/mojo/public/bindings/buffer.h new file mode 100644 index 0000000..bdd3349 --- /dev/null +++ b/mojo/public/bindings/buffer.h @@ -0,0 +1,31 @@ +// Copyright 2013 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef MOJO_PUBLIC_BINDINGS_BUFFER_H_ +#define MOJO_PUBLIC_BINDINGS_BUFFER_H_ + +#include <stddef.h> + +namespace mojo { + +// Buffer provides a way to allocate memory. Allocations are 8-byte aligned and +// zero-initialized. Allocations remain valid for the lifetime of the Buffer. +class Buffer { + public: + typedef void (*Destructor)(void* address); + + Buffer(); + virtual ~Buffer(); + + virtual void* Allocate(size_t num_bytes, Destructor func = NULL) = 0; + + static Buffer* current(); + + private: + Buffer* previous_; +}; + +} // namespace mojo + +#endif // MOJO_PUBLIC_BINDINGS_BUFFER_H_ diff --git a/mojo/public/bindings/generators/cpp_templates/module.h.tmpl b/mojo/public/bindings/generators/cpp_templates/module.h.tmpl index 776d367..ee75b71 100644 --- a/mojo/public/bindings/generators/cpp_templates/module.h.tmpl +++ b/mojo/public/bindings/generators/cpp_templates/module.h.tmpl @@ -8,7 +8,7 @@ #ifndef {{header_guard}} #define {{header_guard}} -#include "mojo/public/bindings/lib/bindings.h" +#include "mojo/public/bindings/array.h" #include "mojo/public/bindings/lib/message.h" #include "{{include_prefix}}{{module_name|camel_to_underscores}}_internal.h" diff --git a/mojo/public/bindings/lib/array.cc b/mojo/public/bindings/lib/array.cc index df1a1bc..dc4f657 100644 --- a/mojo/public/bindings/lib/array.cc +++ b/mojo/public/bindings/lib/array.cc @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include "mojo/public/bindings/lib/array.h" +#include "mojo/public/bindings/array.h" namespace mojo { diff --git a/mojo/public/bindings/lib/array_internal.h b/mojo/public/bindings/lib/array_internal.h index 3894ed7..2368b87 100644 --- a/mojo/public/bindings/lib/array_internal.h +++ b/mojo/public/bindings/lib/array_internal.h @@ -7,10 +7,10 @@ #include <new> +#include "mojo/public/bindings/buffer.h" #include "mojo/public/bindings/lib/bindings_internal.h" #include "mojo/public/bindings/lib/bindings_serialization.h" -#include "mojo/public/bindings/lib/buffer.h" -#include "mojo/public/bindings/lib/passable.h" +#include "mojo/public/bindings/passable.h" #include "mojo/public/system/core_cpp.h" namespace mojo { diff --git a/mojo/public/bindings/lib/bindings.h b/mojo/public/bindings/lib/bindings.h deleted file mode 100644 index e647201..0000000 --- a/mojo/public/bindings/lib/bindings.h +++ /dev/null @@ -1,10 +0,0 @@ -// Copyright 2013 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef MOJO_PUBLIC_BINDINGS_LIB_BINDINGS_H_ -#define MOJO_PUBLIC_BINDINGS_LIB_BINDINGS_H_ - -#include "mojo/public/bindings/lib/array.h" - -#endif // MOJO_PUBLIC_BINDINGS_LIB_BINDINGS_H_ diff --git a/mojo/public/bindings/lib/bindings_internal.h b/mojo/public/bindings/lib/bindings_internal.h index 6509d66..7c2a8f1 100644 --- a/mojo/public/bindings/lib/bindings_internal.h +++ b/mojo/public/bindings/lib/bindings_internal.h @@ -8,8 +8,6 @@ #include "mojo/public/system/core_cpp.h" namespace mojo { -template <typename T, typename U> class TypeConverter {}; - namespace internal { template <typename T> class Array_Data; diff --git a/mojo/public/bindings/lib/bindings_serialization.h b/mojo/public/bindings/lib/bindings_serialization.h index 35f4a1d..1c89c26 100644 --- a/mojo/public/bindings/lib/bindings_serialization.h +++ b/mojo/public/bindings/lib/bindings_serialization.h @@ -7,7 +7,7 @@ #include <vector> -#include "mojo/public/bindings/lib/buffer.h" +#include "mojo/public/bindings/buffer.h" #include "mojo/public/bindings/lib/message.h" namespace mojo { diff --git a/mojo/public/bindings/lib/buffer.cc b/mojo/public/bindings/lib/buffer.cc index bb7a53a..0f1ce86 100644 --- a/mojo/public/bindings/lib/buffer.cc +++ b/mojo/public/bindings/lib/buffer.cc @@ -2,28 +2,15 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include "mojo/public/bindings/lib/buffer.h" +#include "mojo/public/bindings/buffer.h" #include <assert.h> -#include <stdlib.h> -#include <string.h> -#include <algorithm> - -#include "mojo/public/bindings/lib/bindings_serialization.h" #include "mojo/public/environment/buffer_tls.h" - -// Scrub memory in debug builds to help catch use-after-free bugs. -#ifdef NDEBUG -#define DEBUG_SCRUB(address, size) (void) (address), (void) (size) -#else -#define DEBUG_SCRUB(address, size) memset(address, 0xCD, size) -#endif +#include "mojo/public/system/macros.h" namespace mojo { -//----------------------------------------------------------------------------- - Buffer::Buffer() { previous_ = internal::SetCurrentBuffer(this); } @@ -37,119 +24,4 @@ Buffer* Buffer::current() { return internal::GetCurrentBuffer(); } -//----------------------------------------------------------------------------- - -namespace internal { - -ScratchBuffer::ScratchBuffer() - : overflow_(NULL) { - fixed_.next = NULL; - fixed_.cursor = fixed_data_; - fixed_.end = fixed_data_ + kMinSegmentSize; -} - -ScratchBuffer::~ScratchBuffer() { - // Invoke destructors in reverse order to mirror allocation order. - std::deque<PendingDestructor>::reverse_iterator it; - for (it = pending_dtors_.rbegin(); it != pending_dtors_.rend(); ++it) - it->func(it->address); - - while (overflow_) { - Segment* doomed = overflow_; - overflow_ = overflow_->next; - DEBUG_SCRUB(doomed, doomed->end - reinterpret_cast<char*>(doomed)); - free(doomed); - } - DEBUG_SCRUB(fixed_data_, sizeof(fixed_data_)); -} - -void* ScratchBuffer::Allocate(size_t delta, Destructor func) { - delta = internal::Align(delta); - - void* result = AllocateInSegment(&fixed_, delta); - if (!result) { - if (overflow_) - result = AllocateInSegment(overflow_, delta); - - if (!result) { - AddOverflowSegment(delta); - result = AllocateInSegment(overflow_, delta); - } - } - - if (func) { - PendingDestructor dtor; - dtor.func = func; - dtor.address = result; - pending_dtors_.push_back(dtor); - } - return result; -} - -void* ScratchBuffer::AllocateInSegment(Segment* segment, size_t delta) { - void* result; - if (static_cast<size_t>(segment->end - segment->cursor) >= delta) { - result = segment->cursor; - memset(result, 0, delta); - segment->cursor += delta; - } else { - result = NULL; - } - return result; -} - -void ScratchBuffer::AddOverflowSegment(size_t delta) { - if (delta < kMinSegmentSize) - delta = kMinSegmentSize; - - // Ensure segment buffer is aligned. - size_t segment_size = internal::Align(sizeof(Segment)) + delta; - - Segment* segment = static_cast<Segment*>(malloc(segment_size)); - segment->next = overflow_; - segment->cursor = reinterpret_cast<char*>(segment + 1); - segment->end = segment->cursor + delta; - - overflow_ = segment; -} - -//----------------------------------------------------------------------------- - -FixedBuffer::FixedBuffer(size_t size) - : ptr_(NULL), - cursor_(0), - size_(internal::Align(size)) { - ptr_ = static_cast<char*>(calloc(size_, 1)); -} - -FixedBuffer::~FixedBuffer() { - free(ptr_); -} - -void* FixedBuffer::Allocate(size_t delta, Destructor dtor) { - assert(!dtor); - - delta = internal::Align(delta); - - // TODO(darin): Using <assert.h> is probably not going to cut it. - assert(delta > 0); - assert(cursor_ + delta <= size_); - if (cursor_ + delta > size_) - return NULL; - - char* result = ptr_ + cursor_; - cursor_ += delta; - - return result; -} - -void* FixedBuffer::Leak() { - char* ptr = ptr_; - ptr_ = NULL; - cursor_ = 0; - size_ = 0; - return ptr; -} - -} // namespace internal } // namespace mojo diff --git a/mojo/public/bindings/lib/fixed_buffer.cc b/mojo/public/bindings/lib/fixed_buffer.cc new file mode 100644 index 0000000..6b20dd1 --- /dev/null +++ b/mojo/public/bindings/lib/fixed_buffer.cc @@ -0,0 +1,55 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "mojo/public/bindings/lib/fixed_buffer.h" + +#include <assert.h> +#include <stdlib.h> +#include <string.h> + +#include <algorithm> + +#include "mojo/public/bindings/lib/bindings_serialization.h" + +namespace mojo { +namespace internal { + +FixedBuffer::FixedBuffer(size_t size) + : ptr_(NULL), + cursor_(0), + size_(internal::Align(size)) { + ptr_ = static_cast<char*>(calloc(size_, 1)); +} + +FixedBuffer::~FixedBuffer() { + free(ptr_); +} + +void* FixedBuffer::Allocate(size_t delta, Destructor dtor) { + assert(!dtor); + + delta = internal::Align(delta); + + // TODO(darin): Using <assert.h> is probably not going to cut it. + assert(delta > 0); + assert(cursor_ + delta <= size_); + if (cursor_ + delta > size_) + return NULL; + + char* result = ptr_ + cursor_; + cursor_ += delta; + + return result; +} + +void* FixedBuffer::Leak() { + char* ptr = ptr_; + ptr_ = NULL; + cursor_ = 0; + size_ = 0; + return ptr; +} + +} // namespace internal +} // namespace mojo diff --git a/mojo/public/bindings/lib/buffer.h b/mojo/public/bindings/lib/fixed_buffer.h index 6f0ce88..900c84e 100644 --- a/mojo/public/bindings/lib/buffer.h +++ b/mojo/public/bindings/lib/fixed_buffer.h @@ -1,72 +1,16 @@ -// Copyright 2013 The Chromium Authors. All rights reserved. +// Copyright 2014 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#ifndef MOJO_PUBLIC_BINDINGS_LIB_BUFFER_H_ -#define MOJO_PUBLIC_BINDINGS_LIB_BUFFER_H_ - -#include <stddef.h> - -#include <deque> +#ifndef MOJO_PUBLIC_BINDINGS_LIB_FIXED_BUFFER_H_ +#define MOJO_PUBLIC_BINDINGS_LIB_FIXED_BUFFER_H_ +#include "mojo/public/bindings/buffer.h" #include "mojo/public/system/macros.h" namespace mojo { - -// Buffer provides a way to allocate memory. Allocations are 8-byte aligned and -// zero-initialized. Allocations remain valid for the lifetime of the Buffer. -class Buffer { - public: - typedef void (*Destructor)(void* address); - - Buffer(); - virtual ~Buffer(); - - virtual void* Allocate(size_t num_bytes, Destructor func = NULL) = 0; - - static Buffer* current(); - - private: - Buffer* previous_; -}; - namespace internal { -// The following class is designed to be allocated on the stack. If necessary, -// it will failover to allocating objects on the heap. -class ScratchBuffer : public Buffer { - public: - ScratchBuffer(); - virtual ~ScratchBuffer(); - - virtual void* Allocate(size_t num_bytes, Destructor func = NULL) - MOJO_OVERRIDE; - - private: - enum { kMinSegmentSize = 512 }; - - struct Segment { - Segment* next; - char* cursor; - char* end; - }; - - void* AllocateInSegment(Segment* segment, size_t num_bytes); - void AddOverflowSegment(size_t delta); - - char fixed_data_[kMinSegmentSize]; - Segment fixed_; - Segment* overflow_; - - struct PendingDestructor { - Destructor func; - void* address; - }; - std::deque<PendingDestructor> pending_dtors_; - - MOJO_DISALLOW_COPY_AND_ASSIGN(ScratchBuffer); -}; - // FixedBuffer provides a simple way to allocate objects within a fixed chunk // of memory. Objects are allocated by calling the |Allocate| method, which // extends the buffer accordingly. Objects allocated in this way are not freed @@ -119,20 +63,6 @@ class FixedBuffer : public Buffer { }; } // namespace internal - -class AllocationScope { - public: - AllocationScope() {} - ~AllocationScope() {} - - Buffer* buffer() { return &buffer_; } - - private: - internal::ScratchBuffer buffer_; - - MOJO_DISALLOW_COPY_AND_ASSIGN(AllocationScope); -}; - } // namespace mojo -#endif // MOJO_PUBLIC_BINDINGS_LIB_BUFFER_H_ +#endif // MOJO_PUBLIC_BINDINGS_LIB_FIXED_BUFFER_H_ diff --git a/mojo/public/bindings/lib/message_builder.h b/mojo/public/bindings/lib/message_builder.h index 4014686..a8dfdfa 100644 --- a/mojo/public/bindings/lib/message_builder.h +++ b/mojo/public/bindings/lib/message_builder.h @@ -7,7 +7,7 @@ #include <stdint.h> -#include "mojo/public/bindings/lib/buffer.h" +#include "mojo/public/bindings/lib/fixed_buffer.h" namespace mojo { struct MessageData; diff --git a/mojo/public/bindings/lib/scratch_buffer.cc b/mojo/public/bindings/lib/scratch_buffer.cc new file mode 100644 index 0000000..abfb5fe --- /dev/null +++ b/mojo/public/bindings/lib/scratch_buffer.cc @@ -0,0 +1,98 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "mojo/public/bindings/lib/scratch_buffer.h" + +#include <assert.h> +#include <stdlib.h> +#include <string.h> + +#include <algorithm> + +#include "mojo/public/bindings/lib/bindings_serialization.h" + +// Scrub memory in debug builds to help catch use-after-free bugs. +#ifdef NDEBUG +#define DEBUG_SCRUB(address, size) (void) (address), (void) (size) +#else +#define DEBUG_SCRUB(address, size) memset(address, 0xCD, size) +#endif + +namespace mojo { +namespace internal { + +ScratchBuffer::ScratchBuffer() + : overflow_(NULL) { + fixed_.next = NULL; + fixed_.cursor = fixed_data_; + fixed_.end = fixed_data_ + kMinSegmentSize; +} + +ScratchBuffer::~ScratchBuffer() { + // Invoke destructors in reverse order to mirror allocation order. + std::deque<PendingDestructor>::reverse_iterator it; + for (it = pending_dtors_.rbegin(); it != pending_dtors_.rend(); ++it) + it->func(it->address); + + while (overflow_) { + Segment* doomed = overflow_; + overflow_ = overflow_->next; + DEBUG_SCRUB(doomed, doomed->end - reinterpret_cast<char*>(doomed)); + free(doomed); + } + DEBUG_SCRUB(fixed_data_, sizeof(fixed_data_)); +} + +void* ScratchBuffer::Allocate(size_t delta, Destructor func) { + delta = internal::Align(delta); + + void* result = AllocateInSegment(&fixed_, delta); + if (!result) { + if (overflow_) + result = AllocateInSegment(overflow_, delta); + + if (!result) { + AddOverflowSegment(delta); + result = AllocateInSegment(overflow_, delta); + } + } + + if (func) { + PendingDestructor dtor; + dtor.func = func; + dtor.address = result; + pending_dtors_.push_back(dtor); + } + return result; +} + +void* ScratchBuffer::AllocateInSegment(Segment* segment, size_t delta) { + void* result; + if (static_cast<size_t>(segment->end - segment->cursor) >= delta) { + result = segment->cursor; + memset(result, 0, delta); + segment->cursor += delta; + } else { + result = NULL; + } + return result; +} + +void ScratchBuffer::AddOverflowSegment(size_t delta) { + if (delta < kMinSegmentSize) + delta = kMinSegmentSize; + + // Ensure segment buffer is aligned. + size_t segment_size = internal::Align(sizeof(Segment)) + delta; + + Segment* segment = static_cast<Segment*>(malloc(segment_size)); + segment->next = overflow_; + segment->cursor = reinterpret_cast<char*>(segment + 1); + segment->end = segment->cursor + delta; + + overflow_ = segment; +} + +} // namespace internal +} // namespace mojo diff --git a/mojo/public/bindings/lib/scratch_buffer.h b/mojo/public/bindings/lib/scratch_buffer.h new file mode 100644 index 0000000..2ab933a9 --- /dev/null +++ b/mojo/public/bindings/lib/scratch_buffer.h @@ -0,0 +1,54 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef MOJO_PUBLIC_BINDINGS_LIB_SCRATCH_BUFFER_H_ +#define MOJO_PUBLIC_BINDINGS_LIB_SCRATCH_BUFFER_H_ + +#include <deque> + +#include "mojo/public/bindings/buffer.h" +#include "mojo/public/system/macros.h" + +namespace mojo { +namespace internal { + +// The following class is designed to be allocated on the stack. If necessary, +// it will failover to allocating objects on the heap. +class ScratchBuffer : public Buffer { + public: + ScratchBuffer(); + virtual ~ScratchBuffer(); + + virtual void* Allocate(size_t num_bytes, Destructor func = NULL) + MOJO_OVERRIDE; + + private: + enum { kMinSegmentSize = 512 }; + + struct Segment { + Segment* next; + char* cursor; + char* end; + }; + + void* AllocateInSegment(Segment* segment, size_t num_bytes); + void AddOverflowSegment(size_t delta); + + char fixed_data_[kMinSegmentSize]; + Segment fixed_; + Segment* overflow_; + + struct PendingDestructor { + Destructor func; + void* address; + }; + std::deque<PendingDestructor> pending_dtors_; + + MOJO_DISALLOW_COPY_AND_ASSIGN(ScratchBuffer); +}; + +} // namespace internal +} // namespace mojo + +#endif // MOJO_PUBLIC_BINDINGS_LIB_SCRATCH_BUFFER_H_ diff --git a/mojo/public/bindings/lib/passable.h b/mojo/public/bindings/passable.h index 07cd2be..6fbd38e 100644 --- a/mojo/public/bindings/lib/passable.h +++ b/mojo/public/bindings/passable.h @@ -2,9 +2,10 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#ifndef MOJO_PUBLIC_BINDINGS_LIB_PASSABLE_H_ -#define MOJO_PUBLIC_BINDINGS_LIB_PASSABLE_H_ +#ifndef MOJO_PUBLIC_BINDINGS_PASSABLE_H_ +#define MOJO_PUBLIC_BINDINGS_PASSABLE_H_ +#include "mojo/public/bindings/lib/bindings_internal.h" #include "mojo/public/system/core_cpp.h" namespace mojo { @@ -69,4 +70,4 @@ inline AssignableAndPassable<HandleType> MakeAssignableAndPassable( } // namespace mojo -#endif // MOJO_PUBLIC_BINDINGS_LIB_PASSABLE_H_ +#endif // MOJO_PUBLIC_BINDINGS_PASSABLE_H_ diff --git a/mojo/public/bindings/lib/remote_ptr.h b/mojo/public/bindings/remote_ptr.h index 3ab5fb2..7acd971 100644 --- a/mojo/public/bindings/lib/remote_ptr.h +++ b/mojo/public/bindings/remote_ptr.h @@ -2,8 +2,8 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#ifndef MOJO_PUBLIC_BINDINGS_LIB_REMOTE_PTR_H_ -#define MOJO_PUBLIC_BINDINGS_LIB_REMOTE_PTR_H_ +#ifndef MOJO_PUBLIC_BINDINGS_REMOTE_PTR_H_ +#define MOJO_PUBLIC_BINDINGS_REMOTE_PTR_H_ #include <assert.h> @@ -126,4 +126,4 @@ class RemotePtr { } // namespace mojo -#endif // MOJO_PUBLIC_BINDINGS_LIB_REMOTE_PTR_H_ +#endif // MOJO_PUBLIC_BINDINGS_REMOTE_PTR_H_ diff --git a/mojo/public/tests/bindings/DEPS b/mojo/public/bindings/tests/DEPS index eac29ee..eac29ee 100644 --- a/mojo/public/tests/bindings/DEPS +++ b/mojo/public/bindings/tests/DEPS diff --git a/mojo/public/tests/bindings/array_unittest.cc b/mojo/public/bindings/tests/array_unittest.cc index dac4b50..db87756 100644 --- a/mojo/public/tests/bindings/array_unittest.cc +++ b/mojo/public/bindings/tests/array_unittest.cc @@ -1,8 +1,11 @@ -// Copyright 2013 The Chromium Authors. All rights reserved. +// Copyright 2014 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include "mojo/public/bindings/lib/array.h" +#include "mojo/public/bindings/allocation_scope.h" +#include "mojo/public/bindings/array.h" +#include "mojo/public/bindings/lib/fixed_buffer.h" +#include "mojo/public/bindings/lib/scratch_buffer.h" #include "mojo/public/environment/environment.h" #include "testing/gtest/include/gtest/gtest.h" diff --git a/mojo/public/tests/bindings/buffer_unittest.cc b/mojo/public/bindings/tests/buffer_unittest.cc index 06cb4ca..ae1de49 100644 --- a/mojo/public/tests/bindings/buffer_unittest.cc +++ b/mojo/public/bindings/tests/buffer_unittest.cc @@ -1,9 +1,11 @@ -// Copyright 2013 The Chromium Authors. All rights reserved. +// Copyright 2014 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +#include "mojo/public/bindings/buffer.h" #include "mojo/public/bindings/lib/bindings_serialization.h" -#include "mojo/public/bindings/lib/buffer.h" +#include "mojo/public/bindings/lib/fixed_buffer.h" +#include "mojo/public/bindings/lib/scratch_buffer.h" #include "mojo/public/environment/environment.h" #include "testing/gtest/include/gtest/gtest.h" diff --git a/mojo/public/tests/bindings/connector_unittest.cc b/mojo/public/bindings/tests/connector_unittest.cc index a814ffc..a814ffc 100644 --- a/mojo/public/tests/bindings/connector_unittest.cc +++ b/mojo/public/bindings/tests/connector_unittest.cc diff --git a/mojo/public/tests/bindings/handle_passing_unittest.cc b/mojo/public/bindings/tests/handle_passing_unittest.cc index ca2d2709..98fc913 100644 --- a/mojo/public/tests/bindings/handle_passing_unittest.cc +++ b/mojo/public/bindings/tests/handle_passing_unittest.cc @@ -2,7 +2,8 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include "mojo/public/bindings/lib/remote_ptr.h" +#include "mojo/public/bindings/allocation_scope.h" +#include "mojo/public/bindings/remote_ptr.h" #include "mojo/public/environment/environment.h" #include "mojo/public/tests/test_support.h" #include "mojo/public/utility/run_loop.h" diff --git a/mojo/public/tests/bindings/math_calculator.mojom b/mojo/public/bindings/tests/math_calculator.mojom index 949a902..949a902 100644 --- a/mojo/public/tests/bindings/math_calculator.mojom +++ b/mojo/public/bindings/tests/math_calculator.mojom diff --git a/mojo/public/tests/bindings/remote_ptr_unittest.cc b/mojo/public/bindings/tests/remote_ptr_unittest.cc index f4f3b23..c99459a 100644 --- a/mojo/public/tests/bindings/remote_ptr_unittest.cc +++ b/mojo/public/bindings/tests/remote_ptr_unittest.cc @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include "mojo/public/bindings/lib/remote_ptr.h" +#include "mojo/public/bindings/remote_ptr.h" #include "mojo/public/environment/environment.h" #include "mojo/public/utility/run_loop.h" #include "mojom/math_calculator.h" diff --git a/mojo/public/tests/bindings/sample_factory.mojom b/mojo/public/bindings/tests/sample_factory.mojom index 7ae6b34..7ae6b34 100644 --- a/mojo/public/tests/bindings/sample_factory.mojom +++ b/mojo/public/bindings/tests/sample_factory.mojom diff --git a/mojo/public/tests/bindings/sample_service.mojom b/mojo/public/bindings/tests/sample_service.mojom index b7d328a..b7d328a 100644 --- a/mojo/public/tests/bindings/sample_service.mojom +++ b/mojo/public/bindings/tests/sample_service.mojom diff --git a/mojo/public/tests/bindings/sample_service_unittest.cc b/mojo/public/bindings/tests/sample_service_unittest.cc index 62b144b..c04e6b5 100644 --- a/mojo/public/tests/bindings/sample_service_unittest.cc +++ b/mojo/public/bindings/tests/sample_service_unittest.cc @@ -6,6 +6,7 @@ #include <ostream> #include <string> +#include "mojo/public/bindings/allocation_scope.h" #include "mojo/public/environment/environment.h" #include "mojom/sample_service.h" #include "testing/gtest/include/gtest/gtest.h" diff --git a/mojo/public/tests/bindings/test_structs.mojom b/mojo/public/bindings/tests/test_structs.mojom index c205e84..c205e84 100644 --- a/mojo/public/tests/bindings/test_structs.mojom +++ b/mojo/public/bindings/tests/test_structs.mojom diff --git a/mojo/public/tests/bindings/type_conversion_unittest.cc b/mojo/public/bindings/tests/type_conversion_unittest.cc index ad0d1f7..723d036 100644 --- a/mojo/public/tests/bindings/type_conversion_unittest.cc +++ b/mojo/public/bindings/tests/type_conversion_unittest.cc @@ -2,6 +2,7 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +#include "mojo/public/bindings/allocation_scope.h" #include "mojo/public/environment/environment.h" #include "mojom/test_structs.h" #include "testing/gtest/include/gtest/gtest.h" diff --git a/mojo/public/bindings/type_converter.h b/mojo/public/bindings/type_converter.h new file mode 100644 index 0000000..d89ddf6 --- /dev/null +++ b/mojo/public/bindings/type_converter.h @@ -0,0 +1,18 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef MOJO_PUBLIC_BINDINGS_TYPE_CONVERTER_H_ +#define MOJO_PUBLIC_BINDINGS_TYPE_CONVERTER_H_ + +namespace mojo { + +// Specialize to perform type conversion. +template <typename T, typename U> class TypeConverter { + // static T ConvertFrom(const U& input, Buffer* buf); + // static U ConvertTo(const T& input); +}; + +} // namespace mojo + +#endif // MOJO_PUBLIC_BINDINGS_TYPE_CONVERTER_H_ diff --git a/mojo/services/gles2/gles2_impl.h b/mojo/services/gles2/gles2_impl.h index ad97dae..077bdcd 100644 --- a/mojo/services/gles2/gles2_impl.h +++ b/mojo/services/gles2/gles2_impl.h @@ -7,7 +7,7 @@ #include "base/memory/scoped_ptr.h" #include "base/timer/timer.h" -#include "mojo/public/bindings/lib/remote_ptr.h" +#include "mojo/public/bindings/remote_ptr.h" #include "mojo/public/system/core_cpp.h" #include "mojom/gles2.h" #include "ui/gfx/native_widget_types.h" diff --git a/mojo/services/native_viewport/geometry_conversions.h b/mojo/services/native_viewport/geometry_conversions.h index 16f6a20..505674b 100644 --- a/mojo/services/native_viewport/geometry_conversions.h +++ b/mojo/services/native_viewport/geometry_conversions.h @@ -28,7 +28,6 @@ template<> class TypeConverter<Size, gfx::Size> { public: static Size ConvertFrom(const gfx::Size& input, Buffer* buf) { - mojo::AllocationScope scope; Size::Builder size(buf); size.set_width(input.width()); size.set_height(input.height()); diff --git a/mojo/services/native_viewport/native_viewport_service.cc b/mojo/services/native_viewport/native_viewport_service.cc index 3d42382..ddb46c7 100644 --- a/mojo/services/native_viewport/native_viewport_service.cc +++ b/mojo/services/native_viewport/native_viewport_service.cc @@ -6,6 +6,7 @@ #include "base/message_loop/message_loop.h" #include "base/time/time.h" +#include "mojo/public/bindings/allocation_scope.h" #include "mojo/services/gles2/gles2_impl.h" #include "mojo/services/native_viewport/geometry_conversions.h" #include "mojo/services/native_viewport/native_viewport.h" diff --git a/mojo/services/native_viewport/native_viewport_service.h b/mojo/services/native_viewport/native_viewport_service.h index 85b734a..69e7f83 100644 --- a/mojo/services/native_viewport/native_viewport_service.h +++ b/mojo/services/native_viewport/native_viewport_service.h @@ -6,7 +6,7 @@ #define MOJO_SERVICES_NATIVE_VIEWPORT_SERVICE_H_ #include "base/memory/scoped_vector.h" -#include "mojo/public/bindings/lib/remote_ptr.h" +#include "mojo/public/bindings/remote_ptr.h" #include "mojo/services/native_viewport/native_viewport_export.h" #include "mojom/shell.h" diff --git a/mojo/shell/service_manager.cc b/mojo/shell/service_manager.cc index 55fd41f..bd0d9d2 100644 --- a/mojo/shell/service_manager.cc +++ b/mojo/shell/service_manager.cc @@ -5,7 +5,7 @@ #include "mojo/shell/service_manager.h" #include "base/logging.h" -#include "mojo/public/bindings/lib/remote_ptr.h" +#include "mojo/public/bindings/remote_ptr.h" #include "mojom/shell.h" namespace mojo { diff --git a/mojo/shell/service_manager_unittest.cc b/mojo/shell/service_manager_unittest.cc index 549d762..d5fc31e 100644 --- a/mojo/shell/service_manager_unittest.cc +++ b/mojo/shell/service_manager_unittest.cc @@ -3,7 +3,8 @@ // found in the LICENSE file. #include "base/message_loop/message_loop.h" -#include "mojo/public/bindings/lib/remote_ptr.h" +#include "mojo/public/bindings/allocation_scope.h" +#include "mojo/public/bindings/remote_ptr.h" #include "mojo/shell/service_manager.h" #include "mojom/shell.h" #include "mojom/test.h" |