Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gather contributions #4

Open
wants to merge 21 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
4abe756
trim returned string when the given offset and length overflows
pietern Feb 10, 2010
c0fe2cd
added method to query mapped file for its length
pietern Feb 10, 2010
a597305
use jeweler and prepare for release on gemcutter
pietern Feb 10, 2010
52009f4
added summary
pietern Feb 10, 2010
b0f47c2
use rake-compiler instead of own rakefile hacks for compilation
pietern Feb 10, 2010
d62786f
Version bump to 1.1.1
pietern Feb 10, 2010
306a9f3
fix build process
pietern Feb 10, 2010
f15c95e
when length/2 overflowed, uninitialized memory was put in the string …
pietern Feb 10, 2010
bc01b19
Version bump to 1.1.2
pietern Feb 10, 2010
3e64c93
remove superfluous allocations to stop leaking memory
pietern Feb 10, 2010
2ea16c8
Version bump to 1.1.3
pietern Feb 10, 2010
f37a907
don't copy the data in the stack, let ruby handle the mmap'ed region
pietern Feb 11, 2010
cb9823a
Version bump to 1.1.4
pietern Feb 11, 2010
e23f381
add Gemfile and .travis.yml to try Travis CI
shoulderpower Nov 5, 2013
cd8a755
add magic comment to Rakefile to specify its encoding
shoulderpower Nov 5, 2013
f4fabe8
use NUM2SIZET instead of NUM2INT for offset and length
shoulderpower Nov 5, 2013
a077d7a
add arguments to MappedFile.new and FileWindow.new (offset and length…
shoulderpower Nov 5, 2013
8eb82e0
introduce bundler instead of jeweler
shoulderpower Nov 6, 2013
2c2ec32
SimpleMmap::MappedFile#size returns SIZET2NUM(sm_map->read_len)
takada-rist Nov 14, 2013
a02abb0
SimpleMmap::FileWindow#[]
takada-rist Nov 14, 2013
e20c2b0
Update version and copyright years
js Oct 4, 2016
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
/ext/*.o
/ext/*.bundle
/ext/*.so
/ext/*.dll
/ext/mkmf.log
/tmp
/lib/simple_mmap/*.bundle
/lib/simple_mmap/*.so
/lib/simple_mmap/*.dll
/ext/Makefile
/pkg
/doc/*
6 changes: 6 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
language: ruby
rvm:
- 2.0.0
- 1.9.3
- 1.9.2
- 1.8.7
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
source 'https://rubygems.org'

# Specify your gem's dependencies in simple_mmap.gemspec
gemspec
41 changes: 9 additions & 32 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,38 +1,15 @@
# encoding: utf-8
require "bundler/gem_tasks"

require 'rubygems'
require 'hoe'
require './lib/simple_mmap/version.rb'
require "rake/clean"

Hoe.new('simple_mmap', SimpleMmap::VERSION) do |p|
p.rubyforge_name = 'simple-mmap'
p.developer('Johan Sørensen', '[email protected]')
p.spec_extras = {
"extensions" => ["Rakefile"]
}
end

DLEXT = Config::CONFIG['DLEXT']

file 'ext/Makefile' => FileList['ext/{*.c,*.h,*.rb}'] do
chdir('ext') { ruby 'extconf.rb' }
require 'rake/extensiontask'
Rake::ExtensionTask.new('simple_mmap') do |ext|
ext.name = "mapped_file"
ext.lib_dir = File.join('lib', 'simple_mmap')
end
CLEAN.include 'ext/Makefile', 'ext/mkmf.log'

file "ext/mapped_file.#{DLEXT}" => FileList['ext/Makefile', 'ext/*.{c,h,rb}'] do |f|
sh 'cd ext && make'
require 'rake/testtask'
Rake::TestTask.new(:test => :compile) do |test|
test.libs << 'ext'
test.verbose = true
end
CLEAN.include 'ext/*.{o,bundle,so,dll}'

file "lib/simple_mmap/mapped_file.#{DLEXT}" => "ext/mapped_file.#{DLEXT}" do |f|
cp f.prerequisites, "lib/simple_mmap/", :preserve => true
end
#CLEAN.include "lib/simple_mmap/mapped_file.#{DLEXT}"

desc 'Build the mapped_file extension'
task :build => "lib/simple_mmap/mapped_file.#{DLEXT}"

task :test => [:build]

task :default => :test
1 change: 1 addition & 0 deletions VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1.1.4
5 changes: 0 additions & 5 deletions ext/extconf.rb

This file was deleted.

4 changes: 4 additions & 0 deletions ext/simple_mmap/extconf.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
require 'mkmf'

have_func('mmap')
create_makefile('simple_mmap/mapped_file')
102 changes: 74 additions & 28 deletions ext/mapped_file.c → ext/simple_mmap/mapped_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,31 @@
#include <unistd.h>
#include "ruby.h"

#ifndef NUM2SIZET
# include <limits.h>
# include <stdint.h>
# if SIZE_MAX > ULONG_MAX
# define NUM2SIZET NUM2ULL
# define SIZET2NUM ULONG2NUM
# elif SIZE_MAX > UINT_MAX
# define NUM2SIZET NUM2ULONG
# define SIZET2NUM ULONG2NUM
# elif SIZE_MAX > USHRT_MAX
# define NUM2SIZET NUM2UINT
# define SIZET2NUM UINT2NUM
# else
# define NUM2SIZET NUM2USHORT
# define SIZET2NUM USHORT2NUM
# endif
#endif

// a memory mapped file
typedef struct {
int fd;
int fd;
caddr_t map;
size_t len;
size_t len;
caddr_t read_base;
size_t read_len;
} simple_mmap_map;

static VALUE mod_simple_mmap;
Expand All @@ -45,7 +65,7 @@ static VALUE sm_map_data;
*
* mmap() the file at +path+
*/
static VALUE sm_mapped_file_initialize(VALUE vself, VALUE filename)
static VALUE sm_mapped_file_initialize(int argc, VALUE *argv, VALUE vself)
{
int fd = -1;
size_t length;
Expand All @@ -54,6 +74,15 @@ static VALUE sm_mapped_file_initialize(VALUE vself, VALUE filename)
VALUE vsm_map;
simple_mmap_map *sm_map;

VALUE filename, voffset, vlength;
off_t offset, offset_mod;

rb_scan_args(argc, argv, "12", &filename, &voffset, &vlength);
if (!NIL_P(voffset) && NUM2LL(voffset) < 0)
rb_raise(rb_eRangeError, "offset out of range: %lld", NUM2LL(voffset));
if (!NIL_P(vlength) && NUM2LL(vlength) < 0)
rb_raise(rb_eRangeError, "length out of range: %lld", NUM2LL(vlength));

fd = open(RSTRING_PTR(filename), O_RDONLY);
if (fd == -1) {
rb_raise(rb_eArgError, "Failed to open file %s", RSTRING_PTR(filename));
Expand All @@ -65,10 +94,15 @@ static VALUE sm_mapped_file_initialize(VALUE vself, VALUE filename)
rb_raise(rb_eArgError, "Failed to stat file %s", RSTRING_PTR(filename));
close(fd);
}
length = st.st_size;
offset = NIL_P(voffset) ? 0 : NUM2SIZET(voffset);
length = NIL_P(vlength) ? st.st_size : NUM2SIZET(vlength);
if (offset + length > st.st_size) length = st.st_size - offset;
offset_mod = offset % sysconf(_SC_PAGESIZE);
offset = offset - offset_mod;
length = length + offset_mod;

// do the mmap
base = mmap(NULL, length, PROT_READ, MAP_SHARED, fd, 0);
base = mmap(NULL, length, PROT_READ, MAP_SHARED, fd, offset);
if (base == (caddr_t) -1) {
rb_raise(rb_eArgError, "Failed to mmap file %s", RSTRING_PTR(filename));
close(fd);
Expand All @@ -80,6 +114,8 @@ static VALUE sm_mapped_file_initialize(VALUE vself, VALUE filename)
sm_map->fd = fd;
sm_map->map = base;
sm_map->len = length;
sm_map->read_base = base + offset_mod;
sm_map->read_len = length - offset_mod;
rb_ivar_set(vself, rb_intern("@mmap_data"), vsm_map);

return Qnil;
Expand Down Expand Up @@ -109,51 +145,61 @@ static VALUE sm_mapped_file_close(VALUE vself)
}

/*
* Document-method: close
* Document-method: read_window_data
* call-seq: obj.read_window_data(offset, length)
*
* Read +length+ bytes starting at +offset+
*/
static VALUE sm_mapped_file_read_window_data(VALUE vself, VALUE voffset, VALUE vlength)
{
size_t offset = NUM2INT(voffset);
size_t length = NUM2INT(vlength);
char buff[length];
size_t offset, length;
VALUE vsm_map;
simple_mmap_map *sm_map;

sm_map = ALLOC(simple_mmap_map);
if (NUM2LL(voffset) < 0) return Qnil;
if (NUM2LL(vlength) < 0) rb_raise(rb_eRangeError, "length out of range: %lld", NUM2LL(vlength));
offset = NUM2SIZET(voffset);
length = NUM2SIZET(vlength);

vsm_map = rb_ivar_get(vself, rb_intern("@mmap_data"));
Data_Get_Struct(vsm_map, simple_mmap_map, sm_map);

if (offset > sm_map->len) {
return Qnil;
}

size_t curr;
curr = offset;
size_t i;
for(i = 0; i < length; ++i) {
//printf("i=%i offset=%i length=%i curr=%i map->len=%i\n", i, offset, length, curr, sm_map->len);
if ((curr + i) > sm_map->len)
break;
buff[i] = sm_map->map[curr++];
if (offset > sm_map->read_len) return Qnil;

// If the range overflows, return part that overlaps
if ((offset + length) > sm_map->read_len) {
length = sm_map->read_len - offset;
}

if ((offset + length) > sm_map->len)
return rb_str_new(buff, ((offset+length) - sm_map->len)-1);

return rb_str_new(buff, length);

return rb_str_new(sm_map->read_base + offset, length);
}

/*
* Document-method: size
* call-seq: obj.size
*
* Return size of mapped file
*/

static VALUE sm_mapped_file_size(VALUE vself)
{
VALUE vsm_map;
simple_mmap_map *sm_map;

vsm_map = rb_ivar_get(vself, rb_intern("@mmap_data"));
Data_Get_Struct(vsm_map, simple_mmap_map, sm_map);
return SIZET2NUM(sm_map->read_len);
}

void Init_mapped_file()
{
mod_simple_mmap = rb_define_module("SimpleMmap");

sm_mapped_file = rb_define_class_under(mod_simple_mmap, "MappedFile", rb_cObject);
rb_define_private_method(sm_mapped_file, "initialize", sm_mapped_file_initialize, 1);
rb_define_private_method(sm_mapped_file, "initialize", sm_mapped_file_initialize, -1);
rb_define_method(sm_mapped_file, "close", sm_mapped_file_close, 0);
rb_define_method(sm_mapped_file, "read_window_data", sm_mapped_file_read_window_data, 2);
rb_define_method(sm_mapped_file, "size", sm_mapped_file_size, 0);

sm_map_data = rb_define_class_under(sm_mapped_file, "MmapData", rb_cObject);
}
2 changes: 1 addition & 1 deletion lib/simple_mmap.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2009 Johan Sørensen
# Copyright (c) 2009-2016 Johan Sørensen
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
Expand Down
19 changes: 12 additions & 7 deletions lib/simple_mmap/file_window.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ module SimpleMmap
class FileWindow
# Create a mmap'ed window for file at +path+
# You are responsible for closing it when you're done using #close
def initialize(path)
def initialize(path, offset = nil, length = nil)
@path = path
@offset = 0
@mmap = SimpleMmap::MappedFile.new(@path)
@mmap = SimpleMmap::MappedFile.new(@path, offset, length)
end
attr_reader :path, :offset

Expand Down Expand Up @@ -70,11 +70,11 @@ def [](*index)
offset = index
length = 0
when Range
offset = index.begin
length = index.end - index.begin
unless index.exclude_end?
length += 1
end
offset = index.begin < 0 ? index.begin + @mmap.size : index.begin
return nil if offset < 0 or offset > @mmap.size
length = (index.end < 0 ? index.end + @mmap.size : index.end) - offset
length += 1 unless index.exclude_end?
return '' if length <= 0
end

@offset = offset + length
Expand All @@ -92,5 +92,10 @@ def read(length)
@offset += length
data
end

# Return size of mapped file
def size
@mmap.size
end
end
end
23 changes: 1 addition & 22 deletions lib/simple_mmap/version.rb
Original file line number Diff line number Diff line change
@@ -1,24 +1,3 @@
# Copyright (c) 2009 Johan Sørensen
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# 'Software'), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

module SimpleMmap
VERSION = '1.0.0'
VERSION = "1.2"
end
26 changes: 26 additions & 0 deletions simple-mmap.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# coding: utf-8
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'simple_mmap/version'

Gem::Specification.new do |spec|
spec.name = "simple-mmap"
spec.version = SimpleMmap::VERSION
spec.authors = ["Johan Sørensen", "Pieter Noordhuis", "TAKADA Daisuke"]
spec.email = ["[email protected]", "[email protected]"]
# spec.description = %q{TODO: Write a gem description}
spec.description = %q{A simplistic interface for reading memory mapped files}
spec.summary = %q{A simplistic interface for reading memory mapped files}
spec.homepage = "http://github.com/pietern/simple-mmap"
spec.license = "MIT"

spec.files = `git ls-files`.split($/)
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
spec.require_paths = ["ext", "lib"]
spec.extensions << 'ext/simple_mmap/extconf.rb'

spec.add_development_dependency "bundler", "~> 1.3"
spec.add_development_dependency "rake"
spec.add_development_dependency 'rake-compiler', '>= 0.7.0'
end
19 changes: 18 additions & 1 deletion test/test_file_window.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
class TestFileWindow < Test::Unit::TestCase
def setup
@file = Tempfile.new("TestFileWindow.data")
File.open(@file.path, "w"){|f| f.puts(('a'..'z').to_a.join) }
File.open(@file.path, "w"){|f| f.write(('a'..'z').to_a.join) }
@fw = SimpleMmap::FileWindow.new(@file.path)
end

Expand Down Expand Up @@ -39,6 +39,19 @@ def test_get_single_byte_at_current_offset_with_index
assert_equal 1, @fw.offset
end

def test_get_bytes_past_length
assert_equal "z", @fw[25, 10]
end

def test_get_all_bytes
assert_equal ('a'..'z').to_a.join, @fw[0, 26]
end

def test_nil_on_negative_index
assert_equal nil, @fw[-1]
assert_equal nil, @fw[-1, 2]
end

def test_get_from_x_to_y_with_index_comma_notation
assert_equal "cde", @fw[2, 3]
assert_equal 5, @fw.pos
Expand All @@ -59,4 +72,8 @@ def test_read
assert_equal "abc", @fw.read(3)
assert_equal 3, @fw.offset
end

def test_size
assert_equal 26, @fw.size
end
end