Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #18704

Re: What is the right way to do this simple thing

From cri@tiac.net (Richard Harter)
Newsgroups comp.lang.c
Subject Re: What is the right way to do this simple thing
Date 2012-03-13 21:46 +0000
Message-ID <4f5fb9e1.2212140665@text.giganews.com> (permalink)
References (5 earlier) <9s2cmhFf52U1@mid.individual.net> <4f5c2af2.1978932789@text.giganews.com> <0.5b82e106999cbb8812e8.20120311115929GMT.87aa3n8i1q.fsf@bsb.me.uk> <4f5cd20b.2021711914@text.giganews.com> <02e1d915-b66c-44d7-b0c7-1684b5ac9646@gw9g2000vbb.googlegroups.com>

Show all headers | View raw


On Tue, 13 Mar 2012 07:32:58 -0700 (PDT), Gene
<gene.ressler@gmail.com> wrote:

>On Mar 11, 12:51=A0pm, c...@tiac.net (Richard Harter) wrote:
>> On Sun, 11 Mar 2012 11:59:29 +0000, Ben Bacarisse
>>
>> [ elision ]
>>
>> I've used that one in the past. =A0As you say, for all practical
>> purposes it works. =A0At least it does until a wave of architecture
>> changes arrives. =A0One of the nasty things about these "for all
>> practical purposes" solutions is that they can make for latent bugs
>> that don't show up until many years later.
>>
>> What I am hoping for is a coding technique that is "bullet proof",
>> i.e., it is guaranteed to work if one has a conforming C compiler.
>
>At an abstract level, alignment of the payload and its fields is a
>pure function of type information in the header. C provides no
>way to introspectively learn what the payload alignment is.  You
>approached it with the "offset" field in the header, but this
>doesn't quite get it if you are planning to access fields directly
>from the receive buffer: you have no way of being absolutely sure
>the payload is properly aligned. I think this is provably
>an unsolvable problem.

Your general point is well taken, but it seems to me that you've
misunderstood what I was doing.  Either that or I'm missing something
subtle.

>
>So any bulletproof strategy will have to implement the package as
>two separate values: a header with type information and a blob
>that is the payload. Receiving is always in two stages: Get the
>header
>in order to recover the payload type, then either receive the blob
>directly into a variable of payload type or receive the blob into an
>unaligned buffer and copy it to a variable of payload type.  I've
>used both approaches (sort of).  The interesting thing is with
>care you can use the same interface for both:

Let's go through this carefully.  We agree that we need a header and a
blob.  Now I argue that we can do this:

We have two struct types, package and load package.  They look like
this:

    struct package {
       struct package_header header;
       char[1] data;
    };

    struct load_package {  /* On of these for each different type */
       struct package_header header;
       payload_t payload;
    };

We load the payload into the payload field.  We compute the offset of
the payload field to the start of load_package.  We compute the offset
of the data field to the start of package.  The offset in the
package_header is the difference.  This is what goes into the offset
field in the header.

Now we cast the load_package to a package.  The cast does not change
anything inside the struct.  We pass the package to the receiver.  It
in turn can do one of two things.  It can cast it back to
load_package, or it can access the payload by a cast to data+offset.
The point of the latter scheme is data+offset must point to the start
of payload.

Now if I have missed something I would be happy to hear about it.]
 
 
>
>struct header {
>  msg_tag_t tag;
>  size_t size;
>  ... blah blah
>} hdr[1];
>
>struct foo_payload {
>  ... blah blah
>} foo[1];
>
>to send:
>hdr->tag =3D FOO;
>hdr->size =3D sizeof *foo;
>
>to send
>
>write(f, hdr, sizeof *hdr);
>write(f, foo, hdr->size);
>
>to receive with copying:
>
>struct raw_msg {
>  struct header hdr[1];
>  char *data;
>} msg[1];
>
>void receive_message(int f, struct raw_msg *msg)
>{
>  read(f, msg->hdr);
>  msg->data =3D allocate_buffer(msg->hdr->size);
>  read(f, msg->data, msg->hdr->size);
>}
>
>void unpack(void *data, struct raw_msg *msg)
>{
>  memcpy(data, msg->data, msg->hdr->size);
>  deallocate(msg->data);
>}
>
>... receive_message(msg);
>
>Now pass the raw message from the communications code to the point of
>use and unpack there.
>
>void foo_processor(struct raw_msg *msg)
>{
>  struct foo_payload foo[1];
>
>  if (msg->hdr->type !=3D FOO) error(...);
>  unpack(foo, msg);
>}
>
>To use the "lazy" method where you receive without copying, these
>substitutions:
>
>struct raw_msg {
>  struct header hdr[1];
>  int h; // file handle for data blob
>} msg[1];
>
>void receive_message(struct raw_msg *msg)
>{
>  read(f, msg->hdr);
>  msg->h =3D f;
>}
>
>void unpack(void *data, struct raw_msg *msg)
>{
>  read(f, data, msg->hdr->size);
>}
>
>Of course this has the huge disadvantage that processing must be
>synchronous.

Oh, didn't I mention that processing might be asynchronous?  :-)

Not bad, but a bit cumbersome.




Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

