From 4f373a4284e2ac07dabd862d408a215df0d9e45a Mon Sep 17 00:00:00 2001 From: Dan Moodie Date: Mon, 30 Nov 2015 15:45:48 -0500 Subject: [PATCH] Clarified default allocator interface. Conflicts: modules/core/src/matrix.cpp --- modules/core/src/matrix.cpp | 23 ++++++++++++++++++++--- modules/core/src/ocl.cpp | 6 +++--- modules/core/src/umatrix.cpp | 10 +++++----- 3 files changed, 28 insertions(+), 11 deletions(-) diff --git a/modules/core/src/matrix.cpp b/modules/core/src/matrix.cpp index 488b2b8..ee3239e 100644 --- a/modules/core/src/matrix.cpp +++ b/modules/core/src/matrix.cpp @@ -218,10 +218,27 @@ public: delete u; } }; +namespace +{ + MatAllocator* g_matAllocator = nullptr; +} + +MatAllocator* Mat::getDefaultAllocator() +{ + if (g_matAllocator == nullptr) + { + g_matAllocator = getStdAllocator(); + } + return g_matAllocator; +} +void Mat::setDefaultAllocator(MatAllocator* allocator) +{ + g_matAllocator = allocator; +} MatAllocator* Mat::getStdAllocator() { - CV_SINGLETON_LAZY_INIT(MatAllocator, new StdMatAllocator()) + CV_SINGLETON_LAZY_INIT(MatAllocator, new StdMatAllocator()) } void swap( Mat& a, Mat& b ) @@ -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; } diff --git a/modules/core/src/ocl.cpp b/modules/core/src/ocl.cpp index 1345c52..77d12a6 100644 --- a/modules/core/src/ocl.cpp +++ b/modules/core/src/ocl.cpp @@ -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; diff --git a/modules/core/src/umatrix.cpp b/modules/core/src/umatrix.cpp index 999a5cc..98d6b8c 100644 --- a/modules/core/src/umatrix.cpp +++ b/modules/core/src/umatrix.cpp @@ -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 { -- 2.7.4