<div dir="auto"><div><br><div data-smartmail="gmail_signature"><br></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">пн, 17 февр. 2020 г., 17:06 Andrey Savchenko <<a href="mailto:bircoph@altlinux.org">bircoph@altlinux.org</a>>:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">On Mon, 17 Feb 2020 11:41:36 +0300 (MSK) Ivan Zakharyaschev wrote:<br>
> Появилась идея, что есть способ здесь не просто ответственность возложить <br>
> на программиста на словах (вызывать Allocate() с ненулевым аргументом), но <br>
> и формально предложить такой способ программирования, где не будет <br>
> возможности делать не так, как задумано.<br>
> <br>
> Да и попроще вызовы получаются.<br>
> <br>
> Ветка alloc-templates в <br>
> <a href="http://git.altlinux.org/people/imz/packages/apt.git" rel="noreferrer noreferrer" target="_blank">http://git.altlinux.org/people/imz/packages/apt.git</a><br>
> <br>
> (Развивая это, можно RawAllocate() превратить в какой-нибудь<br>
> template<typename T> RawAllocateArray<T>(unsigned long ItemCount),<br>
> там, правда, Pools нетривиально надо будет переделать.<br>
> <br>
> Пришлось, правда, перенести определение из .cc в .h и патчи дальнейшие <br>
> неудобно прикладывать; но можно их ifdef-ами обложить в .cc и заинклудить <br>
> в .h.)<br>
> <br>
> From 89499e5810575336e6a3b02d71d6c323321a0eca Mon Sep 17 00:00:00 2001<br>
> From: Ivan Zakharyaschev <<a href="mailto:imz@altlinux.org" target="_blank" rel="noreferrer">imz@altlinux.org</a>><br>
> Date: Fri, 14 Feb 2020 04:04:42 +0300<br>
> Subject: [PATCH] don't pass compile-time constants as an argument to<br>
> DynamicMMap::Allocate()<br>
> <br>
> The argument was used for the size of the item we want to allocate.<br>
> <br>
> Instead of an argument, use a parameter for the template DynamicMMap::Allocate()<br>
> which denotes the type of the item.<br>
> <br>
> Now, the truth of our assertion that the size is not zero is evident<br>
> (and checked by the compiler).<br>
> <br>
> Note that this change may also potentially lead to more optimized code<br>
> due to compile-time specialization for each particular size.<br>
> ---<br>
> apt/apt-pkg/contrib/mmap.h | 12 ++++++------<br>
> apt/apt-pkg/pkgcachegen.cc | 19 ++++++++++---------<br>
> apt/apt-pkg/pkgcachegen.h | 3 ++-<br>
> 3 files changed, 18 insertions(+), 16 deletions(-)<br>
> <br>
> diff --git a/apt/apt-pkg/contrib/mmap.h b/apt/apt-pkg/contrib/mmap.h<br>
> index 5b14ac9d9..7ec0fb8b1 100644<br>
> --- a/apt/apt-pkg/contrib/mmap.h<br>
> +++ b/apt/apt-pkg/contrib/mmap.h<br>
> @@ -94,7 +94,8 @@ class DynamicMMap : public MMap<br>
> <br>
> // Allocation<br>
> std::experimental::optional<unsigned long> RawAllocate(unsigned long Size,unsigned long Aln = 0);<br>
> - std::experimental::optional<unsigned long> Allocate(unsigned long ItemSize);<br>
> + template<typename T><br>
> + std::experimental::optional<unsigned long> Allocate();<br>
> std::experimental::optional<unsigned long> WriteString(const char *String,unsigned long Len = std::numeric_limits<unsigned long>::max());<br>
> inline std::experimental::optional<unsigned long> WriteString(const string &S) {return WriteString(S.c_str(),S.length());};<br>
> void UsePools(Pool &P,unsigned int Count) {Pools = &P; PoolCount = Count;};<br>
> @@ -107,17 +108,16 @@ class DynamicMMap : public MMap<br>
> /* Templates */<br>
> <br>
> #include <apt-pkg/error.h><br>
> -#include <cassert><br>
> <br>
> // DynamicMMap::Allocate - Pooled aligned allocation /*{{{*/<br>
> // ---------------------------------------------------------------------<br>
> /* This allocates an Item of size ItemSize so that it is aligned to its<br>
> size in the file. */<br>
> -std::experimental::optional<unsigned long> DynamicMMap::Allocate(unsigned long ItemSize)<br>
> +template<typename T><br>
> +std::experimental::optional<unsigned long> DynamicMMap::Allocate()<br>
> {<br>
> - assert(ItemSize != 0); /* Actually, we are always called with sizeof(...)<br>
> - compile-time non-zero constant as the argument.<br>
> - */<br>
> + constexpr unsigned long ItemSize = sizeof(T);<br>
> + static_assert(ItemSize != 0);<br>
<br>
Давайте не закладываться на C++17:<br>
<a href="https://en.cppreference.com/w/cpp/language/static_assert" rel="noreferrer noreferrer" target="_blank">https://en.cppreference.com/w/cpp/language/static_assert</a></blockquote></div></div><div dir="auto"><br></div><div dir="auto">+1.</div><div dir="auto"><br></div><div dir="auto">Чтобы не было проблем с бекпортированием. В том числе на e2k. </div><div dir="auto"><br></div><div dir="auto">Rgrds, Алексей</div><div dir="auto"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><br>
<br>
static_assert ( bool_constexpr , message ) (since C++11)<br>
static_assert ( bool_constexpr ) (since C++17)<br>
<br>
Так что прошу использовать первую форму (C++11 с сообщением)<br>
и убедится, что явно задан как минимум -std=c++11/gnu++11.<br>
<br>
> <br>
> // Look for a matching pool entry<br>
> Pool *I;<br>
> diff --git a/apt/apt-pkg/pkgcachegen.cc b/apt/apt-pkg/pkgcachegen.cc<br>
> index 4bbab07bf..2a75f31e6 100644<br>
> --- a/apt/apt-pkg/pkgcachegen.cc<br>
> +++ b/apt/apt-pkg/pkgcachegen.cc<br>
> @@ -112,8 +112,9 @@ std::experimental::optional<unsigned long> pkgCacheGenerator::WriteStringInMap(c<br>
> return Map.WriteString(String);<br>
> }<br>
> /*}}}*/<br>
> -std::experimental::optional<unsigned long> pkgCacheGenerator::AllocateInMap(unsigned long size) {/*{{{*/<br>
> - return Map.Allocate(size);<br>
> +template<typename T><br>
> +std::experimental::optional<unsigned long> pkgCacheGenerator::AllocateInMap() {/*{{{*/<br>
> + return Map.Allocate<T>();<br>
> }<br>
> /*}}}*/<br>
> // CacheGenerator::MergeList - Merge the package list /*{{{*/<br>
> @@ -376,7 +377,7 @@ bool pkgCacheGenerator::NewPackage(pkgCache::PkgIterator &Pkg, const string &Nam<br>
> #endif<br>
> <br>
> // Get a structure<br>
> - const auto Package = AllocateInMap(sizeof(pkgCache::Package));<br>
> + const auto Package = AllocateInMap<pkgCache::Package>();<br>
> const auto idxName = WriteStringInMap(Name);<br>
> if ((!Package) || (!idxName))<br>
> return false;<br>
> @@ -429,7 +430,7 @@ bool pkgCacheGenerator::NewFileVer(pkgCache::VerIterator &Ver,<br>
> return true;<br>
> <br>
> // Get a structure<br>
> - const auto VerFile = AllocateInMap(sizeof(pkgCache::VerFile));<br>
> + const auto VerFile = AllocateInMap<pkgCache::VerFile>();<br>
> if (!VerFile)<br>
> return false;<br>
> <br>
> @@ -460,7 +461,7 @@ std::experimental::optional<unsigned long> pkgCacheGenerator::NewVersion(pkgCach<br>
> unsigned long Next)<br>
> {<br>
> // Get a structure<br>
> - const auto Version = AllocateInMap(sizeof(pkgCache::Version));<br>
> + const auto Version = AllocateInMap<pkgCache::Version>();<br>
> const auto idxVerStr = WriteStringInMap(VerStr);<br>
> if ((!Version) || (!idxVerStr))<br>
> return std::experimental::nullopt;<br>
> @@ -487,7 +488,7 @@ bool pkgCacheGenerator::ListParser::NewDepends(pkgCache::VerIterator &Ver,<br>
> pkgCache &Cache = Owner->Cache;<br>
> <br>
> // Get a structure<br>
> - const auto Dependency = Owner->AllocateInMap(sizeof(pkgCache::Dependency));<br>
> + const auto Dependency = Owner->AllocateInMap<pkgCache::Dependency>();<br>
> if (!Dependency)<br>
> return false;<br>
> <br>
> @@ -574,7 +575,7 @@ bool pkgCacheGenerator::ListParser::NewProvides(pkgCache::VerIterator &Ver,<br>
> }<br>
> }<br>
> // Get a structure<br>
> - const auto Provides = Owner->AllocateInMap(sizeof(pkgCache::Provides));<br>
> + const auto Provides = Owner->AllocateInMap<pkgCache::Provides>();<br>
> if (!Provides)<br>
> return false;<br>
> /* Note:<br>
> @@ -618,7 +619,7 @@ bool pkgCacheGenerator::SelectFile(const string &File, const string &Site,<br>
> unsigned long Flags)<br>
> {<br>
> // Get some space for the structure<br>
> - const auto idxFile = AllocateInMap(sizeof(*CurrentFile));<br>
> + const auto idxFile = AllocateInMap<decltype(*CurrentFile)>();<br>
> const auto idxFileName = WriteStringInMap(File);<br>
> const auto idxSite = WriteUniqString(Site);<br>
> const auto idxIndexType = WriteUniqString(Index.GetType()->Label);<br>
> @@ -676,7 +677,7 @@ std::experimental::optional<unsigned long> pkgCacheGenerator::WriteUniqString(co<br>
> }<br>
> <br>
> // Get a structure<br>
> - const auto Item = AllocateInMap(sizeof(pkgCache::StringItem));<br>
> + const auto Item = AllocateInMap<pkgCache::StringItem>();<br>
> const auto idxString = WriteStringInMap(S, Size);<br>
> if ((!Item) || (!idxString))<br>
> return std::experimental::nullopt;<br>
> diff --git a/apt/apt-pkg/pkgcachegen.h b/apt/apt-pkg/pkgcachegen.h<br>
> index b789177ef..73252802b 100644<br>
> --- a/apt/apt-pkg/pkgcachegen.h<br>
> +++ b/apt/apt-pkg/pkgcachegen.h<br>
> @@ -39,7 +39,8 @@ class pkgCacheGenerator<br>
> std::experimental::optional<unsigned long> WriteStringInMap(const std::string &String) { return WriteStringInMap(String.c_str(), String.length()); };<br>
> std::experimental::optional<unsigned long> WriteStringInMap(const char *String);<br>
> std::experimental::optional<unsigned long> WriteStringInMap(const char *String, unsigned long Len);<br>
> - std::experimental::optional<unsigned long> AllocateInMap(unsigned long size);<br>
> + template<typename T><br>
> + std::experimental::optional<unsigned long> AllocateInMap();<br>
> <br>
> public:<br>
> <br>
<br>
<br>
Best regards,<br>
Andrew Savchenko<br>
_______________________________________________<br>
Devel mailing list<br>
<a href="mailto:Devel@lists.altlinux.org" target="_blank" rel="noreferrer">Devel@lists.altlinux.org</a><br>
<a href="https://lists.altlinux.org/mailman/listinfo/devel" rel="noreferrer noreferrer" target="_blank">https://lists.altlinux.org/mailman/listinfo/devel</a></blockquote></div></div></div>