Annotation of gforth/blocks.fs, revision 1.54

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

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