Annotation of gforth/blocks.fs, revision 1.20
1.5 pazsan 1: \ A less simple implementation of the blocks wordset.
1.1 anton 2:
1.19 anton 3: \ Copyright (C) 1995,1996,1997,1998 Free Software Foundation, Inc.
1.7 anton 4:
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
1.1 anton 23: \ provide it and many buffers on OSs that do not provide mmap.
24:
1.5 pazsan 25: \ Now, the replacement algorithm is "direct mapped"; change to LRU
26: \ if too slow. Using more buffers helps, too.
27:
1.1 anton 28: \ I think I avoid the assumption 1 char = 1 here, but I have not tested this
29:
1.2 pazsan 30: \ 1024 constant chars/block \ mandated by the standard
1.1 anton 31:
1.5 pazsan 32: require struct.fs
33:
34: struct
1.17 anton 35: 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
1.5 pazsan 40: end-struct buffer-struct
41:
42: Variable block-buffers
43: Variable last-block
44:
45: $20 Value buffers
46:
47: User block-fid
1.1 anton 48:
1.17 anton 49: : block-cold ( -- )
1.16 jwilke 50: block-fid off last-block off
1.17 anton 51: buffer-struct buffers * %alloc dup block-buffers ! ( addr )
52: buffer-struct %size buffers * erase ;
1.1 anton 53:
1.16 jwilke 54: ' block-cold INIT8 chained
1.5 pazsan 55:
56: block-cold
57:
1.12 anton 58: Defer flush-blocks
1.5 pazsan 59:
1.11 anton 60: : open-blocks ( addr u -- ) \ gforth
1.10 anton 61: \g use the file, whose name is given by @var{addr u}, as blocks file
1.18 pazsan 62: 2dup open-fpath-file 0<>
1.5 pazsan 63: if
1.18 pazsan 64: r/w bin create-file throw
1.5 pazsan 65: else
1.8 pazsan 66: rot close-file throw 2dup file-status throw bin open-file throw
67: >r 2drop r>
1.5 pazsan 68: then
1.12 anton 69: block-fid @ IF flush-blocks block-fid @ close-file throw THEN
1.5 pazsan 70: block-fid ! ;
1.8 pazsan 71:
1.10 anton 72: : use ( "file" -- ) \ gforth
73: \g use @var{file} as blocks file
1.11 anton 74: name open-blocks ;
1.1 anton 75:
1.3 anton 76: \ the file is opened as binary file, since it either will contain text
77: \ without newlines or binary data
1.20 ! pazsan 78: : get-block-fid ( -- fid ) \ gforth
1.1 anton 79: block-fid @ 0=
80: if
1.11 anton 81: s" blocks.fb" open-blocks
1.1 anton 82: then
83: block-fid @ ;
84:
1.20 ! pazsan 85: : block-position ( u -- ) \ block
! 86: \G positions the block file to the start of block u
1.3 anton 87: 1- chars/block chars um* get-block-fid reposition-file throw ;
1.1 anton 88:
1.20 ! pazsan 89: : update ( -- ) \ block
1.5 pazsan 90: last-block @ ?dup IF buffer-dirty on THEN ;
1.1 anton 91:
1.20 ! pazsan 92: : save-buffer ( buffer -- ) \ gforth
! 93: >r
1.5 pazsan 94: r@ buffer-dirty @ r@ buffer-block @ 0<> and
1.1 anton 95: if
1.5 pazsan 96: 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:
1.20 ! pazsan 102: : empty-buffer ( buffer -- ) \ gforth
1.5 pazsan 103: buffer-block off ;
104:
1.20 ! pazsan 105: : save-buffers ( -- ) \ block
! 106: block-buffers @
1.5 pazsan 107: buffers 0 ?DO dup save-buffer next-buffer LOOP drop ;
1.1 anton 108:
1.20 ! pazsan 109: : empty-buffers ( -- ) \ block
! 110: block-buffers @
1.5 pazsan 111: buffers 0 ?DO dup empty-buffer next-buffer LOOP drop ;
1.1 anton 112:
1.20 ! pazsan 113: : flush ( -- ) \ block
1.1 anton 114: save-buffers
115: empty-buffers ;
116:
1.12 anton 117: ' flush IS flush-blocks
1.5 pazsan 118:
1.20 ! pazsan 119: : get-buffer ( n -- a-addr ) \ gforth
1.17 anton 120: buffers mod buffer-struct %size * block-buffers @ + ;
1.5 pazsan 121:
1.20 ! pazsan 122: : block ( u -- a-addr ) \ block
1.1 anton 123: dup 0= -35 and throw
1.5 pazsan 124: dup get-buffer >r
125: dup r@ buffer-block @ <>
1.9 pazsan 126: r@ buffer-fid @ block-fid @ <> or
1.1 anton 127: if
1.5 pazsan 128: r@ save-buffer
1.1 anton 129: dup block-position
1.5 pazsan 130: r@ block-buffer chars/block get-block-fid read-file throw
1.1 anton 131: \ clear the rest of the buffer if the file is too short
1.5 pazsan 132: r@ block-buffer over chars + chars/block rot chars - blank
133: r@ buffer-block !
134: get-block-fid r@ buffer-fid !
1.1 anton 135: else
136: drop
137: then
1.5 pazsan 138: r> dup last-block ! block-buffer ;
1.1 anton 139:
1.20 ! pazsan 140: : buffer ( u -- a-addr ) \ block
1.1 anton 141: \ reading in the block is unnecessary, but simpler
142: block ;
143:
1.2 pazsan 144: User scr 0 scr !
1.1 anton 145:
1.20 ! pazsan 146: : updated? ( n -- f ) \ gforth
! 147: scr @ buffer
1.5 pazsan 148: [ 0 buffer-dirty 0 block-buffer - ] Literal + @ ;
149:
1.20 ! pazsan 150: : list ( u -- ) \ block
1.1 anton 151: \ calling block again and again looks inefficient but is necessary
152: \ in a multitasking environment
153: dup scr !
1.5 pazsan 154: ." Screen " u.
155: updated? 0= IF ." not " THEN ." modified " cr
1.1 anton 156: 16 0
157: ?do
1.4 anton 158: i 2 .r space scr @ block i 64 * chars + 64 type cr
1.1 anton 159: loop ;
160:
1.2 pazsan 161: : (source) ( -- addr len )
162: blk @ ?dup
163: IF block chars/block
164: ELSE tib #tib @
165: THEN ;
166:
167: ' (source) IS source
168:
1.20 ! pazsan 169: : load ( i*x n -- j*x ) \ block
1.2 pazsan 170: push-file
1.18 pazsan 171: dup loadline ! blk ! >in off ['] interpret catch
172: pop-file throw ;
1.1 anton 173:
1.20 ! pazsan 174: : thru ( i*x n1 n2 -- j*x ) \ block
1.15 pazsan 175: 1+ swap ?DO I load LOOP ;
1.1 anton 176:
1.20 ! pazsan 177: : +load ( i*x n -- j*x ) \ block
! 178: blk @ + load ;
1.2 pazsan 179:
1.20 ! pazsan 180: : +thru ( i*x n1 n2 -- j*x ) \ block
1.15 pazsan 181: 1+ swap ?DO I +load LOOP ;
1.4 anton 182:
1.20 ! pazsan 183: : --> ( -- ) \ block- block
! 184: refill drop ; immediate
1.5 pazsan 185:
1.20 ! pazsan 186: : block-included ( addr u -- ) \ gforth
1.11 anton 187: block-fid @ >r block-fid off open-blocks
1.5 pazsan 188: 1 load block-fid @ close-file throw flush
189: r> block-fid ! ;
190:
1.13 anton 191: \ thrown out because it may provide unpleasant surprises - anton
192: \ : include ( "name" -- )
193: \ name 2dup dup 3 - /string s" .fb" compare
194: \ 0= IF block-included ELSE included THEN ;
1.5 pazsan 195:
1.4 anton 196: get-current environment-wordlist set-current
197: true constant block
198: true constant block-ext
199: set-current
1.5 pazsan 200:
1.16 jwilke 201: : bye ['] flush catch drop bye ;
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>