From f1f9f9bfec4c2ad664698a026773276b0b5a4ac8 Mon Sep 17 00:00:00 2001 From: Julian Andres Klode Date: Fri, 23 Oct 2015 17:17:54 +0200 Subject: algorithms: Replace qsort() by thread-safe std::sort() Gbp-Dch: ignore --- apt-pkg/algorithms.cc | 81 ++++++++++++++++++++++++-------------------- apt-pkg/algorithms.h | 3 +- debian/libapt-pkg5.0.symbols | 1 - 3 files changed, 45 insertions(+), 40 deletions(-) diff --git a/apt-pkg/algorithms.cc b/apt-pkg/algorithms.cc index 6d982c551..7ddbb9d44 100644 --- a/apt-pkg/algorithms.cc +++ b/apt-pkg/algorithms.cc @@ -34,8 +34,6 @@ /*}}}*/ using namespace std; -pkgProblemResolver *pkgProblemResolver::This = 0; - // Simulate::Simulate - Constructor /*{{{*/ // --------------------------------------------------------------------- /* The legacy translations here of input Pkg iterators is obsolete, @@ -360,13 +358,11 @@ pkgProblemResolver::~pkgProblemResolver() // ProblemResolver::ScoreSort - Sort the list by score /*{{{*/ // --------------------------------------------------------------------- /* */ -int pkgProblemResolver::ScoreSort(const void *a,const void *b) +int pkgProblemResolver::ScoreSort(Package const *A,Package const *B) { - Package const **A = (Package const **)a; - Package const **B = (Package const **)b; - if (This->Scores[(*A)->ID] > This->Scores[(*B)->ID]) + if (Scores[A->ID] > Scores[B->ID]) return -1; - if (This->Scores[(*A)->ID] < This->Scores[(*B)->ID]) + if (Scores[A->ID] < Scores[B->ID]) return 1; return 0; } @@ -704,8 +700,8 @@ bool pkgProblemResolver::ResolveInternal(bool const BrokenFix) pkgCache::Package **PEnd = PList.get(); for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; ++I) *PEnd++ = I; - This = this; - qsort(PList.get(),PEnd - PList.get(),sizeof(PList[0]),&ScoreSort); + + std::sort(PList.get(), PEnd, [this](Package *a, Package *b) { return ScoreSort(a, b) < 0; }); if (_config->FindB("Debug::pkgProblemResolver::ShowScores",false) == true) { @@ -1163,8 +1159,9 @@ bool pkgProblemResolver::ResolveByKeepInternal() pkgCache::Package **PEnd = PList; for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; ++I) *PEnd++ = I; - This = this; - qsort(PList,PEnd - PList,sizeof(*PList),&ScoreSort); + + std::sort(PList,PEnd,[this](Package *a, Package *b) { return ScoreSort(a, b) < 0; }); + if (_config->FindB("Debug::pkgProblemResolver::ShowScores",false) == true) { @@ -1314,36 +1311,46 @@ void pkgProblemResolver::InstallProtect() // --------------------------------------------------------------------- /* This is ment to be used in conjunction with AllTargets to get a list of versions ordered by preference. */ -static pkgCache *PrioCache; -static int PrioComp(const void *A,const void *B) -{ - pkgCache::VerIterator L(*PrioCache,*(pkgCache::Version **)A); - pkgCache::VerIterator R(*PrioCache,*(pkgCache::Version **)B); - - if ((L.ParentPkg()->Flags & pkgCache::Flag::Essential) == pkgCache::Flag::Essential && - (R.ParentPkg()->Flags & pkgCache::Flag::Essential) != pkgCache::Flag::Essential) - return 1; - if ((L.ParentPkg()->Flags & pkgCache::Flag::Essential) != pkgCache::Flag::Essential && - (R.ParentPkg()->Flags & pkgCache::Flag::Essential) == pkgCache::Flag::Essential) - return -1; - - if ((L.ParentPkg()->Flags & pkgCache::Flag::Important) == pkgCache::Flag::Important && - (R.ParentPkg()->Flags & pkgCache::Flag::Important) != pkgCache::Flag::Important) - return 1; - if ((L.ParentPkg()->Flags & pkgCache::Flag::Important) != pkgCache::Flag::Important && - (R.ParentPkg()->Flags & pkgCache::Flag::Important) == pkgCache::Flag::Important) - return -1; - - if (L->Priority != R->Priority) - return R->Priority - L->Priority; - return strcmp(L.ParentPkg().Name(),R.ParentPkg().Name()); -} + +struct PrioComp { + pkgCache &PrioCache; + + PrioComp(pkgCache &PrioCache) : PrioCache(PrioCache) { + } + + bool operator() (pkgCache::Version * const &A, pkgCache::Version * const &B) { + return compare(A, B) < 0; + } + + int compare(pkgCache::Version * const &A, pkgCache::Version * const &B) { + pkgCache::VerIterator L(PrioCache,A); + pkgCache::VerIterator R(PrioCache,B); + + if ((L.ParentPkg()->Flags & pkgCache::Flag::Essential) == pkgCache::Flag::Essential && + (R.ParentPkg()->Flags & pkgCache::Flag::Essential) != pkgCache::Flag::Essential) + return 1; + if ((L.ParentPkg()->Flags & pkgCache::Flag::Essential) != pkgCache::Flag::Essential && + (R.ParentPkg()->Flags & pkgCache::Flag::Essential) == pkgCache::Flag::Essential) + return -1; + + if ((L.ParentPkg()->Flags & pkgCache::Flag::Important) == pkgCache::Flag::Important && + (R.ParentPkg()->Flags & pkgCache::Flag::Important) != pkgCache::Flag::Important) + return 1; + if ((L.ParentPkg()->Flags & pkgCache::Flag::Important) != pkgCache::Flag::Important && + (R.ParentPkg()->Flags & pkgCache::Flag::Important) == pkgCache::Flag::Important) + return -1; + + if (L->Priority != R->Priority) + return R->Priority - L->Priority; + return strcmp(L.ParentPkg().Name(),R.ParentPkg().Name()); + } +}; + void pkgPrioSortList(pkgCache &Cache,pkgCache::Version **List) { unsigned long Count = 0; - PrioCache = &Cache; for (pkgCache::Version **I = List; *I != 0; I++) Count++; - qsort(List,Count,sizeof(*List),PrioComp); + std::sort(List,List+Count,PrioComp(Cache)); } /*}}}*/ diff --git a/apt-pkg/algorithms.h b/apt-pkg/algorithms.h index aad261b63..77a39b415 100644 --- a/apt-pkg/algorithms.h +++ b/apt-pkg/algorithms.h @@ -113,8 +113,7 @@ class pkgProblemResolver /*{{{*/ bool Debug; // Sort stuff - static pkgProblemResolver *This; - APT_HIDDEN static int ScoreSort(const void *a,const void *b) APT_PURE; + APT_HIDDEN int ScoreSort(Package const *A, Package const *B) APT_PURE; struct PackageKill { diff --git a/debian/libapt-pkg5.0.symbols b/debian/libapt-pkg5.0.symbols index 20deaed77..5ff1fa2cd 100644 --- a/debian/libapt-pkg5.0.symbols +++ b/debian/libapt-pkg5.0.symbols @@ -369,7 +369,6 @@ libapt-pkg.so.5.0 libapt-pkg5.0 #MINVER# (c++)"pkgPackageManager::pkgPackageManager(pkgDepCache*)@APTPKG_5.0" 0.8.0 (c++)"pkgPackageManager::~pkgPackageManager()@APTPKG_5.0" 0.8.0 (c++)"pkgProblemResolver::InstallProtect()@APTPKG_5.0" 0.8.0 - (c++)"pkgProblemResolver::This@APTPKG_5.0" 0.8.0 (c++)"pkgProblemResolver::pkgProblemResolver(pkgDepCache*)@APTPKG_5.0" 0.8.0 (c++)"pkgProblemResolver::~pkgProblemResolver()@APTPKG_5.0" 0.8.0 (c++)"debVersioningSystem::CmpFragment(char const*, char const*, char const*, char const*)@APTPKG_5.0" 0.8.0 -- cgit v1.2.3