in reply to sysread/syswrite and O_DIRECT alignment problem

First, I don't know how to do that in perl. Second, I've looked through CPAN and found nothing. Thus, I think, if you haven't played with XS programming, now should be good time to begin doing that :) I'd suggest writing a module that operates with filehandles that require aligned access, and deals with the aligned buffers internally in C. This approach will create slight memory copying overhead (and this is perl, so that impact must be negligible I guess), but will produce a nice and clean open/read/write/etc perl API, that can be added to the big family of IO:: modules, and/or used as a tied filehandle.

There's also a possibility to write a module that somehow manages to create SVs that refer internally to aligned memory, but that approach raises (for me) more questions than answers (should they be R/O or R/W? and if R/W, how perl should be told to re-allocate the memory correctly?). Apologies if I've muddied the waters more than necessary, but possibly some of these ideas can help you.

  • Comment on Re: sysread/syswrite and O_DIRECT alignment problem

Replies are listed 'Best First'.
Re^2: sysread/syswrite and O_DIRECT alignment problem
by fishnuts (Acolyte) on Nov 27, 2007 at 11:09 UTC
    The solution Tye provided worked well, and took advantage of the "offset" argument to sysread/syswrite, which I previously thought wasn't necessary... Once you know "how far out of alignment" a scalar variable is, you just pass around that offset and use it wherever you need to align the buffer for sysread/syswrite, and use substr to extract or inject your working data from/to that buffer.