From: Ivan Zakharyaschev <imz@altlinux.org>
To: ALT Linux Team development discussions <devel@lists.altlinux.org>
Subject: Re: [devel] re APT patch with impossible error on "Can't allocate an item of size zero"
Date: Mon, 17 Feb 2020 20:48:39 +0300 (MSK)
Message-ID: <alpine.LFD.2.20.2002172045330.6363@imap.altlinux.org> (raw)
In-Reply-To: <20200217170540.d08bec530dc90ee5fbe3f822@altlinux.org>
[-- Attachment #1: Type: text/plain, Size: 3412 bytes --]
Hello!
On Mon, 17 Feb 2020, Andrey Savchenko wrote:
> On Mon, 17 Feb 2020 11:41:36 +0300 (MSK) Ivan Zakharyaschev wrote:
> > Появилась идея, что есть способ здесь не просто ответственность возложить
> > на программиста на словах (вызывать Allocate() с ненулевым аргументом), но
> > и формально предложить такой способ программирования, где не будет
> > возможности делать не так, как задумано.
> >
> > Да и попроще вызовы получаются.
> >
> > Ветка alloc-templates в
> > http://git.altlinux.org/people/imz/packages/apt.git
> >
> > (Развивая это, можно RawAllocate() превратить в какой-нибудь
> > template<typename T> RawAllocateArray<T>(unsigned long ItemCount),
> > там, правда, Pools нетривиально надо будет переделать.
> >
> > Пришлось, правда, перенести определение из .cc в .h и патчи дальнейшие
> > неудобно прикладывать; но можно их ifdef-ами обложить в .cc и заинклудить
> > в .h.)
> >
> > From 89499e5810575336e6a3b02d71d6c323321a0eca Mon Sep 17 00:00:00 2001
> > From: Ivan Zakharyaschev <imz@altlinux.org>
> > Date: Fri, 14 Feb 2020 04:04:42 +0300
> > Subject: [PATCH] don't pass compile-time constants as an argument to
> > DynamicMMap::Allocate()
> >
> > The argument was used for the size of the item we want to allocate.
> >
> > Instead of an argument, use a parameter for the template DynamicMMap::Allocate()
> > which denotes the type of the item.
> >
> > Now, the truth of our assertion that the size is not zero is evident
> > (and checked by the compiler).
> >
> > Note that this change may also potentially lead to more optimized code
> > due to compile-time specialization for each particular size.
> > ---
> > apt/apt-pkg/contrib/mmap.h | 12 ++++++------
> > apt/apt-pkg/pkgcachegen.cc | 19 ++++++++++---------
> > apt/apt-pkg/pkgcachegen.h | 3 ++-
> > 3 files changed, 18 insertions(+), 16 deletions(-)
> > -std::experimental::optional<unsigned long> DynamicMMap::Allocate(unsigned long ItemSize)
> > +template<typename T>
> > +std::experimental::optional<unsigned long> DynamicMMap::Allocate()
> > {
> > - assert(ItemSize != 0); /* Actually, we are always called with sizeof(...)
> > - compile-time non-zero constant as the argument.
> > - */
> > + constexpr unsigned long ItemSize = sizeof(T);
> > + static_assert(ItemSize != 0);
>
> Давайте не закладываться на C++17:
> https://en.cppreference.com/w/cpp/language/static_assert
>
> static_assert ( bool_constexpr , message ) (since C++11)
> static_assert ( bool_constexpr ) (since C++17)
>
> Так что прошу использовать первую форму (C++11 с сообщением)
> и убедится, что явно задан как минимум -std=c++11/gnu++11.
Спасибо за замечание!
Я об этом просто не думал пока, хотел проиллюстрировать идею
компилирующимся кодом (на x86_64).
next prev parent reply other threads:[~2020-02-17 17:48 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-02-13 2:08 Ivan Zakharyaschev
2020-02-13 2:43 ` Ivan Zakharyaschev
2020-02-13 12:59 ` Aleksei Nikiforov
2020-02-13 14:10 ` Ivan Zakharyaschev
2020-02-13 17:05 ` Ivan Zakharyaschev
2020-02-17 8:41 ` Ivan Zakharyaschev
2020-02-17 14:05 ` Andrey Savchenko
2020-02-17 17:48 ` Ivan Zakharyaschev [this message]
2020-02-17 21:35 ` Dmitry V. Levin
2020-02-17 21:58 ` Ivan Zakharyaschev
2020-02-13 14:51 ` Ivan Zakharyaschev
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=alpine.LFD.2.20.2002172045330.6363@imap.altlinux.org \
--to=imz@altlinux.org \
--cc=devel@lists.altlinux.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
ALT Linux Team development discussions
This inbox may be cloned and mirrored by anyone:
git clone --mirror http://lore.altlinux.org/devel/0 devel/git/0.git
# If you have public-inbox 1.1+ installed, you may
# initialize and index your mirror using the following commands:
public-inbox-init -V2 devel devel/ http://lore.altlinux.org/devel \
devel@altlinux.org devel@altlinux.ru devel@lists.altlinux.org devel@lists.altlinux.ru devel@linux.iplabs.ru mandrake-russian@linuxteam.iplabs.ru sisyphus@linuxteam.iplabs.ru
public-inbox-index devel
Example config snippet for mirrors.
Newsgroup available over NNTP:
nntp://lore.altlinux.org/org.altlinux.lists.devel
AGPL code for this site: git clone https://public-inbox.org/public-inbox.git