File tree Expand file tree Collapse file tree 4 files changed +42
-2
lines changed Expand file tree Collapse file tree 4 files changed +42
-2
lines changed Original file line number Diff line number Diff line change @@ -662,9 +662,18 @@ if(UNIX)
662
662
CHECK_SYMBOL_EXISTS (memalign malloc.h HAVE_MEMALIGN )
663
663
endif ()
664
664
# TODO:
665
- # - _aligned_malloc() on Win32
666
665
# - std::aligned_alloc() C++17 / C11
667
666
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 ()
668
677
endif ()
669
678
670
679
include (cmake/OpenCVPCHSupport.cmake )
Original file line number Diff line number Diff line change @@ -80,6 +80,9 @@ endif()
80
80
if (HAVE_MEMALIGN )
81
81
ocv_append_source_file_compile_definitions (${CMAKE_CURRENT_SOURCE_DIR} /src/alloc.cpp "HAVE_MEMALIGN=1" )
82
82
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 ()
83
86
if (HAVE_VA_INTEL_OLD_HEADER )
84
87
ocv_append_source_file_compile_definitions ("${CMAKE_CURRENT_LIST_DIR} /src/va_intel.cpp" "HAVE_VA_INTEL_OLD_HEADER" )
85
88
endif ()
Original file line number Diff line number Diff line change @@ -82,7 +82,7 @@ cv::utils::AllocatorStatisticsInterface& getAllocatorStatistics()
82
82
return allocator_stats;
83
83
}
84
84
85
- #if defined HAVE_POSIX_MEMALIGN || defined HAVE_MEMALIGN
85
+ #if defined HAVE_POSIX_MEMALIGN || defined HAVE_MEMALIGN || defined HAVE_WIN32_ALIGNED_MALLOC
86
86
static bool readMemoryAlignmentParameter ()
87
87
{
88
88
bool value = true ;
@@ -148,6 +148,14 @@ void* fastMalloc(size_t size)
148
148
return OutOfMemoryError (size);
149
149
return ptr;
150
150
}
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
+ }
151
159
#endif
152
160
uchar* udata = (uchar*)malloc (size + sizeof (void *) + CV_MALLOC_ALIGN);
153
161
if (!udata)
@@ -170,6 +178,12 @@ void fastFree(void* ptr)
170
178
free (ptr);
171
179
return ;
172
180
}
181
+ #elif defined HAVE_WIN32_ALIGNED_MALLOC
182
+ if (isAlignedAllocationEnabled ())
183
+ {
184
+ _aligned_free (ptr);
185
+ return ;
186
+ }
173
187
#endif
174
188
if (ptr)
175
189
{
Original file line number Diff line number Diff line change 2
2
// It is subject to the license terms in the LICENSE file found in the top-level directory
3
3
// of this distribution and at http://opencv.org/license.html.
4
4
#include " test_precomp.hpp"
5
+ #include < cmath>
5
6
6
7
namespace opencv_test { namespace {
7
8
@@ -783,5 +784,18 @@ TEST(Core_Check, testSize_1)
783
784
}
784
785
}
785
786
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
+ }
786
800
787
801
}} // namespace
You can’t perform that action at this time.
0 commit comments