I think the method I proposed above would look something like this, if rendered in a manner similar to the snippet in the OP -- but please just think of this as pseudo-code (and take it with a good bit of salt), since I'm not as fluent or comfortable as I once was with the more C-like syntax you used up there, and I don't have the means to test this.
void swapBufferChunks( U8 *buf, U32 start1, U32 start2, U32 bufsize ) +{ register U64 *p1 = (U64*)&buf[ start1 ]; register U64 *p2 = (U64*)&buf[ start2 ]; register U64 temp; shift_size = p2 - p1; end_buf = p1 + bufsize; # assuming bufsize is byte count item_count = bufsize / 8; # assuming elements are 8-bytes each iter_count = 0; bgn_ofs = p1; temp = *p1; while ( iter_count < item_count ) { if ( p2 = bgn_ofs ) { *p1 = temp; iter_count++; last if ( iter_count == item_count ); # break out of while + loop as soon as this is true p1++; p2++; bgn_ofs = p1; temp = *p1; } *p1 = *p2; iter_count++; p1 = p2; p2 += shift_size; p2 -= bufsize if ( p2 >= end_buf ); } }
(updated include a line inside the while loop to increment iter_count - rather a stupid thing to forget.)

(Updating again to mention that my snippet is bound to have a problem when it hits the last iteration; I haven't tried to fix that in the snippet (left as an exercise for the reader) -- hint: must increment iter_count every time a value is assigned to *p1, and must exit the while loop as soon as iter_count reaches the value of item_count.

(final update: last line used to say ... if ( p2 > end_buf ) which was bound to be wrong. Hell, while I'm at. might as well fix the last-iteration problem too. Still, this sort of thing needs really careful testing, which I haven't done.)


In reply to Re: [OT] Swapping buffers in place. by graff
in thread [OT] Swapping buffers in place. by BrowserUk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.