Add DL::Importable::Internal::Struct#alloc.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@2360 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
2990a00153
commit
3bd7404423
@ -74,14 +74,19 @@ module DL
|
|||||||
return @names
|
return @names
|
||||||
end
|
end
|
||||||
|
|
||||||
def new(size = nil)
|
# ptr must be a PtrData object.
|
||||||
|
def new(ptr)
|
||||||
|
ptr.struct!(@tys, *@names)
|
||||||
|
mem = Memory.new(ptr, @names, @ty, @len, @enc, @dec)
|
||||||
|
return mem
|
||||||
|
end
|
||||||
|
|
||||||
|
def alloc(size = nil)
|
||||||
if( !size )
|
if( !size )
|
||||||
size = @size
|
size = @size
|
||||||
end
|
end
|
||||||
ptr = DL::malloc(size)
|
ptr = DL::malloc(size)
|
||||||
ptr.struct!(@tys, *@names)
|
return new(ptr)
|
||||||
mem = Memory.new(ptr, @names, @ty, @len, @enc, @dec)
|
|
||||||
return mem
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def parse(contents)
|
def parse(contents)
|
||||||
|
@ -1,64 +1,42 @@
|
|||||||
require 'dl'
|
require "dl/import"
|
||||||
|
require "dl/struct"
|
||||||
|
|
||||||
module LIBC
|
module LIBC
|
||||||
|
extend DL::Importable
|
||||||
|
|
||||||
begin
|
begin
|
||||||
LIB = DL.dlopen('libc.so.6')
|
dlload "libc.so.6"
|
||||||
rescue RuntimeError
|
rescue
|
||||||
LIB = DL.dlopen('libc.so.5')
|
dlload "libc.so.5"
|
||||||
end
|
end
|
||||||
|
|
||||||
SYM = {
|
extern "int atoi(char*)"
|
||||||
:atoi => LIB['atoi', 'IS'],
|
extern "ibool isdigit(int)"
|
||||||
:isdigit => LIB['isdigit', 'II'],
|
extern "int gettimeofday(struct timeval *, struct timezone *)"
|
||||||
}
|
extern "char* strcat(char*, char*)"
|
||||||
|
extern "FILE* fopen(char*, char*)"
|
||||||
|
extern "int fclose(FILE*)"
|
||||||
|
extern "int fgetc(FILE*)"
|
||||||
|
extern "int strlen(char*)"
|
||||||
|
extern "void qsort(void*, int, int, void*)"
|
||||||
|
|
||||||
def atoi(str)
|
def str_qsort(ary, comp)
|
||||||
r,rs = SYM[:atoi].call(str)
|
len = ary.length
|
||||||
return r
|
r,rs = qsort(ary, len, DL.sizeof('P'), comp)
|
||||||
|
return rs[0].to_a('S', len)
|
||||||
end
|
end
|
||||||
|
|
||||||
def isdigit(c)
|
Timeval = struct [
|
||||||
r,rs = SYM[:isdigit].call(c)
|
"long tv_sec",
|
||||||
return (r != 0)
|
"long tv_usec",
|
||||||
end
|
]
|
||||||
|
|
||||||
|
Timezone = struct [
|
||||||
|
"int tz_minuteswest",
|
||||||
|
"int tz_dsttime",
|
||||||
|
]
|
||||||
end
|
end
|
||||||
|
|
||||||
module LIBC
|
|
||||||
SYM[:strcat] = LIB['strcat', 'SsS']
|
|
||||||
def strcat(str1,str2)
|
|
||||||
r,rs = SYM[:strcat].call(str1 + "\0#{str2}",str2)
|
|
||||||
return rs[0]
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
module LIBC
|
|
||||||
SYM[:fopen] = LIB['fopen', 'PSS']
|
|
||||||
SYM[:fclose] = LIB['fclose', '0P']
|
|
||||||
SYM[:fgetc] = LIB['fgetc', 'IP']
|
|
||||||
|
|
||||||
def fopen(filename, mode)
|
|
||||||
r,rs = SYM[:fopen].call(filename, mode)
|
|
||||||
return r
|
|
||||||
end
|
|
||||||
|
|
||||||
def fclose(ptr)
|
|
||||||
SYM[:fclose].call(ptr)
|
|
||||||
return nil
|
|
||||||
end
|
|
||||||
|
|
||||||
def fgetc(ptr)
|
|
||||||
r,rs = SYM[:fgetc].call(ptr)
|
|
||||||
return r
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
module LIBC
|
|
||||||
SYM[:strlen] = LIB['strlen', 'IP']
|
|
||||||
def strlen(str)
|
|
||||||
r,rs = SYM[:strlen].call(str)
|
|
||||||
return r
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
$cb1 = DL.set_callback('IPP', 0){|ptr1, ptr2|
|
$cb1 = DL.set_callback('IPP', 0){|ptr1, ptr2|
|
||||||
str1 = ptr1.ptr.to_s
|
str1 = ptr1.ptr.to_s
|
||||||
@ -66,19 +44,21 @@ $cb1 = DL.set_callback('IPP', 0){|ptr1, ptr2|
|
|||||||
str1 <=> str2
|
str1 <=> str2
|
||||||
}
|
}
|
||||||
|
|
||||||
module LIBC
|
p LIBC.atoi("10")
|
||||||
SYM[:qsort] = LIB['qsort', '0aIIP']
|
|
||||||
def qsort(ary, comp)
|
|
||||||
len = ary.length
|
|
||||||
r,rs = SYM[:qsort].call(ary, len, DL.sizeof('P'), comp)
|
|
||||||
return rs[0].to_a('S', len)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
include LIBC
|
p LIBC.isdigit(?1)
|
||||||
|
|
||||||
p atoi("10")
|
p LIBC.isdigit(?a)
|
||||||
p isdigit(?1)
|
|
||||||
p isdigit(?a)
|
p LIBC.strcat("a", "b")
|
||||||
p strcat("a", "b")
|
|
||||||
p qsort(["a","c","b"],$cb1)
|
ary = ["a","c","b"]
|
||||||
|
ptr = ary.to_ptr
|
||||||
|
LIBC.qsort(ptr, ary.length, DL.sizeof('P'), $cb1)
|
||||||
|
p ptr.to_a('S', ary.length)
|
||||||
|
|
||||||
|
tv = LIBC::Timeval.alloc
|
||||||
|
tz = LIBC::Timezone.alloc
|
||||||
|
LIBC.gettimeofday(tv, tz)
|
||||||
|
|
||||||
|
p Time.at(tv.tv_sec)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user