From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Eli Zaretskii Newsgroups: gmane.emacs.devel Subject: Re: sorting in C Date: Sun, 27 Feb 2022 11:29:29 +0200 Message-ID: <83r17oodxi.fsf@gnu.org> References: <87ilt7bokp.fsf@ust.hk> <83tucrt75y.fsf@gnu.org> <8735kakymb.fsf@ust.hk> <835yp5u5h7.fsf@gnu.org> <87ee3thhh3.fsf@ust.hk> <83zgmhsp13.fsf@gnu.org> <8735k97k4b.fsf@ust.hk> <83wnhlsm3m.fsf@gnu.org> <87pmn9gp5q.fsf@ust.hk> <87h78l9h7x.fsf@ust.hk> <83zgmcojj5.fsf@gnu.org> <87o82sfzc6.fsf@ust.hk> Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="6463"; mail-complaints-to="usenet@ciao.gmane.io" Cc: emacs-devel@gnu.org To: Andrew Cohen Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Sun Feb 27 10:31:04 2022 Return-path: Envelope-to: ged-emacs-devel@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nOFtP-0001UX-MJ for ged-emacs-devel@m.gmane-mx.org; Sun, 27 Feb 2022 10:31:03 +0100 Original-Received: from localhost ([::1]:32852 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nOFtO-00019d-Ai for ged-emacs-devel@m.gmane-mx.org; Sun, 27 Feb 2022 04:31:02 -0500 Original-Received: from eggs.gnu.org ([209.51.188.92]:60726) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nOFs7-0000Bi-0v for emacs-devel@gnu.org; Sun, 27 Feb 2022 04:29:43 -0500 Original-Received: from [2001:470:142:3::e] (port=47042 helo=fencepost.gnu.org) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nOFs5-0003OF-TG; Sun, 27 Feb 2022 04:29:41 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=gnu.org; s=fencepost-gnu-org; h=References:Subject:In-Reply-To:To:From:Date: mime-version; bh=BePks+aJx5AO8ior1jj4nTBm7yhO7s3K2a660glNQB8=; b=gMG0jTFSgRRp yWs2uNJZNv/yJwV/VDmxjTVVIfWwPRqg7xfNQZVKDz6k4O5H9j0HXWpJK1/ICQTsTzh/uNPTa3suQ HtbQ2k1nVcRRwTXGU+wQ1cS9zZHIYHzsikWB2PvCyphct1yTE0WWZIDeb/Nd1iJr9+zHdMg7zikVD RDF/Jl9jqyM+cGtJIsreCl0R0RyR0F6v6UVKPNBBRKurcA2jSne6I1Wh4teIByVMZ5c7s5MOQHfvu Yz+vc2SQBqzH074FLqhwAGpaLx+X5eoJvhpOLU01aIpDgXFUs2DHJzk9ji2NWhTCZs8/pYSQlW1Kb CaM8v26RKHrNl2ELrSEl2Q==; Original-Received: from [87.69.77.57] (port=1135 helo=home-c4e4a596f7) by fencepost.gnu.org with esmtpsa (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nOFs5-0001Gn-Bu; Sun, 27 Feb 2022 04:29:41 -0500 In-Reply-To: <87o82sfzc6.fsf@ust.hk> (message from Andrew Cohen on Sun, 27 Feb 2022 17:11:53 +0800) X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Original-Sender: "Emacs-devel" Xref: news.gmane.io gmane.emacs.devel:286714 Archived-At: > From: Andrew Cohen > Date: Sun, 27 Feb 2022 17:11:53 +0800 > > EZ> However, from your description, it doesn't sound like the more > EZ> optimal approach of allocating dynamically is much more > EZ> complicated. In particular, what Mattias said should be easy > EZ> using the unwind-protect machinery we already have (and use in > EZ> many similar situations). See the calls to > EZ> record_unwind_protect_ptr whose first argument is 'xfree'. We > EZ> also have reallocation routines ready to be used. > > That is how I am handling it now, but I'm not sure if I have it right > (sorry for the naive question): > > When I need new memory I call > > : specpdl_ref sa_count = SPECPDL_INDEX (); > : a = (Lisp_Object *) record_xmalloc (need * sizeof (Lisp_Object)); > > and I save =sa_count=; I guess =record_xmalloc= handles freeing the > memory on exception. Later during the sorting process I free the memory > explicitly with > > : safe_free (sa_count) > > Does this seem right? (Probably, since I've been running this way for > awhile and would have expected lots of problems if I weren't allocating > and freeing the memory :)) I'd rather you didn't use safe_free, since that is for SAFE_ALLOCA etc. Just use unbind_to directly, like we do elsewhere where record_xmalloc is used.