Skip to content

Commit cd68cc1

Browse files
committed
Merge pull request opencv#19195 from diablodale:win32AlignAlloc
2 parents b13b5d8 + 109255a commit cd68cc1

File tree

4 files changed

+42
-2
lines changed

4 files changed

+42
-2
lines changed

CMakeLists.txt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -662,9 +662,18 @@ if(UNIX)
662662
CHECK_SYMBOL_EXISTS(memalign malloc.h HAVE_MEMALIGN)
663663
endif()
664664
# TODO:
665-
# - _aligned_malloc() on Win32
666665
# - std::aligned_alloc() C++17 / C11
667666
endif()
667+
elseif(WIN32)
668+
include(CheckIncludeFile)
669+
include(CheckSymbolExists)
670+
671+
if(OPENCV_ENABLE_MEMALIGN)
672+
CHECK_INCLUDE_FILE(malloc.h HAVE_MALLOC_H)
673+
if(HAVE_MALLOC_H)
674+
CHECK_SYMBOL_EXISTS(_aligned_malloc malloc.h HAVE_WIN32_ALIGNED_MALLOC)
675+
endif()
676+
endif()
668677
endif()
669678

670679
include(cmake/OpenCVPCHSupport.cmake)

modules/core/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ endif()
8080
if(HAVE_MEMALIGN)
8181
ocv_append_source_file_compile_definitions(${CMAKE_CURRENT_SOURCE_DIR}/src/alloc.cpp "HAVE_MEMALIGN=1")
8282
endif()
83+
if(HAVE_WIN32_ALIGNED_MALLOC)
84+
ocv_append_source_file_compile_definitions(${CMAKE_CURRENT_SOURCE_DIR}/src/alloc.cpp "HAVE_WIN32_ALIGNED_MALLOC=1")
85+
endif()
8386
if(HAVE_VA_INTEL_OLD_HEADER)
8487
ocv_append_source_file_compile_definitions("${CMAKE_CURRENT_LIST_DIR}/src/va_intel.cpp" "HAVE_VA_INTEL_OLD_HEADER")
8588
endif()

modules/core/src/alloc.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ cv::utils::AllocatorStatisticsInterface& getAllocatorStatistics()
8282
return allocator_stats;
8383
}
8484

85-
#if defined HAVE_POSIX_MEMALIGN || defined HAVE_MEMALIGN
85+
#if defined HAVE_POSIX_MEMALIGN || defined HAVE_MEMALIGN || defined HAVE_WIN32_ALIGNED_MALLOC
8686
static bool readMemoryAlignmentParameter()
8787
{
8888
bool value = true;
@@ -148,6 +148,14 @@ void* fastMalloc(size_t size)
148148
return OutOfMemoryError(size);
149149
return ptr;
150150
}
151+
#elif defined HAVE_WIN32_ALIGNED_MALLOC
152+
if (isAlignedAllocationEnabled())
153+
{
154+
void* ptr = _aligned_malloc(size, CV_MALLOC_ALIGN);
155+
if(!ptr)
156+
return OutOfMemoryError(size);
157+
return ptr;
158+
}
151159
#endif
152160
uchar* udata = (uchar*)malloc(size + sizeof(void*) + CV_MALLOC_ALIGN);
153161
if(!udata)
@@ -170,6 +178,12 @@ void fastFree(void* ptr)
170178
free(ptr);
171179
return;
172180
}
181+
#elif defined HAVE_WIN32_ALIGNED_MALLOC
182+
if (isAlignedAllocationEnabled())
183+
{
184+
_aligned_free(ptr);
185+
return;
186+
}
173187
#endif
174188
if(ptr)
175189
{

modules/core/test/test_misc.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// It is subject to the license terms in the LICENSE file found in the top-level directory
33
// of this distribution and at http://opencv.org/license.html.
44
#include "test_precomp.hpp"
5+
#include <cmath>
56

67
namespace opencv_test { namespace {
78

@@ -783,5 +784,18 @@ TEST(Core_Check, testSize_1)
783784
}
784785
}
785786

787+
TEST(Core_Allocation, alignedAllocation)
788+
{
789+
// iterate from size=1 to approximate byte size of 8K 32bpp image buffer
790+
for (int i = 0; i < 200; i++) {
791+
const size_t size = static_cast<size_t>(std::pow(1.091, (double)i));
792+
void * const buf = cv::fastMalloc(size);
793+
ASSERT_NE((uintptr_t)0, (uintptr_t)buf)
794+
<< "failed to allocate memory";
795+
ASSERT_EQ((uintptr_t)0, (uintptr_t)buf % CV_MALLOC_ALIGN)
796+
<< "memory not aligned to " << CV_MALLOC_ALIGN;
797+
cv::fastFree(buf);
798+
}
799+
}
786800

787801
}} // namespace

0 commit comments

Comments
 (0)