diff options
author | dspringer@chromium.org <dspringer@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-07-05 21:37:00 +0000 |
---|---|---|
committer | dspringer@chromium.org <dspringer@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-07-05 21:37:00 +0000 |
commit | 14710e220b1e6fb4db5661b0569f978d850a7be6 (patch) | |
tree | 16fbd38c33750147601521ad7199e3821f01f32c /ppapi | |
parent | abbd2f4b36da8c89e6f3002eb84f60064a6ff067 (diff) | |
download | chromium_src-14710e220b1e6fb4db5661b0569f978d850a7be6.zip chromium_src-14710e220b1e6fb4db5661b0569f978d850a7be6.tar.gz chromium_src-14710e220b1e6fb4db5661b0569f978d850a7be6.tar.bz2 |
Add PPB_Memory_Dev. This is the first of a few CLs needed to pull MemAlloc and
MemFree from PPB_Core.
BUG=80610
TEST=ui_tests
Review URL: http://codereview.chromium.org/7300010
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@91518 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'ppapi')
-rw-r--r-- | ppapi/c/dev/ppb_memory_dev.h | 51 | ||||
-rw-r--r-- | ppapi/cpp/dev/memory_dev.cc | 33 | ||||
-rw-r--r-- | ppapi/cpp/dev/memory_dev.h | 36 | ||||
-rw-r--r-- | ppapi/ppapi_cpp.gypi | 3 | ||||
-rw-r--r-- | ppapi/ppapi_tests.gypi | 2 | ||||
-rw-r--r-- | ppapi/tests/all_c_includes.h | 1 | ||||
-rw-r--r-- | ppapi/tests/all_cpp_includes.h | 1 | ||||
-rw-r--r-- | ppapi/tests/test_memory.cc | 57 | ||||
-rw-r--r-- | ppapi/tests/test_memory.h | 31 |
9 files changed, 215 insertions, 0 deletions
diff --git a/ppapi/c/dev/ppb_memory_dev.h b/ppapi/c/dev/ppb_memory_dev.h new file mode 100644 index 0000000..34773bf --- /dev/null +++ b/ppapi/c/dev/ppb_memory_dev.h @@ -0,0 +1,51 @@ +/* Copyright (c) 2011 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 PPAPI_C_DEV_PPB_MEMORY_DEV_H_ +#define PPAPI_C_DEV_PPB_MEMORY_DEV_H_ + +#include "ppapi/c/pp_stdint.h" + +#define PPB_MEMORY_DEV_INTERFACE_0_1 "PPB_Memory(Dev);0.1" +#define PPB_MEMORY_DEV_INTERFACE PPB_MEMORY_DEV_INTERFACE_0_1 + +/** + * @file + * This file defines the PPB_Memory interface defined by the browser and + * and containing pointers to functions related to memory management. + */ + +/** + * @addtogroup Interfaces + * @{ + */ + +/** + * The PPB_Memory_Dev interface contains pointers to functions related to memory + * management. + * + */ +struct PPB_Memory_Dev { + /** + * MemAlloc is a pointer to a function that allocate memory. + * + * @param[in] num_bytes A number of bytes to allocate. + * @return A pointer to the memory if successful, NULL If the + * allocation fails. + */ + void* (*MemAlloc)(uint32_t num_bytes); + + /** + * MemFree is a pointer to a function that deallocates memory. + * + * @param[in] ptr A pointer to the memory to deallocate. It is safe to + * pass NULL to this function. + */ + void (*MemFree)(void* ptr); +}; +/** + * @} + */ + +#endif /* PPAPI_C_DEV_PPB_MEMORY_DEV_H_ */ diff --git a/ppapi/cpp/dev/memory_dev.cc b/ppapi/cpp/dev/memory_dev.cc new file mode 100644 index 0000000..ca7c125f --- /dev/null +++ b/ppapi/cpp/dev/memory_dev.cc @@ -0,0 +1,33 @@ +// Copyright (c) 2011 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 "ppapi/cpp/dev/memory_dev.h" + +#include "ppapi/c/dev/ppb_memory_dev.h" +#include "ppapi/cpp/module.h" +#include "ppapi/cpp/module_impl.h" + +namespace pp { + +namespace { + +template <> const char* interface_name<PPB_Memory_Dev>() { + return PPB_MEMORY_DEV_INTERFACE; +} + +} // namespace + +void* Memory_Dev::MemAlloc(uint32_t num_bytes) { + if (!has_interface<PPB_Memory_Dev>()) + return NULL; + return get_interface<PPB_Memory_Dev>()->MemAlloc(num_bytes); +} + +void Memory_Dev::MemFree(void* ptr) { + if (!has_interface<PPB_Memory_Dev>() || !ptr) + return; + get_interface<PPB_Memory_Dev>()->MemFree(ptr); +} + +} // namespace pp diff --git a/ppapi/cpp/dev/memory_dev.h b/ppapi/cpp/dev/memory_dev.h new file mode 100644 index 0000000..2695cae --- /dev/null +++ b/ppapi/cpp/dev/memory_dev.h @@ -0,0 +1,36 @@ +// Copyright (c) 2011 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 PPAPI_CPP_DEV_MEMORY_DEV_H_ +#define PPAPI_CPP_DEV_MEMORY_DEV_H_ + +#include "ppapi/c/pp_stdint.h" + +/// @file +/// This file defines APIs related to memory management. + +namespace pp { + +/// APIs related to memory management, time, and threads. +class Memory_Dev { + public: + Memory_Dev() {} + + /// A function that allocates memory. + /// + /// @param[in] num_bytes A number of bytes to allocate. + /// @return A pointer to the memory if successful, NULL If the + /// allocation fails. + void* MemAlloc(uint32_t num_bytes); + + /// A function that deallocates memory. + /// + /// @param[in] ptr A pointer to the memory to deallocate. It is safe to + /// pass NULL to this function. + void MemFree(void* ptr); +}; + +} // namespace pp + +#endif // PPAPI_CPP_DEV_MEMORY_DEV_H_ diff --git a/ppapi/ppapi_cpp.gypi b/ppapi/ppapi_cpp.gypi index 315e71c..b9a87da 100644 --- a/ppapi/ppapi_cpp.gypi +++ b/ppapi/ppapi_cpp.gypi @@ -68,6 +68,7 @@ 'c/dev/ppb_font_dev.h', 'c/dev/ppb_fullscreen_dev.h', 'c/dev/ppb_graphics_3d_dev.h', + 'c/dev/ppb_memory_dev.h', 'c/dev/ppb_opengles_dev.h', 'c/dev/ppb_scrollbar_dev.h', 'c/dev/ppb_surface_3d_dev.h', @@ -201,6 +202,8 @@ 'cpp/dev/graphics_3d_client_dev.h', 'cpp/dev/graphics_3d_dev.cc', 'cpp/dev/graphics_3d_dev.h', + 'cpp/dev/memory_dev.cc', + 'cpp/dev/memory_dev.h', 'cpp/dev/printing_dev.cc', 'cpp/dev/printing_dev.h', 'cpp/dev/scrollbar_dev.cc', diff --git a/ppapi/ppapi_tests.gypi b/ppapi/ppapi_tests.gypi index 2661de9..625b0e1 100644 --- a/ppapi/ppapi_tests.gypi +++ b/ppapi/ppapi_tests.gypi @@ -94,6 +94,8 @@ 'tests/test_graphics_2d.h', 'tests/test_image_data.cc', 'tests/test_image_data.h', + 'tests/test_memory.cc', + 'tests/test_memory.h', 'tests/test_paint_aggregator.cc', 'tests/test_paint_aggregator.h', 'tests/test_post_message.cc', diff --git a/ppapi/tests/all_c_includes.h b/ppapi/tests/all_c_includes.h index d4f510d..3100deb 100644 --- a/ppapi/tests/all_c_includes.h +++ b/ppapi/tests/all_c_includes.h @@ -31,6 +31,7 @@ #include "ppapi/c/dev/ppb_fullscreen_dev.h" #include "ppapi/c/dev/ppb_graphics_3d_dev.h" #include "ppapi/c/dev/ppb_layer_compositor_dev.h" +#include "ppapi/c/dev/ppb_memory_dev.h" #include "ppapi/c/dev/ppb_opengles_dev.h" #include "ppapi/c/dev/ppb_scrollbar_dev.h" #include "ppapi/c/dev/ppb_surface_3d_dev.h" diff --git a/ppapi/tests/all_cpp_includes.h b/ppapi/tests/all_cpp_includes.h index f815986..5c4b21d 100644 --- a/ppapi/tests/all_cpp_includes.h +++ b/ppapi/tests/all_cpp_includes.h @@ -26,6 +26,7 @@ #include "ppapi/cpp/dev/fullscreen_dev.h" #include "ppapi/cpp/dev/graphics_3d_client_dev.h" #include "ppapi/cpp/dev/graphics_3d_dev.h" +#include "ppapi/cpp/dev/memory_dev.h" #include "ppapi/cpp/dev/printing_dev.h" #include "ppapi/cpp/dev/scriptable_object_deprecated.h" #include "ppapi/cpp/dev/scrollbar_dev.h" diff --git a/ppapi/tests/test_memory.cc b/ppapi/tests/test_memory.cc new file mode 100644 index 0000000..1d44db4 --- /dev/null +++ b/ppapi/tests/test_memory.cc @@ -0,0 +1,57 @@ +// Copyright (c) 2011 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 "ppapi/tests/test_memory.h" + +#include "ppapi/c/dev/ppb_testing_dev.h" +#include "ppapi/c/dev/ppb_memory_dev.h" +#include "ppapi/cpp/instance.h" +#include "ppapi/cpp/module.h" +#include "ppapi/tests/testing_instance.h" + +namespace { + +size_t kTestBufferSize = 1000; + +} // namespace + +REGISTER_TEST_CASE(Memory); + +bool TestMemory::Init() { + memory_dev_interface_ = static_cast<const PPB_Memory_Dev*>( + pp::Module::Get()->GetBrowserInterface(PPB_MEMORY_DEV_INTERFACE)); + return memory_dev_interface_ && InitTestingInterface(); +} + +void TestMemory::RunTest() { + RUN_TEST(MemAlloc); + RUN_TEST(NullMemFree); +} + +std::string TestMemory::TestMemAlloc() { + uint32_t before_object = testing_interface_->GetLiveObjectsForInstance( + instance_->pp_instance()); + { + char* buffer = static_cast<char*>( + memory_dev_interface_->MemAlloc(kTestBufferSize)); + // Touch a couple of locations. Failure will crash the test. + buffer[0] = '1'; + buffer[kTestBufferSize - 1] = '1'; + memory_dev_interface_->MemFree(buffer); + } + + // Make sure nothing leaked. + ASSERT_TRUE(testing_interface_->GetLiveObjectsForInstance( + instance_->pp_instance()) == before_object); + + PASS(); +} + +std::string TestMemory::TestNullMemFree() { + // Failure crashes the test. + memory_dev_interface_->MemFree(NULL); + + PASS(); +} + diff --git a/ppapi/tests/test_memory.h b/ppapi/tests/test_memory.h new file mode 100644 index 0000000..4215ba0 --- /dev/null +++ b/ppapi/tests/test_memory.h @@ -0,0 +1,31 @@ +// Copyright (c) 2011 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 PPAPI_TEST_TEST_MEMORY_H_ +#define PPAPI_TEST_TEST_MEMORY_H_ + +#include <string> + +#include "ppapi/tests/test_case.h" + +struct PPB_Memory_Dev; + +class TestMemory : public TestCase { + public: + explicit TestMemory(TestingInstance* instance) : TestCase(instance) {} + + private: + // TestCase implementation. + virtual bool Init(); + virtual void RunTest(); + + std::string TestMemAlloc(); + std::string TestNullMemFree(); + + // Used by the tests that access the C API directly. + const PPB_Memory_Dev* memory_dev_interface_; +}; + +#endif // PPAPI_TEST_TEST_VAR_H_ + |