File:  [gforth] / gforth / blocks.fs
Revision 1.27: download - view: text, annotated - select for diffs
Mon Nov 8 22:01:07 1999 UTC (19 years, 8 months ago) by crook
Branches: MAIN
CVS tags: HEAD
Various minor documentation changes to match the latest gforth.ds

    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: User offset   0 offset !  \ store 1 here fore 0.4.0 compatibility
   49: 
   50: : block-cold ( -- )
   51:     block-fid off  last-block off
   52:     buffer-struct buffers * %alloc dup block-buffers ! ( addr )
   53:     buffer-struct %size buffers * erase ;
   54: 
   55: ' block-cold INIT8 chained
   56: 
   57: block-cold
   58: 
   59: Defer flush-blocks ( -- ) \ gforth
   60: 
   61: : open-blocks ( c-addr u -- ) \ gforth
   62:     \g Use the file, whose name is given by @i{c-addr u}, as the blocks file.
   63:     2dup open-fpath-file 0<>
   64:     if
   65: 	r/w bin create-file throw
   66:     else
   67: 	rot close-file throw  2dup file-status throw bin open-file throw
   68: 	>r 2drop r>
   69:     then
   70:     block-fid @ IF  flush-blocks block-fid @ close-file throw  THEN
   71:     block-fid ! ;
   72: 
   73: : use ( "file" -- ) \ gforth
   74:     \g Use @i{file} as the blocks file.
   75:     name open-blocks ;
   76: 
   77: \ the file is opened as binary file, since it either will contain text
   78: \ without newlines or binary data
   79: : get-block-fid ( -- wfileid ) \ gforth
   80:     \G Return the file-id of the current blocks file. If no blocks
   81:     \G file has been opened, use @file{blocks.fb} as the default
   82:     \G blocks file.
   83:     block-fid @ 0=
   84:     if
   85: 	s" blocks.fb" open-blocks
   86:     then
   87:     block-fid @ ;
   88: 
   89: : block-position ( u -- ) \ block
   90:     \G Position the block file to the start of block @i{u}.
   91:     offset @ - chars/block chars um* get-block-fid reposition-file throw ;
   92: 
   93: : update ( -- ) \ block
   94:     \G Mark the current block buffer as dirty.
   95:     last-block @ ?dup IF  buffer-dirty on  THEN ;
   96: 
   97: : save-buffer ( buffer -- ) \ gforth
   98:     >r
   99:     r@ buffer-dirty @ r@ buffer-block @ 0<> and
  100:     if
  101: 	r@ buffer-block @ block-position
  102: 	r@ block-buffer chars/block  r@ buffer-fid @  write-file throw
  103: 	r@ buffer-dirty off
  104:     endif
  105:     rdrop ;
  106: 
  107: : empty-buffer ( buffer -- ) \ gforth
  108:     buffer-block off ;
  109: 
  110: : save-buffers  ( -- ) \ block
  111:     \G Transfer the contents of each @code{update}d block buffer to
  112:     \G mass storage, then mark all block buffers as unassigned.
  113:     block-buffers @
  114:     buffers 0 ?DO dup save-buffer next-buffer LOOP drop ;
  115: 
  116: : empty-buffers ( -- ) \ block-ext
  117:     \G Mark all block buffers as unassigned; if any had been marked as
  118:     \G assigned-dirty (by @code{update}), the changes to those blocks
  119:     \G will be lost.
  120:     block-buffers @
  121:     buffers 0 ?DO dup empty-buffer next-buffer LOOP drop ;
  122: 
  123: : flush ( -- ) \ block
  124:     \G Perform the functions of @code{save-buffers} then
  125:     \G @code{empty-buffers}.
  126:     save-buffers
  127:     empty-buffers ;
  128: 
  129: ' flush IS flush-blocks
  130: 
  131: : get-buffer ( u -- a-addr ) \ gforth
  132:     0 buffers um/mod drop buffer-struct %size * block-buffers @ + ;
  133: 
  134: : block ( u -- a-addr ) \ block- block
  135:     \G If a block buffer is assigned for block @i{u}, return its
  136:     \G start address, @i{a-addr}. Otherwise, assign a block buffer
  137:     \G for block @i{u} (if the assigned block buffer has been
  138:     \G @code{update}d, transfer the contents to mass storage), read
  139:     \G the block into the block buffer and return its start address,
  140:     \G @i{a-addr}.
  141:     dup offset @ u< -35 and throw
  142:     dup get-buffer >r
  143:     dup r@ buffer-block @ <>
  144:     r@ buffer-fid @ block-fid @ <> or
  145:     if
  146: 	r@ save-buffer
  147: 	dup block-position
  148: 	r@ block-buffer chars/block get-block-fid read-file throw
  149: 	\ clear the rest of the buffer if the file is too short
  150: 	r@ block-buffer over chars + chars/block rot chars - blank
  151: 	r@ buffer-block !
  152: 	get-block-fid r@ buffer-fid !
  153:     else
  154: 	drop
  155:     then
  156:     r> dup last-block ! block-buffer ;
  157: 
  158: : buffer ( u -- a-addr ) \ block
  159:     \G If a block buffer is assigned for block @i{u}, return its
  160:     \G start address, @i{a-addr}. Otherwise, assign a block buffer
  161:     \G for block @i{u} (if the assigned block buffer has been
  162:     \G @code{update}d, transfer the contents to mass storage) and
  163:     \G return its start address, @i{a-addr}.  The subtle difference
  164:     \G between @code{buffer} and @code{block} mean that you should
  165:     \G only use @code{buffer} if you don't care about the previous
  166:     \G contents of block @i{u}. In Gforth, this simply calls
  167:     \G @code{block}.
  168:     \ reading in the block is unnecessary, but simpler
  169:     block ;
  170: 
  171: User scr ( -- a-addr ) \ block-ext
  172:     \G @code{User} variable -- @i{a-addr} is the address of a cell containing
  173:     \G the block number of the block most recently processed by
  174:     \G @code{list}.
  175: 0 scr !
  176: 
  177: \ nac31Mar1999 moved "scr @" to list to make the stack comment correct
  178: : updated?  ( n -- f ) \ gforth
  179:     \G Return true if block @i{n} has been marked as dirty.
  180:     buffer
  181:     [ 0 buffer-dirty 0 block-buffer - ] Literal + @ ;
  182: 
  183: : list ( u -- ) \ block-ext
  184:     \G Display block @i{u}. In Gforth, the block is displayed as 16
  185:     \G numbered lines, each of 64 characters.
  186:     \ calling block again and again looks inefficient but is necessary
  187:     \ in a multitasking environment
  188:     dup scr !
  189:     ." Screen " u.
  190:     scr @ updated?  0= IF ." not "  THEN  ." modified     " cr
  191:     16 0
  192:     ?do
  193: 	i 2 .r space scr @ block i 64 * chars + 64 type cr
  194:     loop ;
  195: 
  196: : (source)  ( -- c-addr u )
  197:   blk @ ?dup
  198:   IF    block chars/block
  199:   ELSE  tib #tib @
  200:   THEN ;
  201: 
  202: ' (source) IS source ( -- c-addr u ) \ core
  203: \G @i{c-addr} is the address of the input buffer and @i{u} is the
  204: \G number of characters in it.
  205: 
  206: : load ( i*x n -- j*x ) \ block
  207:     \G Save the current input source specification. Store @i{n} in
  208:     \G @code{BLK}, set @code{>IN} to 0 and interpret. When the parse
  209:     \G area is exhausted, restore the input source specification.
  210:     push-file
  211:     dup loadline ! blk ! >in off ['] interpret catch
  212:     pop-file throw ;
  213: 
  214: : thru ( i*x n1 n2 -- j*x ) \ block-ext
  215:     \G @code{load} the blocks @i{n1} through @i{n2} in sequence.
  216:     1+ swap ?DO  I load  LOOP ;
  217: 
  218: : +load ( i*x n -- j*x ) \ gforth
  219:     \G Used within a block to load the block specified as the
  220:     \G current block + @i{n}.
  221:     blk @ + load ;
  222: 
  223: : +thru ( i*x n1 n2 -- j*x ) \ gforth
  224:     \G Used within a block to load the range of blocks specified as the
  225:     \G current block + @i{n1} thru the current block + @i{n2}.
  226:     1+ swap ?DO  I +load  LOOP ;
  227: 
  228: : --> ( -- ) \ gforth- gforth chain
  229:     \G If this symbol is encountered whilst loading block @i{n},
  230:     \G discard the remainder of the block and load block @i{n+1}. Used
  231:     \G for chaining multiple blocks together as a single loadable
  232:     \G unit.  Not recommended, because it destroys the independence of
  233:     \G loading.  Use @code{thru} (which is standard) or @code{+thru}
  234:     \G instead.
  235:     refill drop ; immediate
  236: 
  237: : block-included ( a-addr u -- ) \ gforth
  238:     \G Use within a block that is to be processed by @code{load}. Save
  239:     \G the current blocks file specification, open the blocks file
  240:     \G specified by @i{a-addr u} and @code{load} block 1 from that
  241:     \G file (which may in turn chain or load other blocks). Finally,
  242:     \G close the blocks file and restore the original blocks file.
  243:     block-fid @ >r block-fid off open-blocks
  244:     1 load block-fid @ close-file throw flush
  245:     r> block-fid ! ;
  246: 
  247: \ thrown out because it may provide unpleasant surprises - anton
  248: \ : include ( "name" -- )
  249: \     name 2dup dup 3 - /string s" .fb" compare
  250: \     0= IF  block-included  ELSE  included  THEN ;
  251: 
  252: get-current environment-wordlist set-current
  253: true constant block
  254: true constant block-ext
  255: set-current
  256: 
  257: : bye ( -- ) \ tools-ext
  258:   \G Return control to the host operating system (if any).
  259:   ['] flush catch drop bye ;

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