Merge pull request #19195 from diablodale:win32AlignAlloc

pull/19212/head
Alexander Alekhin 4 years ago
commit cd68cc1f46
  1. 11
      CMakeLists.txt
  2. 3
      modules/core/CMakeLists.txt
  3. 16
      modules/core/src/alloc.cpp
  4. 14
      modules/core/test/test_misc.cpp

@ -662,9 +662,18 @@ if(UNIX)
CHECK_SYMBOL_EXISTS(memalign malloc.h HAVE_MEMALIGN)
endif()
# TODO:
# - _aligned_malloc() on Win32
# - std::aligned_alloc() C++17 / C11
endif()
elseif(WIN32)
include(CheckIncludeFile)
include(CheckSymbolExists)
if(OPENCV_ENABLE_MEMALIGN)
CHECK_INCLUDE_FILE(malloc.h HAVE_MALLOC_H)
if(HAVE_MALLOC_H)
CHECK_SYMBOL_EXISTS(_aligned_malloc malloc.h HAVE_WIN32_ALIGNED_MALLOC)
endif()
endif()
endif()
include(cmake/OpenCVPCHSupport.cmake)

@ -80,6 +80,9 @@ endif()
if(HAVE_MEMALIGN)
ocv_append_source_file_compile_definitions(${CMAKE_CURRENT_SOURCE_DIR}/src/alloc.cpp "HAVE_MEMALIGN=1")
endif()
if(HAVE_WIN32_ALIGNED_MALLOC)
ocv_append_source_file_compile_definitions(${CMAKE_CURRENT_SOURCE_DIR}/src/alloc.cpp "HAVE_WIN32_ALIGNED_MALLOC=1")
endif()
if(HAVE_VA_INTEL_OLD_HEADER)
ocv_append_source_file_compile_definitions("${CMAKE_CURRENT_LIST_DIR}/src/va_intel.cpp" "HAVE_VA_INTEL_OLD_HEADER")
endif()

@ -82,7 +82,7 @@ cv::utils::AllocatorStatisticsInterface& getAllocatorStatistics()
return allocator_stats;
}
#if defined HAVE_POSIX_MEMALIGN || defined HAVE_MEMALIGN
#if defined HAVE_POSIX_MEMALIGN || defined HAVE_MEMALIGN || defined HAVE_WIN32_ALIGNED_MALLOC
static bool readMemoryAlignmentParameter()
{
bool value = true;
@ -148,6 +148,14 @@ void* fastMalloc(size_t size)
return OutOfMemoryError(size);
return ptr;
}
#elif defined HAVE_WIN32_ALIGNED_MALLOC
if (isAlignedAllocationEnabled())
{
void* ptr = _aligned_malloc(size, CV_MALLOC_ALIGN);
if(!ptr)
return OutOfMemoryError(size);
return ptr;
}
#endif
uchar* udata = (uchar*)malloc(size + sizeof(void*) + CV_MALLOC_ALIGN);
if(!udata)
@ -170,6 +178,12 @@ void fastFree(void* ptr)
free(ptr);
return;
}
#elif defined HAVE_WIN32_ALIGNED_MALLOC
if (isAlignedAllocationEnabled())
{
_aligned_free(ptr);
return;
}
#endif
if(ptr)
{

@ -2,6 +2,7 @@
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
#include "test_precomp.hpp"
#include <cmath>
namespace opencv_test { namespace {
@ -783,5 +784,18 @@ TEST(Core_Check, testSize_1)
}
}
TEST(Core_Allocation, alignedAllocation)
{
// iterate from size=1 to approximate byte size of 8K 32bpp image buffer
for (int i = 0; i < 200; i++) {
const size_t size = static_cast<size_t>(std::pow(1.091, (double)i));
void * const buf = cv::fastMalloc(size);
ASSERT_NE((uintptr_t)0, (uintptr_t)buf)
<< "failed to allocate memory";
ASSERT_EQ((uintptr_t)0, (uintptr_t)buf % CV_MALLOC_ALIGN)
<< "memory not aligned to " << CV_MALLOC_ALIGN;
cv::fastFree(buf);
}
}
}} // namespace

Loading…
Cancel
Save