What is the right way to do this simple thing cri@tiac.net (Richard Harter) - 2012-03-10 19:26 +0000
  Re: What is the right way to do this simple thing Don Y <this@isnotme.com> - 2012-03-10 12:57 -0700
  Re: What is the right way to do this simple thing ImpalerCore <jadill33@gmail.com> - 2012-03-10 14:12 -0800
    Re: What is the right way to do this simple thing cri@tiac.net (Richard Harter) - 2012-03-10 23:31 +0000
      Re: What is the right way to do this simple thing Ian Collins <ian-news@hotmail.com> - 2012-03-11 13:06 +1300
        Re: What is the right way to do this simple thing cri@tiac.net (Richard Harter) - 2012-03-11 00:44 +0000
          Re: What is the right way to do this simple thing Ian Collins <ian-news@hotmail.com> - 2012-03-11 14:15 +1300
            Re: What is the right way to do this simple thing cri@tiac.net (Richard Harter) - 2012-03-11 04:46 +0000
              Re: What is the right way to do this simple thing Ian Collins <ian-news@hotmail.com> - 2012-03-11 21:27 +1300
                Re: What is the right way to do this simple thing cri@tiac.net (Richard Harter) - 2012-03-11 17:23 +0000
                Re: What is the right way to do this simple thing Ian Collins <ian-news@hotmail.com> - 2012-03-12 10:01 +1300
                Re: What is the right way to do this simple thing cri@tiac.net (Richard Harter) - 2012-03-13 21:18 +0000
                Re: What is the right way to do this simple thing Ian Collins <ian-news@hotmail.com> - 2012-03-14 10:23 +1300
              Re: What is the right way to do this simple thing Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-03-11 11:59 +0000
                Re: What is the right way to do this simple thing cri@tiac.net (Richard Harter) - 2012-03-11 16:51 +0000
                Re: What is the right way to do this simple thing Gene <gene.ressler@gmail.com> - 2012-03-13 07:32 -0700
                Re: What is the right way to do this simple thing cri@tiac.net (Richard Harter) - 2012-03-13 21:46 +0000
      Re: What is the right way to do this simple thing ImpalerCore <jadill33@gmail.com> - 2012-03-12 07:28 -0700
        Re: What is the right way to do this simple thing ImpalerCore <jadill33@gmail.com> - 2012-03-12 08:13 -0700
        Re: What is the right way to do this simple thing cri@tiac.net (Richard Harter) - 2012-03-13 21:51 +0000
  Re: What is the right way to do this simple thing Tim Rentsch <txr@alumni.caltech.edu> - 2012-03-10 14:21 -0800
    Re: What is the right way to do this simple thing cri@tiac.net (Richard Harter) - 2012-03-11 01:40 +0000
      Re: What is the right way to do this simple thing Tim Rentsch <txr@alumni.caltech.edu> - 2012-03-11 14:11 -0700
        Re: What is the right way to do this simple thing cri@tiac.net (Richard Harter) - 2012-03-13 21:56 +0000
          Re: What is the right way to do this simple thing Tim Rentsch <txr@alumni.caltech.edu> - 2012-03-14 14:55 -0700
            Re: What is the right way to do this simple thing cri@tiac.net (Richard Harter) - 2012-03-15 21:55 +0000
              Re: What is the right way to do this simple thing Tim Rentsch <txr@alumni.caltech.edu> - 2012-03-19 13:33 -0700
  Re: What is the right way to do this simple thing Malcolm McLean <malcolm.mclean5@btinternet.com> - 2012-03-10 15:21 -0800
    Re: What is the right way to do this simple thing cri@tiac.net (Richard Harter) - 2012-03-11 00:47 +0000
  Re: What is the right way to do this simple thing Philip Lantz <prl@canterey.us> - 2012-03-11 01:07 -0800
    Re: What is the right way to do this simple thing cri@tiac.net (Richard Harter) - 2012-03-11 16:14 +0000
  Re: What is the right way to do this simple thing Markus Wichmann <nullplan@gmx.net> - 2012-03-11 13:47 +0100
    Re: What is the right way to do this simple thing cri@tiac.net (Richard Harter) - 2012-03-11 16:24 +0000
  Re: What is the right way to do this simple thing cri@tiac.net (Richard Harter) - 2012-03-11 21:01 +0000
    Re: What is the right way to do this simple thing Anders Wegge Keller <wegge@wegge.dk> - 2012-03-12 00:05 +0100
      Re: What is the right way to do this simple thing cri@tiac.net (Richard Harter) - 2012-03-13 21:57 +0000
    Re: What is the right way to do this simple thing cri@tiac.net (Richard Harter) - 2012-03-11 23:06 +0000
      Re: What is the right way to do this simple thing Geoff <geoff@invalid.invalid> - 2012-03-11 17:34 -0700
        Re: What is the right way to do this simple thing cri@tiac.net (Richard Harter) - 2012-03-13 22:00 +0000
          Re: What is the right way to do this simple thing Geoff <geoff@invalid.invalid> - 2012-03-13 18:12 -0700
            Re: What is the right way to do this simple thing cri@tiac.net (Richard Harter) - 2012-03-14 03:38 +0000
              Re: What is the right way to do this simple thing Geoff <geoff@invalid.invalid> - 2012-03-14 00:49 -0700

csiph-web