From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: taylanbayirli@gmail.com (Taylan Ulrich =?utf-8?Q?Bay=C4=B1rl=C4=B1?= =?utf-8?Q?=2FKammer?=) Newsgroups: gmane.lisp.guile.user Subject: Re: Bytestructures: a "type system" for bytevectors Date: Mon, 31 Aug 2015 15:50:00 +0200 Message-ID: <8737yzftyv.fsf@T420.taylan> References: <87r3mkg2iy.fsf@T420.taylan> 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 1441029020 24251 80.91.229.3 (31 Aug 2015 13:50:20 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Mon, 31 Aug 2015 13:50:20 +0000 (UTC) To: guile-user@gnu.org Original-X-From: guile-user-bounces+guile-user=m.gmane.org@gnu.org Mon Aug 31 15:50:20 2015 Return-path: Envelope-to: guile-user@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 1ZWPTU-0005di-GN for guile-user@m.gmane.org; Mon, 31 Aug 2015 15:50:16 +0200 Original-Received: from localhost ([::1]:37463 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZWPTT-00031X-VV for guile-user@m.gmane.org; Mon, 31 Aug 2015 09:50:15 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:55213) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZWPTJ-0002zv-W4 for guile-user@gnu.org; Mon, 31 Aug 2015 09:50:06 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ZWPTG-000507-3g for guile-user@gnu.org; Mon, 31 Aug 2015 09:50:05 -0400 Original-Received: from mail-wi0-x22f.google.com ([2a00:1450:400c:c05::22f]:35041) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZWPTF-0004yj-Ty for guile-user@gnu.org; Mon, 31 Aug 2015 09:50:02 -0400 Original-Received: by wibq14 with SMTP id q14so981275wib.0 for ; Mon, 31 Aug 2015 06:50:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:subject:references:date:in-reply-to:message-id:user-agent :mime-version:content-type:content-transfer-encoding; bh=cxBbbbfC+ldXpRW04sKfjhzqFLFOqdFLrlSDGi0ZcDg=; b=G6C7DiT1O3PLjCV+ZT5kV/fDGMrJRHOfpd9tMhwd96ECfKfWcJcf5tEs4E0I9GzNFU yQiDyntLd70HwCGOfn+CYzNC6EgpfkcV3L7o1zSYV8YENm97s//I0dI3yHlUWmYRaxQi GNcHBYQljLmcNgu+EEAbyFdUPU/CWTLB+DB7AzvxI2x0c/NjsjzJT+NiGVTuZQigNVRH 9PHO8u1gr7bqzLFJyzryMfCUr2vLI0Xp4nwDuaJdPBdvumEw8U7qqidjjuRkr4IdyNDV Q3kEkJhIx1/h55y7Lp9hjVqUeIlTMMHnSZiNZnTpI5kaldr9Lsi2JNFXbzScikX3Sh4p Sj1Q== X-Received: by 10.194.93.198 with SMTP id cw6mr29618410wjb.113.1441029001033; Mon, 31 Aug 2015 06:50:01 -0700 (PDT) Original-Received: from T420.taylan ([2a02:908:c32:4740:221:ccff:fe66:68f0]) by smtp.gmail.com with ESMTPSA id cw8sm22305223wjb.49.2015.08.31.06.49.59 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 31 Aug 2015 06:50:00 -0700 (PDT) In-Reply-To: <87r3mkg2iy.fsf@T420.taylan> ("Taylan Ulrich \=\?utf-8\?Q\?\=5C\=22Bay\=C4\=B1rl\=C4\=B1\=2FKammer\=5C\=22\=22's\?\= message of "Sun, 30 Aug 2015 18:32:53 +0200") User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.5 (gnu/linux) X-detected-operating-system: by eggs.gnu.org: Error: Malformed IPv6 address (bad octet value). X-Received-From: 2a00:1450:400c:c05::22f X-BeenThere: guile-user@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: General Guile related discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-user-bounces+guile-user=m.gmane.org@gnu.org Original-Sender: guile-user-bounces+guile-user=m.gmane.org@gnu.org Xref: news.gmane.org gmane.lisp.guile.user:11970 Archived-At: taylanbayirli@gmail.com (Taylan Ulrich "Bay=C4=B1rl=C4=B1/Kammer") writes: > That's all there is to it, and you could populate those bytes directly, > one by one: > > the_struct_t my_struct =3D { a, b, c, d, e, f } > Whoops, C isn't as dumb as I had in memory. You'll need to memcpy it into a char[] to be able to hack on the bytes that freely. Anyway. So I've been made aware that if I want my library to work with C data structures, I'll probably want to add alignment support. :-) I did that now, though I don't know if I did it right because I couldn't find precise information on what an FFI system should support wrt. data structure alignment. I read a little on Wikipedia and peeked into the documentation of Haskell's FFI and CL's CFFI. The struct constructor takes an `align?' argument now, which if true will enable default alignment for struct fields. E.g. struct { uint8_t; uint16_t; uint64_t; } becomes: 1: 1 byte uint8 2: 1 byte padding so uint16 is 2-byte aligned 3-4: 2 bytes uint16 5-8: 4 bytes padding so uint64 is 8-byte aligned 9-16: 8 bytes uint64 16 bytes in total. The struct's own alignment is 8, equal to the alignment of its element with the highest alignment. Vectors' alignment is equal to that of their element-type's alignment. Unions' is equal to their highest member. I see C compilers support stuff like "pack(2)" to force 2-byte alignment for >2 byte sized fields. I might add support for that too; I don't know if C libraries typically use that for their ABI? --- Next up I'll see if I can implement some example programs using the library. Something parsing a binary file format, something using the FFI to work with some C data structures, etc. On the meanwhile, testers and feedback welcome. Taylan