| 1 : |
pazsan
|
1.5
|
\ A less simple implementation of the blocks wordset. |
| 2 : |
anton
|
1.1
|
|
| 3 : |
anton
|
1.19
|
\ Copyright (C) 1995,1996,1997,1998 Free Software Foundation, Inc. |
| 4 : |
anton
|
1.7
|
|
| 5 : |
|
|
\ This file is part of Gforth. |
| 6 : |
|
|
|
| 7 : |
|
|
\ Gforth is free software; you can redistribute it and/or |
| 8 : |
|
|
\ modify it under the terms of the GNU General Public License |
| 9 : |
|
|
\ as published by the Free Software Foundation; either version 2 |
| 10 : |
|
|
\ of the License, or (at your option) any later version. |
| 11 : |
|
|
|
| 12 : |
|
|
\ This program is distributed in the hope that it will be useful, |
| 13 : |
|
|
\ but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 : |
|
|
\ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 : |
|
|
\ GNU General Public License for more details. |
| 16 : |
|
|
|
| 17 : |
|
|
\ You should have received a copy of the GNU General Public License |
| 18 : |
|
|
\ along with this program; if not, write to the Free Software |
| 19 : |
|
|
\ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 20 : |
|
|
|
| 21 : |
|
|
|
| 22 : |
|
|
\ A more efficient implementation would use mmap on OSs that |
| 23 : |
anton
|
1.1
|
\ provide it and many buffers on OSs that do not provide mmap. |
| 24 : |
|
|
|
| 25 : |
pazsan
|
1.5
|
\ Now, the replacement algorithm is "direct mapped"; change to LRU |
| 26 : |
|
|
\ if too slow. Using more buffers helps, too. |
| 27 : |
|
|
|
| 28 : |
anton
|
1.1
|
\ I think I avoid the assumption 1 char = 1 here, but I have not tested this |
| 29 : |
|
|
|
| 30 : |
pazsan
|
1.2
|
\ 1024 constant chars/block \ mandated by the standard |
| 31 : |
anton
|
1.1
|
|
| 32 : |
pazsan
|
1.5
|
require struct.fs |
| 33 : |
|
|
|
| 34 : |
|
|
struct |
| 35 : |
anton
|
1.17
|
cell% field buffer-block \ the block number |
| 36 : |
|
|
cell% field buffer-fid \ the block's fid |
| 37 : |
|
|
cell% field buffer-dirty \ the block dirty flag |
| 38 : |
|
|
char% chars/block * field block-buffer \ the data |
| 39 : |
|
|
cell% 0 * field next-buffer |
| 40 : |
pazsan
|
1.5
|
end-struct buffer-struct |
| 41 : |
|
|
|
| 42 : |
|
|
Variable block-buffers |
| 43 : |
|
|
Variable last-block |
| 44 : |
|
|
|
| 45 : |
|
|
$20 Value buffers |
| 46 : |
|
|
|
| 47 : |
|
|
User block-fid |
| 48 : |
anton
|
1.1
|
|
| 49 : |
anton
|
1.17
|
: block-cold ( -- ) |
| 50 : |
jwilke
|
1.16
|
block-fid off last-block off |
| 51 : |
anton
|
1.17
|
buffer-struct buffers * %alloc dup block-buffers ! ( addr ) |
| 52 : |
|
|
buffer-struct %size buffers * erase ; |
| 53 : |
anton
|
1.1
|
|
| 54 : |
jwilke
|
1.16
|
' block-cold INIT8 chained |
| 55 : |
pazsan
|
1.5
|
|
| 56 : |
|
|
block-cold |
| 57 : |
|
|
|
| 58 : |
anton
|
1.12
|
Defer flush-blocks |
| 59 : |
pazsan
|
1.5
|
|
| 60 : |
anton
|
1.11
|
: open-blocks ( addr u -- ) \ gforth |
| 61 : |
crook
|
1.23
|
\g Use the file, whose name is given by @var{addr u}, as the blocks file. |
| 62 : |
pazsan
|
1.18
|
2dup open-fpath-file 0<> |
| 63 : |
pazsan
|
1.5
|
if |
| 64 : |
pazsan
|
1.18
|
r/w bin create-file throw |
| 65 : |
pazsan
|
1.5
|
else |
| 66 : |
pazsan
|
1.8
|
rot close-file throw 2dup file-status throw bin open-file throw |
| 67 : |
|
|
>r 2drop r> |
| 68 : |
pazsan
|
1.5
|
then |
| 69 : |
anton
|
1.12
|
block-fid @ IF flush-blocks block-fid @ close-file throw THEN |
| 70 : |
pazsan
|
1.5
|
block-fid ! ; |
| 71 : |
pazsan
|
1.8
|
|
| 72 : |
anton
|
1.10
|
: use ( "file" -- ) \ gforth |
| 73 : |
crook
|
1.23
|
\g Use @var{file} as the blocks file. |
| 74 : |
anton
|
1.11
|
name open-blocks ; |
| 75 : |
anton
|
1.1
|
|
| 76 : |
anton
|
1.3
|
\ the file is opened as binary file, since it either will contain text |
| 77 : |
|
|
\ without newlines or binary data |
| 78 : |
pazsan
|
1.20
|
: get-block-fid ( -- fid ) \ gforth |
| 79 : |
anton
|
1.1
|
block-fid @ 0= |
| 80 : |
|
|
if |
| 81 : |
anton
|
1.11
|
s" blocks.fb" open-blocks |
| 82 : |
anton
|
1.1
|
then |
| 83 : |
|
|
block-fid @ ; |
| 84 : |
|
|
|
| 85 : |
pazsan
|
1.20
|
: block-position ( u -- ) \ block |
| 86 : |
crook
|
1.22
|
\G Position the block file to the start of block @var{u}. |
| 87 : |
anton
|
1.3
|
1- chars/block chars um* get-block-fid reposition-file throw ; |
| 88 : |
anton
|
1.1
|
|
| 89 : |
pazsan
|
1.20
|
: update ( -- ) \ block |
| 90 : |
pazsan
|
1.5
|
last-block @ ?dup IF buffer-dirty on THEN ; |
| 91 : |
anton
|
1.1
|
|
| 92 : |
pazsan
|
1.20
|
: save-buffer ( buffer -- ) \ gforth |
| 93 : |
|
|
>r |
| 94 : |
pazsan
|
1.5
|
r@ buffer-dirty @ r@ buffer-block @ 0<> and |
| 95 : |
anton
|
1.1
|
if |
| 96 : |
pazsan
|
1.5
|
r@ buffer-block @ block-position |
| 97 : |
|
|
r@ block-buffer chars/block r@ buffer-fid @ write-file throw |
| 98 : |
|
|
r@ buffer-dirty off |
| 99 : |
|
|
endif |
| 100 : |
|
|
rdrop ; |
| 101 : |
|
|
|
| 102 : |
pazsan
|
1.20
|
: empty-buffer ( buffer -- ) \ gforth |
| 103 : |
pazsan
|
1.5
|
buffer-block off ; |
| 104 : |
|
|
|
| 105 : |
pazsan
|
1.20
|
: save-buffers ( -- ) \ block |
| 106 : |
|
|
block-buffers @ |
| 107 : |
pazsan
|
1.5
|
buffers 0 ?DO dup save-buffer next-buffer LOOP drop ; |
| 108 : |
anton
|
1.1
|
|
| 109 : |
pazsan
|
1.20
|
: empty-buffers ( -- ) \ block |
| 110 : |
|
|
block-buffers @ |
| 111 : |
pazsan
|
1.5
|
buffers 0 ?DO dup empty-buffer next-buffer LOOP drop ; |
| 112 : |
anton
|
1.1
|
|
| 113 : |
pazsan
|
1.20
|
: flush ( -- ) \ block |
| 114 : |
anton
|
1.1
|
save-buffers |
| 115 : |
|
|
empty-buffers ; |
| 116 : |
|
|
|
| 117 : |
anton
|
1.12
|
' flush IS flush-blocks |
| 118 : |
pazsan
|
1.5
|
|
| 119 : |
pazsan
|
1.20
|
: get-buffer ( n -- a-addr ) \ gforth |
| 120 : |
anton
|
1.17
|
buffers mod buffer-struct %size * block-buffers @ + ; |
| 121 : |
pazsan
|
1.5
|
|
| 122 : |
crook
|
1.21
|
: block ( u -- a-addr ) \ block- block |
| 123 : |
crook
|
1.22
|
\G @var{u} identifies a block number. Assign a block buffer to @var{u}, |
| 124 : |
crook
|
1.21
|
\G make it the current block buffer and return its start |
| 125 : |
crook
|
1.22
|
\G address, @var{a-addr}. |
| 126 : |
anton
|
1.1
|
dup 0= -35 and throw |
| 127 : |
pazsan
|
1.5
|
dup get-buffer >r |
| 128 : |
|
|
dup r@ buffer-block @ <> |
| 129 : |
pazsan
|
1.9
|
r@ buffer-fid @ block-fid @ <> or |
| 130 : |
anton
|
1.1
|
if |
| 131 : |
pazsan
|
1.5
|
r@ save-buffer |
| 132 : |
anton
|
1.1
|
dup block-position |
| 133 : |
pazsan
|
1.5
|
r@ block-buffer chars/block get-block-fid read-file throw |
| 134 : |
anton
|
1.1
|
\ clear the rest of the buffer if the file is too short |
| 135 : |
pazsan
|
1.5
|
r@ block-buffer over chars + chars/block rot chars - blank |
| 136 : |
|
|
r@ buffer-block ! |
| 137 : |
|
|
get-block-fid r@ buffer-fid ! |
| 138 : |
anton
|
1.1
|
else |
| 139 : |
|
|
drop |
| 140 : |
|
|
then |
| 141 : |
pazsan
|
1.5
|
r> dup last-block ! block-buffer ; |
| 142 : |
anton
|
1.1
|
|
| 143 : |
pazsan
|
1.20
|
: buffer ( u -- a-addr ) \ block |
| 144 : |
anton
|
1.1
|
\ reading in the block is unnecessary, but simpler |
| 145 : |
|
|
block ; |
| 146 : |
|
|
|
| 147 : |
crook
|
1.21
|
User scr ( -- a-addr ) \ block-ext |
| 148 : |
crook
|
1.22
|
\G USER VARIABLE @var{a-addr} is the address of a cell containing |
| 149 : |
crook
|
1.21
|
\G the block number of the block most recently processed by |
| 150 : |
|
|
\G @code{LIST}. |
| 151 : |
|
|
0 scr ! |
| 152 : |
anton
|
1.1
|
|
| 153 : |
pazsan
|
1.20
|
: updated? ( n -- f ) \ gforth |
| 154 : |
|
|
scr @ buffer |
| 155 : |
pazsan
|
1.5
|
[ 0 buffer-dirty 0 block-buffer - ] Literal + @ ; |
| 156 : |
|
|
|
| 157 : |
pazsan
|
1.20
|
: list ( u -- ) \ block |
| 158 : |
anton
|
1.1
|
\ calling block again and again looks inefficient but is necessary |
| 159 : |
|
|
\ in a multitasking environment |
| 160 : |
|
|
dup scr ! |
| 161 : |
pazsan
|
1.5
|
." Screen " u. |
| 162 : |
|
|
updated? 0= IF ." not " THEN ." modified " cr |
| 163 : |
anton
|
1.1
|
16 0 |
| 164 : |
|
|
?do |
| 165 : |
anton
|
1.4
|
i 2 .r space scr @ block i 64 * chars + 64 type cr |
| 166 : |
anton
|
1.1
|
loop ; |
| 167 : |
|
|
|
| 168 : |
crook
|
1.23
|
: (source) ( -- c-addr u ) |
| 169 : |
pazsan
|
1.2
|
blk @ ?dup |
| 170 : |
|
|
IF block chars/block |
| 171 : |
|
|
ELSE tib #tib @ |
| 172 : |
|
|
THEN ; |
| 173 : |
|
|
|
| 174 : |
crook
|
1.23
|
' (source) IS source ( -- c-addr u ) \ core |
| 175 : |
|
|
\G @var{c-addr} is the address of the input buffer and @var{u} is the |
| 176 : |
|
|
\G number of characters in it. |
| 177 : |
pazsan
|
1.2
|
|
| 178 : |
pazsan
|
1.20
|
: load ( i*x n -- j*x ) \ block |
| 179 : |
pazsan
|
1.2
|
push-file |
| 180 : |
pazsan
|
1.18
|
dup loadline ! blk ! >in off ['] interpret catch |
| 181 : |
|
|
pop-file throw ; |
| 182 : |
anton
|
1.1
|
|
| 183 : |
pazsan
|
1.20
|
: thru ( i*x n1 n2 -- j*x ) \ block |
| 184 : |
pazsan
|
1.15
|
1+ swap ?DO I load LOOP ; |
| 185 : |
anton
|
1.1
|
|
| 186 : |
pazsan
|
1.20
|
: +load ( i*x n -- j*x ) \ block |
| 187 : |
|
|
blk @ + load ; |
| 188 : |
pazsan
|
1.2
|
|
| 189 : |
pazsan
|
1.20
|
: +thru ( i*x n1 n2 -- j*x ) \ block |
| 190 : |
pazsan
|
1.15
|
1+ swap ?DO I +load LOOP ; |
| 191 : |
anton
|
1.4
|
|
| 192 : |
pazsan
|
1.20
|
: --> ( -- ) \ block- block |
| 193 : |
crook
|
1.21
|
\G If this symbol is encountered whilst loading block @var{n}, |
| 194 : |
|
|
\G discard the remainder of the block and load block @var{n+1}. Used |
| 195 : |
|
|
\G for chaining multiple blocks together as a single loadable unit. |
| 196 : |
pazsan
|
1.20
|
refill drop ; immediate |
| 197 : |
pazsan
|
1.5
|
|
| 198 : |
pazsan
|
1.20
|
: block-included ( addr u -- ) \ gforth |
| 199 : |
anton
|
1.11
|
block-fid @ >r block-fid off open-blocks |
| 200 : |
pazsan
|
1.5
|
1 load block-fid @ close-file throw flush |
| 201 : |
|
|
r> block-fid ! ; |
| 202 : |
|
|
|
| 203 : |
anton
|
1.13
|
\ thrown out because it may provide unpleasant surprises - anton |
| 204 : |
|
|
\ : include ( "name" -- ) |
| 205 : |
|
|
\ name 2dup dup 3 - /string s" .fb" compare |
| 206 : |
|
|
\ 0= IF block-included ELSE included THEN ; |
| 207 : |
pazsan
|
1.5
|
|
| 208 : |
anton
|
1.4
|
get-current environment-wordlist set-current |
| 209 : |
|
|
true constant block |
| 210 : |
|
|
true constant block-ext |
| 211 : |
|
|
set-current |
| 212 : |
pazsan
|
1.5
|
|
| 213 : |
crook
|
1.21
|
: bye ( -- ) \ tools-ext |
| 214 : |
|
|
\G Return control to the host operating system (if any). |
| 215 : |
|
|
['] flush catch drop bye ; |