Commit d2e16992 authored by Vadim Pisarevsky's avatar Vadim Pisarevsky

Merge pull request #5767 from dtmoodie:cpu_mat_memory_allocator

parents bb4b4acc 237f33d4
...@@ -1895,6 +1895,8 @@ public: ...@@ -1895,6 +1895,8 @@ public:
MatAllocator* allocator; MatAllocator* allocator;
//! and the standard allocator //! and the standard allocator
static MatAllocator* getStdAllocator(); static MatAllocator* getStdAllocator();
static MatAllocator* getDefaultAllocator();
static void setDefaultAllocator(MatAllocator* allocator);
//! interaction with UMat //! interaction with UMat
UMatData* u; UMatData* u;
......
...@@ -218,7 +218,24 @@ public: ...@@ -218,7 +218,24 @@ public:
delete u; delete u;
} }
}; };
namespace
{
MatAllocator* g_matAllocator = NULL;
}
MatAllocator* Mat::getDefaultAllocator()
{
if (g_matAllocator == NULL)
{
g_matAllocator = getStdAllocator();
}
return g_matAllocator;
}
void Mat::setDefaultAllocator(MatAllocator* allocator)
{
g_matAllocator = allocator;
}
MatAllocator* Mat::getStdAllocator() MatAllocator* Mat::getStdAllocator()
{ {
CV_SINGLETON_LAZY_INIT(MatAllocator, new StdMatAllocator()) CV_SINGLETON_LAZY_INIT(MatAllocator, new StdMatAllocator())
...@@ -388,7 +405,7 @@ void Mat::create(int d, const int* _sizes, int _type) ...@@ -388,7 +405,7 @@ void Mat::create(int d, const int* _sizes, int _type)
if( total() > 0 ) if( total() > 0 )
{ {
MatAllocator *a = allocator, *a0 = getStdAllocator(); MatAllocator *a = allocator, *a0 = getDefaultAllocator();
#ifdef HAVE_TGPU #ifdef HAVE_TGPU
if( !a || a == tegra::getAllocator() ) if( !a || a == tegra::getAllocator() )
a = tegra::getAllocator(d, _sizes, _type); a = tegra::getAllocator(d, _sizes, _type);
...@@ -426,7 +443,7 @@ void Mat::copySize(const Mat& m) ...@@ -426,7 +443,7 @@ void Mat::copySize(const Mat& m)
void Mat::deallocate() void Mat::deallocate()
{ {
if(u) if(u)
(u->currAllocator ? u->currAllocator : allocator ? allocator : getStdAllocator())->unmap(u); (u->currAllocator ? u->currAllocator : allocator ? allocator : getDefaultAllocator())->unmap(u);
u = NULL; u = NULL;
} }
......
...@@ -4292,7 +4292,7 @@ public: ...@@ -4292,7 +4292,7 @@ public:
bufferPoolSVM.setMaxReservedSize(poolSize); bufferPoolSVM.setMaxReservedSize(poolSize);
#endif #endif
matStdAllocator = Mat::getStdAllocator(); matStdAllocator = Mat::getDefaultAllocator();
} }
UMatData* defaultAllocate(int dims, const int* sizes, int type, void* data, size_t* step, UMatData* defaultAllocate(int dims, const int* sizes, int type, void* data, size_t* step,
...@@ -4918,7 +4918,7 @@ public: ...@@ -4918,7 +4918,7 @@ public:
if( u->data && !u->hostCopyObsolete() ) if( u->data && !u->hostCopyObsolete() )
{ {
Mat::getStdAllocator()->download(u, dstptr, dims, sz, srcofs, srcstep, dststep); Mat::getDefaultAllocator()->download(u, dstptr, dims, sz, srcofs, srcstep, dststep);
return; return;
} }
CV_Assert( u->handle != 0 ); CV_Assert( u->handle != 0 );
...@@ -5042,7 +5042,7 @@ public: ...@@ -5042,7 +5042,7 @@ public:
// 2. we overwrite part of the matrix, but the GPU copy is out-of-date // 2. we overwrite part of the matrix, but the GPU copy is out-of-date
if( u->data && (u->hostCopyObsolete() < u->deviceCopyObsolete() || total == u->size)) if( u->data && (u->hostCopyObsolete() < u->deviceCopyObsolete() || total == u->size))
{ {
Mat::getStdAllocator()->upload(u, srcptr, dims, sz, dstofs, dststep, srcstep); Mat::getDefaultAllocator()->upload(u, srcptr, dims, sz, dstofs, dststep, srcstep);
u->markHostCopyObsolete(false); u->markHostCopyObsolete(false);
u->markDeviceCopyObsolete(true); u->markDeviceCopyObsolete(true);
return; return;
......
...@@ -94,7 +94,7 @@ UMatData::~UMatData() ...@@ -94,7 +94,7 @@ UMatData::~UMatData()
// simulate Mat::deallocate // simulate Mat::deallocate
if (u->mapcount != 0) if (u->mapcount != 0)
{ {
(u->currAllocator ? u->currAllocator : /* TODO allocator ? allocator :*/ Mat::getStdAllocator())->unmap(u); (u->currAllocator ? u->currAllocator : /* TODO allocator ? allocator :*/ Mat::getDefaultAllocator())->unmap(u);
} }
else else
{ {
...@@ -144,7 +144,7 @@ MatAllocator* UMat::getStdAllocator() ...@@ -144,7 +144,7 @@ MatAllocator* UMat::getStdAllocator()
if( ocl::haveOpenCL() && ocl::useOpenCL() ) if( ocl::haveOpenCL() && ocl::useOpenCL() )
return ocl::getOpenCLAllocator(); return ocl::getOpenCLAllocator();
#endif #endif
return Mat::getStdAllocator(); return Mat::getDefaultAllocator();
} }
void swap( UMat& a, UMat& b ) void swap( UMat& a, UMat& b )
...@@ -286,7 +286,7 @@ UMat Mat::getUMat(int accessFlags, UMatUsageFlags usageFlags) const ...@@ -286,7 +286,7 @@ UMat Mat::getUMat(int accessFlags, UMatUsageFlags usageFlags) const
accessFlags |= ACCESS_RW; accessFlags |= ACCESS_RW;
UMatData* new_u = NULL; UMatData* new_u = NULL;
{ {
MatAllocator *a = allocator, *a0 = getStdAllocator(); MatAllocator *a = allocator, *a0 = getDefaultAllocator();
if(!a) if(!a)
a = a0; a = a0;
new_u = a->allocate(dims, size.p, type(), data, step.p, accessFlags, usageFlags); new_u = a->allocate(dims, size.p, type(), data, step.p, accessFlags, usageFlags);
...@@ -302,7 +302,7 @@ UMat Mat::getUMat(int accessFlags, UMatUsageFlags usageFlags) const ...@@ -302,7 +302,7 @@ UMat Mat::getUMat(int accessFlags, UMatUsageFlags usageFlags) const
} }
if (!allocated) if (!allocated)
{ {
allocated = getStdAllocator()->allocate(new_u, accessFlags, usageFlags); allocated = getDefaultAllocator()->allocate(new_u, accessFlags, usageFlags);
CV_Assert(allocated); CV_Assert(allocated);
} }
if (u != NULL) if (u != NULL)
...@@ -358,7 +358,7 @@ void UMat::create(int d, const int* _sizes, int _type, UMatUsageFlags _usageFlag ...@@ -358,7 +358,7 @@ void UMat::create(int d, const int* _sizes, int _type, UMatUsageFlags _usageFlag
if (!a) if (!a)
{ {
a = a0; a = a0;
a0 = Mat::getStdAllocator(); a0 = Mat::getDefaultAllocator();
} }
try try
{ {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment