From owner-freebsd-hackers@FreeBSD.ORG Sun Nov 25 21:53:53 2012 Return-Path: Delivered-To: freebsd-hackers@FreeBSD.org Received: from mx1.freebsd.org (mx1.freebsd.org [69.147.83.52]) by hub.freebsd.org (Postfix) with ESMTP id C14C9432; Sun, 25 Nov 2012 21:53:53 +0000 (UTC) (envelope-from jilles@stack.nl) Received: from mx1.stack.nl (unknown [IPv6:2001:610:1108:5012::107]) by mx1.freebsd.org (Postfix) with ESMTP id 5C2F78FC12; Sun, 25 Nov 2012 21:53:53 +0000 (UTC) Received: from snail.stack.nl (snail.stack.nl [IPv6:2001:610:1108:5010::131]) by mx1.stack.nl (Postfix) with ESMTP id 595111200BA; Sun, 25 Nov 2012 22:53:48 +0100 (CET) Received: by snail.stack.nl (Postfix, from userid 1677) id 426302848C; Sun, 25 Nov 2012 22:53:48 +0100 (CET) Date: Sun, 25 Nov 2012 22:53:48 +0100 From: Jilles Tjoelker To: freebsd-hackers@FreeBSD.org Subject: Incorrect use of posix_memalign() (was: Re: svn commit: r243405 - in stable/9: include lib/libc/stdlib) Message-ID: <20121125215348.GB65833@stack.nl> References: <201211221519.qAMFJroe007462@svn.freebsd.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <201211221519.qAMFJroe007462@svn.freebsd.org> User-Agent: Mutt/1.5.21 (2010-09-15) Cc: Ed Schouten X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 25 Nov 2012 21:53:53 -0000 On Thu, Nov 22, 2012 at 03:19:53PM +0000, Ed Schouten wrote: > Author: ed > Date: Thu Nov 22 15:19:53 2012 > New Revision: 243405 > URL: http://svnweb.freebsd.org/changeset/base/243405 > Log: > MFC r229848: > Add aligned_alloc(3). > The C11 folks reinvented the wheel by introducing an aligned version of > malloc(3) called aligned_alloc(3), instead of posix_memalign(3). Instead > of returning the allocation by reference, it returns the address, just > like malloc(3). > I'm MFCing this now, as it seems aligned_alloc(3) is needed to make the > new version of libc++ work, which was merged back to FreeBSD 9 in r243376. The C11 committee knew about posix_memalign() and had several reasons for creating a new function; see for example http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1397.htm . In particular, posix_memalign() is a little annoying to use correctly, often requiring a temporary variable of type void *. It is tempting to do something like error = posix_memalign((void **)&some_ptr, aln, sz); and some FreeBSD code does this, but it violates strict-aliasing. A further mostly theoretical objection is that assumes that the representation of some_ptr and void * are compatible which C does not guarantee. The problem can be fixed by adding the temporary pointer variable like void *tmp_ptr; error = posix_memalign(&tmp_ptr, aln, sz); some_ptr = tmp_ptr; or by using aligned_alloc() instead of posix_memalign() some_ptr = aligned_alloc(aln, sz); with error checking against some_ptr instead of error. -- Jilles Tjoelker