mirror of
https://github.com/opencv/opencv.git
synced 2026-07-29 15:23:05 +04:00
Merge pull request #15544 from mshabunin:disable_posix_memalign
* Disable posix_memalign by default * core: fix memalign parameter handling
This commit is contained in:
committed by
Alexander Alekhin
parent
c3a588037a
commit
1ca74c3c03
+56
-15
@@ -46,6 +46,7 @@
|
||||
#undef CV_LOG_STRIP_LEVEL
|
||||
#define CV_LOG_STRIP_LEVEL CV_LOG_LEVEL_VERBOSE + 1
|
||||
#include <opencv2/core/utils/logger.hpp>
|
||||
#include <opencv2/core/utils/configuration.private.hpp>
|
||||
|
||||
#define CV__ALLOCATOR_STATS_LOG(...) CV_LOG_VERBOSE(NULL, 0, "alloc.cpp: " << __VA_ARGS__)
|
||||
#include "opencv2/core/utils/allocator_stats.impl.hpp"
|
||||
@@ -81,6 +82,38 @@ cv::utils::AllocatorStatisticsInterface& getAllocatorStatistics()
|
||||
return allocator_stats;
|
||||
}
|
||||
|
||||
#if defined HAVE_POSIX_MEMALIGN || defined HAVE_MEMALIGN
|
||||
static bool readMemoryAlignmentParameter()
|
||||
{
|
||||
bool value = true;
|
||||
#if defined(__GLIBC__) && defined(__linux__) \
|
||||
&& !defined(CV_STATIC_ANALYSIS) \
|
||||
&& !defined(OPENCV_ENABLE_MEMORY_SANITIZER) \
|
||||
&& !defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION) /* oss-fuzz */ \
|
||||
&& !defined(_WIN32) /* MinGW? */
|
||||
{
|
||||
// https://github.com/opencv/opencv/issues/15526
|
||||
value = false;
|
||||
}
|
||||
#endif
|
||||
value = cv::utils::getConfigurationParameterBool("OPENCV_ENABLE_MEMALIGN", value); // should not call fastMalloc() internally
|
||||
// TODO add checks for valgrind, ASAN if value == false
|
||||
return value;
|
||||
}
|
||||
static inline
|
||||
bool isAlignedAllocationEnabled()
|
||||
{
|
||||
static bool initialized = false;
|
||||
static bool useMemalign = true;
|
||||
if (!initialized)
|
||||
{
|
||||
initialized = true; // trick to avoid stuck in acquire (works only if allocations are scope based)
|
||||
useMemalign = readMemoryAlignmentParameter();
|
||||
}
|
||||
return useMemalign;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef OPENCV_ALLOC_ENABLE_STATISTICS
|
||||
static inline
|
||||
void* fastMalloc_(size_t size)
|
||||
@@ -89,25 +122,30 @@ void* fastMalloc(size_t size)
|
||||
#endif
|
||||
{
|
||||
#ifdef HAVE_POSIX_MEMALIGN
|
||||
void* ptr = NULL;
|
||||
if(posix_memalign(&ptr, CV_MALLOC_ALIGN, size))
|
||||
ptr = NULL;
|
||||
if(!ptr)
|
||||
return OutOfMemoryError(size);
|
||||
return ptr;
|
||||
if (isAlignedAllocationEnabled())
|
||||
{
|
||||
void* ptr = NULL;
|
||||
if(posix_memalign(&ptr, CV_MALLOC_ALIGN, size))
|
||||
ptr = NULL;
|
||||
if(!ptr)
|
||||
return OutOfMemoryError(size);
|
||||
return ptr;
|
||||
}
|
||||
#elif defined HAVE_MEMALIGN
|
||||
void* ptr = memalign(CV_MALLOC_ALIGN, size);
|
||||
if(!ptr)
|
||||
return OutOfMemoryError(size);
|
||||
return ptr;
|
||||
#else
|
||||
if (isAlignedAllocationEnabled())
|
||||
{
|
||||
void* ptr = memalign(CV_MALLOC_ALIGN, size);
|
||||
if(!ptr)
|
||||
return OutOfMemoryError(size);
|
||||
return ptr;
|
||||
}
|
||||
#endif
|
||||
uchar* udata = (uchar*)malloc(size + sizeof(void*) + CV_MALLOC_ALIGN);
|
||||
if(!udata)
|
||||
return OutOfMemoryError(size);
|
||||
uchar** adata = alignPtr((uchar**)udata + 1, CV_MALLOC_ALIGN);
|
||||
adata[-1] = udata;
|
||||
return adata;
|
||||
#endif
|
||||
}
|
||||
|
||||
#ifdef OPENCV_ALLOC_ENABLE_STATISTICS
|
||||
@@ -118,8 +156,12 @@ void fastFree(void* ptr)
|
||||
#endif
|
||||
{
|
||||
#if defined HAVE_POSIX_MEMALIGN || defined HAVE_MEMALIGN
|
||||
free(ptr);
|
||||
#else
|
||||
if (isAlignedAllocationEnabled())
|
||||
{
|
||||
free(ptr);
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
if(ptr)
|
||||
{
|
||||
uchar* udata = ((uchar**)ptr)[-1];
|
||||
@@ -127,7 +169,6 @@ void fastFree(void* ptr)
|
||||
((uchar*)ptr - udata) <= (ptrdiff_t)(sizeof(void*)+CV_MALLOC_ALIGN));
|
||||
free(udata);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
#ifdef OPENCV_ALLOC_ENABLE_STATISTICS
|
||||
|
||||
Reference in New Issue
Block a user