Annotation of gforth/blocks.fs, revision 1.35
1.5 pazsan 1: \ A less simple implementation of the blocks wordset.
1.1 anton 2:
1.32 anton 3: \ Copyright (C) 1995,1996,1997,1998,2000 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
1.33 anton 19: \ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
1.7 anton 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.30 anton 48: User block-offset ( -- addr ) \ gforth
49: \G User variable containing the number of the first block (default
50: \G since 0.5.0: 0). Block files created with Gforth versions before
51: \G 0.5.0 have the offset 1. If you use these files you can: @code{1
52: \G offset !}; or add 1 to every block number used; or prepend 1024
53: \G characters to the file.
54: 0 block-offset ! \ store 1 here fore 0.4.0 compatibility
55:
56: ' block-offset alias offset \ !! eliminate this?
1.1 anton 57:
1.17 anton 58: : block-cold ( -- )
1.16 jwilke 59: block-fid off last-block off
1.17 anton 60: buffer-struct buffers * %alloc dup block-buffers ! ( addr )
61: buffer-struct %size buffers * erase ;
1.1 anton 62:
1.16 jwilke 63: ' block-cold INIT8 chained
1.5 pazsan 64:
65: block-cold
66:
1.24 crook 67: Defer flush-blocks ( -- ) \ gforth
1.5 pazsan 68:
1.24 crook 69: : open-blocks ( c-addr u -- ) \ gforth
70: \g Use the file, whose name is given by @i{c-addr u}, as the blocks file.
1.18 pazsan 71: 2dup open-fpath-file 0<>
1.5 pazsan 72: if
1.18 pazsan 73: r/w bin create-file throw
1.5 pazsan 74: else
1.8 pazsan 75: rot close-file throw 2dup file-status throw bin open-file throw
76: >r 2drop r>
1.5 pazsan 77: then
1.12 anton 78: block-fid @ IF flush-blocks block-fid @ close-file throw THEN
1.5 pazsan 79: block-fid ! ;
1.8 pazsan 80:
1.10 anton 81: : use ( "file" -- ) \ gforth
1.24 crook 82: \g Use @i{file} as the blocks file.
1.11 anton 83: name open-blocks ;
1.1 anton 84:
1.3 anton 85: \ the file is opened as binary file, since it either will contain text
86: \ without newlines or binary data
1.24 crook 87: : get-block-fid ( -- wfileid ) \ gforth
88: \G Return the file-id of the current blocks file. If no blocks
89: \G file has been opened, use @file{blocks.fb} as the default
90: \G blocks file.
1.1 anton 91: block-fid @ 0=
92: if
1.11 anton 93: s" blocks.fb" open-blocks
1.1 anton 94: then
95: block-fid @ ;
96:
1.20 pazsan 97: : block-position ( u -- ) \ block
1.24 crook 98: \G Position the block file to the start of block @i{u}.
1.26 pazsan 99: offset @ - chars/block chars um* get-block-fid reposition-file throw ;
1.1 anton 100:
1.20 pazsan 101: : update ( -- ) \ block
1.29 crook 102: \G Mark the state of the current block buffer as assigned-dirty.
1.5 pazsan 103: last-block @ ?dup IF buffer-dirty on THEN ;
1.1 anton 104:
1.20 pazsan 105: : save-buffer ( buffer -- ) \ gforth
106: >r
1.5 pazsan 107: r@ buffer-dirty @ r@ buffer-block @ 0<> and
1.1 anton 108: if
1.5 pazsan 109: r@ buffer-block @ block-position
110: r@ block-buffer chars/block r@ buffer-fid @ write-file throw
1.35 ! anton 111: buffer-fid @ flush-file throw
1.5 pazsan 112: r@ buffer-dirty off
113: endif
114: rdrop ;
115:
1.20 pazsan 116: : empty-buffer ( buffer -- ) \ gforth
1.5 pazsan 117: buffer-block off ;
118:
1.20 pazsan 119: : save-buffers ( -- ) \ block
1.24 crook 120: \G Transfer the contents of each @code{update}d block buffer to
1.30 anton 121: \G mass storage, then mark all block buffers as assigned-clean.
1.20 pazsan 122: block-buffers @
1.24 crook 123: buffers 0 ?DO dup save-buffer next-buffer LOOP drop ;
1.1 anton 124:
1.24 crook 125: : empty-buffers ( -- ) \ block-ext
126: \G Mark all block buffers as unassigned; if any had been marked as
127: \G assigned-dirty (by @code{update}), the changes to those blocks
128: \G will be lost.
1.20 pazsan 129: block-buffers @
1.24 crook 130: buffers 0 ?DO dup empty-buffer next-buffer LOOP drop ;
1.1 anton 131:
1.20 pazsan 132: : flush ( -- ) \ block
1.24 crook 133: \G Perform the functions of @code{save-buffers} then
134: \G @code{empty-buffers}.
1.1 anton 135: save-buffers
136: empty-buffers ;
137:
1.12 anton 138: ' flush IS flush-blocks
1.5 pazsan 139:
1.26 pazsan 140: : get-buffer ( u -- a-addr ) \ gforth
141: 0 buffers um/mod drop buffer-struct %size * block-buffers @ + ;
1.5 pazsan 142:
1.28 crook 143: : block ( u -- a-addr ) \ gforthman- block
1.24 crook 144: \G If a block buffer is assigned for block @i{u}, return its
145: \G start address, @i{a-addr}. Otherwise, assign a block buffer
146: \G for block @i{u} (if the assigned block buffer has been
147: \G @code{update}d, transfer the contents to mass storage), read
148: \G the block into the block buffer and return its start address,
149: \G @i{a-addr}.
1.26 pazsan 150: dup offset @ u< -35 and throw
1.5 pazsan 151: dup get-buffer >r
152: dup r@ buffer-block @ <>
1.9 pazsan 153: r@ buffer-fid @ block-fid @ <> or
1.1 anton 154: if
1.5 pazsan 155: r@ save-buffer
1.1 anton 156: dup block-position
1.5 pazsan 157: r@ block-buffer chars/block get-block-fid read-file throw
1.1 anton 158: \ clear the rest of the buffer if the file is too short
1.5 pazsan 159: r@ block-buffer over chars + chars/block rot chars - blank
160: r@ buffer-block !
161: get-block-fid r@ buffer-fid !
1.1 anton 162: else
163: drop
164: then
1.5 pazsan 165: r> dup last-block ! block-buffer ;
1.1 anton 166:
1.20 pazsan 167: : buffer ( u -- a-addr ) \ block
1.24 crook 168: \G If a block buffer is assigned for block @i{u}, return its
169: \G start address, @i{a-addr}. Otherwise, assign a block buffer
170: \G for block @i{u} (if the assigned block buffer has been
171: \G @code{update}d, transfer the contents to mass storage) and
172: \G return its start address, @i{a-addr}. The subtle difference
173: \G between @code{buffer} and @code{block} mean that you should
174: \G only use @code{buffer} if you don't care about the previous
175: \G contents of block @i{u}. In Gforth, this simply calls
176: \G @code{block}.
1.1 anton 177: \ reading in the block is unnecessary, but simpler
178: block ;
179:
1.28 crook 180: User scr ( -- a-addr ) \ block-ext s-c-r
1.27 crook 181: \G @code{User} variable -- @i{a-addr} is the address of a cell containing
1.21 crook 182: \G the block number of the block most recently processed by
1.24 crook 183: \G @code{list}.
184: 0 scr !
1.1 anton 185:
1.24 crook 186: \ nac31Mar1999 moved "scr @" to list to make the stack comment correct
1.20 pazsan 187: : updated? ( n -- f ) \ gforth
1.29 crook 188: \G Return true if @code{updated} has been used to mark block @i{n}
189: \G as assigned-dirty.
1.24 crook 190: buffer
1.5 pazsan 191: [ 0 buffer-dirty 0 block-buffer - ] Literal + @ ;
192:
1.24 crook 193: : list ( u -- ) \ block-ext
194: \G Display block @i{u}. In Gforth, the block is displayed as 16
195: \G numbered lines, each of 64 characters.
1.1 anton 196: \ calling block again and again looks inefficient but is necessary
197: \ in a multitasking environment
198: dup scr !
1.5 pazsan 199: ." Screen " u.
1.24 crook 200: scr @ updated? 0= IF ." not " THEN ." modified " cr
1.1 anton 201: 16 0
202: ?do
1.4 anton 203: i 2 .r space scr @ block i 64 * chars + 64 type cr
1.1 anton 204: loop ;
205:
1.34 pazsan 206: [IFDEF] current-input
207: :noname 2 <> -12 and throw >in ! blk ! ;
208: \ restore-input
209: :noname blk @ >in @ 2 ; \ save-input
210: :noname 2 ; \ source-id "*a block*"
211: :noname 1 blk +! 1 loadline +! true ; \ refill
212: :noname blk @ block chars/block ; \ source
213:
214: Create block-input A, A, A, A, A,
215:
216: : load ( i*x n -- j*x ) \ block
217: \G Save the current input source specification. Store @i{n} in
218: \G @code{BLK}, set @code{>IN} to 0 and interpret. When the parse
219: \G area is exhausted, restore the input source specification.
220: block-input 0 new-tib dup loadline ! blk ! 2 loadfilename# !
221: ['] interpret catch pop-file throw ;
222: [ELSE]
1.23 crook 223: : (source) ( -- c-addr u )
1.2 pazsan 224: blk @ ?dup
225: IF block chars/block
226: ELSE tib #tib @
227: THEN ;
228:
1.23 crook 229: ' (source) IS source ( -- c-addr u ) \ core
1.24 crook 230: \G @i{c-addr} is the address of the input buffer and @i{u} is the
1.23 crook 231: \G number of characters in it.
1.2 pazsan 232:
1.20 pazsan 233: : load ( i*x n -- j*x ) \ block
1.24 crook 234: \G Save the current input source specification. Store @i{n} in
235: \G @code{BLK}, set @code{>IN} to 0 and interpret. When the parse
236: \G area is exhausted, restore the input source specification.
1.31 anton 237: loadfilename# @ >r
1.34 pazsan 238: 2 loadfilename# ! \ "*a block*"
1.24 crook 239: push-file
240: dup loadline ! blk ! >in off ['] interpret catch
1.31 anton 241: pop-file
242: r> loadfilename# !
243: throw ;
1.34 pazsan 244: [THEN]
1.24 crook 245:
246: : thru ( i*x n1 n2 -- j*x ) \ block-ext
247: \G @code{load} the blocks @i{n1} through @i{n2} in sequence.
248: 1+ swap ?DO I load LOOP ;
249:
250: : +load ( i*x n -- j*x ) \ gforth
251: \G Used within a block to load the block specified as the
252: \G current block + @i{n}.
1.20 pazsan 253: blk @ + load ;
1.2 pazsan 254:
1.24 crook 255: : +thru ( i*x n1 n2 -- j*x ) \ gforth
256: \G Used within a block to load the range of blocks specified as the
257: \G current block + @i{n1} thru the current block + @i{n2}.
258: 1+ swap ?DO I +load LOOP ;
259:
1.28 crook 260: : --> ( -- ) \ gforthman- gforth chain
1.24 crook 261: \G If this symbol is encountered whilst loading block @i{n},
262: \G discard the remainder of the block and load block @i{n+1}. Used
1.25 anton 263: \G for chaining multiple blocks together as a single loadable
264: \G unit. Not recommended, because it destroys the independence of
265: \G loading. Use @code{thru} (which is standard) or @code{+thru}
266: \G instead.
1.20 pazsan 267: refill drop ; immediate
1.5 pazsan 268:
1.24 crook 269: : block-included ( a-addr u -- ) \ gforth
270: \G Use within a block that is to be processed by @code{load}. Save
271: \G the current blocks file specification, open the blocks file
272: \G specified by @i{a-addr u} and @code{load} block 1 from that
273: \G file (which may in turn chain or load other blocks). Finally,
274: \G close the blocks file and restore the original blocks file.
1.11 anton 275: block-fid @ >r block-fid off open-blocks
1.5 pazsan 276: 1 load block-fid @ close-file throw flush
277: r> block-fid ! ;
278:
1.13 anton 279: \ thrown out because it may provide unpleasant surprises - anton
280: \ : include ( "name" -- )
281: \ name 2dup dup 3 - /string s" .fb" compare
282: \ 0= IF block-included ELSE included THEN ;
1.5 pazsan 283:
1.4 anton 284: get-current environment-wordlist set-current
285: true constant block
286: true constant block-ext
287: set-current
1.5 pazsan 288:
1.21 crook 289: : bye ( -- ) \ tools-ext
290: \G Return control to the host operating system (if any).
291: ['] flush catch drop bye ;
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>