duskos

dusk os fork
git clone git://git.alexwennerberg.com/duskos
Log | Files | Refs | README | LICENSE

commit daaa389b5dff08246414dc752a7f82b3440bf726
parent 2cc18190d479b458ae0171ddb3771a7884150602
Author: Virgil Dupras <hsoft@hardcoded.net>
Date:   Thu, 26 Jan 2023 21:02:46 -0500

sys/io: make MemIO bounded

Necessary if one wants to :interpret from it...

Diffstat:
Mfs/asm/uxntal.fs | 2+-
Mfs/doc/sys/io.txt | 16++++++++++------
Mfs/emul/uxn/varvara.fs | 2+-
Mfs/sys/io.fs | 21++++++++++++++-------
Mfs/tests/ar/ungz.fs | 2+-
Mfs/tests/harness.fs | 10+++++-----
Mfs/tests/sys/io.fs | 14+++++++-------
7 files changed, 39 insertions(+), 28 deletions(-)

diff --git a/fs/asm/uxntal.fs b/fs/asm/uxntal.fs @@ -3,7 +3,7 @@ ?f<< /lib/arena.h cc<< /asm/uxntal.c -uxn_ram MemIO :new structbind MemIO _memio +uxn_ram $10000 MemIO :new structbind MemIO _memio : tal>vm ( strpath -- res ) uxn_ram $100 + to _memio ptr _memio :self to stdio writeio diff --git a/fs/doc/sys/io.txt b/fs/doc/sys/io.txt @@ -213,18 +213,22 @@ mycrc32io SumIO res ^ .x \ prints crc32 of otheriohandle's contents ## MemIO MemIO is a structure that extends IO and provides read/write capability to a -memory area. It is unbounded and simply starts at a particular address, from -which you start reading or writing from/to. This is often used as a way to use -IO's nice formatting capabilities directly to memory. +memory area. When it reaches the end of its buffer, reads will yield EOF and +writes will fail. You can seek inside the area by setting "ptr" directly. Fields: -ptr -- where the MemIO is currently pointing +buf( Lower bound of the buffer +)buf Upper bound of the buffer +ptr Where the MemIO is currently pointing Words: -:new ( a -- hdl ) - Allocate a new MemIO pointing to address a +:new ( a u -- hdl ) + Allocate a new MemIO pointing to address range "a u". + +:rewind ( hdl -- ) + Set "ptr" to "buf(". ## ByteWriter diff --git a/fs/emul/uxn/varvara.fs b/fs/emul/uxn/varvara.fs @@ -26,7 +26,7 @@ \ read/write operation. Slightly more costly in multi-calls, but simpler. create _fnbuf $101 allot create _fpos 8 allot0 \ current file position -0 MemIO :new structbind MemIO _memio +uxn_ram $10000 MemIO :new structbind MemIO _memio : _findfn ( dev -- path-or-0 ) 8 swap devshort@ uxn_ram + ( a ) diff --git a/fs/sys/io.fs b/fs/sys/io.fs @@ -59,15 +59,22 @@ extends IO struct[ SumIO ]struct extends IO struct[ MemIO + sfield buf( + sfield )buf sfield ptr + + : _bounds ( n hdl -- n ) dup )buf swap ptr - min ; : _readbuf ( n hdl -- a? read-n ) >r \ V1=self - dup if r@ ptr swap dup to+ r> ptr else rdrop then ; - : _writebuf ( a n hdl -- written-n ) - dup >r ptr ( src n dst ) over >r swap move - r> dup r> ( written-n written-n hdl ) to+ ptr ; - : :new ( a -- hdl ) - here 0 ( putback ) , ['] _readbuf , ['] _writebuf , ['] drop , ['] drop , - swap ( a ) , ; + V1 _bounds dup if r@ ptr swap dup to+ r> ptr else rdrop then ; + : _writebuf ( a n hdl -- written-n ) >r \ V1=self + V1 _bounds dup if ( a n ) + dup >r V1 ptr ( src n dst ) swap move \ V2=n + r> dup r> ( n n hdl ) to+ ptr ( written-n ) + else nip rdrop then ; + : :rewind ( hdl -- ) dup buf( swap to ptr ; + : :new ( a u -- hdl ) + here >r 0 ( putback ) , ['] _readbuf , ['] _writebuf , ['] drop , ['] drop , + over , over + , , r> ; ]struct extends IO struct[ ByteWriter diff --git a/fs/tests/ar/ungz.fs b/fs/tests/ar/ungz.fs @@ -4,7 +4,7 @@ testbegin \ Testing ar/ungz create _expected ," Hello from compressed file!" create _resultbuf $20 allot -_resultbuf MemIO :new to stdio writeio +_resultbuf $20 MemIO :new to stdio writeio ' ungz S" /tests/ar/hello.gz" with-stdin-file ( err ) 0 #eq _resultbuf _expected 27 []= # diff --git a/fs/tests/harness.fs b/fs/tests/harness.fs @@ -3,19 +3,19 @@ : #eq ( n n -- ) 2dup = if 2drop else swap .x ." != " .x abort then ; create _buf $100 allot -_buf 1+ MemIO :new const _memio +_buf 1+ $ff MemIO :new structbind MemIO _memio \ capture is called with one word to call with capture on. It returns the \ captured string. $ff bytes max. : capture ( -- str ) word - _buf 1+ to _memio MemIO ptr - _memio to@! console writeio >r - _memio to@! stdio writeio >r + _memio :rewind + _memio :self to@! console writeio >r + _memio :self to@! stdio writeio >r runword r> to stdio writeio r> to console writeio - _memio MemIO ptr _buf - 1- ( sz ) + _memio ptr _memio buf( - ( sz ) _buf c! _buf ; : #s= ( s1 s2 -- ) 2dup s= if 2drop else swap stype ." != " stype abort then ; diff --git a/fs/tests/sys/io.fs b/fs/tests/sys/io.fs @@ -3,28 +3,28 @@ testbegin \ Tests for sys/io \ MemIO create mymem ," hello there!" -mymem MemIO :new structbind MemIO memio +mymem 12 MemIO :new structbind MemIO memio here 12 memio :read mymem here 12 []= # -mymem to memio ptr +memio :rewind S" foobar\n" dup memio :puts ( s ) c@+ ( a len ) mymem rot> []= # mymem 7 + memio ptr #eq -mymem to memio ptr +memio :rewind memio :readline S" foobar" #s= \ Pipes -mymem to memio ptr +memio :rewind memio :self dup Pipe :new structbind Pipe pipe S" hello pipe\n" pipe :puts -mymem to memio ptr +memio :rewind pipe :readline S" hello pipe" #s= \ Pipe filters : encrypt ( n hdl -- a? read-n ) r> execute dup if ( a u ) 2dup >r begin dup c@ dup SPC > if 1+ then swap c!+ next drop then ; -mymem to memio ptr +memio :rewind ' encrypt pipe :addrfilter pipe :readline S" ifmmp qjqf" #s= @@ -34,7 +34,7 @@ pipe :readline S" ifmmp qjqf" #s= >r swap r> execute ( written-n R:n-diff ) r> + ; ' half pipe :addwfilter -mymem to memio ptr +memio :rewind mymem 12 0 fill S" foobarbaz" pipe :puts mymem S" fooba\0" c@+ []= #