Skip to content

Commit

Permalink
Merge pull request #3695 from lambday/feature/memcpy
Browse files Browse the repository at this point in the history
added shogun::memcpy wrapper for std::memcpy
  • Loading branch information
lambday committed Mar 14, 2017
2 parents 7a5305f + 145aec5 commit 4657e0f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
8 changes: 8 additions & 0 deletions src/shogun/lib/memory.h
Expand Up @@ -17,6 +17,7 @@
#ifndef DOXYGEN_SHOULD_SKIP_THIS

#include <new>
#include <cstring>

/* wrappers for malloc, free, realloc, calloc */

Expand Down Expand Up @@ -95,6 +96,13 @@ template <class T> void sg_generic_free(T* ptr)
{
sg_free(ptr);
}

template <class InputIt, class OutputIt>
SG_FORCED_INLINE void* memcpy(InputIt dest, OutputIt src, size_t count)
{
return std::memcpy(static_cast<void*>(dest), static_cast<const void*>(src), count);
}

#endif //TRACE_MEMORY_ALLOCS
#ifdef TRACE_MEMORY_ALLOCS
/** @brief memory block */
Expand Down
29 changes: 27 additions & 2 deletions tests/unit/lib/Memory_unittest.cc
@@ -1,10 +1,11 @@
#include <shogun/lib/memory.h>
#include <shogun/mathematics/Math.h>
#include <gtest/gtest.h>

namespace shogun { template <class T> class SGMatrix; }
namespace shogun { template <class T> class SGSparseVector; }
namespace shogun { template <class T> class SGVector; }

#include <gtest/gtest.h>

using namespace shogun;

TEST(MemoryTest,get_copy)
Expand Down Expand Up @@ -54,3 +55,27 @@ TEST(MemoryTest,SGMatrix)
EXPECT_NE((SGMatrix<float64_t>*) NULL, m);
SG_FREE(m);
}

template <typename T>
static void clone(T* dest, T const * const src, size_t size)
{
shogun::memcpy(dest, src, size);
}

TEST(MemoryTest, memcpy)
{
const index_t size = 10;
auto src = SG_CALLOC(float64_t, size);
for (index_t i=0; i<size; ++i)
src[i]=CMath::randn_double();

auto dest = SG_CALLOC(float64_t, size);

clone(dest, src, size*sizeof(float64_t));

for (index_t i=0; i<size; ++i)
EXPECT_NEAR(src[i], dest[i], 1E-15);

SG_FREE(src);
SG_FREE(dest);
}

0 comments on commit 4657e0f

Please sign in to comment.