From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Date: Mon, 17 Feb 2020 20:48:39 +0300 (MSK) From: Ivan Zakharyaschev To: ALT Linux Team development discussions In-Reply-To: <20200217170540.d08bec530dc90ee5fbe3f822@altlinux.org> Message-ID: References: <2007feb6-b08c-b463-a0fe-d51a264bee45@altlinux.org> <20200217170540.d08bec530dc90ee5fbe3f822@altlinux.org> User-Agent: Alpine 2.20 (LFD 67 2015-01-07) MIME-Version: 1.0 Content-Type: multipart/mixed; BOUNDARY="1807885841-903263417-1581961719=:6363" Subject: Re: [devel] re APT patch with impossible error on "Can't allocate an item of size zero" X-BeenThere: devel@lists.altlinux.org X-Mailman-Version: 2.1.12 Precedence: list Reply-To: ALT Linux Team development discussions List-Id: ALT Linux Team development discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 17 Feb 2020 17:48:40 -0000 Archived-At: List-Archive: List-Post: This message is in MIME format. The first part should be readable text, while the remaining parts are likely unreadable without MIME-aware tools. --1807885841-903263417-1581961719=:6363 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8BIT 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 RawAllocateArray(unsigned long ItemCount), > > там, правда, Pools нетривиально надо будет переделать. > > > > Пришлось, правда, перенести определение из .cc в .h и патчи дальнейшие > > неудобно прикладывать; но можно их ifdef-ами обложить в .cc и заинклудить > > в .h.) > > > > From 89499e5810575336e6a3b02d71d6c323321a0eca Mon Sep 17 00:00:00 2001 > > From: Ivan Zakharyaschev > > 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 DynamicMMap::Allocate(unsigned long ItemSize) > > +template > > +std::experimental::optional 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). --1807885841-903263417-1581961719=:6363--