From 1f4e2ab7462f5e05e452fb8505185895d91651c2 Mon Sep 17 00:00:00 2001 From: Julian Andres Klode Date: Mon, 24 Feb 2020 18:09:49 +0100 Subject: Wrap AllocateInMap with a templated version --- apt-pkg/pkgcachegen.cc | 30 ++++++++++++++++-------------- apt-pkg/pkgcachegen.h | 5 ++++- 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/apt-pkg/pkgcachegen.cc b/apt-pkg/pkgcachegen.cc index 7a6ada5a2..a1e6ca745 100644 --- a/apt-pkg/pkgcachegen.cc +++ b/apt-pkg/pkgcachegen.cc @@ -226,13 +226,15 @@ map_stringitem_t pkgCacheGenerator::WriteStringInMap(const char *String) { return index; } /*}}}*/ -map_pointer pkgCacheGenerator::AllocateInMap(const unsigned long &size) {/*{{{*/ +uint32_t pkgCacheGenerator::AllocateInMap(const unsigned long &size) {/*{{{*/ size_t oldSize = Map.Size(); void const * const oldMap = Map.Data(); - map_pointer const index = Map.Allocate(size); + auto index = Map.Allocate(size); if (index != 0) ReMap(oldMap, Map.Data(), oldSize); - return index; + if (index != static_cast(index)) + abort(); // Internal error + return static_cast(index); } /*}}}*/ // CacheGenerator::MergeList - Merge the package list /*{{{*/ @@ -553,7 +555,7 @@ bool pkgCacheGenerator::NewGroup(pkgCache::GrpIterator &Grp, StringView Name) return true; // Get a structure - map_pointer const Group = AllocateInMap(sizeof(pkgCache::Group)); + auto const Group = AllocateInMap(); if (unlikely(Group == 0)) return false; @@ -594,7 +596,7 @@ bool pkgCacheGenerator::NewPackage(pkgCache::PkgIterator &Pkg, StringView Name, return true; // Get a structure - map_pointer const Package = AllocateInMap(sizeof(pkgCache::Package)); + auto const Package = AllocateInMap(); if (unlikely(Package == 0)) return false; Pkg = pkgCache::PkgIterator(Cache,Cache.PkgP + Package); @@ -820,7 +822,7 @@ bool pkgCacheGenerator::NewFileVer(pkgCache::VerIterator &Ver, return true; // Get a structure - map_pointer const VerFile = AllocateInMap(sizeof(pkgCache::VerFile)); + auto const VerFile = AllocateInMap(); if (VerFile == 0) return false; @@ -853,7 +855,7 @@ map_pointer pkgCacheGenerator::NewVersion(pkgCache::VerIterat map_pointer const Next) { // Get a structure - map_pointer const Version = AllocateInMap(sizeof(pkgCache::Version)); + auto const Version = AllocateInMap(); if (Version == 0) return 0; @@ -905,7 +907,7 @@ bool pkgCacheGenerator::NewFileDesc(pkgCache::DescIterator &Desc, return true; // Get a structure - map_pointer const DescFile = AllocateInMap(sizeof(pkgCache::DescFile)); + auto const DescFile = AllocateInMap(); if (DescFile == 0) return false; @@ -938,7 +940,7 @@ map_pointer pkgCacheGenerator::NewDescription(pkgCache::D map_stringitem_t const idxmd5str) { // Get a structure - map_pointer const Description = AllocateInMap(sizeof(pkgCache::Description)); + auto const Description = AllocateInMap(); if (Description == 0) return 0; @@ -976,7 +978,7 @@ bool pkgCacheGenerator::NewDepends(pkgCache::PkgIterator &Pkg, { void const * const oldMap = Map.Data(); // Get a structure - map_pointer const Dependency = AllocateInMap(sizeof(pkgCache::Dependency)); + auto const Dependency = AllocateInMap(); if (unlikely(Dependency == 0)) return false; @@ -1003,7 +1005,7 @@ bool pkgCacheGenerator::NewDepends(pkgCache::PkgIterator &Pkg, if (isDuplicate == false) { - DependencyData = AllocateInMap(sizeof(pkgCache::DependencyData)); + DependencyData = AllocateInMap(); if (unlikely(DependencyData == 0)) return false; } @@ -1175,7 +1177,7 @@ bool pkgCacheGenerator::NewProvides(pkgCache::VerIterator &Ver, uint8_t const Flags) { // Get a structure - map_pointer const Provides = AllocateInMap(sizeof(pkgCache::Provides)); + auto const Provides = AllocateInMap(); if (unlikely(Provides == 0)) return false; ++Cache.HeaderP->ProvidesCount; @@ -1249,7 +1251,7 @@ bool pkgCacheGenerator::SelectReleaseFile(const string &File,const string &Site, return true; // Get some space for the structure - map_pointer const idxFile = AllocateInMap(sizeof(*CurrentRlsFile)); + auto const idxFile = AllocateInMap(); if (unlikely(idxFile == 0)) return false; CurrentRlsFile = Cache.RlsFileP + idxFile; @@ -1283,7 +1285,7 @@ bool pkgCacheGenerator::SelectFile(std::string const &File, { CurrentFile = nullptr; // Get some space for the structure - map_pointer const idxFile = AllocateInMap(sizeof(*CurrentFile)); + auto const idxFile = AllocateInMap(); if (unlikely(idxFile == 0)) return false; CurrentFile = Cache.PkgFileP + idxFile; diff --git a/apt-pkg/pkgcachegen.h b/apt-pkg/pkgcachegen.h index d088bca52..c042625c4 100644 --- a/apt-pkg/pkgcachegen.h +++ b/apt-pkg/pkgcachegen.h @@ -40,7 +40,10 @@ class APT_HIDDEN pkgCacheGenerator /*{{{*/ APT_HIDDEN map_stringitem_t WriteStringInMap(APT::StringView String) { return WriteStringInMap(String.data(), String.size()); }; APT_HIDDEN map_stringitem_t WriteStringInMap(const char *String); APT_HIDDEN map_stringitem_t WriteStringInMap(const char *String, const unsigned long &Len); - APT_HIDDEN map_pointer AllocateInMap(const unsigned long &size); + APT_HIDDEN uint32_t AllocateInMap(const unsigned long &size); + template map_pointer AllocateInMap() { + return map_pointer{AllocateInMap(sizeof(T))}; + } // Dirty hack for public users that do not use C++11 yet #if __cplusplus >= 201103L -- cgit v1.2.3