Alignment puzzle

Discussion in 'C Programming' started by JFG, Sep 20, 2007.

  1. JFG

    JFG Guest

    Hi -

    I have this program to maintain and I'm puzzling over the following
    function -

    void
    lance_copytobuf_gap2(sc, fromv, boff, len)
    struct lance_softc *sc;
    void *fromv;
    int boff;
    register int len;
    {
    volatile caddr_t buf = sc->sc_mem;
    register caddr_t from = fromv;
    register volatile u_int16_t *bptr;
    if (boff & 0x1) {
    /* handle unaligned first byte */
    bptr = ((volatile u_int16_t *)buf) + (boff - 1);
    *bptr = (*from++ << 8) | (*bptr & 0xff);
    bptr += 2;
    len--;
    } else
    bptr = ((volatile u_int16_t *)buf) + boff;
    while (len > 1) {
    *bptr = (from[1] << 8) | (from[0] & 0xff);
    bptr += 2;
    from += 2;
    len -= 2;
    }
    if (len == 1)
    *bptr = (u_int16_t)*from;
    }

    What exactly is happening in the "handle unaligned first byte" part? It
    seems quite opaque to me.

    --
    We all live in a yellow submarine.
    JFG, Sep 20, 2007
    #1
    1. Advertising

  2. JFG

    JFG Guest

    Can no one help with this? I'd have thought it's bread and butter for
    the experts!

    On 20 Sep 2007 at 0:16, JFG wrote:
    > Hi -
    >
    > I have this program to maintain and I'm puzzling over the following
    > function -
    >
    > void
    > lance_copytobuf_gap2(sc, fromv, boff, len)
    > struct lance_softc *sc;
    > void *fromv;
    > int boff;
    > register int len;
    > {
    > volatile caddr_t buf = sc->sc_mem;
    > register caddr_t from = fromv;
    > register volatile u_int16_t *bptr;
    > if (boff & 0x1) {
    > /* handle unaligned first byte */
    > bptr = ((volatile u_int16_t *)buf) + (boff - 1);
    > *bptr = (*from++ << 8) | (*bptr & 0xff);
    > bptr += 2;
    > len--;
    > } else
    > bptr = ((volatile u_int16_t *)buf) + boff;
    > while (len > 1) {
    > *bptr = (from[1] << 8) | (from[0] & 0xff);
    > bptr += 2;
    > from += 2;
    > len -= 2;
    > }
    > if (len == 1)
    > *bptr = (u_int16_t)*from;
    > }
    >
    > What exactly is happening in the "handle unaligned first byte" part? It
    > seems quite opaque to me.
    >
    > --
    > We all live in a yellow submarine.


    --
    We all live in a yellow submarine.
    JFG, Sep 20, 2007
    #2
    1. Advertising

  3. JFG

    Lew Pitcher Guest

    On Sep 19, 8:16 pm, JFG <> wrote:
    > Hi -
    >
    > I have this program to maintain and I'm puzzling over the following
    > function -
    >
    > void
    > lance_copytobuf_gap2(sc, fromv, boff, len)
    > struct lance_softc *sc;
    > void *fromv;
    > int boff;
    > register int len;
    > {
    > volatile caddr_t buf = sc->sc_mem;
    > register caddr_t from = fromv;
    > register volatile u_int16_t *bptr;
    > if (boff & 0x1) {
    > /* handle unaligned first byte */
    > bptr = ((volatile u_int16_t *)buf) + (boff - 1);
    > *bptr = (*from++ << 8) | (*bptr & 0xff);
    > bptr += 2;
    > len--;
    > } else
    > bptr = ((volatile u_int16_t *)buf) + boff;
    > while (len > 1) {
    > *bptr = (from[1] << 8) | (from[0] & 0xff);
    > bptr += 2;
    > from += 2;
    > len -= 2;
    > }
    > if (len == 1)
    > *bptr = (u_int16_t)*from;
    >
    > }
    >
    > What exactly is happening in the "handle unaligned first byte" part? It
    > seems quite opaque to me.


    While you haven't supplied enough information to be certain (how are
    lance_softc and caddr_t defined?) I can certainly supply you with an
    educated guess.

    The K&R C function you show will copy 16bit quantities into a buffer,
    at a specified offset from the beginning of the buffer. The code seems
    to assume that the buffer is otherwise "aligned" to some boundary, and
    the offset may cause the start of the copy to move off of that
    alignment.

    So, the code in question checks to see if the offset is a whole
    multiple of 2, and if it is not, it "merges" the first byte of the
    data to be copied into the high order 8 bits of the 16bit quantity
    that the offset breaks across.

    After that, the code then copies the remaining data byte by byte,
    building pairs of bytes into 16bit quantities and populating the
    remaining space in the buffer with these 16bit quantities.
    Lew Pitcher, Sep 20, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Earl Teigrob
    Replies:
    3
    Views:
    6,640
    Nedu N
    Aug 6, 2003
  2. dwa

    Design Puzzle!

    dwa, Jun 10, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    362
    Cowboy \(Gregory A. Beamer\) [MVP]
    Jun 10, 2004
  3. Shankara Narayanan

    Booking puzzle....

    Shankara Narayanan, Jun 17, 2004, in forum: ASP .Net
    Replies:
    20
    Views:
    912
    bredal Jensen
    Jun 30, 2004
  4. VB Programmer
    Replies:
    2
    Views:
    425
    Alan Lambert
    Nov 4, 2004
  5. G. Stewart

    regex puzzle!

    G. Stewart, Nov 23, 2004, in forum: ASP .Net
    Replies:
    8
    Views:
    501
    G. Stewart
    Nov 25, 2004
Loading...

Share This Page