IGNITE-2564: CPP: Fixed a bug preventing CPP memory reallocation from Java. This closes #460.
Project: http://git-wip-us.apache.org/repos/asf/ignite/repo Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/c3aa1375 Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/c3aa1375 Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/c3aa1375 Branch: refs/heads/ignite-961 Commit: c3aa1375171b5f3a97cd50fcd209256e46579b0d Parents: b7475f0 Author: isapego <[email protected]> Authored: Wed Feb 10 15:00:42 2016 +0300 Committer: vozerov-gridgain <[email protected]> Committed: Wed Feb 10 15:00:42 2016 +0300 ---------------------------------------------------------------------- modules/platforms/cpp/core-test/Makefile.am | 1 + .../cpp/core-test/project/vs/core-test.vcxproj | 1 + .../project/vs/core-test.vcxproj.filters | 3 + .../platforms/cpp/core-test/src/cache_test.cpp | 12 +++ .../cpp/core-test/src/interop_memory_test.cpp | 95 ++++++++++++++++++++ .../include/ignite/impl/ignite_environment.h | 19 ++-- .../cpp/core/src/impl/cache/cache_impl.cpp | 2 +- .../cpp/core/src/impl/ignite_environment.cpp | 30 +++++-- 8 files changed, 149 insertions(+), 14 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ignite/blob/c3aa1375/modules/platforms/cpp/core-test/Makefile.am ---------------------------------------------------------------------- diff --git a/modules/platforms/cpp/core-test/Makefile.am b/modules/platforms/cpp/core-test/Makefile.am index aa81c65..531fee0 100644 --- a/modules/platforms/cpp/core-test/Makefile.am +++ b/modules/platforms/cpp/core-test/Makefile.am @@ -29,6 +29,7 @@ ignite_tests_SOURCES = src/cache_test.cpp \ src/cache_query_test.cpp \ src/concurrent_test.cpp \ src/ignition_test.cpp \ + src/interop_memory_test.cpp \ src/handle_registry_test.cpp \ src/binary_test_defs.cpp \ src/binary_reader_writer_raw_test.cpp \ http://git-wip-us.apache.org/repos/asf/ignite/blob/c3aa1375/modules/platforms/cpp/core-test/project/vs/core-test.vcxproj ---------------------------------------------------------------------- diff --git a/modules/platforms/cpp/core-test/project/vs/core-test.vcxproj b/modules/platforms/cpp/core-test/project/vs/core-test.vcxproj index 422199a..d98d202 100644 --- a/modules/platforms/cpp/core-test/project/vs/core-test.vcxproj +++ b/modules/platforms/cpp/core-test/project/vs/core-test.vcxproj @@ -40,6 +40,7 @@ <ClCompile Include="..\..\src\binary_session_test.cpp" /> <ClCompile Include="..\..\src\binary_test_defs.cpp" /> <ClCompile Include="..\..\src\cache_query_test.cpp" /> + <ClCompile Include="..\..\src\interop_memory_test.cpp" /> <ClCompile Include="..\..\src\teamcity_boost.cpp" /> <ClCompile Include="..\..\src\teamcity_messages.cpp" /> </ItemGroup> http://git-wip-us.apache.org/repos/asf/ignite/blob/c3aa1375/modules/platforms/cpp/core-test/project/vs/core-test.vcxproj.filters ---------------------------------------------------------------------- diff --git a/modules/platforms/cpp/core-test/project/vs/core-test.vcxproj.filters b/modules/platforms/cpp/core-test/project/vs/core-test.vcxproj.filters index 32737be..15b9c40 100644 --- a/modules/platforms/cpp/core-test/project/vs/core-test.vcxproj.filters +++ b/modules/platforms/cpp/core-test/project/vs/core-test.vcxproj.filters @@ -34,6 +34,9 @@ <ClCompile Include="..\..\src\binary_reader_writer_raw_test.cpp"> <Filter>Code</Filter> </ClCompile> + <ClCompile Include="..\..\src\interop_memory_test.cpp"> + <Filter>Code</Filter> + </ClCompile> </ItemGroup> <ItemGroup> <ClInclude Include="..\..\include\teamcity_messages.h"> http://git-wip-us.apache.org/repos/asf/ignite/blob/c3aa1375/modules/platforms/cpp/core-test/src/cache_test.cpp ---------------------------------------------------------------------- diff --git a/modules/platforms/cpp/core-test/src/cache_test.cpp b/modules/platforms/cpp/core-test/src/cache_test.cpp index 32c5bd6..a11865d 100644 --- a/modules/platforms/cpp/core-test/src/cache_test.cpp +++ b/modules/platforms/cpp/core-test/src/cache_test.cpp @@ -476,4 +476,16 @@ BOOST_AUTO_TEST_CASE(TestGetOrCreateCache) BOOST_REQUIRE(7 == cache2.Get(5)); } +BOOST_AUTO_TEST_CASE(TestGetBigString) +{ + // Get existing cache + cache::Cache<int, std::string> cache = grid0.GetOrCreateCache<int, std::string>("partitioned"); + + std::string longStr(impl::IgniteEnvironment::DEFAULT_ALLOCATION_SIZE * 10, 'a'); + + cache.Put(5, longStr); + + BOOST_REQUIRE(longStr == cache.Get(5)); +} + BOOST_AUTO_TEST_SUITE_END() \ No newline at end of file http://git-wip-us.apache.org/repos/asf/ignite/blob/c3aa1375/modules/platforms/cpp/core-test/src/interop_memory_test.cpp ---------------------------------------------------------------------- diff --git a/modules/platforms/cpp/core-test/src/interop_memory_test.cpp b/modules/platforms/cpp/core-test/src/interop_memory_test.cpp new file mode 100644 index 0000000..07e928c --- /dev/null +++ b/modules/platforms/cpp/core-test/src/interop_memory_test.cpp @@ -0,0 +1,95 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef _MSC_VER + #define BOOST_TEST_DYN_LINK +#endif + +#include <boost/test/unit_test.hpp> + +#include "ignite/ignite.h" +#include "ignite/ignition.h" + +using namespace ignite; +using namespace impl; +using namespace boost::unit_test; + +BOOST_AUTO_TEST_SUITE(MemoryTestSuite) + +BOOST_AUTO_TEST_CASE(MemoryReallocationTest) +{ + using impl::interop::InteropMemory; + using common::concurrent::SharedPointer; + + IgniteEnvironment env; + + SharedPointer<InteropMemory> mem = env.AllocateMemory(); + + BOOST_CHECK_EQUAL(mem.Get()->Capacity(), IgniteEnvironment::DEFAULT_ALLOCATION_SIZE); + + BOOST_CHECK(mem.Get()->Data() != NULL); + + // Checking memory for write access. + int32_t capBeforeReallocation = mem.Get()->Capacity(); + + for (int32_t i = 0; i <capBeforeReallocation; ++i) + { + int8_t *data = mem.Get()->Data(); + + data[i] = static_cast<int8_t>(i); + } + + mem.Get()->Reallocate(mem.Get()->Capacity() * 3); + + BOOST_CHECK(mem.Get()->Capacity() >= IgniteEnvironment::DEFAULT_ALLOCATION_SIZE * 3); + + // Checking memory data. + for (int32_t i = 0; i <capBeforeReallocation; ++i) + { + int8_t *data = mem.Get()->Data(); + + BOOST_REQUIRE_EQUAL(data[i], static_cast<int8_t>(i)); + } + + // Checking memory for write access. + capBeforeReallocation = mem.Get()->Capacity(); + + for (int32_t i = 0; i <capBeforeReallocation; ++i) + { + int8_t *data = mem.Get()->Data(); + + data[i] = static_cast<int8_t>(i + 42); + } + + // Trying reallocate memory once more. + mem.Get()->Reallocate(mem.Get()->Capacity() * 3); + + // Checking memory data. + for (int32_t i = 0; i <capBeforeReallocation; ++i) + { + int8_t *data = mem.Get()->Data(); + + BOOST_REQUIRE_EQUAL(data[i], static_cast<int8_t>(i + 42)); + } + + BOOST_CHECK(mem.Get()->Capacity() >= IgniteEnvironment::DEFAULT_ALLOCATION_SIZE * 9); + + // Checking memory for write access. + memset(mem.Get()->Data(), 0xF0F0F0F0, mem.Get()->Capacity()); +} + +BOOST_AUTO_TEST_SUITE_END() \ No newline at end of file http://git-wip-us.apache.org/repos/asf/ignite/blob/c3aa1375/modules/platforms/cpp/core/include/ignite/impl/ignite_environment.h ---------------------------------------------------------------------- diff --git a/modules/platforms/cpp/core/include/ignite/impl/ignite_environment.h b/modules/platforms/cpp/core/include/ignite/impl/ignite_environment.h index 2fbdb44..02facfc 100644 --- a/modules/platforms/cpp/core/include/ignite/impl/ignite_environment.h +++ b/modules/platforms/cpp/core/include/ignite/impl/ignite_environment.h @@ -25,16 +25,21 @@ #include "binary/binary_type_manager.h" namespace ignite -{ +{ namespace impl { /** * Defines environment in which Ignite operates. */ class IGNITE_IMPORT_EXPORT IgniteEnvironment - { + { public: /** + * Default memory block allocation size. + */ + enum { DEFAULT_ALLOCATION_SIZE = 1024 }; + + /** * Default constructor. */ IgniteEnvironment(); @@ -51,7 +56,7 @@ namespace ignite * @return JNI handlers. */ ignite::common::java::JniHandlers GetJniHandlers(ignite::common::concurrent::SharedPointer<IgniteEnvironment>* target); - + /** * Perform initialization on successful start. * @@ -64,8 +69,8 @@ namespace ignite * * @param memPtr Memory pointer. */ - void OnStartCallback(long long memPtr); - + void OnStartCallback(long long memPtr); + /** * Get name of Ignite instance. * @@ -120,11 +125,11 @@ namespace ignite char* name; /** Type manager. */ - binary::BinaryTypeManager* metaMgr; + binary::BinaryTypeManager* metaMgr; IGNITE_NO_COPY_ASSIGNMENT(IgniteEnvironment); }; - } + } } #endif \ No newline at end of file http://git-wip-us.apache.org/repos/asf/ignite/blob/c3aa1375/modules/platforms/cpp/core/src/impl/cache/cache_impl.cpp ---------------------------------------------------------------------- diff --git a/modules/platforms/cpp/core/src/impl/cache/cache_impl.cpp b/modules/platforms/cpp/core/src/impl/cache/cache_impl.cpp index 08526b5..f66a228 100644 --- a/modules/platforms/cpp/core/src/impl/cache/cache_impl.cpp +++ b/modules/platforms/cpp/core/src/impl/cache/cache_impl.cpp @@ -379,7 +379,7 @@ namespace ignite if (outPtr) { - env.Get()->Context()->TargetInStreamOutStream(javaRef, opType, WriteTo(outMem.Get(), inOp, err), + env.Get()->Context()->TargetInStreamOutStream(javaRef, opType, outPtr, inMem.Get()->PointerLong(), &jniErr); IgniteError::SetError(jniErr.code, jniErr.errCls, jniErr.errMsg, err); http://git-wip-us.apache.org/repos/asf/ignite/blob/c3aa1375/modules/platforms/cpp/core/src/impl/ignite_environment.cpp ---------------------------------------------------------------------- diff --git a/modules/platforms/cpp/core/src/impl/ignite_environment.cpp b/modules/platforms/cpp/core/src/impl/ignite_environment.cpp index 013a139..c9c57a0 100644 --- a/modules/platforms/cpp/core/src/impl/ignite_environment.cpp +++ b/modules/platforms/cpp/core/src/impl/ignite_environment.cpp @@ -53,7 +53,23 @@ namespace ignite SharedPointer<IgniteEnvironment>* ptr = static_cast<SharedPointer<IgniteEnvironment>*>(target); delete ptr; - } + } + + /** + * Memory reallocate callback. + * + * @param target Target environment. + * @param memPtr Memory pointer. + * @param cap Required capasity. + */ + void IGNITE_CALL MemoryReallocate(void* target, long long memPtr, int cap) + { + SharedPointer<IgniteEnvironment>* env = static_cast<SharedPointer<IgniteEnvironment>*>(target); + + SharedPointer<InteropMemory> mem = env->Get()->GetMemory(memPtr); + + mem.Get()->Reallocate(cap); + } IgniteEnvironment::IgniteEnvironment() : ctx(SharedPointer<JniContext>()), latch(new SingleLatch), name(NULL), metaMgr(new BinaryTypeManager()) @@ -80,18 +96,20 @@ namespace ignite hnds.onStart = OnStart; hnds.onStop = OnStop; + hnds.memRealloc = MemoryReallocate; + hnds.error = NULL; return hnds; } - + void IgniteEnvironment::Initialize(SharedPointer<JniContext> ctx) { this->ctx = ctx; - + latch->CountDown(); } - + const char* IgniteEnvironment::InstanceName() const { return name; @@ -104,7 +122,7 @@ namespace ignite SharedPointer<InteropMemory> IgniteEnvironment::AllocateMemory() { - SharedPointer<InteropMemory> ptr(new InteropUnpooledMemory(1024)); + SharedPointer<InteropMemory> ptr(new InteropUnpooledMemory(DEFAULT_ALLOCATION_SIZE)); return ptr; } @@ -147,7 +165,7 @@ namespace ignite InteropInputStream stream(&mem); BinaryReaderImpl reader(&stream); - + int32_t nameLen = reader.ReadString(NULL, 0); if (nameLen >= 0)
