Merge branch 'master' of git://factorcode.org/git/factor
commit
cbaeda3195
|
@ -1,6 +1,6 @@
|
||||||
! Copyright (C) 2008 Slava Pestov.
|
! Copyright (C) 2008, 2009 Slava Pestov.
|
||||||
! See http://factorcode.org/license.txt for BSD license.
|
! See http://factorcode.org/license.txt for BSD license.
|
||||||
USING: accessors alien.c-types kernel destructors bit-arrays
|
USING: accessors classes.struct kernel destructors bit-arrays
|
||||||
sequences assocs struct-arrays math namespaces locals fry unix
|
sequences assocs struct-arrays math namespaces locals fry unix
|
||||||
unix.linux.epoll unix.time io.ports io.backend.unix
|
unix.linux.epoll unix.time io.ports io.backend.unix
|
||||||
io.backend.unix.multiplexers ;
|
io.backend.unix.multiplexers ;
|
||||||
|
@ -16,14 +16,14 @@ TUPLE: epoll-mx < mx events ;
|
||||||
: <epoll-mx> ( -- mx )
|
: <epoll-mx> ( -- mx )
|
||||||
epoll-mx new-mx
|
epoll-mx new-mx
|
||||||
max-events epoll_create dup io-error >>fd
|
max-events epoll_create dup io-error >>fd
|
||||||
max-events "epoll-event" <struct-array> >>events ;
|
max-events epoll-event <struct-array> >>events ;
|
||||||
|
|
||||||
M: epoll-mx dispose* fd>> close-file ;
|
M: epoll-mx dispose* fd>> close-file ;
|
||||||
|
|
||||||
: make-event ( fd events -- event )
|
: make-event ( fd events -- event )
|
||||||
"epoll-event" <c-object>
|
epoll-event <struct>
|
||||||
[ set-epoll-event-events ] keep
|
swap >>events
|
||||||
[ set-epoll-event-fd ] keep ;
|
swap >>fd ;
|
||||||
|
|
||||||
:: do-epoll-ctl ( fd mx what events -- )
|
:: do-epoll-ctl ( fd mx what events -- )
|
||||||
mx fd>> what fd fd events make-event epoll_ctl io-error ;
|
mx fd>> what fd fd events make-event epoll_ctl io-error ;
|
||||||
|
@ -55,7 +55,7 @@ M: epoll-mx remove-output-callbacks ( fd mx -- seq )
|
||||||
epoll_wait multiplexer-error ;
|
epoll_wait multiplexer-error ;
|
||||||
|
|
||||||
: handle-event ( event mx -- )
|
: handle-event ( event mx -- )
|
||||||
[ epoll-event-fd ] dip
|
[ fd>> ] dip
|
||||||
[ EPOLLIN EPOLLOUT bitor do-epoll-del ]
|
[ EPOLLIN EPOLLOUT bitor do-epoll-del ]
|
||||||
[ input-available ] [ output-available ] 2tri ;
|
[ input-available ] [ output-available ] 2tri ;
|
||||||
|
|
||||||
|
|
|
@ -7,11 +7,11 @@ $nl
|
||||||
"The " { $slot "underlying" } " slot holds a " { $link c-ptr } " with the raw data. This pointer can be passed to C functions." } ;
|
"The " { $slot "underlying" } " slot holds a " { $link c-ptr } " with the raw data. This pointer can be passed to C functions." } ;
|
||||||
|
|
||||||
HELP: <struct-array>
|
HELP: <struct-array>
|
||||||
{ $values { "length" integer } { "c-type" string } { "struct-array" struct-array } }
|
{ $values { "length" integer } { "struct-class" struct-class } { "struct-array" struct-array } }
|
||||||
{ $description "Creates a new array for holding values of the specified C type." } ;
|
{ $description "Creates a new array for holding values of the specified struct type." } ;
|
||||||
|
|
||||||
HELP: <direct-struct-array>
|
HELP: <direct-struct-array>
|
||||||
{ $values { "alien" c-ptr } { "length" integer } { "c-type" string } { "struct-array" struct-array } }
|
{ $values { "alien" c-ptr } { "length" integer } { "struct-class" struct-class } { "struct-array" struct-array } }
|
||||||
{ $description "Creates a new array for holding values of the specified C type, backed by the memory at " { $snippet "alien" } "." } ;
|
{ $description "Creates a new array for holding values of the specified C type, backed by the memory at " { $snippet "alien" } "." } ;
|
||||||
|
|
||||||
HELP: struct-array-on
|
HELP: struct-array-on
|
||||||
|
|
|
@ -51,3 +51,5 @@ STRUCT: fixed-string { text char[100] } ;
|
||||||
[ { ALIEN: 123 ALIEN: 223 ALIEN: 323 ALIEN: 423 } ] [
|
[ { ALIEN: 123 ALIEN: 223 ALIEN: 323 ALIEN: 423 } ] [
|
||||||
ALIEN: 123 4 fixed-string <direct-struct-array> [ (underlying)>> ] { } map-as
|
ALIEN: 123 4 fixed-string <direct-struct-array> [ (underlying)>> ] { } map-as
|
||||||
] unit-test
|
] unit-test
|
||||||
|
|
||||||
|
[ 10 "int" <struct-array> ] must-fail
|
|
@ -5,9 +5,6 @@ classes classes.struct kernel libc math parser sequences
|
||||||
sequences.private words fry memoize compiler.units ;
|
sequences.private words fry memoize compiler.units ;
|
||||||
IN: struct-arrays
|
IN: struct-arrays
|
||||||
|
|
||||||
: c-type-struct-class ( c-type -- class )
|
|
||||||
c-type boxed-class>> ; foldable
|
|
||||||
|
|
||||||
TUPLE: struct-array
|
TUPLE: struct-array
|
||||||
{ underlying c-ptr read-only }
|
{ underlying c-ptr read-only }
|
||||||
{ length array-capacity read-only }
|
{ length array-capacity read-only }
|
||||||
|
@ -15,35 +12,39 @@ TUPLE: struct-array
|
||||||
{ class read-only }
|
{ class read-only }
|
||||||
{ ctor read-only } ;
|
{ ctor read-only } ;
|
||||||
|
|
||||||
M: struct-array length length>> ; inline
|
<PRIVATE
|
||||||
M: struct-array byte-length [ length>> ] [ element-size>> ] bi * ; inline
|
|
||||||
|
|
||||||
: (nth-ptr) ( i struct-array -- alien )
|
: (nth-ptr) ( i struct-array -- alien )
|
||||||
[ element-size>> * >fixnum ] [ underlying>> ] bi <displaced-alien> ; inline
|
[ element-size>> * >fixnum ] [ underlying>> ] bi <displaced-alien> ; inline
|
||||||
|
|
||||||
|
: (struct-element-constructor) ( struct-class -- word )
|
||||||
|
[
|
||||||
|
"struct-array-ctor" f <word>
|
||||||
|
[ swap '[ _ memory>struct ] (( alien -- object )) define-inline ] keep
|
||||||
|
] with-compilation-unit ;
|
||||||
|
|
||||||
|
! Foldable memo word. This is an optimization; by precompiling a
|
||||||
|
! constructor for array elements, we avoid memory>struct's slow path.
|
||||||
|
MEMO: struct-element-constructor ( struct-class -- word )
|
||||||
|
(struct-element-constructor) ; foldable
|
||||||
|
|
||||||
|
PRIVATE>
|
||||||
|
|
||||||
|
M: struct-array length length>> ; inline
|
||||||
|
|
||||||
|
M: struct-array byte-length [ length>> ] [ element-size>> ] bi * ; inline
|
||||||
|
|
||||||
M: struct-array nth-unsafe
|
M: struct-array nth-unsafe
|
||||||
[ (nth-ptr) ] [ ctor>> ] bi execute( alien -- object ) ; inline
|
[ (nth-ptr) ] [ ctor>> ] bi execute( alien -- object ) ; inline
|
||||||
|
|
||||||
M: struct-array set-nth-unsafe
|
M: struct-array set-nth-unsafe
|
||||||
[ (nth-ptr) swap ] [ element-size>> ] bi memcpy ; inline
|
[ (nth-ptr) swap ] [ element-size>> ] bi memcpy ; inline
|
||||||
|
|
||||||
: (struct-element-constructor) ( c-type -- word )
|
ERROR: not-a-struct-class struct-class ;
|
||||||
[
|
|
||||||
"struct-array-ctor" f <word>
|
|
||||||
[
|
|
||||||
swap dup struct-class?
|
|
||||||
[ '[ _ memory>struct ] [ ] like ] [ drop [ ] ] if
|
|
||||||
(( alien -- object )) define-inline
|
|
||||||
] keep
|
|
||||||
] with-compilation-unit ;
|
|
||||||
|
|
||||||
! Foldable memo word. This is an optimization; by precompiling a
|
: <direct-struct-array> ( alien length struct-class -- struct-array )
|
||||||
! constructor for array elements, we avoid memory>struct's slow path.
|
dup struct-class? [ not-a-struct-class ] unless
|
||||||
MEMO: struct-element-constructor ( c-type -- word )
|
[ heap-size ] [ ] [ struct-element-constructor ]
|
||||||
(struct-element-constructor) ; foldable
|
|
||||||
|
|
||||||
: <direct-struct-array> ( alien length c-type -- struct-array )
|
|
||||||
[ heap-size ] [ c-type-struct-class ] [ struct-element-constructor ]
|
|
||||||
tri struct-array boa ; inline
|
tri struct-array boa ; inline
|
||||||
|
|
||||||
M: struct-array new-sequence
|
M: struct-array new-sequence
|
||||||
|
@ -54,7 +55,7 @@ M: struct-array resize ( n seq -- newseq )
|
||||||
[ [ element-size>> * ] [ underlying>> ] bi resize ] [ class>> ] 2bi
|
[ [ element-size>> * ] [ underlying>> ] bi resize ] [ class>> ] 2bi
|
||||||
<direct-struct-array> ; inline
|
<direct-struct-array> ; inline
|
||||||
|
|
||||||
: <struct-array> ( length c-type -- struct-array )
|
: <struct-array> ( length struct-class -- struct-array )
|
||||||
[ heap-size * <byte-array> ] 2keep <direct-struct-array> ; inline
|
[ heap-size * <byte-array> ] 2keep <direct-struct-array> ; inline
|
||||||
|
|
||||||
ERROR: bad-byte-array-length byte-array ;
|
ERROR: bad-byte-array-length byte-array ;
|
||||||
|
|
|
@ -1,11 +1,11 @@
|
||||||
IN: struct-vectors
|
IN: struct-vectors
|
||||||
USING: help.markup help.syntax alien strings math ;
|
USING: help.markup help.syntax classes.struct alien strings math ;
|
||||||
|
|
||||||
HELP: struct-vector
|
HELP: struct-vector
|
||||||
{ $class-description "The class of growable C struct and union arrays." } ;
|
{ $class-description "The class of growable C struct and union arrays." } ;
|
||||||
|
|
||||||
HELP: <struct-vector>
|
HELP: <struct-vector>
|
||||||
{ $values { "capacity" integer } { "c-type" string } { "struct-vector" struct-vector } }
|
{ $values { "capacity" integer } { "struct-class" struct-class } { "struct-vector" struct-vector } }
|
||||||
{ $description "Creates a new vector with the given initial capacity." } ;
|
{ $description "Creates a new vector with the given initial capacity." } ;
|
||||||
|
|
||||||
ARTICLE: "struct-vectors" "C struct and union vectors"
|
ARTICLE: "struct-vectors" "C struct and union vectors"
|
||||||
|
|
|
@ -1,21 +1,16 @@
|
||||||
IN: struct-vectors.tests
|
IN: struct-vectors.tests
|
||||||
USING: struct-vectors tools.test alien.c-types alien.syntax
|
USING: struct-vectors tools.test alien.c-types classes.struct accessors
|
||||||
namespaces kernel sequences ;
|
namespaces kernel sequences ;
|
||||||
|
|
||||||
C-STRUCT: point
|
STRUCT: point { x float } { y float } ;
|
||||||
{ "float" "x" }
|
|
||||||
{ "float" "y" } ;
|
|
||||||
|
|
||||||
: make-point ( x y -- point )
|
: make-point ( x y -- point ) point <struct-boa> ;
|
||||||
"point" <c-object>
|
|
||||||
[ set-point-y ] keep
|
|
||||||
[ set-point-x ] keep ;
|
|
||||||
|
|
||||||
[ ] [ 1 "point" <struct-vector> "v" set ] unit-test
|
[ ] [ 1 point <struct-vector> "v" set ] unit-test
|
||||||
|
|
||||||
[ 1.5 6.0 ] [
|
[ 1.5 6.0 ] [
|
||||||
1.0 2.0 make-point "v" get push
|
1.0 2.0 make-point "v" get push
|
||||||
3.0 4.5 make-point "v" get push
|
3.0 4.5 make-point "v" get push
|
||||||
1.5 6.0 make-point "v" get push
|
1.5 6.0 make-point "v" get push
|
||||||
"v" get pop [ point-x ] [ point-y ] bi
|
"v" get pop [ x>> ] [ y>> ] bi
|
||||||
] unit-test
|
] unit-test
|
|
@ -9,10 +9,11 @@ TUPLE: struct-vector
|
||||||
{ length array-capacity }
|
{ length array-capacity }
|
||||||
{ c-type read-only } ;
|
{ c-type read-only } ;
|
||||||
|
|
||||||
: <struct-vector> ( capacity c-type -- struct-vector )
|
: <struct-vector> ( capacity struct-class -- struct-vector )
|
||||||
[ <struct-array> 0 ] keep struct-vector boa ; inline
|
[ <struct-array> 0 ] keep struct-vector boa ; inline
|
||||||
|
|
||||||
M: struct-vector byte-length underlying>> byte-length ;
|
M: struct-vector byte-length underlying>> byte-length ;
|
||||||
|
|
||||||
M: struct-vector new-sequence
|
M: struct-vector new-sequence
|
||||||
[ c-type>> <struct-array> ] [ [ >fixnum ] [ c-type>> ] bi* ] 2bi
|
[ c-type>> <struct-array> ] [ [ >fixnum ] [ c-type>> ] bi* ] 2bi
|
||||||
struct-vector boa ;
|
struct-vector boa ;
|
||||||
|
|
|
@ -1,16 +1,16 @@
|
||||||
! Copyright (C) 2008 Slava Pestov.
|
! Copyright (C) 2008 Slava Pestov.
|
||||||
! See http://factorcode.org/license.txt for BSD license.
|
! See http://factorcode.org/license.txt for BSD license.
|
||||||
IN: unix.linux.epoll
|
IN: unix.linux.epoll
|
||||||
USING: alien.syntax math ;
|
USING: alien.syntax classes.struct math ;
|
||||||
|
|
||||||
FUNCTION: int epoll_create ( int size ) ;
|
FUNCTION: int epoll_create ( int size ) ;
|
||||||
|
|
||||||
FUNCTION: int epoll_ctl ( int epfd, int op, int fd, epoll_event* event ) ;
|
FUNCTION: int epoll_ctl ( int epfd, int op, int fd, epoll_event* event ) ;
|
||||||
|
|
||||||
C-STRUCT: epoll-event
|
STRUCT: epoll-event
|
||||||
{ "uint" "events" }
|
{ events uint }
|
||||||
{ "uint" "fd" }
|
{ fd uint }
|
||||||
{ "uint" "padding" } ;
|
{ padding uint } ;
|
||||||
|
|
||||||
FUNCTION: int epoll_wait ( int epfd, epoll_event* events, int maxevents, int timeout ) ;
|
FUNCTION: int epoll_wait ( int epfd, epoll_event* events, int maxevents, int timeout ) ;
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue