fadvise 1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/ext/extconf.rb +4 -0
  2. data/ext/fadvise.c +77 -0
  3. data/test/test.rb +26 -0
  4. metadata +55 -0
data/ext/extconf.rb ADDED
@@ -0,0 +1,4 @@
1
+ require 'mkmf'
2
+
3
+ have_func("posix_fadvise")
4
+ create_makefile("fadvise")
data/ext/fadvise.c ADDED
@@ -0,0 +1,77 @@
1
+ #include "ruby.h"
2
+ #include <errno.h>
3
+
4
+ static int symbols[6];
5
+ static VALUE rb_Errno_EINVAL;
6
+
7
+ #ifdef HAVE_POSIX_FADVISE
8
+
9
+ #define _XOPEN_SOURCE 600
10
+ #include <fcntl.h>
11
+ /*
12
+ * call-seq:
13
+ * file.fadvise(offset,len,advice) -> file
14
+ *
15
+ * Advise the operating system how you intend to use the data in this file,
16
+ * starting from byte +offset+ and counting +len+ bytes, so that the kernel can
17
+ * schedule it to be fetched in the background while you do more processing. This
18
+ * call is intended to avoid blocking later when you actually read the data,
19
+ * but whether this actually happens is up to the the kernel's IO scheduler.
20
+ * Valid values for advice are:
21
+ * * <tt>:normal</tt>
22
+ * * <tt>:sequential</tt>
23
+ * * <tt>:random</tt>
24
+ * * <tt>:no_reuse</tt>
25
+ * * <tt>:will_need</tt>
26
+ * * <tt>:dont_need</tt>
27
+ *
28
+ * This call does not block.
29
+ *
30
+ * See the posix_fadvise(2) manpage for more information about these types of advice.
31
+ */
32
+ static VALUE File_fadvise(VALUE self, VALUE offset, VALUE len, VALUE advice){
33
+ int fd;
34
+ int madvice;
35
+ fd=NUM2INT(rb_funcall(self,rb_intern("fileno"),0));
36
+ madvice=SYM2ID(advice);
37
+ if(madvice == symbols[0]) madvice=POSIX_FADV_NORMAL;
38
+ else if(madvice == symbols[1]) madvice=POSIX_FADV_SEQUENTIAL;
39
+ else if(madvice == symbols[2]) madvice=POSIX_FADV_RANDOM;
40
+ else if(madvice == symbols[3]) madvice=POSIX_FADV_NOREUSE;
41
+ else if(madvice == symbols[4]) madvice=POSIX_FADV_WILLNEED;
42
+ else if(madvice == symbols[5]) madvice=POSIX_FADV_DONTNEED;
43
+ else rb_raise(rb_Errno_EINVAL,":%s is not valid advice",rb_id2name(SYM2ID(advice)));
44
+
45
+ if (!posix_fadvise(self,NUM2OFFT(offset),NUM2OFFT(len),madvice)){
46
+ rb_sys_fail(0);
47
+ }
48
+
49
+ return self;
50
+ }
51
+ #else
52
+ //dummy implementation for systems that don't have fadvise
53
+ //do all the same error checking
54
+ static VALUE File_fadvise(VALUE self, VALUE offset, VALUE len, VALUE advice){
55
+ int fd;
56
+ int madvice;
57
+ int i;
58
+ fd=NUM2INT(rb_funcall(self,rb_intern("fileno"),0));
59
+ madvice=SYM2ID(advice);
60
+ for (i=0; i<6; ++i){
61
+ if(madvice == symbols[i]) return self;
62
+ }
63
+ rb_raise(rb_Errno_EINVAL,":%s is not valid advice",rb_id2name(SYM2ID(advice)));
64
+ return self;
65
+ }
66
+ #endif
67
+
68
+ void Init_fadvise(){
69
+ rb_Errno_EINVAL = rb_eval_string("Errno::EINVAL");
70
+ symbols[0]=rb_intern("normal");
71
+ symbols[1]=rb_intern("sequential");
72
+ symbols[2]=rb_intern("random");
73
+ symbols[3]=rb_intern("no_reuse");
74
+ symbols[4]=rb_intern("will_need");
75
+ symbols[5]=rb_intern("dont_need");
76
+ rb_define_method(rb_cFile,"fadvise",File_fadvise,3);
77
+ }
data/test/test.rb ADDED
@@ -0,0 +1,26 @@
1
+ #!/usr/bin/env ruby
2
+ require 'test/unit'
3
+
4
+ $: << File.expand_path(File.dirname(__FILE__)) + '/../ext'
5
+ require 'fadvise'
6
+
7
+ class TestFadvise < Test::Unit::TestCase
8
+ def test_symbols
9
+ f=open("/dev/zero")
10
+ assert_nothing_thrown{ f.fadvise 0,0,:normal }
11
+ assert_nothing_thrown{ f.fadvise 0,0,:sequential }
12
+ assert_nothing_thrown{ f.fadvise 0,0,:random }
13
+ assert_nothing_thrown{ f.fadvise 0,0,:no_reuse }
14
+ assert_nothing_thrown{ f.fadvise 0,0,:will_need }
15
+ assert_nothing_thrown{ f.fadvise 0,0,:dont_need }
16
+ assert_raise(Errno::EINVAL){ f.fadvise 0,0,:foobar }
17
+ f.close
18
+ end
19
+
20
+ def test_closed
21
+ f=open("/dev/zero")
22
+ assert_nothing_thrown{ f.fadvise 0,0,:normal }
23
+ f.close
24
+ assert_raise(IOError){ f.fadvise 0,0,:normal }
25
+ end
26
+ end
metadata ADDED
@@ -0,0 +1,55 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fadvise
3
+ version: !ruby/object:Gem::Version
4
+ version: "1.0"
5
+ platform: ruby
6
+ authors:
7
+ - Ken Bloom
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-09-27 00:00:00 -05:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: kbloom@gmail.com
18
+ executables: []
19
+
20
+ extensions:
21
+ - ext/extconf.rb
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - ext/fadvise.c
26
+ - ext/extconf.rb
27
+ - test/test.rb
28
+ has_rdoc: true
29
+ homepage:
30
+ post_install_message:
31
+ rdoc_options: []
32
+
33
+ require_paths:
34
+ - ext
35
+ required_ruby_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: "0"
40
+ version:
41
+ required_rubygems_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: "0"
46
+ version:
47
+ requirements: []
48
+
49
+ rubyforge_project:
50
+ rubygems_version: 1.2.0
51
+ signing_key:
52
+ specification_version: 2
53
+ summary: A binding to the posix_fadvise system call
54
+ test_files: []
55
+