guile-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: packed structures usefulness


From: Neil Jerram
Subject: Re: packed structures usefulness
Date: Sat, 03 Nov 2007 10:43:02 +0000
User-agent: Gnus/5.110006 (No Gnus v0.6) Emacs/21.4 (gnu/linux)

"Marco Maggi" <address@hidden> writes:

> one can define a SMOB whose internal
> representation is equivalent to the C language
> type:
>
> struct triplet {
>   int one;
>   double two;
>   int64_t three[3];
> }; 

I implemented something like this for an application whose basic
control mechanism is message passing using flat C structures ("flat"
=> no pointers), because I wanted to be able to build and manipulate
the structures from Scheme.

I handled the padding issue by autogenerating structure definitions
like this (from the header files that define the structures):

struct struct_desc triplet_desc[] =
{
  { "one", offsetof(struct triplet, one), sizeof(one) },
  { "two", offsetof(struct triplet, two), sizeof(two) },
  ...
};

(It was more complex than shown here, to handle embedded structures,
array fields, etc., but I'm sure you get the idea.)

Then writing C code to read these definitions and export them to the
Scheme level.

I didn't use SMOBs.  On the Scheme level the structure is just a
uniform byte array, and there are procedures for reading and writing
fields (identified by name), using the structure definition
information.

So to answer your actual question: yes, I think this is useful, but
only in a rather specific kind of application context.

(One could also consider using this approach for reading and writing
binary network protocols, but that feels less robust to me than the
traditional approach of reading and writing a sequential byte stream.)

Regards,
        Neil





reply via email to

[Prev in Thread] Current Thread [Next in Thread]