diff --git a/basis/alien/data/data.factor b/basis/alien/data/data.factor index 7bde58c7e9..ac7478d24a 100644 --- a/basis/alien/data/data.factor +++ b/basis/alien/data/data.factor @@ -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 - 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 stream alien<< + n ; inline +M: memory-stream stream-read-partial-unsafe stream-read-unsafe ; inline M: value-type c-type-rep drop int-rep ; diff --git a/core/io/streams/memory/memory-tests.factor b/core/io/streams/memory/memory-tests.factor new file mode 100644 index 0000000000..d6810084e3 --- /dev/null +++ b/core/io/streams/memory/memory-tests.factor @@ -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 } + [ stream-read1 ] [ stream-read1 ] [ stream-read1 ] tri +] unit-test + +{ 1 2 3 } [ + [ + B{ 1 2 3 } malloc-byte-array &free + [ 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 } + [ 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 + [ 3 swap stream-read ] [ 5 swap stream-read ] bi + ] with-destructors +] unit-test diff --git a/core/io/streams/memory/memory.factor b/core/io/streams/memory/memory.factor index e7b4338388..67dc53844e 100644 --- a/core/io/streams/memory/memory.factor +++ b/core/io/streams/memory/memory.factor @@ -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 ; : ( 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 ] change-alien drop + 0 alien-unsigned-1 ; inline