[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.7 \ Copyright (C) 1995 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 : 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 :     1 cells: field buffer-block \ the block number
36 :     1 cells: field buffer-fid \ the block's fid
37 :     1 cells: field buffer-dirty \ the block dirty flag
38 :     chars/block chars: field block-buffer \ the data
39 :     0 cells: 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 : anton 1.1
49 : pazsan 1.5 : block-cold
50 :     defers 'cold block-fid off last-block off
51 :     buffers buffer-struct drop * allocate throw dup block-buffers !
52 :     buffers buffer-struct drop * erase ;
53 : anton 1.1
54 : pazsan 1.5 ' block-cold IS 'cold
55 :    
56 :     block-cold
57 :    
58 :     Defer flush-file
59 :    
60 :     : use-file ( addr u -- )
61 : pazsan 1.8 2dup ['] open-path-file catch 0<>
62 : pazsan 1.5 if
63 : pazsan 1.8 2drop r/w bin create-file throw
64 : pazsan 1.5 else
65 : pazsan 1.8 rot close-file throw 2dup file-status throw bin open-file throw
66 :     >r 2drop r>
67 : pazsan 1.5 then
68 : pazsan 1.6 block-fid @ IF flush-file block-fid @ close-file throw THEN
69 : pazsan 1.5 block-fid ! ;
70 : pazsan 1.8
71 :     : use ( "file" -- )
72 :     name use-file ;
73 : anton 1.1
74 : anton 1.3 \ the file is opened as binary file, since it either will contain text
75 :     \ without newlines or binary data
76 : anton 1.1 : get-block-fid ( -- fid )
77 :     block-fid @ 0=
78 :     if
79 : pazsan 1.5 s" blocks.fb" use-file
80 : anton 1.1 then
81 :     block-fid @ ;
82 :    
83 :     : block-position ( u -- )
84 :     \ positions the block file to the start of block u
85 : anton 1.3 1- chars/block chars um* get-block-fid reposition-file throw ;
86 : anton 1.1
87 :     : update ( -- )
88 : pazsan 1.5 last-block @ ?dup IF buffer-dirty on THEN ;
89 : anton 1.1
90 : pazsan 1.5 : save-buffer ( buffer -- ) >r
91 :     r@ buffer-dirty @ r@ buffer-block @ 0<> and
92 : anton 1.1 if
93 : pazsan 1.5 r@ buffer-block @ block-position
94 :     r@ block-buffer chars/block r@ buffer-fid @ write-file throw
95 :     r@ buffer-dirty off
96 :     endif
97 :     rdrop ;
98 :    
99 :     : empty-buffer ( buffer -- )
100 :     buffer-block off ;
101 :    
102 :     : save-buffers ( -- ) block-buffers @
103 :     buffers 0 ?DO dup save-buffer next-buffer LOOP drop ;
104 : anton 1.1
105 : pazsan 1.5 : empty-buffers ( -- ) block-buffers @
106 :     buffers 0 ?DO dup empty-buffer next-buffer LOOP drop ;
107 : anton 1.1
108 :     : flush ( -- )
109 :     save-buffers
110 :     empty-buffers ;
111 :    
112 : pazsan 1.5 ' flush IS flush-file
113 :    
114 :     : get-buffer ( n -- a-addr )
115 :     buffers mod buffer-struct drop * block-buffers @ + ;
116 :    
117 : anton 1.1 : block ( u -- a-addr )
118 :     dup 0= -35 and throw
119 : pazsan 1.5 dup get-buffer >r
120 :     dup r@ buffer-block @ <>
121 : pazsan 1.9 r@ buffer-fid @ block-fid @ <> or
122 : anton 1.1 if
123 : pazsan 1.5 r@ save-buffer
124 : anton 1.1 dup block-position
125 : pazsan 1.5 r@ block-buffer chars/block get-block-fid read-file throw
126 : anton 1.1 \ clear the rest of the buffer if the file is too short
127 : pazsan 1.5 r@ block-buffer over chars + chars/block rot chars - blank
128 :     r@ buffer-block !
129 :     get-block-fid r@ buffer-fid !
130 : anton 1.1 else
131 :     drop
132 :     then
133 : pazsan 1.5 r> dup last-block ! block-buffer ;
134 : anton 1.1
135 :     : buffer ( u -- a-addr )
136 :     \ reading in the block is unnecessary, but simpler
137 :     block ;
138 :    
139 : pazsan 1.2 User scr 0 scr !
140 : anton 1.1
141 : pazsan 1.5 : updated? ( n -- f ) scr @ buffer
142 :     [ 0 buffer-dirty 0 block-buffer - ] Literal + @ ;
143 :    
144 : anton 1.1 : list ( u -- )
145 :     \ calling block again and again looks inefficient but is necessary
146 :     \ in a multitasking environment
147 :     dup scr !
148 : pazsan 1.5 ." Screen " u.
149 :     updated? 0= IF ." not " THEN ." modified " cr
150 : anton 1.1 16 0
151 :     ?do
152 : anton 1.4 i 2 .r space scr @ block i 64 * chars + 64 type cr
153 : anton 1.1 loop ;
154 :    
155 : pazsan 1.2 : (source) ( -- addr len )
156 :     blk @ ?dup
157 :     IF block chars/block
158 :     ELSE tib #tib @
159 :     THEN ;
160 :    
161 :     ' (source) IS source
162 :    
163 :     : load ( i*x n -- j*x )
164 :     push-file
165 :     dup loadline ! blk ! >in off ( ['] ) interpret ( catch )
166 :     pop-file ( throw ) ;
167 : anton 1.1
168 : pazsan 1.2 : thru ( i*x n1 n2 -- j*x )
169 :     1+ swap 0 ?DO I load LOOP ;
170 : anton 1.1
171 : pazsan 1.2 : +load ( i*x n -- j*x ) blk @ + load ;
172 :    
173 :     : +thru ( i*x n1 n2 -- j*x )
174 :     1+ swap 0 ?DO I +load LOOP ;
175 : anton 1.4
176 : pazsan 1.5 : --> ( -- ) refill drop ; immediate
177 :    
178 :     : block-included ( addr u -- )
179 :     block-fid @ >r block-fid off use-file
180 :     1 load block-fid @ close-file throw flush
181 :     r> block-fid ! ;
182 :    
183 :     : include ( "name" -- )
184 :     name 2dup dup 3 - /string s" .fb" compare
185 :     0= IF block-included ELSE included THEN ;
186 :    
187 : anton 1.4 get-current environment-wordlist set-current
188 :     true constant block
189 :     true constant block-ext
190 :     set-current
191 : pazsan 1.5
192 :     : bye ['] flush catch drop bye ;

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help