[gforth] / gforth / blocks.fs  

gforth: gforth/blocks.fs


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

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help