From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: ludo@gnu.org (Ludovic =?UTF-8?Q?Court=C3=A8s?=) Newsgroups: gmane.lisp.guile.bugs Subject: bug#14599: An option to make vector allocation aligned Date: Wed, 12 Jun 2013 16:59:01 +0200 Message-ID: <87a9mvide2.fsf@gnu.org> References: <51B87998.9060402@email.de> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Trace: ger.gmane.org 1371049233 3681 80.91.229.3 (12 Jun 2013 15:00:33 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Wed, 12 Jun 2013 15:00:33 +0000 (UTC) Cc: 14599@debbugs.gnu.org, request@debbugs.gnu.org To: Jan Schukat Original-X-From: bug-guile-bounces+guile-bugs=m.gmane.org@gnu.org Wed Jun 12 17:00:32 2013 Return-path: Envelope-to: guile-bugs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1UmmXD-0007oh-1p for guile-bugs@m.gmane.org; Wed, 12 Jun 2013 17:00:27 +0200 Original-Received: from localhost ([::1]:41308 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UmmXC-0004OJ-Fr for guile-bugs@m.gmane.org; Wed, 12 Jun 2013 11:00:26 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:36147) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UmmX4-0004NW-QF for bug-guile@gnu.org; Wed, 12 Jun 2013 11:00:24 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1UmmWv-0006sK-VZ for bug-guile@gnu.org; Wed, 12 Jun 2013 11:00:18 -0400 Original-Received: from debbugs.gnu.org ([140.186.70.43]:54754) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UmmWv-0006sD-SG for bug-guile@gnu.org; Wed, 12 Jun 2013 11:00:09 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.80) (envelope-from ) id 1UmmWv-0000Ly-6W for bug-guile@gnu.org; Wed, 12 Jun 2013 11:00:09 -0400 X-Loop: help-debbugs@gnu.org Resent-From: ludo@gnu.org (Ludovic =?UTF-8?Q?Court=C3=A8s?=) Original-Sender: "Debbugs-submit" Resent-CC: bug-guile@gnu.org Resent-Date: Wed, 12 Jun 2013 15:00:06 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 14599 X-GNU-PR-Package: guile X-GNU-PR-Keywords: Original-Received: via spool by 14599-submit@debbugs.gnu.org id=B14599.13710491471133 (code B ref 14599); Wed, 12 Jun 2013 15:00:06 +0000 Original-Received: (at 14599) by debbugs.gnu.org; 12 Jun 2013 14:59:07 +0000 Original-Received: from localhost ([127.0.0.1]:58986 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.80) (envelope-from ) id 1UmmVu-0000I9-Il for submit@debbugs.gnu.org; Wed, 12 Jun 2013 10:59:06 -0400 Original-Received: from hera.aquilenet.fr ([141.255.128.1]:46805) by debbugs.gnu.org with esmtp (Exim 4.80) (envelope-from ) id 1UmmVr-0000Hc-L7; Wed, 12 Jun 2013 10:59:05 -0400 Original-Received: from localhost (localhost [127.0.0.1]) by hera.aquilenet.fr (Postfix) with ESMTP id 28E87CDD; Wed, 12 Jun 2013 16:59:02 +0200 (CEST) Original-Received: from hera.aquilenet.fr ([127.0.0.1]) by localhost (hera.aquilenet.fr [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id OZr31Gj4XSwm; Wed, 12 Jun 2013 16:59:02 +0200 (CEST) Original-Received: from pluto (eduroam-3-182.epfl.ch [192.33.205.182]) by hera.aquilenet.fr (Postfix) with ESMTPSA id D0A9ECC1; Wed, 12 Jun 2013 16:59:01 +0200 (CEST) In-Reply-To: <51B87998.9060402@email.de> (Jan Schukat's message of "Wed, 12 Jun 2013 15:37:28 +0200") User-Agent: Gnus/5.130007 (Ma Gnus v0.7) Emacs/24.3 (gnu/linux) X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.15 Precedence: list X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6.x X-Received-From: 140.186.70.43 X-BeenThere: bug-guile@gnu.org List-Id: "Bug reports for GUILE, GNU's Ubiquitous Extension Language" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-guile-bounces+guile-bugs=m.gmane.org@gnu.org Original-Sender: bug-guile-bounces+guile-bugs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.lisp.guile.bugs:7168 Archived-At: severity 14599 wishlist thanks Hi! Jan Schukat skribis: > If you want to access native uniform vectors from c, sometimes you > really want guarantees about the alignment. [...] > This isn't necessarily true for vectors created from pre-existing > buffers (the take_*vector functions), but there you have control over > the pointer you pass, so you can make it true if needed. > > So if there is interest, maybe this could be integrated into the build > system as a configuration like this: > > > --- libguile/bytevectors.c 2013-04-11 02:16:30.000000000 +0200 > +++ bytevectors.c 2013-06-12 14:45:16.000000000 +0200 > @@ -223,10 +223,18 @@ > > c_len =3D len * (scm_i_array_element_type_sizes[element_type] / 8); > > +#ifdef SCM_VECTOR_ALIGN > + contents =3D scm_gc_malloc_pointerless > (SCM_BYTEVECTOR_HEADER_BYTES + c_len + SCM_VECTOR_ALIGN, > + SCM_GC_BYTEVECTOR); > + ret =3D PTR2SCM (contents); > + contents +=3D SCM_BYTEVECTOR_HEADER_BYTES; > + contents +=3D (addr + (SCM_VECTOR_ALIGN - 1)) & -SCM_VECTOR_ALIGN; > +#else > contents =3D scm_gc_malloc_pointerless > (SCM_BYTEVECTOR_HEADER_BYTES + c_len, > SCM_GC_BYTEVECTOR); > ret =3D PTR2SCM (contents); > contents +=3D SCM_BYTEVECTOR_HEADER_BYTES; > +#endif > > SCM_BYTEVECTOR_SET_LENGTH (ret, c_len); > SCM_BYTEVECTOR_SET_CONTENTS (ret, contents); I don=E2=80=99t think it should be a compile-time option, because it would = be inflexible and inconvenient. Instead, I would suggest using the scm_take_ functions if allocating from C, as you noted. In Scheme, I came up with the following hack: --8<---------------cut here---------------start------------->8--- (use-modules (system foreign) (rnrs bytevectors) (ice-9 match)) (define (memalign len alignment) (let* ((b (make-bytevector (+ len alignment))) (p (bytevector->pointer b)) (a (pointer-address p))) (match (modulo a alignment) (0 b) (padding (let ((p (make-pointer (+ a (- alignment padding))))) ;; XXX: Keep a weak reference to B or it can be collected ;; behind our back. (pointer->bytevector p len)))))) --8<---------------cut here---------------end--------------->8--- Not particularly elegant, but it does the job. ;-) Do you think there=E2=80=99s additional support that should be provided? Thanks, Ludo=E2=80=99.