From 9bb068fbdccc014512a4971afce2e7ca85154f0a Mon Sep 17 00:00:00 2001 From: guy Decoux Date: Sat, 28 Feb 2009 20:14:19 +0100 Subject: mmap-0.2.1 --- docs/mmap.rb | 320 ----------------------------------------------------------- extconf.rb | 27 +++-- mmap.c | 46 +++++++-- 3 files changed, 54 insertions(+), 339 deletions(-) delete mode 100644 docs/mmap.rb diff --git a/docs/mmap.rb b/docs/mmap.rb deleted file mode 100644 index ba00571..0000000 --- a/docs/mmap.rb +++ /dev/null @@ -1,320 +0,0 @@ -# The Mmap class implement memory-mapped file objects -# -# === WARNING -# === The variables $' and $` are not available with gsub! and sub! -class Mmap -include Comparable -include Enumerable -class << self - -#disable paging of all pages mapped. flag can be -#Mmap::MCL_CURRENT or Mmap::MCL_FUTURE -# -def lockall(flag) -end - -#create a new Mmap object -# -#* file -# Pathname of the file, if nil is given an anonymous map -# is created Mmanp::MAP_ANON -# -#* mode -# Mode to open the file, it can be "r", "w", "rw", "a" -# -#* protection -# specify the nature of the mapping -# -# * Mmap::MAP_SHARED -# Creates a mapping that's shared with all other processes -# mapping the same areas of the file. -# The default value is Mmap::MAP_SHARED -# -# * Mmap::MAP_PRIVATE -# Creates a private copy-on-write mapping, so changes to the -# contents of the mmap object will be private to this process -# -#* options -# Hash. If one of the options length or offset -# is specified it will not possible to modify the size of -# the mapped file. -# -# * length -# Maps length bytes from the file -# -# * offset -# The mapping begin at offset -# -# * advice -# The type of the access (see #madvise) -# -# -def new(file, mode = "r", protection = Mmap::MAP_SHARED, options = {}) -end - -#reenable paging -# -def unlockall -end -end - -#add count bytes to the file (i.e. pre-extend the file) -# -def extend(count) -end - -#advice can have the value Mmap::MADV_NORMAL, -#Mmap::MADV_RANDOM, Mmap::MADV_SEQUENTIAL, -#Mmap::MADV_WILLNEED, Mmap::MADV_DONTNEED -# -def madvise(advice) -end - -#change the mode, value must be "r", "w" or "rw" -# -def mprotect(mode) -end - -#disable paging -# -def mlock -end - -#flush the file -# -def msync -end -#same than msync -def flush -end - -#reenable paging -# -def munlock -end - -#terminate the association -# -#=== Other methods with the same syntax than for the class String -# -# -def munmap -end - -# -def self == other -end - -# -def self > other -end - -# -def self >= other -end - -# -def self < other -end - -# -def self <= other -end - -# -def self === other -end - -# -def self << other -end - -# -def self =~ other -end - -# -def self[nth] -end - -# -def self[start..last] -end - -# -def self[start, length] -end - -# -def self[nth] = val -end - -# -def self[start..last] = val -end - -# -def self[start, len] = val -end - -# -def self <=> other -end - -# -def <<(other) -end - -# -def casecmp(other) >= 1.7.1 -end - -# -def concat(other) -end - -# -def capitalize! -end - -# -def chop! -end - -# -def chomp!([rs]) -end - -# -def count(o1 [, o2, ...]) -end - -# -def crypt(salt) -end - -# -def delete!(str) -end - -# -def downcase! -end - -# -def each_byte -yield char -end - -# -def each([rs]) -yield line -end - -# -def each_line([rs]) -yield line -end - -# -def empty? -end - -# -def freeze -end - -# -def frozen -end - -# -def gsub!(pattern, replace) -end - -# -def gsub!(pattern) -yield str -end - -# -def include?(other) -end - -# -def index(substr[, pos]) -end - -# -def insert(index, str) >= 1.7.1 -end - -# -def length -end - -# -def reverse! -end - -# -def rindex(substr[, pos]) -end - -# -def scan(pattern) -end - -# -def scan(pattern) -yield str -end - -# -def size -end - -# -def slice -end - -# -def slice! -end - -# -def split([sep[, limit]]) -end - -# -def squeeze!([str]) -end - -# -def strip! -end - -# -def sub!(pattern, replace) -end - -# -def sub!(pattern) -yield str -end - -# -def sum([bits]) -end - -# -def swapcase! -end - -# -def tr!(search, replace) -end - -# -def tr_s!(search, replace) -end diff --git a/extconf.rb b/extconf.rb index 78d9bd8..5f2e6ce 100644 --- a/extconf.rb +++ b/extconf.rb @@ -1,12 +1,21 @@ -require "mkmf" +#!/usr/bin/ruby +ARGV.collect! {|x| x.sub(/^--with-mmap-prefix=/, "--with-mmap-dir=") } -$stat_lib = if CONFIG.key?("LIBRUBYARG_STATIC") - $LDFLAGS += " -L#{CONFIG['libdir']}" - CONFIG["LIBRUBYARG_STATIC"] - else - "-lruby" - end -$static ||= nil +require 'mkmf' + + +def resolve(key) + name = key.dup + true while name.gsub!(/\$\((\w+)\)/) { CONFIG[$1] } + name +end + +if ! find_library(resolve(CONFIG["LIBRUBY"]).sub(/^lib(.*)\.\w+\z/, '\\1'), + "ruby_init", resolve(CONFIG["archdir"])) + raise "can't find -lruby" +end + +dir_config("mmap") create_makefile "mmap" @@ -22,7 +31,7 @@ begin unknown: $(DLLIB) \t@echo "main() {}" > /tmp/a.c -\t$(CC) -static /tmp/a.c $(OBJS) $(CPPFLAGS) $(DLDFLAGS) #$stat_lib #{CONFIG["LIBS"]} $(LIBS) $(LOCAL_LIBS) +\t$(CC) -static /tmp/a.c $(OBJS) $(CPPFLAGS) $(DLDFLAGS) #{CONFIG["LIBS"]} $(LIBS) $(LOCAL_LIBS) \t@-rm /tmp/a.c a.out %.html: %.rd diff --git a/mmap.c b/mmap.c index 7846fd4..fa04f48 100644 --- a/mmap.c +++ b/mmap.c @@ -8,6 +8,14 @@ #include #include "version.h" +#if RUBY_VERSION_CODE < 180 +#define StringValue(x) do { \ + if (TYPE(x) != T_STRING) x = rb_str_to_str(x); \ +} while (0) +#define StringValuePtr(x) STR2CSTR(x) +#define SafeStringValue(x) Check_SafeStr(x) +#endif + #ifndef MADV_NORMAL #ifdef POSIX_MADV_NORMAL #define MADV_NORMAL POSIX_MADV_NORMAL @@ -137,6 +145,7 @@ mm_freeze(obj) rb_obj_freeze(obj); GetMmap(obj, t_mm, 0); t_mm->flag |= MM_FROZEN; + return obj; } static VALUE @@ -282,7 +291,7 @@ mm_i_options(arg, obj) key = rb_ary_entry(arg, 0); value = rb_ary_entry(arg, 1); key = rb_obj_as_string(key); - options = RSTRING(key)->ptr; + options = StringValuePtr(key); if (strcmp(options, "length") == 0) { t_mm->len = NUM2INT(value); if (t_mm->len <= 0) { @@ -371,8 +380,8 @@ mm_s_new(argc, argv, obj) } if (NIL_P(fdv)) { fname = rb_str_to_str(fname); - Check_SafeStr(fname); - path = RSTRING(fname)->ptr; + SafeStringValue(fname); + path = StringValuePtr(fname); } else { fd = NUM2INT(fdv); @@ -398,11 +407,11 @@ mm_s_new(argc, argv, obj) } else if (TYPE(vmode) == T_ARRAY && RARRAY(vmode)->len >= 2) { VALUE tmp = RARRAY(vmode)->ptr[0]; - mode = STR2CSTR(tmp); + mode = StringValuePtr(tmp); perm = NUM2INT(RARRAY(vmode)->ptr[1]); } else { - mode = STR2CSTR(vmode); + mode = StringValuePtr(vmode); } if (strcmp(mode, "r") == 0) { smode = O_RDONLY; @@ -566,7 +575,7 @@ mm_mprotect(obj, a) GetMmap(obj, t_mm, 0); if (TYPE(a) == T_STRING) { - smode = STR2CSTR(a); + smode = StringValuePtr(a); if (strcmp(smode, "r") == 0) pmode = PROT_READ; else if (strcmp(smode, "w") == 0) pmode = PROT_WRITE; else if (strcmp(smode, "rw") == 0 || strcmp(smode, "wr") == 0) @@ -625,8 +634,7 @@ do { \ bl = b_mm->real; \ } \ else { \ - if (TYPE(b) != T_STRING) b = rb_str_to_str(b); \ - bp = RSTRING(b)->ptr; \ + bp = StringValuePtr(b); \ bl = RSTRING(b)->len; \ } \ } while (0); @@ -740,7 +748,7 @@ mm_correct_backref(t_mm) if (NIL_P(match)) return 0; if (RMATCH(match)->BEG(0) == -1) return 0; start = RMATCH(match)->BEG(0); - RMATCH(match)->str = rb_str_new(RSTRING(RMATCH(match)->str)->ptr + start, + RMATCH(match)->str = rb_str_new(StringValuePtr(RMATCH(match)->str) + start, RMATCH(match)->END(0) - start); if (OBJ_TAINTED(match)) OBJ_TAINT(RMATCH(match)->str); for (i = 0; i < RMATCH(match)->regs->num_regs && RMATCH(match)->BEG(i) != -1; i++) { @@ -1112,7 +1120,7 @@ mm_append(str1, str2) VALUE str1, str2; { str2 = rb_str_to_str(str2); - str1 = mm_cat(str1, RSTRING(str2)->ptr, RSTRING(str2)->len); + str1 = mm_cat(str1, StringValuePtr(str2), RSTRING(str2)->len); return str1; } @@ -1393,6 +1401,17 @@ mm_bang_i(obj, flag, id, argc, argv) } +#if RUBY_VERSION_CODE >= 180 + +static VALUE +mm_match_m(a, b) + VALUE a, b; +{ + return mm_bang_i(a, MM_ORIGIN, rb_intern("match"), 1, &b); +} + +#endif + static VALUE mm_upcase_bang(a) VALUE a; @@ -1752,6 +1771,9 @@ Init_mmap() rb_define_method(mm_cMap, "extend", mm_extend, 1); rb_define_method(mm_cMap, "freeze", mm_freeze, 0); rb_define_method(mm_cMap, "clone", mm_undefined, -1); +#if RUBY_VERSION_CODE >= 180 + rb_define_method(mm_cMap, "initialize_copy", mm_undefined, -1); +#endif rb_define_method(mm_cMap, "dup", mm_undefined, -1); rb_define_method(mm_cMap, "<=>", mm_cmp, 1); rb_define_method(mm_cMap, "==", mm_equal, 1); @@ -1774,6 +1796,9 @@ Init_mmap() rb_define_method(mm_cMap, "empty?", mm_empty, 0); rb_define_method(mm_cMap, "=~", mm_match, 1); rb_define_method(mm_cMap, "~", mm_undefined, -1); +#if RUBY_VERSION_CODE >= 180 + rb_define_method(mm_cMap, "match", mm_match_m, 1); +#endif rb_define_method(mm_cMap, "succ", mm_undefined, -1); rb_define_method(mm_cMap, "succ!", mm_undefined, -1); rb_define_method(mm_cMap, "next", mm_undefined, -1); @@ -1785,6 +1810,7 @@ Init_mmap() rb_define_method(mm_cMap, "to_i", mm_undefined, -1); rb_define_method(mm_cMap, "to_f", mm_undefined, -1); + rb_define_method(mm_cMap, "to_sym", mm_undefined, -1); rb_define_method(mm_cMap, "to_s", rb_any_to_s, 0); rb_define_method(mm_cMap, "to_str", mm_to_str, 0); rb_define_method(mm_cMap, "inspect", mm_inspect, 0); -- cgit v1.1-2-g2b99