File:
[gforth] /
gforth /
blocks.fs
Revision
1.24:
download - view:
text,
annotated -
select for diffs
Fri Apr 16 22:19:50 1999 UTC (23 years, 11 months ago) by
crook
Branches:
MAIN
CVS tags:
HEAD
.cvsignore -- added a couple of other files I was tired of seeing flagged
by CVS
README -- added references to a couple more .fs files that are part of
the gforth distribution
blocks.fs -- fixed a bug in UPDATED? and added glossary entries for all
words.
colorize.fs -- fixed a bug that was introduced by a dictionary
structure change between 0.3.0 and 0.4.0 (I think.. it used to work
on 0.3.0 and I compared the color WORDS with the normal WORDS and found
some dirrerences
doc/gforth.1 -- minor tweaks to man page. I now think that I'd like to
be able to auto-generate the man page from what is now Chapter 3 of the
manual. That's in line with GNU's general attitude towards man pages..
doc/gforth.ds -- added stuff about blocks, revamped Chapter 3 and other
miscellaneous changes.
kernel/comp.fs -- glossary tweaks
kernel/require.fs -- glossary tweaks
1: \ A less simple implementation of the blocks wordset.
2:
3: \ Copyright (C) 1995,1996,1997,1998 Free Software Foundation, Inc.
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
23: \ provide it and many buffers on OSs that do not provide mmap.
24:
25: \ Now, the replacement algorithm is "direct mapped"; change to LRU
26: \ if too slow. Using more buffers helps, too.
27:
28: \ I think I avoid the assumption 1 char = 1 here, but I have not tested this
29:
30: \ 1024 constant chars/block \ mandated by the standard
31:
32: require struct.fs
33:
34: struct
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
40: end-struct buffer-struct
41:
42: Variable block-buffers
43: Variable last-block
44:
45: $20 Value buffers
46:
47: User block-fid
48:
49: : block-cold ( -- )
50: block-fid off last-block off
51: buffer-struct buffers * %alloc dup block-buffers ! ( addr )
52: buffer-struct %size buffers * erase ;
53:
54: ' block-cold INIT8 chained
55:
56: block-cold
57:
58: Defer flush-blocks ( -- ) \ gforth
59:
60: : open-blocks ( c-addr u -- ) \ gforth
61: \g Use the file, whose name is given by @i{c-addr u}, as the blocks file.
62: 2dup open-fpath-file 0<>
63: if
64: r/w bin create-file throw
65: else
66: rot close-file throw 2dup file-status throw bin open-file throw
67: >r 2drop r>
68: then
69: block-fid @ IF flush-blocks block-fid @ close-file throw THEN
70: block-fid ! ;
71:
72: : use ( "file" -- ) \ gforth
73: \g Use @i{file} as the blocks file.
74: name open-blocks ;
75:
76: \ the file is opened as binary file, since it either will contain text
77: \ without newlines or binary data
78: : get-block-fid ( -- wfileid ) \ gforth
79: \G Return the file-id of the current blocks file. If no blocks
80: \G file has been opened, use @file{blocks.fb} as the default
81: \G blocks file.
82: block-fid @ 0=
83: if
84: s" blocks.fb" open-blocks
85: then
86: block-fid @ ;
87:
88: : block-position ( u -- ) \ block
89: \G Position the block file to the start of block @i{u}.
90: 1- chars/block chars um* get-block-fid reposition-file throw ;
91:
92: : update ( -- ) \ block
93: \G Mark the current block buffer as dirty.
94: last-block @ ?dup IF buffer-dirty on THEN ;
95:
96: : save-buffer ( buffer -- ) \ gforth
97: >r
98: r@ buffer-dirty @ r@ buffer-block @ 0<> and
99: if
100: r@ buffer-block @ block-position
101: r@ block-buffer chars/block r@ buffer-fid @ write-file throw
102: r@ buffer-dirty off
103: endif
104: rdrop ;
105:
106: : empty-buffer ( buffer -- ) \ gforth
107: buffer-block off ;
108:
109: : save-buffers ( -- ) \ block
110: \G Transfer the contents of each @code{update}d block buffer to
111: \G mass storage, then mark all block buffers as unassigned.
112: block-buffers @
113: buffers 0 ?DO dup save-buffer next-buffer LOOP drop ;
114:
115: : empty-buffers ( -- ) \ block-ext
116: \G Mark all block buffers as unassigned; if any had been marked as
117: \G assigned-dirty (by @code{update}), the changes to those blocks
118: \G will be lost.
119: block-buffers @
120: buffers 0 ?DO dup empty-buffer next-buffer LOOP drop ;
121:
122: : flush ( -- ) \ block
123: \G Perform the functions of @code{save-buffers} then
124: \G @code{empty-buffers}.
125: save-buffers
126: empty-buffers ;
127:
128: ' flush IS flush-blocks
129:
130: : get-buffer ( n -- a-addr ) \ gforth
131: buffers mod buffer-struct %size * block-buffers @ + ;
132:
133: : block ( u -- a-addr ) \ block- block
134: \G If a block buffer is assigned for block @i{u}, return its
135: \G start address, @i{a-addr}. Otherwise, assign a block buffer
136: \G for block @i{u} (if the assigned block buffer has been
137: \G @code{update}d, transfer the contents to mass storage), read
138: \G the block into the block buffer and return its start address,
139: \G @i{a-addr}.
140: dup 0= -35 and throw
141: dup get-buffer >r
142: dup r@ buffer-block @ <>
143: r@ buffer-fid @ block-fid @ <> or
144: if
145: r@ save-buffer
146: dup block-position
147: r@ block-buffer chars/block get-block-fid read-file throw
148: \ clear the rest of the buffer if the file is too short
149: r@ block-buffer over chars + chars/block rot chars - blank
150: r@ buffer-block !
151: get-block-fid r@ buffer-fid !
152: else
153: drop
154: then
155: r> dup last-block ! block-buffer ;
156:
157: : buffer ( u -- a-addr ) \ block
158: \G If a block buffer is assigned for block @i{u}, return its
159: \G start address, @i{a-addr}. Otherwise, assign a block buffer
160: \G for block @i{u} (if the assigned block buffer has been
161: \G @code{update}d, transfer the contents to mass storage) and
162: \G return its start address, @i{a-addr}. The subtle difference
163: \G between @code{buffer} and @code{block} mean that you should
164: \G only use @code{buffer} if you don't care about the previous
165: \G contents of block @i{u}. In Gforth, this simply calls
166: \G @code{block}.
167: \ reading in the block is unnecessary, but simpler
168: block ;
169:
170: User scr ( -- a-addr ) \ block-ext
171: \G USER VARIABLE: @i{a-addr} is the address of a cell containing
172: \G the block number of the block most recently processed by
173: \G @code{list}.
174: 0 scr !
175:
176: \ nac31Mar1999 moved "scr @" to list to make the stack comment correct
177: : updated? ( n -- f ) \ gforth
178: \G Return true if block @i{n} has been marked as dirty.
179: buffer
180: [ 0 buffer-dirty 0 block-buffer - ] Literal + @ ;
181:
182: : list ( u -- ) \ block-ext
183: \G Display block @i{u}. In Gforth, the block is displayed as 16
184: \G numbered lines, each of 64 characters.
185: \ calling block again and again looks inefficient but is necessary
186: \ in a multitasking environment
187: dup scr !
188: ." Screen " u.
189: scr @ updated? 0= IF ." not " THEN ." modified " cr
190: 16 0
191: ?do
192: i 2 .r space scr @ block i 64 * chars + 64 type cr
193: loop ;
194:
195: : (source) ( -- c-addr u )
196: blk @ ?dup
197: IF block chars/block
198: ELSE tib #tib @
199: THEN ;
200:
201: ' (source) IS source ( -- c-addr u ) \ core
202: \G @i{c-addr} is the address of the input buffer and @i{u} is the
203: \G number of characters in it.
204:
205: : load ( i*x n -- j*x ) \ block
206: \G Save the current input source specification. Store @i{n} in
207: \G @code{BLK}, set @code{>IN} to 0 and interpret. When the parse
208: \G area is exhausted, restore the input source specification.
209: push-file
210: dup loadline ! blk ! >in off ['] interpret catch
211: pop-file throw ;
212:
213: : thru ( i*x n1 n2 -- j*x ) \ block-ext
214: \G @code{load} the blocks @i{n1} through @i{n2} in sequence.
215: 1+ swap ?DO I load LOOP ;
216:
217: : +load ( i*x n -- j*x ) \ gforth
218: \G Used within a block to load the block specified as the
219: \G current block + @i{n}.
220: blk @ + load ;
221:
222: : +thru ( i*x n1 n2 -- j*x ) \ gforth
223: \G Used within a block to load the range of blocks specified as the
224: \G current block + @i{n1} thru the current block + @i{n2}.
225: 1+ swap ?DO I +load LOOP ;
226:
227: : --> ( -- ) \ block- block chain
228: \G If this symbol is encountered whilst loading block @i{n},
229: \G discard the remainder of the block and load block @i{n+1}. Used
230: \G for chaining multiple blocks together as a single loadable unit.
231: refill drop ; immediate
232:
233: : block-included ( a-addr u -- ) \ gforth
234: \G Use within a block that is to be processed by @code{load}. Save
235: \G the current blocks file specification, open the blocks file
236: \G specified by @i{a-addr u} and @code{load} block 1 from that
237: \G file (which may in turn chain or load other blocks). Finally,
238: \G close the blocks file and restore the original blocks file.
239: block-fid @ >r block-fid off open-blocks
240: 1 load block-fid @ close-file throw flush
241: r> block-fid ! ;
242:
243: \ thrown out because it may provide unpleasant surprises - anton
244: \ : include ( "name" -- )
245: \ name 2dup dup 3 - /string s" .fb" compare
246: \ 0= IF block-included ELSE included THEN ;
247:
248: get-current environment-wordlist set-current
249: true constant block
250: true constant block-ext
251: set-current
252:
253: : bye ( -- ) \ tools-ext
254: \G Return control to the host operating system (if any).
255: ['] flush catch drop bye ;
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>