Merge pull request #5767 from dtmoodie:cpu_mat_memory_allocator

This commit is contained in:
Vadim Pisarevsky 2015-12-10 14:32:04 +00:00
commit d2e169929c
4 changed files with 29 additions and 10 deletions

View File

@ -1895,6 +1895,8 @@ public:
MatAllocator* allocator;
//! and the standard allocator
static MatAllocator* getStdAllocator();
static MatAllocator* getDefaultAllocator();
static void setDefaultAllocator(MatAllocator* allocator);
//! interaction with UMat
UMatData* u;

View File

@ -218,7 +218,24 @@ public:
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()
{
CV_SINGLETON_LAZY_INIT(MatAllocator, new StdMatAllocator())
@ -388,7 +405,7 @@ void Mat::create(int d, const int* _sizes, int _type)
if( total() > 0 )
{
MatAllocator *a = allocator, *a0 = getStdAllocator();
MatAllocator *a = allocator, *a0 = getDefaultAllocator();
#ifdef HAVE_TGPU
if( !a || a == tegra::getAllocator() )
a = tegra::getAllocator(d, _sizes, _type);
@ -426,7 +443,7 @@ void Mat::copySize(const Mat& m)
void Mat::deallocate()
{
if(u)
(u->currAllocator ? u->currAllocator : allocator ? allocator : getStdAllocator())->unmap(u);
(u->currAllocator ? u->currAllocator : allocator ? allocator : getDefaultAllocator())->unmap(u);
u = NULL;
}

View File

@ -4292,7 +4292,7 @@ public:
bufferPoolSVM.setMaxReservedSize(poolSize);
#endif
matStdAllocator = Mat::getStdAllocator();
matStdAllocator = Mat::getDefaultAllocator();
}
UMatData* defaultAllocate(int dims, const int* sizes, int type, void* data, size_t* step,
@ -4918,7 +4918,7 @@ public:
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;
}
CV_Assert( u->handle != 0 );
@ -5042,7 +5042,7 @@ public:
// 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))
{
Mat::getStdAllocator()->upload(u, srcptr, dims, sz, dstofs, dststep, srcstep);
Mat::getDefaultAllocator()->upload(u, srcptr, dims, sz, dstofs, dststep, srcstep);
u->markHostCopyObsolete(false);
u->markDeviceCopyObsolete(true);
return;

View File

@ -94,7 +94,7 @@ UMatData::~UMatData()
// simulate Mat::deallocate
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
{
@ -144,7 +144,7 @@ MatAllocator* UMat::getStdAllocator()
if( ocl::haveOpenCL() && ocl::useOpenCL() )
return ocl::getOpenCLAllocator();
#endif
return Mat::getStdAllocator();
return Mat::getDefaultAllocator();
}
void swap( UMat& a, UMat& b )
@ -286,7 +286,7 @@ UMat Mat::getUMat(int accessFlags, UMatUsageFlags usageFlags) const
accessFlags |= ACCESS_RW;
UMatData* new_u = NULL;
{
MatAllocator *a = allocator, *a0 = getStdAllocator();
MatAllocator *a = allocator, *a0 = getDefaultAllocator();
if(!a)
a = a0;
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
}
if (!allocated)
{
allocated = getStdAllocator()->allocate(new_u, accessFlags, usageFlags);
allocated = getDefaultAllocator()->allocate(new_u, accessFlags, usageFlags);
CV_Assert(allocated);
}
if (u != NULL)
@ -358,7 +358,7 @@ void UMat::create(int d, const int* _sizes, int _type, UMatUsageFlags _usageFlag
if (!a)
{
a = a0;
a0 = Mat::getStdAllocator();
a0 = Mat::getDefaultAllocator();
}
try
{