From owner-freebsd-current@FreeBSD.ORG Thu Mar 6 09:48:44 2008 Return-Path: Delivered-To: current@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id BC238106566B; Thu, 6 Mar 2008 09:48:44 +0000 (UTC) (envelope-from kostikbel@gmail.com) Received: from relay02.kiev.sovam.com (relay02.kiev.sovam.com [62.64.120.197]) by mx1.freebsd.org (Postfix) with ESMTP id 5526D8FC1A; Thu, 6 Mar 2008 09:48:44 +0000 (UTC) (envelope-from kostikbel@gmail.com) Received: from [212.82.216.226] (helo=skuns.kiev.zoral.com.ua) by relay02.kiev.sovam.com with esmtps (TLSv1:AES256-SHA:256) (Exim 4.67) (envelope-from ) id 1JXCiM-000Lqr-UB; Thu, 06 Mar 2008 11:48:42 +0200 Received: from deviant.kiev.zoral.com.ua (root@deviant.kiev.zoral.com.ua [10.1.1.148]) by skuns.kiev.zoral.com.ua (8.14.2/8.14.2) with ESMTP id m269mPUs091392 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Thu, 6 Mar 2008 11:48:25 +0200 (EET) (envelope-from kostikbel@gmail.com) Received: from deviant.kiev.zoral.com.ua (kostik@localhost [127.0.0.1]) by deviant.kiev.zoral.com.ua (8.14.2/8.14.2) with ESMTP id m269mBWr014761; Thu, 6 Mar 2008 11:48:11 +0200 (EET) (envelope-from kostikbel@gmail.com) Received: (from kostik@localhost) by deviant.kiev.zoral.com.ua (8.14.2/8.14.2/Submit) id m269mAPn014760; Thu, 6 Mar 2008 11:48:10 +0200 (EET) (envelope-from kostikbel@gmail.com) X-Authentication-Warning: deviant.kiev.zoral.com.ua: kostik set sender to kostikbel@gmail.com using -f Date: Thu, 6 Mar 2008 11:48:10 +0200 From: Kostik Belousov To: Tim Kientzle Message-ID: <20080306094810.GM57756@deviant.kiev.zoral.com.ua> References: <200802280409.m1S498YJ062561@repoman.freebsd.org> <20080228231522.F57564@delplex.bde.org> <20080229141527.N59899@delplex.bde.org> <18375.43955.908262.696223@hergotha.csail.mit.edu> <47C8D0AB.20506@freebsd.org> <20080302062610.V66431@delplex.bde.org> <47CA2192.8020802@FreeBSD.org> <20080303065527.K69705@delplex.bde.org> <47CF4500.2050509@freebsd.org> Mime-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="X95ZMRfl5Jak4ILv" Content-Disposition: inline In-Reply-To: <47CF4500.2050509@freebsd.org> User-Agent: Mutt/1.4.2.3i X-Virus-Scanned: ClamAV version 0.91.2, clamav-milter version 0.91.2 on skuns.kiev.zoral.com.ua X-Virus-Status: Clean X-Spam-Status: No, score=-4.4 required=5.0 tests=ALL_TRUSTED,AWL,BAYES_00 autolearn=ham version=3.2.4 X-Spam-Checker-Version: SpamAssassin 3.2.4 (2008-01-01) on skuns.kiev.zoral.com.ua X-Scanner-Signature: 021e97fb5facab6d3ae496d7b6c27559 X-DrWeb-checked: yes X-SpamTest-Envelope-From: kostikbel@gmail.com X-SpamTest-Group-ID: 00000000 X-SpamTest-Info: Profiles 2362 [Mar 5 2008] X-SpamTest-Info: helo_type=3 X-SpamTest-Info: {received from trusted relay: not dialup} X-SpamTest-Method: none X-SpamTest-Method: Local Lists X-SpamTest-Rate: 0 X-SpamTest-Status: Not detected X-SpamTest-Status-Extended: not_detected X-SpamTest-Version: SMTP-Filter Version 3.0.0 [0255], KAS30/Release Cc: Garrett Wollman , Jason Evans , Bruce Evans , current@freebsd.org Subject: Re: Breaking the crt1.o -> atexit() -> malloc() dependency X-BeenThere: freebsd-current@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Discussions about the use of FreeBSD-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 06 Mar 2008 09:48:44 -0000 --X95ZMRfl5Jak4ILv Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Wed, Mar 05, 2008 at 05:12:32PM -0800, Tim Kientzle wrote: > There was some recent discussion on the commit mailing > list about how to disentangle crt1.o from malloc(). >=20 > Here's a design that I think addresses all of the > issues people raised, including the POSIX requirement > that atexit() always be able to support 32 registrations. > It does it without using sbrk() or mmap(), either. >=20 > The basic idea is to lift the malloc() call up into > atexit() and have atexit_register() use statically-allocated > storage if atexit() didn't provide dynamically-allocated > storage. >=20 > This basically changes atexit() to something like this pseudocode: >=20 > int atexit(void (*function)(void)) > { > struct atexit *storage =3D malloc(sizeof(struct atexit)); >=20 > /* Note: If malloc() fails, __atexit_register will try > * to statically allocate, so we don't check here > * for malloc() failure. */ > return __atexit_register(function, storage); > } >=20 > Then atexit_register either uses the block that was provided > or grabs an item from a static pool if there wasn't one: >=20 > /* 32 required by POSIX plus a few for crt1.o */ > static struct atexit pool[40]; >=20 > int atexit_register(void (*function)(void), struct atexit *storage) > { > if (storage =3D=3D NULL) { > storage =3D ... next item from static pool ... > } > storage.func =3D function; > ... add storage block to linked list ... > } >=20 > Avoiding free() from the low-level code is a little trickier > but I think it can be done by having the low-level code > put (dynamically-allocated) blocks back onto a free list > and having the higher-level atexit() release that list > on the next registration. This should handle the case > of a dynamic library being repeatedly loaded and unloaded. > Of course, it's unnecessary to release the atexit storage > on program exit. >=20 > In particular, crt1.o can then call atexit_register(f, NULL) > to register its exit functions without creating a dependency on > malloc. >=20 > This does require that atexit() and atexit_register() be in > separate source files, but I think it addresses all of the other > concerns people have raised. I mostly agree with proposal, but there is also __cxa_atexit(). And, besides the issue of the size of the static linked executables, there is more exposed problem of atexit() memory leaks. See http://lists.freebsd.org/pipermail/freebsd-stable/2008-February/040644.html --X95ZMRfl5Jak4ILv Content-Type: application/pgp-signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.8 (FreeBSD) iEYEARECAAYFAkfPvdoACgkQC3+MBN1Mb4ijfQCfWyElBIZX5aJ+ifTi2v0KrLCQ mzkAoKtdALPkHgCPDmBqDh3tXDBH8N3O =6o82 -----END PGP SIGNATURE----- --X95ZMRfl5Jak4ILv--