Annotation of gforth/blocks.fs, revision 1.13

1.5       pazsan      1: \ A less simple implementation of the blocks wordset. 
1.1       anton       2: 
1.7       anton       3: \ 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
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
                     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
1.1       anton      48: 
1.5       pazsan     49: : 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 ;
1.1       anton      53: 
1.5       pazsan     54: ' block-cold IS 'cold
                     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.8       pazsan     62:     2dup ['] open-path-file catch 0<>
1.5       pazsan     63:     if
1.8       pazsan     64:        2drop 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.1       anton      78: : get-block-fid ( -- fid )
                     79:     block-fid @ 0=
                     80:     if
1.11      anton      81:        s" blocks.fb" open-blocks
1.1       anton      82:     then
                     83:     block-fid @ ;
                     84: 
                     85: : block-position ( u -- )
                     86:     \ 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: 
                     89: : update ( -- )
1.5       pazsan     90:     last-block @ ?dup IF  buffer-dirty on  THEN ;
1.1       anton      91: 
1.5       pazsan     92: : save-buffer ( buffer -- ) >r
                     93:     r@ buffer-dirty @ r@ buffer-block @ 0<> and
1.1       anton      94:     if
1.5       pazsan     95:        r@ buffer-block @ block-position
                     96:        r@ block-buffer chars/block  r@ buffer-fid @  write-file throw
                     97:        r@ buffer-dirty off
                     98:     endif
                     99:     rdrop ;
                    100: 
                    101: : empty-buffer ( buffer -- )
                    102:     buffer-block off ;
                    103: 
                    104: : save-buffers  ( -- )    block-buffers @
                    105:     buffers 0 ?DO  dup save-buffer  next-buffer  LOOP  drop ;
1.1       anton     106: 
1.5       pazsan    107: : empty-buffers ( -- )    block-buffers @
                    108:     buffers 0 ?DO  dup empty-buffer  next-buffer  LOOP  drop ;
1.1       anton     109: 
                    110: : flush ( -- )
                    111:     save-buffers
                    112:     empty-buffers ;
                    113: 
1.12      anton     114: ' flush IS flush-blocks
1.5       pazsan    115: 
                    116: : get-buffer ( n -- a-addr )
                    117:     buffers mod buffer-struct drop * block-buffers @ + ;
                    118: 
1.1       anton     119: : block ( u -- a-addr )
                    120:     dup 0= -35 and throw
1.5       pazsan    121:     dup get-buffer >r
                    122:     dup r@ buffer-block @ <>
1.9       pazsan    123:     r@ buffer-fid @ block-fid @ <> or
1.1       anton     124:     if
1.5       pazsan    125:        r@ save-buffer
1.1       anton     126:        dup block-position
1.5       pazsan    127:        r@ block-buffer chars/block get-block-fid read-file throw
1.1       anton     128:        \ clear the rest of the buffer if the file is too short
1.5       pazsan    129:        r@ block-buffer over chars + chars/block rot chars - blank
                    130:        r@ buffer-block !
                    131:        get-block-fid r@ buffer-fid !
1.1       anton     132:     else
                    133:        drop
                    134:     then
1.5       pazsan    135:     r> dup last-block ! block-buffer ;
1.1       anton     136: 
                    137: : buffer ( u -- a-addr )
                    138:     \ reading in the block is unnecessary, but simpler
                    139:     block ;
                    140: 
1.2       pazsan    141: User scr 0 scr !
1.1       anton     142: 
1.5       pazsan    143: : updated?  ( n -- f )   scr @ buffer
                    144:     [ 0 buffer-dirty 0 block-buffer - ] Literal + @ ;
                    145: 
1.1       anton     146: : list ( u -- )
                    147:     \ calling block again and again looks inefficient but is necessary
                    148:     \ in a multitasking environment
                    149:     dup scr !
1.5       pazsan    150:     ." Screen " u.
                    151:     updated?  0= IF ." not "  THEN  ." modified     " cr
1.1       anton     152:     16 0
                    153:     ?do
1.4       anton     154:        i 2 .r space scr @ block i 64 * chars + 64 type cr
1.1       anton     155:     loop ;
                    156: 
1.2       pazsan    157: : (source)  ( -- addr len )
                    158:   blk @ ?dup
                    159:   IF    block chars/block
                    160:   ELSE  tib #tib @
                    161:   THEN ;
                    162: 
                    163: ' (source) IS source
                    164: 
                    165: : load ( i*x n -- j*x )
                    166:   push-file
                    167:   dup loadline ! blk ! >in off ( ['] ) interpret ( catch )
                    168:   pop-file ( throw ) ;
1.1       anton     169: 
1.2       pazsan    170: : thru ( i*x n1 n2 -- j*x )
                    171:   1+ swap 0 ?DO  I load  LOOP ;
1.1       anton     172: 
1.2       pazsan    173: : +load ( i*x n -- j*x )  blk @ + load ;
                    174: 
                    175: : +thru ( i*x n1 n2 -- j*x )
                    176:   1+ swap 0 ?DO  I +load  LOOP ;
1.4       anton     177: 
1.5       pazsan    178: : --> ( -- )  refill drop ; immediate
                    179: 
                    180: : block-included ( addr u -- )
1.11      anton     181:     block-fid @ >r block-fid off open-blocks
1.5       pazsan    182:     1 load block-fid @ close-file throw flush
                    183:     r> block-fid ! ;
                    184: 
1.13    ! anton     185: \ thrown out because it may provide unpleasant surprises - anton
        !           186: \ : include ( "name" -- )
        !           187: \     name 2dup dup 3 - /string s" .fb" compare
        !           188: \     0= IF  block-included  ELSE  included  THEN ;
1.5       pazsan    189: 
1.4       anton     190: get-current environment-wordlist set-current
                    191: true constant block
                    192: true constant block-ext
                    193: set-current
1.5       pazsan    194: 
                    195: : bye  ['] flush catch drop bye ;

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>