提交 1d7bfcc9 编写于 作者: A Alexander Alekhin

Merge pull request #16827 from alalek:update_buffer_area

...@@ -72,6 +72,10 @@ public: ...@@ -72,6 +72,10 @@ public:
CV_Assert(alignment % sizeof(T) == 0); CV_Assert(alignment % sizeof(T) == 0);
CV_Assert((alignment & (alignment - 1)) == 0); CV_Assert((alignment & (alignment - 1)) == 0);
allocate_((void**)(&ptr), static_cast<ushort>(sizeof(T)), count, alignment); allocate_((void**)(&ptr), static_cast<ushort>(sizeof(T)), count, alignment);
#ifndef OPENCV_ENABLE_MEMORY_SANITIZER
if (safe)
#endif
CV_Assert(ptr != NULL);
} }
/** @brief Fill one of buffers with zeroes /** @brief Fill one of buffers with zeroes
...@@ -118,9 +122,11 @@ private: ...@@ -118,9 +122,11 @@ private:
private: private:
class Block; class Block;
std::vector<Block> blocks; std::vector<Block> blocks;
#ifndef OPENCV_ENABLE_MEMORY_SANITIZER
void * oneBuf; void * oneBuf;
size_t totalSize; size_t totalSize;
const bool safe; const bool safe;
#endif
}; };
//! @} //! @}
......
...@@ -5,14 +5,10 @@ ...@@ -5,14 +5,10 @@
#include "opencv2/core/utils/buffer_area.private.hpp" #include "opencv2/core/utils/buffer_area.private.hpp"
#include "opencv2/core/utils/configuration.private.hpp" #include "opencv2/core/utils/configuration.private.hpp"
#ifdef OPENCV_ENABLE_MEMORY_SANITIZER #ifndef OPENCV_ENABLE_MEMORY_SANITIZER
#define BUFFER_AREA_DEFAULT_MODE true
#else
#define BUFFER_AREA_DEFAULT_MODE false
#endif
static bool CV_BUFFER_AREA_OVERRIDE_SAFE_MODE = static bool CV_BUFFER_AREA_OVERRIDE_SAFE_MODE =
cv::utils::getConfigurationParameterBool("OPENCV_BUFFER_AREA_ALWAYS_SAFE", BUFFER_AREA_DEFAULT_MODE); cv::utils::getConfigurationParameterBool("OPENCV_BUFFER_AREA_ALWAYS_SAFE", false);
#endif
namespace cv { namespace utils { namespace cv { namespace utils {
...@@ -58,6 +54,7 @@ public: ...@@ -58,6 +54,7 @@ public:
*ptr = raw_mem; *ptr = raw_mem;
} }
} }
#ifndef OPENCV_ENABLE_MEMORY_SANITIZER
void * fast_allocate(void * buf) const void * fast_allocate(void * buf) const
{ {
CV_Assert(ptr && *ptr == NULL); CV_Assert(ptr && *ptr == NULL);
...@@ -66,6 +63,7 @@ public: ...@@ -66,6 +63,7 @@ public:
*ptr = buf; *ptr = buf;
return static_cast<void*>(static_cast<uchar*>(*ptr) + type_size * count); return static_cast<void*>(static_cast<uchar*>(*ptr) + type_size * count);
} }
#endif
bool operator==(void **other) const bool operator==(void **other) const
{ {
CV_Assert(ptr && other); CV_Assert(ptr && other);
...@@ -86,12 +84,20 @@ private: ...@@ -86,12 +84,20 @@ private:
//================================================================================================== //==================================================================================================
#ifndef OPENCV_ENABLE_MEMORY_SANITIZER
BufferArea::BufferArea(bool safe_) : BufferArea::BufferArea(bool safe_) :
oneBuf(0), oneBuf(0),
totalSize(0), totalSize(0),
safe(safe_ || CV_BUFFER_AREA_OVERRIDE_SAFE_MODE) safe(safe_ || CV_BUFFER_AREA_OVERRIDE_SAFE_MODE)
{ {
// nothing
} }
#else
BufferArea::BufferArea(bool safe_)
{
CV_UNUSED(safe_);
}
#endif
BufferArea::~BufferArea() BufferArea::~BufferArea()
{ {
...@@ -101,10 +107,16 @@ BufferArea::~BufferArea() ...@@ -101,10 +107,16 @@ BufferArea::~BufferArea()
void BufferArea::allocate_(void **ptr, ushort type_size, size_t count, ushort alignment) void BufferArea::allocate_(void **ptr, ushort type_size, size_t count, ushort alignment)
{ {
blocks.push_back(Block(ptr, type_size, count, alignment)); blocks.push_back(Block(ptr, type_size, count, alignment));
if (safe) #ifndef OPENCV_ENABLE_MEMORY_SANITIZER
blocks.back().real_allocate(); if (!safe)
else {
totalSize += blocks.back().getByteCount(); totalSize += blocks.back().getByteCount();
}
else
#endif
{
blocks.back().real_allocate();
}
} }
void BufferArea::zeroFill_(void **ptr) void BufferArea::zeroFill_(void **ptr)
...@@ -129,6 +141,7 @@ void BufferArea::zeroFill() ...@@ -129,6 +141,7 @@ void BufferArea::zeroFill()
void BufferArea::commit() void BufferArea::commit()
{ {
#ifndef OPENCV_ENABLE_MEMORY_SANITIZER
if (!safe) if (!safe)
{ {
CV_Assert(totalSize > 0); CV_Assert(totalSize > 0);
...@@ -141,6 +154,7 @@ void BufferArea::commit() ...@@ -141,6 +154,7 @@ void BufferArea::commit()
ptr = i->fast_allocate(ptr); ptr = i->fast_allocate(ptr);
} }
} }
#endif
} }
void BufferArea::release() void BufferArea::release()
...@@ -150,11 +164,13 @@ void BufferArea::release() ...@@ -150,11 +164,13 @@ void BufferArea::release()
i->cleanup(); i->cleanup();
} }
blocks.clear(); blocks.clear();
#ifndef OPENCV_ENABLE_MEMORY_SANITIZER
if (oneBuf) if (oneBuf)
{ {
fastFree(oneBuf); fastFree(oneBuf);
oneBuf = 0; oneBuf = 0;
} }
#endif
} }
//================================================================================================== //==================================================================================================
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册