commit 07281f1492dedac922415e4f135150d176fade97
parent c3c8b4894bab42621cae1b410cc29e2695c8bfd5
Author: Virgil Dupras <hsoft@hardcoded.net>
Date: Tue, 12 Jul 2022 08:50:42 -0400
Extract /lib/scratch from /sys/scratch
... then make a private scratchpad for /sys/file.
Diffstat:
4 files changed, 55 insertions(+), 27 deletions(-)
diff --git a/fs/lib/scratch.fs b/fs/lib/scratch.fs
@@ -0,0 +1,32 @@
+\ Scratchpads
+?f<< /lib/struct.fs
+
+\ Scratchpads are circular buffers for placing semi-temporary strings (or
+\ other sequences). The scratchpad has a running pointer and when we need
+\ holding space, we reserve the current pointer and then allocate some space.
+\ The pointer advances and is ready for the next piece of data. When it gets
+\ at the end of the buffer, it goes back to the beginning.
+
+\ The system scratchpad lives at sys/scratch.
+
+struct Scratchpad
+ field scratchsize
+ field scratch>
+ 'field scratch(
+
+0 value _here
+
+: scratchpad$ ( size "name" -- ) create dup , here CELLSZ + , allot ;
+: scratch) scratch( scratchsize + ;
+: scratchallot ( n -- a )
+ scratch> over + scratch) >= if ." scratch reload!" nl> scratch( to scratch> then
+ scratch> swap to+ scratch> ( a ) ;
+\ push a range to the scratchpad as a string
+: []>str ( a u -- str )
+ dup 1+ scratchallot ( src u dst-1 ) >r dup r@ c!+ swap ( src dst u ) move r> ;
+
+\ Open a scratch area for writing
+: scratch[ ( -- ) here to _here scratch> to here ;
+\ Stop writing to the scratch area and restore here
+\ Returs the address of the beginning of the written area
+: ]scratch ( -- a ) scratch> here to scratch> _here to here ;
diff --git a/fs/sys/file.fs b/fs/sys/file.fs
@@ -1,5 +1,5 @@
\ File subsystem
-\ requires sys/scratch
+\ require /sys/scratch.fs \ TODO: use "with" to avoid referencing syspad
\ This subsystems defines a "filesystem" protocol and upon it defines
\ convenience word around files. This subsystem is a bit weird because the
@@ -18,10 +18,17 @@
\ Read a single character from fcursor. If the end of file (EOF) is reached,
\ returns -1.
+\ We need a private scratchpad here because some cursors can be quite
+\ long-lived. If we use the system scratchpad, short-lived data will overwrite
+\ our cursors.
+$200 scratchpad$ filespad
+
\ This creates a "f<" reader with the file descriptor embedded in it. This
\ allows for a straightforward override of input/output words.
: [f<] ( curfd -- word )
- scratch[ litn compile fgetc exit, ]scratch ;
+ filespad to Scratchpad
+ scratch[ litn compile fgetc exit, ]scratch
+ syspad to Scratchpad ;
: .floaded floaded begin dup while dup 4 + stype nl> @ repeat drop ;
: require word dup floaded? not if stype abort" required" else drop then ;
diff --git a/fs/sys/scratch.fs b/fs/sys/scratch.fs
@@ -1,27 +1,11 @@
-\ Scratchpad
+\ System scratchpad
+?f<< /lib/scratch.fs
-\ The scratchpad is a circular buffer for placing semi-temporary strings (or
-\ other sequences). The scratchpad has a running pointer and when we need
-\ holding space, we reserve the current pointer and then allocate some space.
-\ The pointer advances and is ready for the next piece of data. When it gets
-\ at the end of the buffer, it goes back to the beginning.
+\ There is only one system scratchpad, but you can create specialized pads for
+\ specific purposes. One such purpose is sys/files cursors, where that data to
+\ scratch is small, but can be long lived (a long running forth script). If
+\ these cursor live in the system scratchpad, they'd be overwritten by faster-
+\ paced data.
-$4000 value scratchsize
-0 value _here
-create scratch( scratchsize allot
-: scratch) scratch( scratchsize + ;
-\ pointer to current position in buffer
-scratch( value scratch>
-
-: scratchallot ( n -- a )
- scratch> over + scratch) >= if scratch( to scratch> then
- scratch> swap to+ scratch> ( a ) ;
-\ push a range to the scratchpad as a string
-: []>str ( a u -- str )
- dup 1+ scratchallot ( src u dst-1 ) >r dup r@ c!+ swap ( src dst u ) move r> ;
-
-\ Open a scratch area for writing
-: scratch[ ( -- ) here to _here scratch> to here ;
-\ Stop writing to the scratch area and restore here
-\ Returs the address of the beginning of the written area
-: ]scratch ( -- a ) scratch> here to scratch> _here to here ;
+$4000 scratchpad$ syspad
+syspad to Scratchpad
diff --git a/fs/tests/lib/crc.fs b/fs/tests/lib/crc.fs
@@ -0,0 +1,5 @@
+?f<< tests/harness.fs
+?f<< lib/crc.fs
+testbegin
+\ Tests for crc.fs
+testend