duskos

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

commit 8dfcb448c36994abe236a01ea556f032e2455ac0
parent b180762cbe74c58a28b05778b0e28b8f87030f6d
Author: Virgil Dupras <hsoft@hardcoded.net>
Date:   Sun, 27 Nov 2022 12:05:19 -0500

sys/io: add MemIO struct

Diffstat:
Mfs/doc/io.txt | 12++++++++++++
Mfs/sys/io.fs | 10++++++++++
Mfs/tests/sys/all.fs | 1+
Afs/tests/sys/io.fs | 11+++++++++++
4 files changed, 34 insertions(+), 0 deletions(-)

diff --git a/fs/doc/io.txt b/fs/doc/io.txt @@ -104,6 +104,18 @@ If, for example, you want to create a CRC32 calculator, you'd do: mycrc32io otheriohandle IO :spit 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. + +ptr -- where the MemIO is currently pointing + +:new ( a -- hdl ) + Allocate a new MemIO pointing to address a + ## SystemIn and SystemOut SystemIn and SystemOut are structures that wrap around "key" and "emit", diff --git a/fs/sys/io.fs b/fs/sys/io.fs @@ -52,3 +52,13 @@ extends IO struct[ SumIO 0 ( putback ) , ['] _ioerr , ['] _writebuf , ['] drop , ['] drop , ( 'fn ) , 0 , ; ]struct + +extends IO struct[ MemIO + sfield ptr + : _readbuf ( n hdl -- a? read-n ) ptr swap ; + : _writebuf ( a n hdl -- written-n ) + ptr ( src n dst ) over >r swap move r> ; + : :new ( a -- hdl ) + here 0 ( putback ) , ['] _readbuf , ['] _writebuf , ['] drop , ['] drop , + swap ( a ) , ; +]struct diff --git a/fs/tests/sys/all.fs b/fs/tests/sys/all.fs @@ -1,2 +1,3 @@ \ Run all sys test suites +f<< /tests/sys/io.fs f<< /tests/sys/file.fs diff --git a/fs/tests/sys/io.fs b/fs/tests/sys/io.fs @@ -0,0 +1,11 @@ +?f<< /tests/harness.fs +testbegin +\ Tests for sys/io +create mymem ," hello there!" +mymem MemIO :new structbind MemIO memio +here 12 memio :read +mymem here 12 []= # +mymem to memio ptr +S" foobar" dup memio :puts ( s ) +c@+ ( a len ) mymem rot> []= # +testend