io.streams.memory: implement stream-read-unsafe

(The actual method is in alien.data due to bootstrap load order issues.)
db4
Joe Groff 2011-10-11 20:07:09 -07:00
parent d5f4b6f155
commit b79d7158be
3 changed files with 40 additions and 10 deletions

View File

@ -2,7 +2,7 @@
USING: accessors alien alien.arrays alien.c-types alien.strings
arrays byte-arrays combinators combinators.short-circuit
cpu.architecture fry generalizations io io.streams.memory kernel
libc macros math math.functions parser sequences
libc locals macros math math.functions parser sequences
stack-checker.dependencies summary words ;
QUALIFIED: math
IN: alien.data
@ -93,11 +93,13 @@ M: bad-byte-array-length summary
: malloc-string ( string encoding -- alien )
string>alien malloc-byte-array ;
M: memory-stream stream-read
[
[ index>> ] [ alien>> ] bi <displaced-alien>
swap memory>byte-array
] [ [ + ] change-index drop ] 2bi ;
INSTANCE: memory-stream noncopying-reader
M:: memory-stream stream-read-unsafe ( n buf stream -- count )
stream alien>> :> src
buf src n memcpy
n src <displaced-alien> stream alien<<
n ; inline
M: memory-stream stream-read-partial-unsafe stream-read-unsafe ; inline
M: value-type c-type-rep drop int-rep ;

View File

@ -0,0 +1,28 @@
! (c)2011 Joe Groff bsd license
USING: alien.data destructors io io.streams.memory kernel libc
tools.test ;
IN: io.streams.memory.tests
{ 1 2 3 } [
B{ 1 2 3 } <memory-stream>
[ stream-read1 ] [ stream-read1 ] [ stream-read1 ] tri
] unit-test
{ 1 2 3 } [
[
B{ 1 2 3 } malloc-byte-array &free <memory-stream>
[ stream-read1 ] [ stream-read1 ] [ stream-read1 ] tri
] with-destructors
] unit-test
{ B{ 1 2 3 } B{ 4 5 6 7 8 } } [
B{ 1 2 3 4 5 6 7 8 } <memory-stream>
[ 3 swap stream-read ] [ 5 swap stream-read ] bi
] unit-test
{ B{ 1 2 3 } B{ 4 5 6 7 8 } } [
[
B{ 1 2 3 4 5 6 7 8 } malloc-byte-array &free <memory-stream>
[ 3 swap stream-read ] [ 5 swap stream-read ] bi
] with-destructors
] unit-test

View File

@ -3,13 +3,13 @@
USING: kernel accessors alien alien.accessors math io ;
IN: io.streams.memory
TUPLE: memory-stream alien index ;
TUPLE: memory-stream alien ;
: <memory-stream> ( alien -- stream )
0 memory-stream boa ;
memory-stream boa ;
M: memory-stream stream-element-type drop +byte+ ;
M: memory-stream stream-read1
[ [ alien>> ] [ index>> ] bi alien-unsigned-1 ]
[ [ 1 + ] change-index drop ] bi ;
[ 1 over <displaced-alien> ] change-alien drop
0 alien-unsigned-1 ; inline