From 23dba65f031954df896bc3c6dfb1a9705574886b Mon Sep 17 00:00:00 2001 From: Herman Semenoff Date: Wed, 8 Apr 2026 12:58:55 +0300 Subject: apt: modernize to make_unique C++17 References: - https://stackoverflow.com/questions/79700634/pros-and-cons-of-make-unique-vs-direct-constructor-call-in-c17 - https://towardsdev.com/why-std-make-unique-beats-new-in-modern-c-7e2ba653737e - https://www.sololearn.com/en/Discuss/3334779/where-make_unique-is-better-than-unique_ptrraw-pointer --- apt-pkg/acquire.cc | 2 +- apt-pkg/algorithms.cc | 6 +++--- apt-pkg/contrib/fileutl.cc | 2 +- apt-pkg/orderlist.cc | 4 ++-- apt-pkg/pkgcachegen.cc | 10 +++++----- apt-pkg/policy.cc | 2 +- cmdline/apt-sortpkgs.cc | 2 +- methods/http.cc | 2 +- 8 files changed, 15 insertions(+), 15 deletions(-) diff --git a/apt-pkg/acquire.cc b/apt-pkg/acquire.cc index 7a60c4476..95b6391cd 100644 --- a/apt-pkg/acquire.cc +++ b/apt-pkg/acquire.cc @@ -636,7 +636,7 @@ static void CheckDropPrivsMustBeDisabled(pkgAcquire const &Fetcher) gid_t const old_egid = getegid(); long const ngroups_max = sysconf(_SC_NGROUPS_MAX); - std::unique_ptr old_gidlist(new gid_t[ngroups_max]); + auto old_gidlist = std::make_unique(ngroups_max); if (unlikely(old_gidlist == NULL)) return; ssize_t old_gidlist_nr; diff --git a/apt-pkg/algorithms.cc b/apt-pkg/algorithms.cc index a08ecd2c6..44f186a82 100644 --- a/apt-pkg/algorithms.cc +++ b/apt-pkg/algorithms.cc @@ -554,7 +554,7 @@ void pkgProblemResolver::MakeScores() } // Copy the scores to advoid additive looping - std::unique_ptr OldScores(new int[Size]); + auto OldScores = std::make_unique(Size); memcpy(&OldScores[0],&Scores[0],sizeof(Scores[0])*Size); /* Now we cause 1 level of dependency inheritance, that is we add the @@ -797,7 +797,7 @@ bool pkgProblemResolver::ResolveInternal(bool const BrokenFix) operates from highest score to lowest. This prevents problems when high score packages cause the removal of lower score packages that would cause the removal of even lower score packages. */ - std::unique_ptr PList(new pkgCache::Package *[Size]); + auto PList = std::make_unique(Size); pkgCache::Package **PEnd = &PList[0]; for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; ++I) *PEnd++ = I; @@ -1291,7 +1291,7 @@ bool pkgProblemResolver::ResolveByKeepInternal() high score packages cause the removal of lower score packages that would cause the removal of even lower score packages. */ auto Size = Cache.Head().PackageCount; - std::unique_ptr PList{new pkgCache::Package *[Size]}; + auto PList = std::make_unique(Size); pkgCache::Package **PEnd = &PList[0]; for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; ++I) *PEnd++ = I; diff --git a/apt-pkg/contrib/fileutl.cc b/apt-pkg/contrib/fileutl.cc index 74f5ec8bc..832e87127 100644 --- a/apt-pkg/contrib/fileutl.cc +++ b/apt-pkg/contrib/fileutl.cc @@ -3346,7 +3346,7 @@ bool DropPrivileges() /*{{{*/ { // Verify that the user isn't still in any supplementary groups long const ngroups_max = sysconf(_SC_NGROUPS_MAX); - std::unique_ptr gidlist(new gid_t[ngroups_max]); + auto gidlist = std::make_unique(ngroups_max); if (unlikely(gidlist == NULL)) return _error->Error("Allocation of a list of size %lu for getgroups failed", ngroups_max); ssize_t gidlist_nr; diff --git a/apt-pkg/orderlist.cc b/apt-pkg/orderlist.cc index 33d17fcec..83d2f2fb4 100644 --- a/apt-pkg/orderlist.cc +++ b/apt-pkg/orderlist.cc @@ -137,8 +137,8 @@ bool pkgOrderList::DoRun() { // Temp list unsigned long Size = Cache.Head().PackageCount; - std::unique_ptr NList(new Package *[Size]); - std::unique_ptr AfterList(new Package *[Size]); + auto NList = std::make_unique(Size); + auto AfterList = std::make_unique(Size); AfterEnd = AfterList.get(); Depth = 0; diff --git a/apt-pkg/pkgcachegen.cc b/apt-pkg/pkgcachegen.cc index 7bf635972..82b88d6b0 100644 --- a/apt-pkg/pkgcachegen.cc +++ b/apt-pkg/pkgcachegen.cc @@ -1502,10 +1502,10 @@ static bool CheckValidity(FileFd &CacheFile, std::string const &CacheFileName, } // Map it - std::unique_ptr Map(new MMap(CacheFile,0)); + auto Map = std::make_unique(CacheFile,0); if (unlikely(Map->validData()) == false) return false; - std::unique_ptr CacheP(new pkgCache(Map.get())); + auto CacheP = std::make_unique(Map.get()); pkgCache &Cache = *CacheP.get(); if (_error->PendingError() || Map->Size() == 0) { @@ -1514,7 +1514,7 @@ static bool CheckValidity(FileFd &CacheFile, std::string const &CacheFileName, return false; } - std::unique_ptr RlsVisited(new bool[Cache.HeaderP->ReleaseFileCount]); + auto RlsVisited = std::make_unique(Cache.HeaderP->ReleaseFileCount); memset(RlsVisited.get(),0,sizeof(RlsVisited[0])*Cache.HeaderP->ReleaseFileCount); std::vector Files; for (pkgSourceList::const_iterator i = List.begin(); i != List.end(); ++i) @@ -1549,7 +1549,7 @@ static bool CheckValidity(FileFd &CacheFile, std::string const &CacheFileName, /* Now we check every index file, see if it is in the cache, verify the IMS data and check that it is on the disk too.. */ - std::unique_ptr Visited(new bool[Cache.HeaderP->PackageFileCount]); + auto Visited = std::make_unique(Cache.HeaderP->PackageFileCount); memset(Visited.get(),0,sizeof(Visited[0])*Cache.HeaderP->PackageFileCount); for (std::vector::const_reverse_iterator PkgFile = Files.rbegin(); PkgFile != Files.rend(); ++PkgFile) { @@ -1927,7 +1927,7 @@ bool pkgCacheGenerator::MakeOnlyStatusCache(OpProgress *Progress,DynamicMMap **O return false; ScopedErrorMerge sem; - std::unique_ptr Map(CreateDynamicMMap(NULL, 0)); + auto Map = std::make_unique(NULL, 0); if (unlikely(Map->validData()) == false) return false; map_filesize_t CurrentSize = 0; diff --git a/apt-pkg/policy.cc b/apt-pkg/policy.cc index 7d11ba898..d91277d3b 100644 --- a/apt-pkg/policy.cc +++ b/apt-pkg/policy.cc @@ -109,7 +109,7 @@ bool pkgPolicy::InitDefaults() } // Apply the defaults.. - std::unique_ptr Fixed(new bool[Cache->HeaderP->PackageFileCount]); + auto Fixed = std::make_unique(Cache->HeaderP->PackageFileCount); memset(Fixed.get(),0,sizeof(Fixed[0])*Cache->HeaderP->PackageFileCount); StatusOverride = false; for (vector::const_iterator I = Defaults.begin(); I != Defaults.end(); ++I) diff --git a/cmdline/apt-sortpkgs.cc b/cmdline/apt-sortpkgs.cc index 8442a959d..f493ad0cb 100644 --- a/cmdline/apt-sortpkgs.cc +++ b/cmdline/apt-sortpkgs.cc @@ -115,7 +115,7 @@ static bool DoIt(string InFile) // Emit FileFd stdoutfd; stdoutfd.OpenDescriptor(STDOUT_FILENO, FileFd::WriteOnly, false); - auto const Buffer = std::unique_ptr(new unsigned char[Largest+1]); + auto const Buffer = std::make_unique(Largest+1); for (vector::iterator I = List.begin(); I != List.end(); ++I) { // Read in the Record. diff --git a/methods/http.cc b/methods/http.cc index 85350785e..09bca47dd 100644 --- a/methods/http.cc +++ b/methods/http.cc @@ -397,7 +397,7 @@ static ResultState UnwrapHTTPConnect(std::string Host, int Port, URI Proxy, std: if (In.WriteSpace()) { // Maybe there is actual data already read, if so we need to buffer it - std::unique_ptr NewFd(new HttpConnectFd()); + auto NewFd = std::make_unique(); In.Write(NewFd->Buffer); NewFd->UnderlyingFd = std::move(Fd); Fd = std::move(NewFd); -- cgit v1.2.3-70-g09d2