From owner-freebsd-hackers@freebsd.org Wed Sep 14 12:54:46 2016 Return-Path: Delivered-To: freebsd-hackers@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 53CDDBDA361 for ; Wed, 14 Sep 2016 12:54:46 +0000 (UTC) (envelope-from crest@rlwinm.de) Received: from smtp.rlwinm.de (smtp.rlwinm.de [148.251.233.239]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 1C67A1E4D for ; Wed, 14 Sep 2016 12:54:45 +0000 (UTC) (envelope-from crest@rlwinm.de) Received: from vader9.bultmann.eu (unknown [87.253.189.132]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.rlwinm.de (Postfix) with ESMTPSA id 6882B10D20 for ; Wed, 14 Sep 2016 14:54:37 +0200 (CEST) Subject: Re: Is replacing alloca(3) where possible a good thing to do? To: freebsd-hackers@freebsd.org References: <3fe9ba0e-0089-a59c-a09e-8c6f8b74b6bc@openmailbox.org> From: Jan Bramkamp Message-ID: Date: Wed, 14 Sep 2016 14:54:36 +0200 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:45.0) Gecko/20100101 Thunderbird/45.2.0 MIME-Version: 1.0 In-Reply-To: <3fe9ba0e-0089-a59c-a09e-8c6f8b74b6bc@openmailbox.org> Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 14 Sep 2016 12:54:46 -0000 On 14/09/16 12:53, twilight wrote: > Hello, > > I again, > in cddl/* alloca(3) is used very intensively for creating dynamic arrays. > But, well, it's kinda obsolete and sometimes not safe and portable. > Is replacing alloca(3) with dynamic arrays a good thing? Or should > everything be left as it is? > > Thanks in advance. alloca() and VLAs aren't completely interchangeable e.g. alloca() should return pointers with the same alignment as malloc while a VLA is just correctly aligned for its member type. Some types can't be stored in VLAs without casting e.g. if you want to store an incomplete type like a struct ending with a zero sized array you can't put such a struct in a union with a char array to allocate space for the array elements. In such cases alloca() is easier to read and more portable. You won't notice the unaligned accesses on a x86 CPU but they would trap on a SPARC or ARM (<= ARMv5). Other CPUs silently round down your unaligned pointer to the next natural aligned address.