Skip to content

Instantly share code, notes, and snippets.

@jkramer
Last active September 28, 2016 15:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jkramer/273e56bb24a07d1eb2a7be4d786dc306 to your computer and use it in GitHub Desktop.
Save jkramer/273e56bb24a07d1eb2a7be4d786dc306 to your computer and use it in GitHub Desktop.
Support for native sendfile(2) for Perl 6
unit package IO::SendFile;
use NativeCall;
multi sub send-file(IO::Handle:D $input, IO::Handle:D $output, Int:D $count where * > 0) is export {
given sendfile($output.native-descriptor, $input.native-descriptor, Nil, $count) {
when -1 {
my $errno := cglobal(Str, 'errno', int32);
fail strerror($errno);
}
default {
return $_;
}
}
}
multi sub send-file(IO::Handle:D $input, IO::Handle:D $output) is export {
constant BLOCK-SIZE = 2048;
my $total = 0;
loop {
$total += my $count = send-file($input, $output, BLOCK-SIZE);
last if $count < BLOCK-SIZE;
}
}
sub sendfile(int32, int32, Pointer, uint32) returns int32 is native(Str) { * }
sub strerror(int32) returns Str is native(Str) { * }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment